Skip to content

Commit

Permalink
fix(parser): detect empty expression in strings to interpolate
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource committed Aug 3, 2015
1 parent 53788ef commit 3d85413
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
21 changes: 17 additions & 4 deletions modules/angular2/src/change_detection/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ var _implicitReceiver = new ImplicitReceiver();
// TODO(tbosch): Cannot make this const/final right now because of the transpiler...
var INTERPOLATION_REGEXP = /\{\{(.*?)\}\}/g;

class ParseException extends BaseException {
constructor(message: string, input: string, errLocation: string, ctxLocation?: any) {
super(`Parser Error: ${message} ${errLocation} [${input}] in ${ctxLocation}`, null, null,
ctxLocation);
}
}

@Injectable()
export class Parser {
_reflector: Reflector;
Expand Down Expand Up @@ -88,14 +95,21 @@ export class Parser {
var expressions = [];

for (var i = 0; i < parts.length; i++) {
var part = parts[i];
var part: string = parts[i];
if (i % 2 === 0) {
// fixed string
strings.push(part);
} else {
} else if (part.trim().length > 0) {
var tokens = this._lexer.tokenize(part);
var ast = new _ParseAST(input, location, tokens, this._reflector, false).parseChain();
expressions.push(ast);
} else {
var errLocation = '';
for (var j = 0; j < i; j++) {
errLocation += j % 2 === 0 ? parts[j] : `{{${parts[j]}}}`;
}
throw new ParseException('Blank expressions are not allowed in interpolated strings', input,
`at column ${errLocation.length} in`, location);
}
}
return new ASTWithSource(new Interpolation(strings, expressions), input, location);
Expand Down Expand Up @@ -596,8 +610,7 @@ export class _ParseAST {
var location = (index < this.tokens.length) ? `at column ${this.tokens[index].index + 1} in` :
`at the end of the expression`;

throw new BaseException(
`Parser Error: ${message} ${location} [${this.input}] in ${this.location}`);
throw new ParseException(message, this.input, location, this.location);
}
}

Expand Down
10 changes: 10 additions & 0 deletions modules/angular2/test/change_detection/parser/parser_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,16 @@ export function main() {
var ast = parseInterpolation(originalExp).ast;
expect(new Unparser().unparse(ast)).toEqual(originalExp);
});

it("should throw on empty interpolation expressions", () => {
expect(() => parseInterpolation("{{}}"))
.toThrowError(new RegExp(
"Parser Error: Blank expressions are not allowed in interpolated strings"));

expect(() => parseInterpolation("foo {{ }}"))
.toThrowError(new RegExp(
"Parser Error: Blank expressions are not allowed in interpolated strings"));
});
});

describe("parseSimpleBinding", () => {
Expand Down

0 comments on commit 3d85413

Please sign in to comment.