Skip to content

Commit 01e2819

Browse files
committed
Support @enum tag post-merge
1 parent ce4b918 commit 01e2819

File tree

3 files changed

+22
-43
lines changed

3 files changed

+22
-43
lines changed

src/compiler/checker.ts

+13-34
Original file line numberDiff line numberDiff line change
@@ -5213,40 +5213,19 @@ namespace ts {
52135213
}
52145214
}
52155215
if (symbol.flags & SymbolFlags.Enum) {
5216-
if (symbol.flags & SymbolFlags.Variable) {
5217-
// Oh boy, this symbol really refers to an `@enum`'d variable declaration. We already emitted the variable
5218-
// declaration part above, but we can't just emit `export const Foo = {}; export enum Foo {}` - that's
5219-
// a symbol merge error. To get the same behavior without a bad merge, we'll do this:
5220-
// export const Foo = {
5221-
// MEMBER: string;
5222-
// };
5223-
// export type Foo = string;
5224-
// Make sense? Yes - the @enum tag does nothing nominal and looks nothing like a real enum.
5225-
// TODO: the @enum tag has nothing to do with enums and is a glorified @typedef - it should bind that way, too
5226-
const typeExpr = getJSDocEnumTag(symbol.valueDeclaration)!.typeExpression;
5227-
addResult(createTypeAliasDeclaration(
5228-
/*decorators*/ undefined,
5229-
/*modifiers*/ undefined,
5230-
localName,
5231-
/*modifiers*/ undefined,
5232-
typeToTypeNodeHelper(typeExpr ? getTypeFromTypeNode(typeExpr) : errorType, context)
5233-
), modifierFlags);
5234-
}
5235-
else {
5236-
addResult(createEnumDeclaration(
5237-
/*decorators*/ undefined,
5238-
createModifiersFromModifierFlags(isConstEnumSymbol(symbol) ? ModifierFlags.Const : 0),
5239-
localName,
5240-
map(filter(getPropertiesOfType(getTypeOfSymbol(symbol)), p => !!(p.flags & SymbolFlags.EnumMember)), p => {
5241-
// TODO: Handle computed names
5242-
// I hate that to get the initialized value we need to walk back to the declarations here; but there's no
5243-
// other way to get the possible const value of an enum member that I'm aware of, as the value is cached
5244-
// _on the declaration_, not on the declaration's symbol...
5245-
const initializedValue = p.declarations && p.declarations[0] && isEnumMember(p.declarations[0]) && getConstantValue(p.declarations[0] as EnumMember);
5246-
return createEnumMember(unescapeLeadingUnderscores(p.escapedName), initializedValue === undefined ? undefined : createLiteral(initializedValue));
5247-
})
5248-
), modifierFlags);
5249-
}
5216+
addResult(createEnumDeclaration(
5217+
/*decorators*/ undefined,
5218+
createModifiersFromModifierFlags(isConstEnumSymbol(symbol) ? ModifierFlags.Const : 0),
5219+
localName,
5220+
map(filter(getPropertiesOfType(getTypeOfSymbol(symbol)), p => !!(p.flags & SymbolFlags.EnumMember)), p => {
5221+
// TODO: Handle computed names
5222+
// I hate that to get the initialized value we need to walk back to the declarations here; but there's no
5223+
// other way to get the possible const value of an enum member that I'm aware of, as the value is cached
5224+
// _on the declaration_, not on the declaration's symbol...
5225+
const initializedValue = p.declarations && p.declarations[0] && isEnumMember(p.declarations[0]) && getConstantValue(p.declarations[0] as EnumMember);
5226+
return createEnumMember(unescapeLeadingUnderscores(p.escapedName), initializedValue === undefined ? undefined : createLiteral(initializedValue));
5227+
})
5228+
), modifierFlags);
52505229
}
52515230
if (symbol.flags & SymbolFlags.Class) {
52525231
if (symbol.flags & SymbolFlags.Property) {

tests/baselines/reference/jsDeclarationsEnumTag.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,23 @@ exports.ff = ff;
104104
//// [index.d.ts]
105105
export function consume(t: string, s: number, f: (arg0: number) => number): void;
106106
export function ff(s: string): any;
107+
export type Target = string;
107108
export const Target: {
108109
START: string;
109110
MIDDLE: string;
110111
END: string;
111112
/** @type {number} */
112113
OK_I_GUESS: number;
113114
};
114-
export type Target = string;
115+
export type Second = number;
115116
export const Second: {
116117
OK: number;
117118
/** @type {number} */
118119
FINE: number;
119120
};
120-
export type Second = number;
121+
export type Fs = (arg0: number) => number;
121122
export const Fs: {
122123
ADD1: (n: any) => any;
123124
ID: (n: any) => any;
124125
SUB1: (n: any) => number;
125126
};
126-
export type Fs = (arg0: number) => number;

tests/baselines/reference/jsDeclarationsEnumTag.symbols

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
=== tests/cases/conformance/jsdoc/declarations/index.js ===
22
/** @enum {string} */
33
export const Target = {
4-
>Target : Symbol(Target, Decl(index.js, 1, 12))
4+
>Target : Symbol(Target, Decl(index.js, 1, 12), Decl(index.js, 0, 4))
55

66
START: "start",
77
>START : Symbol(START, Decl(index.js, 1, 23))
@@ -18,7 +18,7 @@ export const Target = {
1818
}
1919
/** @enum number */
2020
export const Second = {
21-
>Second : Symbol(Second, Decl(index.js, 9, 12))
21+
>Second : Symbol(Second, Decl(index.js, 9, 12), Decl(index.js, 8, 4))
2222

2323
OK: 1,
2424
>OK : Symbol(OK, Decl(index.js, 9, 23))
@@ -29,7 +29,7 @@ export const Second = {
2929
}
3030
/** @enum {function(number): number} */
3131
export const Fs = {
32-
>Fs : Symbol(Fs, Decl(index.js, 15, 12))
32+
>Fs : Symbol(Fs, Decl(index.js, 15, 12), Decl(index.js, 14, 4))
3333

3434
ADD1: n => n + 1,
3535
>ADD1 : Symbol(ADD1, Decl(index.js, 15, 19))
@@ -77,7 +77,7 @@ export function consume(t,s,f) {
7777
var v = Target.START
7878
>v : Symbol(v, Decl(index.js, 34, 7))
7979
>Target.START : Symbol(START, Decl(index.js, 1, 23))
80-
>Target : Symbol(Target, Decl(index.js, 1, 12))
80+
>Target : Symbol(Target, Decl(index.js, 1, 12), Decl(index.js, 0, 4))
8181
>START : Symbol(START, Decl(index.js, 1, 23))
8282

8383
v = 'something else' // allowed, like Typescript's classic enums and unlike its string enums
@@ -90,14 +90,14 @@ export function ff(s) {
9090

9191
// element access with arbitrary string is an error only with noImplicitAny
9292
if (!Target[s]) {
93-
>Target : Symbol(Target, Decl(index.js, 1, 12))
93+
>Target : Symbol(Target, Decl(index.js, 1, 12), Decl(index.js, 0, 4))
9494
>s : Symbol(s, Decl(index.js, 38, 19))
9595

9696
return null
9797
}
9898
else {
9999
return Target[s]
100-
>Target : Symbol(Target, Decl(index.js, 1, 12))
100+
>Target : Symbol(Target, Decl(index.js, 1, 12), Decl(index.js, 0, 4))
101101
>s : Symbol(s, Decl(index.js, 38, 19))
102102
}
103103
}

0 commit comments

Comments
 (0)