Skip to content

Commit fd5a049

Browse files
crisbetokirjs
authored andcommitted
fix(compiler): recover invalid parenthesized expressions (#61815)
When the expression parser consumes tokens inside a parenthesized expression, it looks for valid tokens until it hits and invalid one or a closing paren. If it finds an invalid token, it reports and error and tries to recover until it finds a closing paren. The problem is that in such cases, it would produce the `ParenthesizedExpression` and continue parsing **from** from the closing paren which would then produce more errors that add noise to the output and result in an incorrect representation of the user's code. E.g. `foo((event.target as HTMLElement).value)` would be recovered to `foo((event.target)).value` instead of `foo((event.target).value)`. These changes resolve the issue by skipping over the closing paren at the recovery point. Fixes #61792. PR Close #61815
1 parent 07cb321 commit fd5a049

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,13 @@ class _ParseAST {
10821082
if (this.consumeOptionalCharacter(chars.$LPAREN)) {
10831083
this.rparensExpected++;
10841084
const result = this.parsePipe();
1085+
if (!this.consumeOptionalCharacter(chars.$RPAREN)) {
1086+
this.error('Missing closing parentheses');
1087+
// Calling into `error` above will attempt to recover up until the next closing paren.
1088+
// If that's the case, consume it so we can partially recover the expression.
1089+
this.consumeOptionalCharacter(chars.$RPAREN);
1090+
}
10851091
this.rparensExpected--;
1086-
this.expectCharacter(chars.$RPAREN);
10871092
return new ParenthesizedExpression(this.span(start), this.sourceSpan(start), result);
10881093
} else if (this.next.isKeywordNull()) {
10891094
this.advance();

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,19 @@ describe('parser', () => {
658658
it('should report a missing expected token', () => {
659659
expectActionError('a(b', 'Missing expected ) at the end of the expression [a(b]');
660660
});
661+
662+
it('should report a single error for an `as` expression inside a parenthesized expression', () => {
663+
expectActionError(
664+
`foo(($event.target as HTMLElement).value)`,
665+
'Missing closing parentheses at column 20',
666+
1,
667+
);
668+
expectActionError(
669+
`foo(((($event.target as HTMLElement))).value)`,
670+
'Missing closing parentheses at column 22',
671+
1,
672+
);
673+
});
661674
});
662675

663676
describe('parseBinding', () => {
@@ -1373,6 +1386,12 @@ describe('parser', () => {
13731386
it('should be able to recover from a missing selector', () => recover('a.'));
13741387
it('should be able to recover from a missing selector in a array literal', () =>
13751388
recover('[[a.], b, c]'));
1389+
1390+
it('should recover from parenthesized `as` expressions', () => {
1391+
recover('foo(($event.target as HTMLElement).value)', 'foo(($event.target).value)');
1392+
recover('foo(((($event.target as HTMLElement))).value)', 'foo(((($event.target))).value)');
1393+
recover('foo(((bar as HTMLElement) as Something).value)', 'foo(((bar)).value)');
1394+
});
13761395
});
13771396

13781397
describe('offsets', () => {
@@ -1468,7 +1487,13 @@ function checkAction(exp: string, expected?: string) {
14681487
validate(ast);
14691488
}
14701489

1471-
function expectError(ast: {errors: ParserError[]}, message: string) {
1490+
function expectError(ast: {errors: ParserError[]}, message: string, errorCount?: number) {
1491+
if (errorCount != null) {
1492+
expect(ast.errors.length).toBe(errorCount);
1493+
} else {
1494+
expect(ast.errors.length).toBeGreaterThan(0);
1495+
}
1496+
14721497
for (const error of ast.errors) {
14731498
if (error.message.indexOf(message) >= 0) {
14741499
return;
@@ -1480,8 +1505,8 @@ function expectError(ast: {errors: ParserError[]}, message: string) {
14801505
);
14811506
}
14821507

1483-
function expectActionError(text: string, message: string) {
1484-
expectError(validate(parseAction(text)), message);
1508+
function expectActionError(text: string, message: string, errorCount?: number) {
1509+
expectError(validate(parseAction(text)), message, errorCount);
14851510
}
14861511

14871512
function expectBindingError(text: string, message: string) {

0 commit comments

Comments
 (0)