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 94e790d

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 23c36a2 commit 94e790d
Copy full SHA for 94e790d

File tree

Expand file treeCollapse file tree

2 files changed

+23
-6
lines changed
Filter options
Expand file treeCollapse file tree

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
@@ -974,7 +974,8 @@ export class _ParseAST {
974974
} else {
975975
// Otherwise the key must be a directive keyword, like "of". Transform
976976
// the key to actual key. Eg. of -> ngForOf, trackBy -> ngForTrackBy
977-
key.source = templateKey.source + key.source[0].toUpperCase() + key.source.substring(1);
977+
key.source =
978+
templateKey.source + key.source.charAt(0).toUpperCase() + key.source.substring(1);
978979
bindings.push(...this.parseDirectiveKeywordBindings(key));
979980
}
980981
}

‎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
@@ -779,6 +779,14 @@ describe('parser', () => {
779779
]);
780780
});
781781

782+
it('should report unexpected token when encountering interpolation', () => {
783+
const attr = '*ngIf="name && {{name}}"';
784+
785+
expectParseTemplateBindingsError(
786+
attr,
787+
'Parser Error: Unexpected token {, expected identifier, keyword, or string at column 10 in [name && {{name}}] in foo.html');
788+
});
789+
782790
it('should map variable declaration via "as"', () => {
783791
const attr =
784792
'*ngFor="let item; of items | slice:0:1 as collection, trackBy: func; index as i"';
@@ -1026,17 +1034,25 @@ function parseBinding(text: string, location: any = null, offset: number = 0): A
10261034
}
10271035

10281036
function parseTemplateBindings(attribute: string, templateUrl = 'foo.html'): TemplateBinding[] {
1037+
const result = _parseTemplateBindings(attribute, templateUrl);
1038+
expect(result.errors).toEqual([]);
1039+
expect(result.warnings).toEqual([]);
1040+
return result.templateBindings;
1041+
}
1042+
1043+
function expectParseTemplateBindingsError(attribute: string, error: string) {
1044+
const result = _parseTemplateBindings(attribute, 'foo.html');
1045+
expect(result.errors[0].message).toEqual(error);
1046+
}
1047+
1048+
function _parseTemplateBindings(attribute: string, templateUrl: string) {
10291049
const match = attribute.match(/^\*(.+)="(.*)"$/);
10301050
expect(match).toBeTruthy(`failed to extract key and value from ${attribute}`);
10311051
const [_, key, value] = match;
10321052
const absKeyOffset = 1; // skip the * prefix
10331053
const absValueOffset = attribute.indexOf('=') + '="'.length;
10341054
const parser = createParser();
1035-
const result =
1036-
parser.parseTemplateBindings(key, value, templateUrl, absKeyOffset, absValueOffset);
1037-
expect(result.errors).toEqual([]);
1038-
expect(result.warnings).toEqual([]);
1039-
return result.templateBindings;
1055+
return parser.parseTemplateBindings(key, value, templateUrl, absKeyOffset, absValueOffset);
10401056
}
10411057

10421058
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.