Skip to content

Commit

Permalink
fixup! fix(compiler-cli): allow linker to process minified booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Apr 22, 2021
1 parent a7c015b commit 3edd153
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
12 changes: 7 additions & 5 deletions packages/compiler-cli/linker/babel/src/ast/babel_ast_host.ts
Expand Up @@ -38,16 +38,18 @@ export class BabelAstHost implements AstHost<t.Expression> {
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;
Expand Down
Expand Up @@ -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);
});
});

Expand Down
Expand Up @@ -47,19 +47,18 @@ export class TypeScriptAstHost implements AstHost<ts.Expression> {
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;
Expand Down Expand Up @@ -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};

/**
Expand Down
Expand Up @@ -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);
});
});

Expand Down

0 comments on commit 3edd153

Please sign in to comment.