Skip to content

Commit 4cfff89

Browse files
authored
Fix declaration emit for cross-file enums (#28237)
1 parent 0481d44 commit 4cfff89

File tree

6 files changed

+188
-8
lines changed

6 files changed

+188
-8
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3222,15 +3222,17 @@ namespace ts {
32223222
}
32233223
if (type.flags & TypeFlags.EnumLiteral && !(type.flags & TypeFlags.Union)) {
32243224
const parentSymbol = getParentOfSymbol(type.symbol)!;
3225-
const parentName = symbolToName(parentSymbol, context, SymbolFlags.Type, /*expectsIdentifier*/ false);
3226-
const enumLiteralName = getDeclaredTypeOfSymbol(parentSymbol) === type ? parentName : createQualifiedName(parentName, symbolName(type.symbol));
3227-
context.approximateLength += symbolName(type.symbol).length;
3228-
return createTypeReferenceNode(enumLiteralName, /*typeArguments*/ undefined);
3225+
const parentName = symbolToTypeNode(parentSymbol, context, SymbolFlags.Type);
3226+
const enumLiteralName = getDeclaredTypeOfSymbol(parentSymbol) === type
3227+
? parentName
3228+
: appendReferenceToType(
3229+
parentName as TypeReferenceNode | ImportTypeNode,
3230+
createTypeReferenceNode(symbolName(type.symbol), /*typeArguments*/ undefined)
3231+
);
3232+
return enumLiteralName;
32293233
}
32303234
if (type.flags & TypeFlags.EnumLike) {
3231-
const name = symbolToName(type.symbol, context, SymbolFlags.Type, /*expectsIdentifier*/ false);
3232-
context.approximateLength += symbolName(type.symbol).length;
3233-
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
3235+
return symbolToTypeNode(type.symbol, context, SymbolFlags.Type);
32343236
}
32353237
if (type.flags & TypeFlags.StringLiteral) {
32363238
context.approximateLength += ((<StringLiteralType>type).value.length + 2);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [tests/cases/compiler/declarationEmitQualifiedAliasTypeArgument.ts] ////
2+
3+
//// [bbb.d.ts]
4+
export interface INode<T> {
5+
data: T;
6+
}
7+
8+
export function create<T>(): () => INode<T>;
9+
//// [lib.d.ts]
10+
export type G<T extends string> = { [P in T]: string };
11+
12+
export enum E {
13+
A = "a",
14+
B = "b"
15+
}
16+
17+
export type T = G<E>;
18+
19+
export type Q = G<E.A>;
20+
21+
//// [index.ts]
22+
import { T, Q } from "./lib";
23+
import { create } from "./bbb";
24+
25+
export const fun = create<T>();
26+
27+
export const fun2 = create<Q>();
28+
29+
30+
//// [index.js]
31+
"use strict";
32+
exports.__esModule = true;
33+
var bbb_1 = require("./bbb");
34+
exports.fun = bbb_1.create();
35+
exports.fun2 = bbb_1.create();
36+
37+
38+
//// [index.d.ts]
39+
export declare const fun: () => import("./bbb").INode<import("./lib").G<import("./lib").E>>;
40+
export declare const fun2: () => import("./bbb").INode<import("./lib").G<import("./lib").E.A>>;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
=== tests/cases/compiler/bbb.d.ts ===
2+
export interface INode<T> {
3+
>INode : Symbol(INode, Decl(bbb.d.ts, 0, 0))
4+
>T : Symbol(T, Decl(bbb.d.ts, 0, 23))
5+
6+
data: T;
7+
>data : Symbol(INode.data, Decl(bbb.d.ts, 0, 27))
8+
>T : Symbol(T, Decl(bbb.d.ts, 0, 23))
9+
}
10+
11+
export function create<T>(): () => INode<T>;
12+
>create : Symbol(create, Decl(bbb.d.ts, 2, 1))
13+
>T : Symbol(T, Decl(bbb.d.ts, 4, 23))
14+
>INode : Symbol(INode, Decl(bbb.d.ts, 0, 0))
15+
>T : Symbol(T, Decl(bbb.d.ts, 4, 23))
16+
17+
=== tests/cases/compiler/lib.d.ts ===
18+
export type G<T extends string> = { [P in T]: string };
19+
>G : Symbol(G, Decl(lib.d.ts, --, --))
20+
>T : Symbol(T, Decl(lib.d.ts, --, --))
21+
>P : Symbol(P, Decl(lib.d.ts, --, --))
22+
>T : Symbol(T, Decl(lib.d.ts, --, --))
23+
24+
export enum E {
25+
>E : Symbol(E, Decl(lib.d.ts, --, --))
26+
27+
A = "a",
28+
>A : Symbol(E.A, Decl(lib.d.ts, --, --))
29+
30+
B = "b"
31+
>B : Symbol(E.B, Decl(lib.d.ts, --, --))
32+
}
33+
34+
export type T = G<E>;
35+
>T : Symbol(T, Decl(lib.d.ts, --, --))
36+
>G : Symbol(G, Decl(lib.d.ts, --, --))
37+
>E : Symbol(E, Decl(lib.d.ts, --, --))
38+
39+
export type Q = G<E.A>;
40+
>Q : Symbol(Q, Decl(lib.d.ts, --, --))
41+
>G : Symbol(G, Decl(lib.d.ts, --, --))
42+
>E : Symbol(E, Decl(lib.d.ts, --, --))
43+
>A : Symbol(E.A, Decl(lib.d.ts, --, --))
44+
45+
=== tests/cases/compiler/index.ts ===
46+
import { T, Q } from "./lib";
47+
>T : Symbol(T, Decl(index.ts, 0, 8))
48+
>Q : Symbol(Q, Decl(index.ts, 0, 11))
49+
50+
import { create } from "./bbb";
51+
>create : Symbol(create, Decl(index.ts, 1, 8))
52+
53+
export const fun = create<T>();
54+
>fun : Symbol(fun, Decl(index.ts, 3, 12))
55+
>create : Symbol(create, Decl(index.ts, 1, 8))
56+
>T : Symbol(T, Decl(index.ts, 0, 8))
57+
58+
export const fun2 = create<Q>();
59+
>fun2 : Symbol(fun2, Decl(index.ts, 5, 12))
60+
>create : Symbol(create, Decl(index.ts, 1, 8))
61+
>Q : Symbol(Q, Decl(index.ts, 0, 11))
62+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
=== tests/cases/compiler/bbb.d.ts ===
2+
export interface INode<T> {
3+
data: T;
4+
>data : T
5+
}
6+
7+
export function create<T>(): () => INode<T>;
8+
>create : <T>() => () => INode<T>
9+
10+
=== tests/cases/compiler/lib.d.ts ===
11+
export type G<T extends string> = { [P in T]: string };
12+
>G : G<T>
13+
14+
export enum E {
15+
>E : E
16+
17+
A = "a",
18+
>A : E.A
19+
>"a" : "a"
20+
21+
B = "b"
22+
>B : E.B
23+
>"b" : "b"
24+
}
25+
26+
export type T = G<E>;
27+
>T : G<E>
28+
29+
export type Q = G<E.A>;
30+
>Q : G<E.A>
31+
>E : any
32+
33+
=== tests/cases/compiler/index.ts ===
34+
import { T, Q } from "./lib";
35+
>T : any
36+
>Q : any
37+
38+
import { create } from "./bbb";
39+
>create : <T>() => () => import("tests/cases/compiler/bbb").INode<T>
40+
41+
export const fun = create<T>();
42+
>fun : () => import("tests/cases/compiler/bbb").INode<import("tests/cases/compiler/lib").G<import("tests/cases/compiler/lib").E>>
43+
>create<T>() : () => import("tests/cases/compiler/bbb").INode<import("tests/cases/compiler/lib").G<import("tests/cases/compiler/lib").E>>
44+
>create : <T>() => () => import("tests/cases/compiler/bbb").INode<T>
45+
46+
export const fun2 = create<Q>();
47+
>fun2 : () => import("tests/cases/compiler/bbb").INode<import("tests/cases/compiler/lib").G<import("tests/cases/compiler/lib").E.A>>
48+
>create<Q>() : () => import("tests/cases/compiler/bbb").INode<import("tests/cases/compiler/lib").G<import("tests/cases/compiler/lib").E.A>>
49+
>create : <T>() => () => import("tests/cases/compiler/bbb").INode<T>
50+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// @declaration: true
2+
// @filename: bbb.d.ts
3+
export interface INode<T> {
4+
data: T;
5+
}
6+
7+
export function create<T>(): () => INode<T>;
8+
// @filename: lib.d.ts
9+
export type G<T extends string> = { [P in T]: string };
10+
11+
export enum E {
12+
A = "a",
13+
B = "b"
14+
}
15+
16+
export type T = G<E>;
17+
18+
export type Q = G<E.A>;
19+
20+
// @filename: index.ts
21+
import { T, Q } from "./lib";
22+
import { create } from "./bbb";
23+
24+
export const fun = create<T>();
25+
26+
export const fun2 = create<Q>();

tests/cases/user/prettier/prettier

Submodule prettier updated 1910 files

0 commit comments

Comments
 (0)