From 3edd153213bcccbc9c5bca3f2b0918b4d794fcbb Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 22 Apr 2021 08:43:48 +0100 Subject: [PATCH] fixup! fix(compiler-cli): allow linker to process minified booleans --- .../linker/babel/src/ast/babel_ast_host.ts | 12 +++++----- .../babel/test/ast/babel_ast_host_spec.ts | 2 ++ .../src/ast/typescript/typescript_ast_host.ts | 22 ++++++++++++------- .../typescript/typescript_ast_host_spec.ts | 2 ++ 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/packages/compiler-cli/linker/babel/src/ast/babel_ast_host.ts b/packages/compiler-cli/linker/babel/src/ast/babel_ast_host.ts index fbcce08e8f502..39490b7f4bd61 100644 --- a/packages/compiler-cli/linker/babel/src/ast/babel_ast_host.ts +++ b/packages/compiler-cli/linker/babel/src/ast/babel_ast_host.ts @@ -38,16 +38,18 @@ export class BabelAstHost implements AstHost { return num.value; } - isBooleanLiteral(bool: t.Expression): bool is t.BooleanLiteral|MinifiedBooleanLiteral { + isBooleanLiteral(bool: t.Expression): boolean { return t.isBooleanLiteral(bool) || isMinifiedBooleanLiteral(bool); } parseBooleanLiteral(bool: t.Expression): boolean { - assert(bool, this.isBooleanLiteral, 'a boolean literal'); - if (isMinifiedBooleanLiteral(bool)) { - return !(bool as MinifiedBooleanLiteral).argument.value; + if (t.isBooleanLiteral(bool)) { + return bool.value; + } else if (isMinifiedBooleanLiteral(bool)) { + return !bool.argument.value; + } else { + throw new FatalLinkerError(bool, 'Unsupported syntax, expected a boolean literal.'); } - return bool.value; } isArrayLiteral = t.isArrayExpression; diff --git a/packages/compiler-cli/linker/babel/test/ast/babel_ast_host_spec.ts b/packages/compiler-cli/linker/babel/test/ast/babel_ast_host_spec.ts index b4d384a02e030..2540a6508dde3 100644 --- a/packages/compiler-cli/linker/babel/test/ast/babel_ast_host_spec.ts +++ b/packages/compiler-cli/linker/babel/test/ast/babel_ast_host_spec.ts @@ -111,6 +111,8 @@ describe('BabelAstHost', () => { expect(host.isBooleanLiteral(expr('null'))).toBe(false); expect(host.isBooleanLiteral(expr('\'a\' + \'b\''))).toBe(false); expect(host.isBooleanLiteral(expr('\`moo\`'))).toBe(false); + expect(host.isBooleanLiteral(expr('!2'))).toBe(false); + expect(host.isBooleanLiteral(expr('~1'))).toBe(false); }); }); diff --git a/packages/compiler-cli/linker/src/ast/typescript/typescript_ast_host.ts b/packages/compiler-cli/linker/src/ast/typescript/typescript_ast_host.ts index 52cf9ef45787e..9b6bb9069ff93 100644 --- a/packages/compiler-cli/linker/src/ast/typescript/typescript_ast_host.ts +++ b/packages/compiler-cli/linker/src/ast/typescript/typescript_ast_host.ts @@ -47,19 +47,18 @@ export class TypeScriptAstHost implements AstHost { return parseInt(num.text); } - isBooleanLiteral(node: ts.Expression): node is ts.FalseLiteral|ts.TrueLiteral - |MinifiedBooleanLiteral { - return node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword || - isMinifiedBooleanLiteral(node); + isBooleanLiteral(node: ts.Expression): boolean { + return isBooleanLiteral(node) || isMinifiedBooleanLiteral(node); } parseBooleanLiteral(bool: ts.Expression): boolean { - assert(bool, this.isBooleanLiteral, 'a boolean literal'); - if (isMinifiedBooleanLiteral(bool)) { - // Convert the string operand ("0" or "1") to a number and negate it to get the boolean. + if (isBooleanLiteral(bool)) { + return bool.kind === ts.SyntaxKind.TrueKeyword; + } else if (isMinifiedBooleanLiteral(bool)) { return !(+bool.operand.text); + } else { + throw new FatalLinkerError(bool, 'Unsupported syntax, expected a boolean literal.'); } - return bool.kind === ts.SyntaxKind.TrueKeyword; } isArrayLiteral = ts.isArrayLiteralExpression; @@ -167,6 +166,13 @@ function isPropertyName(e: ts.PropertyName): e is ts.Identifier|ts.StringLiteral return ts.isIdentifier(e) || ts.isStringLiteral(e) || ts.isNumericLiteral(e); } +/** + * Return true if the node is either `true` or `false` literals. + */ +function isBooleanLiteral(node: ts.Expression): node is ts.TrueLiteral|ts.FalseLiteral { + return node.kind === ts.SyntaxKind.TrueKeyword || node.kind === ts.SyntaxKind.FalseKeyword; +} + type MinifiedBooleanLiteral = ts.PrefixUnaryExpression&{operand: ts.NumericLiteral}; /** diff --git a/packages/compiler-cli/linker/test/ast/typescript/typescript_ast_host_spec.ts b/packages/compiler-cli/linker/test/ast/typescript/typescript_ast_host_spec.ts index b345cbe1c5317..1b840eee42aef 100644 --- a/packages/compiler-cli/linker/test/ast/typescript/typescript_ast_host_spec.ts +++ b/packages/compiler-cli/linker/test/ast/typescript/typescript_ast_host_spec.ts @@ -109,6 +109,8 @@ describe('TypeScriptAstHost', () => { expect(host.isBooleanLiteral(expr('null'))).toBe(false); expect(host.isBooleanLiteral(expr('\'a\' + \'b\''))).toBe(false); expect(host.isBooleanLiteral(expr('\`moo\`'))).toBe(false); + expect(host.isBooleanLiteral(expr('!2'))).toBe(false); + expect(host.isBooleanLiteral(expr('~1'))).toBe(false); }); });