Skip to content

Commit a83ef0e

Browse files
JeanMechethePunderWoman
authored andcommitted
refactor(compiler): Error on comment only interpolations (#62590)
This commit introduces a ParserError to prevent an error later in the pipepine fixes #34084 PR Close #62590
1 parent 19bc917 commit a83ef0e

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

packages/compiler/src/expression_parser/parser.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class Parser {
106106
): ASTWithSource {
107107
const errors: ParseError[] = [];
108108
this._checkNoInterpolation(errors, input, parseSourceSpan, interpolationConfig);
109-
const sourceToLex = this._stripComments(input);
109+
const {stripped: sourceToLex} = this._stripComments(input);
110110
const tokens = this._lexer.tokenize(sourceToLex);
111111
const ast = new _ParseAST(
112112
input,
@@ -183,7 +183,7 @@ export class Parser {
183183
errors: ParseError[],
184184
): AST {
185185
this._checkNoInterpolation(errors, input, parseSourceSpan, interpolationConfig);
186-
const sourceToLex = this._stripComments(input);
186+
const {stripped: sourceToLex} = this._stripComments(input);
187187
const tokens = this._lexer.tokenize(sourceToLex);
188188
return new _ParseAST(
189189
input,
@@ -273,8 +273,22 @@ export class Parser {
273273
// indexes inside the tokens.
274274
const expressionSpan = interpolatedTokens?.[i * 2 + 1]?.sourceSpan;
275275
const expressionText = expressions[i].text;
276-
const sourceToLex = this._stripComments(expressionText);
276+
const {stripped: sourceToLex, hasComments} = this._stripComments(expressionText);
277277
const tokens = this._lexer.tokenize(sourceToLex);
278+
279+
if (hasComments && sourceToLex.trim().length === 0 && tokens.length === 0) {
280+
// Empty expressions error are handled futher down, here we only take care of the comment case
281+
errors.push(
282+
getParseError(
283+
'Interpolation expression cannot only contain a comment',
284+
input,
285+
`at column ${expressions[i].start} in`,
286+
parseSourceSpan,
287+
),
288+
);
289+
continue;
290+
}
291+
278292
const ast = new _ParseAST(
279293
expressionSpan ? expressionText : input,
280294
expressionSpan || parseSourceSpan,
@@ -308,7 +322,7 @@ export class Parser {
308322
parseSourceSpan: ParseSourceSpan,
309323
absoluteOffset: number,
310324
): ASTWithSource {
311-
const sourceToLex = this._stripComments(expression);
325+
const {stripped: sourceToLex} = this._stripComments(expression);
312326
const tokens = this._lexer.tokenize(sourceToLex);
313327
const errors: ParseError[] = [];
314328
const ast = new _ParseAST(
@@ -450,9 +464,11 @@ export class Parser {
450464
);
451465
}
452466

453-
private _stripComments(input: string): string {
467+
private _stripComments(input: string): {stripped: string; hasComments: boolean} {
454468
const i = this._commentStart(input);
455-
return i != null ? input.substring(0, i) : input;
469+
return i != null
470+
? {stripped: input.substring(0, i), hasComments: true}
471+
: {stripped: input, hasComments: false};
456472
}
457473

458474
private _commentStart(input: string): number | null {

packages/compiler/test/expression_parser/parser_spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,11 @@ describe('parser', () => {
12491249
parseInterpolation('foo {{ }}')!,
12501250
'Parser Error: Blank expressions are not allowed in interpolated strings',
12511251
);
1252+
1253+
expectError(
1254+
parseInterpolation('{{ }}')!,
1255+
'Parser Error: Blank expressions are not allowed in interpolated strings',
1256+
);
12521257
});
12531258

12541259
it('should produce an empty expression ast for empty interpolations', () => {
@@ -1281,6 +1286,13 @@ describe('parser', () => {
12811286
checkInterpolation('{{a //comment}}', '{{ a }}');
12821287
});
12831288

1289+
it('should error when interpolation only contains a comment', () => {
1290+
expectError(
1291+
parseInterpolation('{{ // foobar }}')!,
1292+
'Parser Error: Interpolation expression cannot only contain a comment at column 0 in [{{ // foobar }}]',
1293+
);
1294+
});
1295+
12841296
it('should retain // in single quote strings', () => {
12851297
checkInterpolation(`{{ 'http://www.google.com' }}`, `{{ "http://www.google.com" }}`);
12861298
});

0 commit comments

Comments
 (0)