Skip to content

Commit 0677496

Browse files
author
Andy
authored
Properly handle JS enum symbols (#26893)
1 parent ff05082 commit 0677496

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

src/compiler/binder.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,10 +1731,6 @@ namespace ts {
17311731
}
17321732
}
17331733

1734-
function bindBlockScopedVariableDeclaration(node: Declaration) {
1735-
bindBlockScopedDeclaration(node, SymbolFlags.BlockScopedVariable, SymbolFlags.BlockScopedVariableExcludes);
1736-
}
1737-
17381734
function delayedBindJSDocTypedefTag() {
17391735
if (!delayedTypeAliases) {
17401736
return;
@@ -2659,8 +2655,11 @@ namespace ts {
26592655
}
26602656

26612657
if (!isBindingPattern(node.name)) {
2658+
const isEnum = !!getJSDocEnumTag(node);
2659+
const enumFlags = (isEnum ? SymbolFlags.RegularEnum : SymbolFlags.None);
2660+
const enumExcludes = (isEnum ? SymbolFlags.RegularEnumExcludes : SymbolFlags.None);
26622661
if (isBlockOrCatchScoped(node)) {
2663-
bindBlockScopedVariableDeclaration(node);
2662+
bindBlockScopedDeclaration(node, SymbolFlags.BlockScopedVariable | enumFlags, SymbolFlags.BlockScopedVariableExcludes | enumExcludes);
26642663
}
26652664
else if (isParameterDeclaration(node)) {
26662665
// It is safe to walk up parent chain to find whether the node is a destructuring parameter declaration
@@ -2675,7 +2674,7 @@ namespace ts {
26752674
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.ParameterExcludes);
26762675
}
26772676
else {
2678-
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable, SymbolFlags.FunctionScopedVariableExcludes);
2677+
declareSymbolAndAddToSymbolTable(node, SymbolFlags.FunctionScopedVariable | enumFlags, SymbolFlags.FunctionScopedVariableExcludes | enumExcludes);
26792678
}
26802679
}
26812680
}

src/compiler/checker.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8208,6 +8208,12 @@ namespace ts {
82088208
return type;
82098209
}
82108210

8211+
// JS are 'string' or 'number', not an enum type.
8212+
const enumTag = symbol.valueDeclaration && getJSDocEnumTag(symbol.valueDeclaration);
8213+
if (enumTag) {
8214+
return enumTag.typeExpression ? getTypeFromTypeNode(enumTag.typeExpression) : errorType;
8215+
}
8216+
82118217
// Get type from reference to named type that cannot be generic (enum or type parameter)
82128218
const res = tryGetDeclaredTypeOfSymbol(symbol);
82138219
if (res) {
@@ -8243,10 +8249,6 @@ namespace ts {
82438249
// TODO: GH#18217 (should the `|| assignedType` be at a lower precedence?)
82448250
return (referenceType && assignedType ? getIntersectionType([assignedType, referenceType]) : referenceType || assignedType)!;
82458251
}
8246-
const enumTag = getJSDocEnumTag(symbol.valueDeclaration);
8247-
if (enumTag && enumTag.typeExpression) {
8248-
return getTypeFromTypeNode(enumTag.typeExpression);
8249-
}
82508252
}
82518253

82528254
function getTypeReferenceTypeWorker(node: NodeWithTypeArguments, symbol: Symbol, typeArguments: Type[] | undefined): Type | undefined {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path="./fourslash.ts"/>
2+
3+
// @allowJs: true
4+
// @noLib: true
5+
6+
// @Filename: /a.js
7+
/////**
8+
//// * Doc
9+
//// * @enum {number}
10+
//// */
11+
////const E = {
12+
//// A: 0,
13+
////}
14+
////
15+
/////** @type {/**/E} */
16+
////const x = E.A;
17+
18+
verify.noErrors();
19+
20+
verify.quickInfoAt("",
21+
`enum E
22+
const E: {
23+
A: number;
24+
}`,
25+
"Doc");

0 commit comments

Comments
 (0)