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.
This commit is contained in:
Alex Rickabaugh
2023-06-26 16:42:05 -07:00
committed by Paul Gschwendtner
parent 0a7a8e496e
commit c402bb81f5
5 changed files with 7 additions and 1 deletions
@@ -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`.
@@ -18,6 +18,7 @@ const UNARY_OPERATORS = new Map<o.UnaryOperator, UnaryOperator>([
const BINARY_OPERATORS = new Map<o.BinaryOperator, BinaryOperator>([
[o.BinaryOperator.And, '&&'],
[o.BinaryOperator.Assign, '='],
[o.BinaryOperator.Bigger, '>'],
[o.BinaryOperator.BiggerEquals, '>='],
[o.BinaryOperator.BitwiseAnd, '&'],
@@ -31,6 +31,7 @@ const UNARY_OPERATORS: Record<UnaryOperator, ts.PrefixUnaryOperator> = {
};
const BINARY_OPERATORS: Record<BinaryOperator, ts.BinaryOperator> = {
'=': ts.SyntaxKind.EqualsToken,
'&&': ts.SyntaxKind.AmpersandAmpersandToken,
'>': ts.SyntaxKind.GreaterThanToken,
'>=': ts.SyntaxKind.GreaterThanEqualsToken,
@@ -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}`);
}
@@ -114,6 +114,7 @@ export enum UnaryOperator {
}
export enum BinaryOperator {
Assign,
Equals,
NotEquals,
Identical,