Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): report better error on interpolation in an expression #30300

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/compiler/src/expression_parser/parser.ts
Expand Up @@ -974,7 +974,8 @@ export class _ParseAST {
} else {
// Otherwise the key must be a directive keyword, like "of". Transform
// the key to actual key. Eg. of -> ngForOf, trackBy -> ngForTrackBy
key.source = templateKey.source + key.source[0].toUpperCase() + key.source.substring(1);
key.source =
templateKey.source + key.source.charAt(0).toUpperCase() + key.source.substring(1);
bindings.push(...this.parseDirectiveKeywordBindings(key));
}
}
Expand Down
26 changes: 21 additions & 5 deletions packages/compiler/test/expression_parser/parser_spec.ts
Expand Up @@ -779,6 +779,14 @@ describe('parser', () => {
]);
});

it('should report unexpected token when encountering interpolation', () => {
const attr = '*ngIf="name && {{name}}"';

expectParseTemplateBindingsError(
attr,
'Parser Error: Unexpected token {, expected identifier, keyword, or string at column 10 in [name && {{name}}] in foo.html');
});

it('should map variable declaration via "as"', () => {
const attr =
'*ngFor="let item; of items | slice:0:1 as collection, trackBy: func; index as i"';
Expand Down Expand Up @@ -1026,17 +1034,25 @@ function parseBinding(text: string, location: any = null, offset: number = 0): A
}

function parseTemplateBindings(attribute: string, templateUrl = 'foo.html'): TemplateBinding[] {
const result = _parseTemplateBindings(attribute, templateUrl);
expect(result.errors).toEqual([]);
expect(result.warnings).toEqual([]);
return result.templateBindings;
}

function expectParseTemplateBindingsError(attribute: string, error: string) {
const result = _parseTemplateBindings(attribute, 'foo.html');
expect(result.errors[0].message).toEqual(error);
}

function _parseTemplateBindings(attribute: string, templateUrl: string) {
const match = attribute.match(/^\*(.+)="(.*)"$/);
expect(match).toBeTruthy(`failed to extract key and value from ${attribute}`);
const [_, key, value] = match;
const absKeyOffset = 1; // skip the * prefix
const absValueOffset = attribute.indexOf('=') + '="'.length;
const parser = createParser();
const result =
parser.parseTemplateBindings(key, value, templateUrl, absKeyOffset, absValueOffset);
expect(result.errors).toEqual([]);
expect(result.warnings).toEqual([]);
return result.templateBindings;
return parser.parseTemplateBindings(key, value, templateUrl, absKeyOffset, absValueOffset);
}

function parseInterpolation(text: string, location: any = null, offset: number = 0): ASTWithSource|
Expand Down