Properly handle JS enum symbols (#26893)

This commit is contained in:
Andy 2018-09-05 11:19:32 -07:00 committed by GitHub
parent ff05082e45
commit 06774962ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 10 deletions

View file

@ -1731,10 +1731,6 @@ namespace ts {
}
}
function bindBlockScopedVariableDeclaration(node: Declaration) {
bindBlockScopedDeclaration(node, SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes);
}
function delayedBindJSDocTypedefTag() {
if (!delayedTypeAliases) {
return;
@ -2659,8 +2655,11 @@ namespace ts {
}
if (!isBindingPattern(node.name)) {
const isEnum = !!getJSDocEnumTag(node);
const enumFlags = (isEnum ? SymbolFlags.RegularEnum : SymbolFlags.None);
const enumExcludes = (isEnum ? SymbolFlags.RegularEnumExcludes : SymbolFlags.None);
if (isBlockOrCatchScoped(node)) {
bindBlockScopedVariableDeclaration(node);
bindBlockScopedDeclaration(node, SymbolFlags.BlockScopedVariable | enumFlags, SymbolFlags.BlockScopedVariableExcludes | enumExcludes);
}
else if (isParameterDeclaration(node)) {
// It is safe to walk up parent chain to find whether the node is a destructuring parameter declaration
@ -2675,7 +2674,7 @@ namespace ts {
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.ParameterExcludes);
}
else {
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.FunctionScopedVariableExcludes);
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable | enumFlags, SymbolFlags.FunctionScopedVariableExcludes | enumExcludes);
}
}
}

View file

@ -8208,6 +8208,12 @@ namespace ts {
return type;
}
// JS are 'string' or 'number', not an enum type.
const enumTag = symbol.valueDeclaration && getJSDocEnumTag(symbol.valueDeclaration);
if (enumTag) {
return enumTag.typeExpression ? getTypeFromTypeNode(enumTag.typeExpression) : errorType;
}
// Get type from reference to named type that cannot be generic (enum or type parameter)
const res = tryGetDeclaredTypeOfSymbol(symbol);
if (res) {
@ -8243,10 +8249,6 @@ namespace ts {
// TODO: GH#18217 (should the `|| assignedType` be at a lower precedence?)
return (referenceType && assignedType ? getIntersectionType([assignedType, referenceType]) : referenceType || assignedType)!;
}
const enumTag = getJSDocEnumTag(symbol.valueDeclaration);
if (enumTag && enumTag.typeExpression) {
return getTypeFromTypeNode(enumTag.typeExpression);
}
}
function getTypeReferenceTypeWorker(node: NodeWithTypeArguments, symbol: Symbol, typeArguments: Type[] | undefined): Type | undefined {

View file

@ -0,0 +1,25 @@
/// <reference path="./fourslash.ts"/>
// @allowJs: true
// @noLib: true
// @Filename: /a.js
/////**
//// * Doc
//// * @enum {number}
//// */
////const E = {
//// A: 0,
////}
////
/////** @type {/**/E} */
////const x = E.A;
verify.noErrors();
verify.quickInfoAt("",
`enum E
const E: {
A: number;
}`,
"Doc");