Skip to content

Commit b229333

Browse files
committed
Only mark const enums as references with preserveConstEnums on in export assignments
1 parent 9a9a2df commit b229333

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17809,8 +17809,12 @@ namespace ts {
1780917809
return type;
1781017810
}
1781117811

17812+
function isExportOrExportExpression(location: Node) {
17813+
return !!findAncestor(location, e => e.parent && isExportAssignment(e.parent) && e.parent.expression === e && isEntityNameExpression(e));
17814+
}
17815+
1781217816
function markAliasReferenced(symbol: Symbol, location: Node) {
17813-
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value) && !isInTypeQuery(location) && (compilerOptions.preserveConstEnums || !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol)))) {
17817+
if (isNonLocalAlias(symbol, /*excludes*/ SymbolFlags.Value) && !isInTypeQuery(location) && ((compilerOptions.preserveConstEnums && isExportOrExportExpression(location)) || !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol)))) {
1781417818
markAliasSymbolAsReferenced(symbol);
1781517819
}
1781617820
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [tests/cases/compiler/amdModuleConstEnumUsage.ts] ////
2+
3+
//// [cc.ts]
4+
export const enum CharCode {
5+
A,
6+
B
7+
}
8+
//// [file.ts]
9+
import { CharCode } from 'defs/cc';
10+
export class User {
11+
method(input: number) {
12+
if (CharCode.A === input) {}
13+
}
14+
}
15+
16+
17+
//// [cc.js]
18+
define(["require", "exports"], function (require, exports) {
19+
"use strict";
20+
exports.__esModule = true;
21+
var CharCode;
22+
(function (CharCode) {
23+
CharCode[CharCode["A"] = 0] = "A";
24+
CharCode[CharCode["B"] = 1] = "B";
25+
})(CharCode = exports.CharCode || (exports.CharCode = {}));
26+
});
27+
//// [file.js]
28+
define(["require", "exports"], function (require, exports) {
29+
"use strict";
30+
exports.__esModule = true;
31+
var User = /** @class */ (function () {
32+
function User() {
33+
}
34+
User.prototype.method = function (input) {
35+
if (0 /* A */ === input) { }
36+
};
37+
return User;
38+
}());
39+
exports.User = User;
40+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== /proj/defs/cc.ts ===
2+
export const enum CharCode {
3+
>CharCode : Symbol(CharCode, Decl(cc.ts, 0, 0))
4+
5+
A,
6+
>A : Symbol(CharCode.A, Decl(cc.ts, 0, 28))
7+
8+
B
9+
>B : Symbol(CharCode.B, Decl(cc.ts, 1, 6))
10+
}
11+
=== /proj/component/file.ts ===
12+
import { CharCode } from 'defs/cc';
13+
>CharCode : Symbol(CharCode, Decl(file.ts, 0, 8))
14+
15+
export class User {
16+
>User : Symbol(User, Decl(file.ts, 0, 35))
17+
18+
method(input: number) {
19+
>method : Symbol(User.method, Decl(file.ts, 1, 19))
20+
>input : Symbol(input, Decl(file.ts, 2, 11))
21+
22+
if (CharCode.A === input) {}
23+
>CharCode.A : Symbol(CharCode.A, Decl(cc.ts, 0, 28))
24+
>CharCode : Symbol(CharCode, Decl(file.ts, 0, 8))
25+
>A : Symbol(CharCode.A, Decl(cc.ts, 0, 28))
26+
>input : Symbol(input, Decl(file.ts, 2, 11))
27+
}
28+
}
29+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== /proj/defs/cc.ts ===
2+
export const enum CharCode {
3+
>CharCode : CharCode
4+
5+
A,
6+
>A : CharCode.A
7+
8+
B
9+
>B : CharCode.B
10+
}
11+
=== /proj/component/file.ts ===
12+
import { CharCode } from 'defs/cc';
13+
>CharCode : typeof CharCode
14+
15+
export class User {
16+
>User : User
17+
18+
method(input: number) {
19+
>method : (input: number) => void
20+
>input : number
21+
22+
if (CharCode.A === input) {}
23+
>CharCode.A === input : boolean
24+
>CharCode.A : CharCode.A
25+
>CharCode : typeof CharCode
26+
>A : CharCode.A
27+
>input : number
28+
}
29+
}
30+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @module: amd
2+
// @preserveConstEnums: true
3+
// @baseUrl: /proj
4+
// @filename: /proj/defs/cc.ts
5+
export const enum CharCode {
6+
A,
7+
B
8+
}
9+
// @filename: /proj/component/file.ts
10+
11+
import { CharCode } from 'defs/cc';
12+
export class User {
13+
method(input: number) {
14+
if (CharCode.A === input) {}
15+
}
16+
}

0 commit comments

Comments
 (0)