Skip to content

Wrong location information when parsing a[b]=1 #62617

@fisker

Description

@fisker

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

Yes

Description

The end location of left is incorrect.

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

import {
  Lexer,
  ParseLocation,
  Parser,
  ParseSourceFile,
  ParseSourceSpan,
} from '@angular/compiler';

function getFakeSpan(fileName = 'test.html') {
  const file = new ParseSourceFile('', fileName);
  const location = new ParseLocation(file, 0, 0, 0);
  return new ParseSourceSpan(location, location);
}

const parse = (text) => {
  const lexer = new Lexer();
  const parser = new Parser(lexer);

  return parser.parseBinding(text, getFakeSpan(), 0);
};

console.log(parse('a[b]=1').ast);
Binary {
  span: ParseSpan { start: 0, end: 6 },
  sourceSpan: AbsoluteSourceSpan { start: 0, end: 6 },
  operation: '=',
  left: KeyedRead {
    span: ParseSpan { start: 0, end: 5 },
    sourceSpan: AbsoluteSourceSpan { start: 0, end: 5 },   // <-- Should end at 4, not 5
    receiver: PropertyRead {
      span: [ParseSpan],
      sourceSpan: [AbsoluteSourceSpan],
      nameSpan: [AbsoluteSourceSpan],
      receiver: [ImplicitReceiver],
      name: 'a'
    },
    key: PropertyRead {
      span: [ParseSpan],
      sourceSpan: [AbsoluteSourceSpan],
      nameSpan: [AbsoluteSourceSpan],
      receiver: [ImplicitReceiver],
      name: 'b'
    }
  },
  right: LiteralPrimitive {
    span: ParseSpan { start: 5, end: 6 },
    sourceSpan: AbsoluteSourceSpan { start: 5, end: 6 },
    value: 1
  }
}

Please provide the environment you discovered this bug in (run ng version)

@angular/compiler 20.1.0

Anything else?

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions