From c402bb81f55df44673dbebedd4c0288911df82ca Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Mon, 26 Jun 2023 16:42:05 -0700 Subject: [PATCH] refactor(compiler): support o.BinaryExpression for assignments Previously the compiler output AST did not support using `o.BinaryExpression` for assignments (as TypeScript does). Instead `o.WriteVarExpr` is defined, which only allows writing to string named variables. --- .../compiler-cli/src/ngtsc/translator/src/api/ast_factory.ts | 2 +- packages/compiler-cli/src/ngtsc/translator/src/translator.ts | 1 + .../src/ngtsc/translator/src/typescript_ast_factory.ts | 1 + packages/compiler/src/output/abstract_emitter.ts | 3 +++ packages/compiler/src/output/output_ast.ts | 1 + 5 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/compiler-cli/src/ngtsc/translator/src/api/ast_factory.ts b/packages/compiler-cli/src/ngtsc/translator/src/api/ast_factory.ts index e94b88bac3e..ad5bc0c78fa 100644 --- a/packages/compiler-cli/src/ngtsc/translator/src/api/ast_factory.ts +++ b/packages/compiler-cli/src/ngtsc/translator/src/api/ast_factory.ts @@ -245,7 +245,7 @@ export type UnaryOperator = '+'|'-'|'!'; * The binary operators supported by the `AstFactory`. */ export type BinaryOperator = - '&&'|'>'|'>='|'&'|'/'|'=='|'==='|'<'|'<='|'-'|'%'|'*'|'!='|'!=='|'||'|'+'|'??'; + '&&'|'>'|'>='|'&'|'/'|'='|'=='|'==='|'<'|'<='|'-'|'%'|'*'|'!='|'!=='|'||'|'+'|'??'; /** * The original location of the start or end of a node created by the `AstFactory`. diff --git a/packages/compiler-cli/src/ngtsc/translator/src/translator.ts b/packages/compiler-cli/src/ngtsc/translator/src/translator.ts index bd7a3b10552..0b53b12fec7 100644 --- a/packages/compiler-cli/src/ngtsc/translator/src/translator.ts +++ b/packages/compiler-cli/src/ngtsc/translator/src/translator.ts @@ -18,6 +18,7 @@ const UNARY_OPERATORS = new Map([ const BINARY_OPERATORS = new Map([ [o.BinaryOperator.And, '&&'], + [o.BinaryOperator.Assign, '='], [o.BinaryOperator.Bigger, '>'], [o.BinaryOperator.BiggerEquals, '>='], [o.BinaryOperator.BitwiseAnd, '&'], diff --git a/packages/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.ts b/packages/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.ts index 99c47124296..b6bd70f9eda 100644 --- a/packages/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.ts +++ b/packages/compiler-cli/src/ngtsc/translator/src/typescript_ast_factory.ts @@ -31,6 +31,7 @@ const UNARY_OPERATORS: Record = { }; const BINARY_OPERATORS: Record = { + '=': ts.SyntaxKind.EqualsToken, '&&': ts.SyntaxKind.AmpersandAmpersandToken, '>': ts.SyntaxKind.GreaterThanToken, '>=': ts.SyntaxKind.GreaterThanEqualsToken, diff --git a/packages/compiler/src/output/abstract_emitter.ts b/packages/compiler/src/output/abstract_emitter.ts index 6443ffbae16..a5057af6f44 100644 --- a/packages/compiler/src/output/abstract_emitter.ts +++ b/packages/compiler/src/output/abstract_emitter.ts @@ -436,6 +436,9 @@ export abstract class AbstractEmitterVisitor implements o.StatementVisitor, o.Ex case o.BinaryOperator.NullishCoalesce: opStr = '??'; break; + case o.BinaryOperator.Assign: + opStr = '='; + break; default: throw new Error(`Unknown operator ${ast.operator}`); } diff --git a/packages/compiler/src/output/output_ast.ts b/packages/compiler/src/output/output_ast.ts index 4b0f91d914a..f03d5a8bcba 100644 --- a/packages/compiler/src/output/output_ast.ts +++ b/packages/compiler/src/output/output_ast.ts @@ -114,6 +114,7 @@ export enum UnaryOperator { } export enum BinaryOperator { + Assign, Equals, NotEquals, Identical,