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:
parent
dd664f694c
commit
a5f9a86520
|
@ -100,6 +100,8 @@ export class StaticInterpreter {
|
||||||
return true;
|
return true;
|
||||||
} else if (node.kind === ts.SyntaxKind.FalseKeyword) {
|
} else if (node.kind === ts.SyntaxKind.FalseKeyword) {
|
||||||
return false;
|
return false;
|
||||||
|
} else if (node.kind === ts.SyntaxKind.NullKeyword) {
|
||||||
|
return null;
|
||||||
} else if (ts.isStringLiteral(node)) {
|
} else if (ts.isStringLiteral(node)) {
|
||||||
return node.text;
|
return node.text;
|
||||||
} else if (ts.isNoSubstitutionTemplateLiteral(node)) {
|
} else if (ts.isNoSubstitutionTemplateLiteral(node)) {
|
||||||
|
@ -215,8 +217,12 @@ export class StaticInterpreter {
|
||||||
private visitIdentifier(node: ts.Identifier, context: Context): ResolvedValue {
|
private visitIdentifier(node: ts.Identifier, context: Context): ResolvedValue {
|
||||||
const decl = this.host.getDeclarationOfIdentifier(node);
|
const decl = this.host.getDeclarationOfIdentifier(node);
|
||||||
if (decl === null) {
|
if (decl === null) {
|
||||||
|
if (node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword) {
|
||||||
|
return undefined;
|
||||||
|
} else {
|
||||||
return DynamicValue.fromUnknownIdentifier(node);
|
return DynamicValue.fromUnknownIdentifier(node);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const result =
|
const result =
|
||||||
this.visitDeclaration(decl.node, {...context, ...joinModuleContext(context, node, decl)});
|
this.visitDeclaration(decl.node, {...context, ...joinModuleContext(context, node, decl)});
|
||||||
if (result instanceof Reference) {
|
if (result instanceof Reference) {
|
||||||
|
|
|
@ -172,6 +172,16 @@ runInEachFileSystem(() => {
|
||||||
expect(evaluate(`const x = 3;`, '!!x')).toEqual(true);
|
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', () => {
|
it('resolves access from external variable declarations as dynamic value', () => {
|
||||||
const value = evaluate('declare const window: any;', 'window.location');
|
const value = evaluate('declare const window: any;', 'window.location');
|
||||||
if (!(value instanceof DynamicValue)) {
|
if (!(value instanceof DynamicValue)) {
|
||||||
|
|
Loading…
Reference in New Issue