Skip to content

Commit e027779

Browse files
Keen Yee LiauIgorMinar
Keen Yee Liau
authored andcommitted
fix(compiler-cli): create LiteralLikeNode for String and Number literal (angular#27536)
Typescript 3.2 introduced BigInt type, and consequently the implementation for checkExpressionWorker() in checkers.ts is refactored. For NumberLiteral and StringLiteral types, 'text' filed must be present in the Node type, therefore they must be LiteralLikeNode instead of Node. PR Close angular#27536
1 parent 0f0c8b5 commit e027779

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

packages/compiler-cli/src/diagnostics/typescript_symbols.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -718,13 +718,20 @@ function getBuiltinTypeFromTs(kind: BuiltinType, context: TypeContext): ts.Type
718718
checker.getTypeAtLocation(setParents(<ts.Node>{kind: ts.SyntaxKind.NullKeyword}, node));
719719
break;
720720
case BuiltinType.Number:
721-
const numeric = <ts.Node>{kind: ts.SyntaxKind.NumericLiteral};
721+
const numeric = <ts.LiteralLikeNode>{
722+
kind: ts.SyntaxKind.NumericLiteral,
723+
text: node.getText(),
724+
};
722725
setParents(<any>{kind: ts.SyntaxKind.ExpressionStatement, expression: numeric}, node);
723726
type = checker.getTypeAtLocation(numeric);
724727
break;
725728
case BuiltinType.String:
726-
type = checker.getTypeAtLocation(
727-
setParents(<ts.Node>{kind: ts.SyntaxKind.NoSubstitutionTemplateLiteral}, node));
729+
type = checker.getTypeAtLocation(setParents(
730+
<ts.LiteralLikeNode>{
731+
kind: ts.SyntaxKind.NoSubstitutionTemplateLiteral,
732+
text: node.getText(),
733+
},
734+
node));
728735
break;
729736
case BuiltinType.Undefined:
730737
type = checker.getTypeAtLocation(setParents(

packages/compiler-cli/test/diagnostics/typescript_symbols_spec.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {EmittingCompilerHost, MockAotCompilerHost, MockCompilerHost, MockData, M
1212
import {ReflectorHost} from '@angular/language-service/src/reflector_host';
1313
import * as ts from 'typescript';
1414

15-
import {Symbol, SymbolQuery, SymbolTable} from '../../src/diagnostics/symbols';
15+
import {BuiltinType, Symbol, SymbolQuery, SymbolTable} from '../../src/diagnostics/symbols';
1616
import {getSymbolQuery, toSymbolTableFactory} from '../../src/diagnostics/typescript_symbols';
1717
import {CompilerOptions} from '../../src/transformers/api';
1818
import {Directory} from '../mocks';
@@ -55,6 +55,29 @@ describe('symbol query', () => {
5555
const symbol = query.getTypeSymbol(unknownType);
5656
expect(symbol).toBeUndefined();
5757
});
58+
59+
it('should return correct built-in types', () => {
60+
const tests: Array<[BuiltinType, boolean, ts.TypeFlags?]> = [
61+
// builtinType, throws, want
62+
[BuiltinType.Any, false, ts.TypeFlags.Any],
63+
[BuiltinType.Boolean, false, ts.TypeFlags.BooleanLiteral],
64+
[BuiltinType.Null, false, ts.TypeFlags.Null],
65+
[BuiltinType.Number, false, ts.TypeFlags.NumberLiteral],
66+
[BuiltinType.String, false, ts.TypeFlags.StringLiteral],
67+
[BuiltinType.Undefined, false, ts.TypeFlags.Undefined],
68+
[BuiltinType.Unbound, true],
69+
[BuiltinType.Other, true],
70+
];
71+
for (const [builtinType, throws, want] of tests) {
72+
if (throws) {
73+
expect(() => query.getBuiltinType(builtinType)).toThrow();
74+
} else {
75+
const symbol = query.getBuiltinType(builtinType);
76+
const got: ts.TypeFlags = (symbol as any).tsType.flags;
77+
expect(got).toBe(want !);
78+
}
79+
}
80+
});
5881
});
5982

6083
describe('toSymbolTableFactory(tsVersion)', () => {

packages/compiler-cli/test/ngc_spec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ describe('ngc transformer command-line', () => {
145145

146146
const exitCode = main(['-p', basePath], errorSpy);
147147
expect(errorSpy).toHaveBeenCalledWith(
148-
`test.ts(1,9): error TS2305: Module '"` + path.join(basePath, 'empty-deps') +
149-
`"' has no exported member 'MyClass'.` +
150-
'\n');
148+
`test.ts(1,9): error TS2305: Module '"./empty-deps"' has no exported member 'MyClass'.\n`);
151149
expect(exitCode).toEqual(1);
152150
});
153151

@@ -874,7 +872,7 @@ describe('ngc transformer command-line', () => {
874872
write('mymodule.ts', `
875873
import {Component, NgModule} from '@angular/core';
876874
import {RouterModule} from '@angular/router';
877-
875+
878876
export function foo(): string {
879877
console.log('side-effect');
880878
return 'test';

packages/language-service/test/diagnostics_spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ describe('diagnostics', () => {
149149
});
150150
});
151151

152+
it('should not report an error for sub-types of number', () => {
153+
const code =
154+
` @Component({template: \`<div *ngIf="something === 123"></div>\`}) export class MyComponent { something: 123 | 456; }`;
155+
addCode(code, fileName => {
156+
const diagnostics = ngService.getDiagnostics(fileName);
157+
expectOnlyModuleDiagnostics(diagnostics);
158+
});
159+
});
160+
152161
it('should report a warning if an event results in a callable expression', () => {
153162
const code =
154163
` @Component({template: \`<div (click)="onClick"></div>\`}) export class MyComponent { onClick() { } }`;

0 commit comments

Comments
 (0)