Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(language-service): getTsTypeFromBuiltinType() should not include value #33778

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 21 additions & 46 deletions packages/language-service/src/typescript_symbols.ts
Expand Up @@ -94,8 +94,11 @@ class TypeScriptSymbolQuery implements SymbolQuery {
getBuiltinType(kind: BuiltinType): Symbol {
let result = this.typeCache.get(kind);
if (!result) {
const type = getBuiltinTypeFromTs(
kind, {checker: this.checker, node: this.source, program: this.program});
const type = getTsTypeFromBuiltinType(kind, {
checker: this.checker,
node: this.source,
program: this.program,
});
result =
new TypeWrapper(type, {program: this.program, checker: this.checker, node: this.source});
this.typeCache.set(kind, result);
Expand Down Expand Up @@ -553,7 +556,7 @@ class PipeSymbol implements Symbol {
resultType = getTypeParameterOf(parameterType.tsType, parameterType.name);
break;
default:
resultType = getBuiltinTypeFromTs(BuiltinType.Any, this.context);
resultType = getTsTypeFromBuiltinType(BuiltinType.Any, this.context);
break;
}
break;
Expand All @@ -580,7 +583,7 @@ class PipeSymbol implements Symbol {
type = this._tsType = this.findTransformMethodType(classSymbol) !;
}
if (!type) {
type = this._tsType = getBuiltinTypeFromTs(BuiltinType.Any, this.context);
type = this._tsType = getTsTypeFromBuiltinType(BuiltinType.Any, this.context);
}
}
return type;
Expand Down Expand Up @@ -633,62 +636,34 @@ function isSymbolPrivate(s: ts.Symbol): boolean {
return !!s.valueDeclaration && isPrivate(s.valueDeclaration);
}

function getBuiltinTypeFromTs(kind: BuiltinType, context: TypeContext): ts.Type {
let type: ts.Type;
const checker = context.checker;
const node = context.node;
switch (kind) {
function getTsTypeFromBuiltinType(builtinType: BuiltinType, ctx: TypeContext): ts.Type {
let syntaxKind: ts.SyntaxKind;
switch (builtinType) {
case BuiltinType.Any:
type = checker.getTypeAtLocation(setParents(
<ts.Node><any>{
kind: ts.SyntaxKind.AsExpression,
expression: <ts.Node>{kind: ts.SyntaxKind.TrueKeyword},
type: <ts.Node>{kind: ts.SyntaxKind.AnyKeyword}
},
node));
syntaxKind = ts.SyntaxKind.AnyKeyword;
break;
case BuiltinType.Boolean:
type =
checker.getTypeAtLocation(setParents(<ts.Node>{kind: ts.SyntaxKind.TrueKeyword}, node));
syntaxKind = ts.SyntaxKind.BooleanKeyword;
break;
case BuiltinType.Null:
type =
checker.getTypeAtLocation(setParents(<ts.Node>{kind: ts.SyntaxKind.NullKeyword}, node));
syntaxKind = ts.SyntaxKind.NullKeyword;
break;
case BuiltinType.Number:
const numeric = <ts.LiteralLikeNode>{
kind: ts.SyntaxKind.NumericLiteral,
text: node.getText(),
};
setParents(<any>{kind: ts.SyntaxKind.ExpressionStatement, expression: numeric}, node);
type = checker.getTypeAtLocation(numeric);
syntaxKind = ts.SyntaxKind.NumberKeyword;
break;
case BuiltinType.String:
type = checker.getTypeAtLocation(setParents(
<ts.LiteralLikeNode>{
kind: ts.SyntaxKind.NoSubstitutionTemplateLiteral,
text: node.getText(),
},
node));
syntaxKind = ts.SyntaxKind.StringKeyword;
break;
case BuiltinType.Undefined:
type = checker.getTypeAtLocation(setParents(
<ts.Node><any>{
kind: ts.SyntaxKind.VoidExpression,
expression: <ts.Node>{kind: ts.SyntaxKind.NumericLiteral}
},
node));
syntaxKind = ts.SyntaxKind.UndefinedKeyword;
break;
default:
throw new Error(`Internal error, unhandled literal kind ${kind}:${BuiltinType[kind]}`);
throw new Error(
`Internal error, unhandled literal kind ${builtinType}:${BuiltinType[builtinType]}`);
}
return type;
}

function setParents<T extends ts.Node>(node: T, parent: ts.Node): T {
node.parent = parent;
ts.forEachChild(node, child => setParents(child, node));
return node;
const node = ts.createNode(syntaxKind);
node.parent = ctx.node;
return ctx.checker.getTypeAtLocation(node);
}

function spanAt(sourceFile: ts.SourceFile, line: number, column: number): Span|undefined {
Expand Down
6 changes: 3 additions & 3 deletions packages/language-service/test/typescript_symbols_spec.ts
Expand Up @@ -53,10 +53,10 @@ describe('symbol query', () => {
const tests: Array<[BuiltinType, boolean, ts.TypeFlags?]> = [
// builtinType, throws, want
[BuiltinType.Any, false, ts.TypeFlags.Any],
[BuiltinType.Boolean, false, ts.TypeFlags.BooleanLiteral],
[BuiltinType.Boolean, false, ts.TypeFlags.Boolean | ts.TypeFlags.Union],
[BuiltinType.Null, false, ts.TypeFlags.Null],
[BuiltinType.Number, false, ts.TypeFlags.NumberLiteral],
[BuiltinType.String, false, ts.TypeFlags.StringLiteral],
[BuiltinType.Number, false, ts.TypeFlags.Number],
[BuiltinType.String, false, ts.TypeFlags.String],
[BuiltinType.Undefined, false, ts.TypeFlags.Undefined],
[BuiltinType.Unbound, true],
[BuiltinType.Other, true],
Expand Down