Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6dc74fd

Browse filesBrowse files
krzysztof-grzybekthePunderWoman
authored andcommitted
fix(compiler): report better error on interpolation in an expression (#30300)
Compiler results in weird error message when encounters interpolation inside existing expression context, e.g. *ngIf="name {{ name }}" PR Close #30300
1 parent 9dc0a4e commit 6dc74fd
Copy full SHA for 6dc74fd

File tree

2 files changed

+23
-6
lines changed
Filter options

2 files changed

+23
-6
lines changed

‎packages/compiler/src/expression_parser/parser.ts

Copy file name to clipboardExpand all lines: packages/compiler/src/expression_parser/parser.ts
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,8 @@ export class _ParseAST {
968968
} else {
969969
// Otherwise the key must be a directive keyword, like "of". Transform
970970
// the key to actual key. Eg. of -> ngForOf, trackBy -> ngForTrackBy
971-
key.source = templateKey.source + key.source[0].toUpperCase() + key.source.substring(1);
971+
key.source =
972+
templateKey.source + key.source.charAt(0).toUpperCase() + key.source.substring(1);
972973
bindings.push(...this.parseDirectiveKeywordBindings(key));
973974
}
974975
}

‎packages/compiler/test/expression_parser/parser_spec.ts

Copy file name to clipboardExpand all lines: packages/compiler/test/expression_parser/parser_spec.ts
+21-5Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,14 @@ describe('parser', () => {
704704
]);
705705
});
706706

707+
it('should report unexpected token when encountering interpolation', () => {
708+
const attr = '*ngIf="name && {{name}}"';
709+
710+
expectParseTemplateBindingsError(
711+
attr,
712+
'Parser Error: Unexpected token {, expected identifier, keyword, or string at column 10 in [name && {{name}}] in foo.html');
713+
});
714+
707715
it('should map variable declaration via "as"', () => {
708716
const attr =
709717
'*ngFor="let item; of items | slice:0:1 as collection, trackBy: func; index as i"';
@@ -951,17 +959,25 @@ function parseBinding(text: string, location: any = null, offset: number = 0): A
951959
}
952960

953961
function parseTemplateBindings(attribute: string, templateUrl = 'foo.html'): TemplateBinding[] {
962+
const result = _parseTemplateBindings(attribute, templateUrl);
963+
expect(result.errors).toEqual([]);
964+
expect(result.warnings).toEqual([]);
965+
return result.templateBindings;
966+
}
967+
968+
function expectParseTemplateBindingsError(attribute: string, error: string) {
969+
const result = _parseTemplateBindings(attribute, 'foo.html');
970+
expect(result.errors[0].message).toEqual(error);
971+
}
972+
973+
function _parseTemplateBindings(attribute: string, templateUrl: string) {
954974
const match = attribute.match(/^\*(.+)="(.*)"$/);
955975
expect(match).toBeTruthy(`failed to extract key and value from ${attribute}`);
956976
const [_, key, value] = match;
957977
const absKeyOffset = 1; // skip the * prefix
958978
const absValueOffset = attribute.indexOf('=') + '="'.length;
959979
const parser = createParser();
960-
const result =
961-
parser.parseTemplateBindings(key, value, templateUrl, absKeyOffset, absValueOffset);
962-
expect(result.errors).toEqual([]);
963-
expect(result.warnings).toEqual([]);
964-
return result.templateBindings;
980+
return parser.parseTemplateBindings(key, value, templateUrl, absKeyOffset, absValueOffset);
965981
}
966982

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.