refactor(compiler): expose shorthand object metadata in expression AST (#56405)

Whenever we parse object property assignment shorthands in expression
ASTs, the AST will have no information about whether the property read
for the `LiteralMap` is built based on the shorthand or not.

Exposing this information in the AST is useful for migrations as those
might need to decompose the shorthand into its longer form to e.g.
invoke a signal read.

PR Close #56405
This commit is contained in:
Paul Gschwendtner 2024-06-12 13:03:52 +00:00 committed by Andrew Kushnir
parent 34dc7007be
commit ef660d1db2
3 changed files with 14 additions and 1 deletions

View file

@ -248,6 +248,7 @@ export class LiteralArray extends AST {
export type LiteralMapKey = {
key: string;
quoted: boolean;
isShorthandInitialized?: boolean;
};
export class LiteralMap extends AST {

View file

@ -1071,7 +1071,8 @@ class _ParseAST {
const keyStart = this.inputIndex;
const quoted = this.next.isString();
const key = this.expectIdentifierOrKeywordOrString();
keys.push({key, quoted});
const literalMapKey: LiteralMapKey = {key, quoted};
keys.push(literalMapKey);
// Properties with quoted keys can't use the shorthand syntax.
if (quoted) {
@ -1080,6 +1081,8 @@ class _ParseAST {
} else if (this.consumeOptionalCharacter(chars.$COLON)) {
values.push(this.parsePipe());
} else {
literalMapKey.isShorthandInitialized = true;
const span = this.span(keyStart);
const sourceSpan = this.sourceSpan(keyStart);
values.push(

View file

@ -13,6 +13,7 @@ import {
Call,
EmptyExpr,
Interpolation,
LiteralMap,
ParserError,
PropertyRead,
TemplateBinding,
@ -628,6 +629,14 @@ describe('parser', () => {
it('should retain // in string literals', () => {
checkBinding(`"http://www.google.com"`, `"http://www.google.com"`);
});
it('should expose object shorthand information in AST', () => {
const parser = new Parser(new Lexer());
const ast = parser.parseBinding('{bla}', '', 0);
expect(ast.ast instanceof LiteralMap).toBe(true);
expect((ast.ast as LiteralMap).keys.length).toBe(1);
expect((ast.ast as LiteralMap).keys[0].isShorthandInitialized).toBe(true);
});
});
describe('parseTemplateBindings', () => {