Skip to content

Commit 4422819

Browse files
fix(parser): detect empty expression in strings to interpolate
Fixes angular#3412 Closes angular#3451
1 parent 6eaa09a commit 4422819

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

modules/angular2/src/change_detection/parser/parser.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ var _implicitReceiver = new ImplicitReceiver();
4848
// TODO(tbosch): Cannot make this const/final right now because of the transpiler...
4949
var INTERPOLATION_REGEXP = /\{\{(.*?)\}\}/g;
5050

51+
class ParseException extends BaseException {
52+
constructor(message: string, input: string, errLocation: string, ctxLocation?: any) {
53+
super(`Parser Error: ${message} ${errLocation} [${input}] in ${ctxLocation}`, null, null,
54+
ctxLocation);
55+
}
56+
}
57+
5158
@Injectable()
5259
export class Parser {
5360
_reflector: Reflector;
@@ -88,14 +95,21 @@ export class Parser {
8895
var expressions = [];
8996

9097
for (var i = 0; i < parts.length; i++) {
91-
var part = parts[i];
98+
var part: string = parts[i];
9299
if (i % 2 === 0) {
93100
// fixed string
94101
strings.push(part);
95-
} else {
102+
} else if (part.trim().length > 0) {
96103
var tokens = this._lexer.tokenize(part);
97104
var ast = new _ParseAST(input, location, tokens, this._reflector, false).parseChain();
98105
expressions.push(ast);
106+
} else {
107+
var errLocation = '';
108+
for (var j = 0; j < i; j++) {
109+
errLocation += j % 2 === 0 ? parts[j] : `{{${parts[j]}}}`;
110+
}
111+
throw new ParseException('Blank expressions are not allowed in interpolated strings', input,
112+
`at column ${errLocation.length} in`, location);
99113
}
100114
}
101115
return new ASTWithSource(new Interpolation(strings, expressions), input, location);
@@ -596,8 +610,7 @@ export class _ParseAST {
596610
var location = (index < this.tokens.length) ? `at column ${this.tokens[index].index + 1} in` :
597611
`at the end of the expression`;
598612

599-
throw new BaseException(
600-
`Parser Error: ${message} ${location} [${this.input}] in ${this.location}`);
613+
throw new ParseException(message, this.input, location, this.location);
601614
}
602615
}
603616

modules/angular2/test/change_detection/parser/parser_spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,16 @@ export function main() {
624624
var ast = parseInterpolation(originalExp).ast;
625625
expect(new Unparser().unparse(ast)).toEqual(originalExp);
626626
});
627+
628+
it("should throw on empty interpolation expressions", () => {
629+
expect(() => parseInterpolation("{{}}"))
630+
.toThrowError(new RegExp(
631+
"Parser Error: Blank expressions are not allowed in interpolated strings"));
632+
633+
expect(() => parseInterpolation("foo {{ }}"))
634+
.toThrowError(new RegExp(
635+
"Parser Error: Blank expressions are not allowed in interpolated strings"));
636+
});
627637
});
628638

629639
describe("parseSimpleBinding", () => {

0 commit comments

Comments
 (0)