feat(ivy): support undefined and null in static interpreter (#31150)

Previously, the usage of `null` and `undefined` keywords in code that is
statically interpreted by ngtsc resulted in a `DynamicValue`, as they were
not recognized as special entities. This commit adds support to interpret
these keywords.

PR Close #31150
This commit is contained in:
JoostK 2019-06-19 23:08:25 +02:00 committed by Miško Hevery
parent dd664f694c
commit a5f9a86520
2 changed files with 17 additions and 1 deletions

View File

@ -100,6 +100,8 @@ export class StaticInterpreter {
return true;
} else if (node.kind === ts.SyntaxKind.FalseKeyword) {
return false;
} else if (node.kind === ts.SyntaxKind.NullKeyword) {
return null;
} else if (ts.isStringLiteral(node)) {
return node.text;
} else if (ts.isNoSubstitutionTemplateLiteral(node)) {
@ -215,7 +217,11 @@ export class StaticInterpreter {
private visitIdentifier(node: ts.Identifier, context: Context): ResolvedValue {
const decl = this.host.getDeclarationOfIdentifier(node);
if (decl === null) {
return DynamicValue.fromUnknownIdentifier(node);
if (node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
return undefined;
} else {
return DynamicValue.fromUnknownIdentifier(node);
}
}
const result =
this.visitDeclaration(decl.node, {...context, ...joinModuleContext(context, node, decl)});

View File

@ -172,6 +172,16 @@ runInEachFileSystem(() => {
expect(evaluate(`const x = 3;`, '!!x')).toEqual(true);
});
it('supports boolean literals', () => {
expect(evaluate('const a = true;', 'a')).toEqual(true);
expect(evaluate('const a = false;', 'a')).toEqual(false);
});
it('supports undefined',
() => { expect(evaluate('const a = undefined;', 'a')).toEqual(undefined); });
it('supports null', () => { expect(evaluate('const a = null;', 'a')).toEqual(null); });
it('resolves access from external variable declarations as dynamic value', () => {
const value = evaluate('declare const window: any;', 'window.location');
if (!(value instanceof DynamicValue)) {