From ef660d1db2687b1e1e4bd8596abe567a5c0ff7eb Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Wed, 12 Jun 2024 13:03:52 +0000 Subject: [PATCH] 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 --- packages/compiler/src/expression_parser/ast.ts | 1 + packages/compiler/src/expression_parser/parser.ts | 5 ++++- packages/compiler/test/expression_parser/parser_spec.ts | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/compiler/src/expression_parser/ast.ts b/packages/compiler/src/expression_parser/ast.ts index 5647ec80b40..a53c5de9fd5 100644 --- a/packages/compiler/src/expression_parser/ast.ts +++ b/packages/compiler/src/expression_parser/ast.ts @@ -248,6 +248,7 @@ export class LiteralArray extends AST { export type LiteralMapKey = { key: string; quoted: boolean; + isShorthandInitialized?: boolean; }; export class LiteralMap extends AST { diff --git a/packages/compiler/src/expression_parser/parser.ts b/packages/compiler/src/expression_parser/parser.ts index 6f4fd153078..b3a1f0e29c4 100644 --- a/packages/compiler/src/expression_parser/parser.ts +++ b/packages/compiler/src/expression_parser/parser.ts @@ -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( diff --git a/packages/compiler/test/expression_parser/parser_spec.ts b/packages/compiler/test/expression_parser/parser_spec.ts index 445b9d0b307..d4762dfc83c 100644 --- a/packages/compiler/test/expression_parser/parser_spec.ts +++ b/packages/compiler/test/expression_parser/parser_spec.ts @@ -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', () => {