diff --git a/packages/compiler/src/expression_parser/parser.ts b/packages/compiler/src/expression_parser/parser.ts index 855b60e89c..18a516649e 100644 --- a/packages/compiler/src/expression_parser/parser.ts +++ b/packages/compiler/src/expression_parser/parser.ts @@ -461,6 +461,19 @@ export class _ParseAST { if (artificialEndIndex !== undefined && artificialEndIndex > this.currentEndIndex) { endIndex = artificialEndIndex; } + + // In some unusual parsing scenarios (like when certain tokens are missing and an `EmptyExpr` is + // being created), the current token may already be advanced beyond the `currentEndIndex`. This + // appears to be a deep-seated parser bug. + // + // As a workaround for now, swap the start and end indices to ensure a valid `ParseSpan`. + // TODO(alxhub): fix the bug upstream in the parser state, and remove this workaround. + if (start > endIndex) { + const tmp = endIndex; + endIndex = start; + start = tmp; + } + return new ParseSpan(start, endIndex); } diff --git a/packages/compiler/test/expression_parser/parser_spec.ts b/packages/compiler/test/expression_parser/parser_spec.ts index e9f180d3d1..5e9b149c83 100644 --- a/packages/compiler/test/expression_parser/parser_spec.ts +++ b/packages/compiler/test/expression_parser/parser_spec.ts @@ -188,6 +188,13 @@ describe('parser', () => { checkAction('a.add(1, 2)'); checkAction('fn().add(1, 2)'); }); + + it('should parse an EmptyExpr with a correct span for a trailing empty argument', () => { + const ast = parseAction('fn(1, )').ast as MethodCall; + expect(ast.args[1]).toBeAnInstanceOf(EmptyExpr); + const sourceSpan = (ast.args[1] as EmptyExpr).sourceSpan; + expect([sourceSpan.start, sourceSpan.end]).toEqual([5, 6]); + }); }); describe('functional calls', () => {