Skip to content

Commit 45affe4

Browse files
committed
Merge pull request #3944 from Microsoft/constEnumModules
treat modules that are merged with values as non-const-enum
2 parents 4e0acc9 + 43347c7 commit 45affe4

22 files changed

+324
-9
lines changed

src/compiler/binder.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -518,15 +518,21 @@ namespace ts {
518518
}
519519
else {
520520
declareSymbolAndAddToSymbolTable(node, SymbolFlags.ValueModule, SymbolFlags.ValueModuleExcludes);
521-
522-
let currentModuleIsConstEnumOnly = state === ModuleInstanceState.ConstEnumOnly;
523-
if (node.symbol.constEnumOnlyModule === undefined) {
524-
// non-merged case - use the current state
525-
node.symbol.constEnumOnlyModule = currentModuleIsConstEnumOnly;
521+
if (node.symbol.flags & (SymbolFlags.Function | SymbolFlags.Class | SymbolFlags.RegularEnum)) {
522+
// if module was already merged with some function, class or non-const enum
523+
// treat is a non-const-enum-only
524+
node.symbol.constEnumOnlyModule = false;
526525
}
527526
else {
528-
// merged case: module is const enum only if all its pieces are non-instantiated or const enum
529-
node.symbol.constEnumOnlyModule = node.symbol.constEnumOnlyModule && currentModuleIsConstEnumOnly;
527+
let currentModuleIsConstEnumOnly = state === ModuleInstanceState.ConstEnumOnly;
528+
if (node.symbol.constEnumOnlyModule === undefined) {
529+
// non-merged case - use the current state
530+
node.symbol.constEnumOnlyModule = currentModuleIsConstEnumOnly;
531+
}
532+
else {
533+
// merged case: module is const enum only if all its pieces are non-instantiated or const enum
534+
node.symbol.constEnumOnlyModule = node.symbol.constEnumOnlyModule && currentModuleIsConstEnumOnly;
535+
}
530536
}
531537
}
532538
}
@@ -1056,4 +1062,4 @@ namespace ts {
10561062
: declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes);
10571063
}
10581064
}
1059-
}
1065+
}

src/compiler/checker.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -14037,7 +14037,11 @@ namespace ts {
1403714037
return true;
1403814038
}
1403914039
// const enums and modules that contain only const enums are not considered values from the emit perespective
14040-
return target !== unknownSymbol && target && target.flags & SymbolFlags.Value && !isConstEnumOrConstEnumOnlyModule(target);
14040+
// unless 'preserveConstEnums' option is set to true
14041+
return target !== unknownSymbol &&
14042+
target &&
14043+
target.flags & SymbolFlags.Value &&
14044+
(compilerOptions.preserveConstEnums || !isConstEnumOrConstEnumOnlyModule(target));
1404114045
}
1404214046

1404314047
function isConstEnumOrConstEnumOnlyModule(s: Symbol): boolean {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [constEnumMergingWithValues1.ts]
2+
3+
function foo() {}
4+
module foo {
5+
const enum E { X }
6+
}
7+
8+
export = foo
9+
10+
//// [constEnumMergingWithValues1.js]
11+
define(["require", "exports"], function (require, exports) {
12+
function foo() { }
13+
return foo;
14+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/constEnumMergingWithValues1.ts ===
2+
3+
function foo() {}
4+
>foo : Symbol(foo, Decl(constEnumMergingWithValues1.ts, 0, 0), Decl(constEnumMergingWithValues1.ts, 1, 17))
5+
6+
module foo {
7+
>foo : Symbol(foo, Decl(constEnumMergingWithValues1.ts, 0, 0), Decl(constEnumMergingWithValues1.ts, 1, 17))
8+
9+
const enum E { X }
10+
>E : Symbol(E, Decl(constEnumMergingWithValues1.ts, 2, 12))
11+
>X : Symbol(E.X, Decl(constEnumMergingWithValues1.ts, 3, 18))
12+
}
13+
14+
export = foo
15+
>foo : Symbol(foo, Decl(constEnumMergingWithValues1.ts, 0, 0), Decl(constEnumMergingWithValues1.ts, 1, 17))
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/constEnumMergingWithValues1.ts ===
2+
3+
function foo() {}
4+
>foo : typeof foo
5+
6+
module foo {
7+
>foo : typeof foo
8+
9+
const enum E { X }
10+
>E : E
11+
>X : E
12+
}
13+
14+
export = foo
15+
>foo : typeof foo
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [constEnumMergingWithValues2.ts]
2+
3+
class foo {}
4+
module foo {
5+
const enum E { X }
6+
}
7+
8+
export = foo
9+
10+
//// [constEnumMergingWithValues2.js]
11+
define(["require", "exports"], function (require, exports) {
12+
var foo = (function () {
13+
function foo() {
14+
}
15+
return foo;
16+
})();
17+
return foo;
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/constEnumMergingWithValues2.ts ===
2+
3+
class foo {}
4+
>foo : Symbol(foo, Decl(constEnumMergingWithValues2.ts, 0, 0), Decl(constEnumMergingWithValues2.ts, 1, 12))
5+
6+
module foo {
7+
>foo : Symbol(foo, Decl(constEnumMergingWithValues2.ts, 0, 0), Decl(constEnumMergingWithValues2.ts, 1, 12))
8+
9+
const enum E { X }
10+
>E : Symbol(E, Decl(constEnumMergingWithValues2.ts, 2, 12))
11+
>X : Symbol(E.X, Decl(constEnumMergingWithValues2.ts, 3, 18))
12+
}
13+
14+
export = foo
15+
>foo : Symbol(foo, Decl(constEnumMergingWithValues2.ts, 0, 0), Decl(constEnumMergingWithValues2.ts, 1, 12))
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/constEnumMergingWithValues2.ts ===
2+
3+
class foo {}
4+
>foo : foo
5+
6+
module foo {
7+
>foo : typeof foo
8+
9+
const enum E { X }
10+
>E : E
11+
>X : E
12+
}
13+
14+
export = foo
15+
>foo : foo
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [constEnumMergingWithValues3.ts]
2+
3+
enum foo { A }
4+
module foo {
5+
const enum E { X }
6+
}
7+
8+
export = foo
9+
10+
//// [constEnumMergingWithValues3.js]
11+
define(["require", "exports"], function (require, exports) {
12+
var foo;
13+
(function (foo) {
14+
foo[foo["A"] = 0] = "A";
15+
})(foo || (foo = {}));
16+
return foo;
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/constEnumMergingWithValues3.ts ===
2+
3+
enum foo { A }
4+
>foo : Symbol(foo, Decl(constEnumMergingWithValues3.ts, 0, 0), Decl(constEnumMergingWithValues3.ts, 1, 14))
5+
>A : Symbol(foo.A, Decl(constEnumMergingWithValues3.ts, 1, 10))
6+
7+
module foo {
8+
>foo : Symbol(foo, Decl(constEnumMergingWithValues3.ts, 0, 0), Decl(constEnumMergingWithValues3.ts, 1, 14))
9+
10+
const enum E { X }
11+
>E : Symbol(E, Decl(constEnumMergingWithValues3.ts, 2, 12))
12+
>X : Symbol(E.X, Decl(constEnumMergingWithValues3.ts, 3, 18))
13+
}
14+
15+
export = foo
16+
>foo : Symbol(foo, Decl(constEnumMergingWithValues3.ts, 0, 0), Decl(constEnumMergingWithValues3.ts, 1, 14))
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/constEnumMergingWithValues3.ts ===
2+
3+
enum foo { A }
4+
>foo : foo
5+
>A : foo
6+
7+
module foo {
8+
>foo : typeof foo
9+
10+
const enum E { X }
11+
>E : E
12+
>X : E
13+
}
14+
15+
export = foo
16+
>foo : foo
17+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [constEnumMergingWithValues4.ts]
2+
3+
module foo {
4+
const enum E { X }
5+
}
6+
7+
module foo {
8+
var x = 1;
9+
}
10+
11+
12+
export = foo
13+
14+
//// [constEnumMergingWithValues4.js]
15+
define(["require", "exports"], function (require, exports) {
16+
var foo;
17+
(function (foo) {
18+
var x = 1;
19+
})(foo || (foo = {}));
20+
return foo;
21+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/constEnumMergingWithValues4.ts ===
2+
3+
module foo {
4+
>foo : Symbol(foo, Decl(constEnumMergingWithValues4.ts, 0, 0), Decl(constEnumMergingWithValues4.ts, 3, 1))
5+
6+
const enum E { X }
7+
>E : Symbol(E, Decl(constEnumMergingWithValues4.ts, 1, 12))
8+
>X : Symbol(E.X, Decl(constEnumMergingWithValues4.ts, 2, 18))
9+
}
10+
11+
module foo {
12+
>foo : Symbol(foo, Decl(constEnumMergingWithValues4.ts, 0, 0), Decl(constEnumMergingWithValues4.ts, 3, 1))
13+
14+
var x = 1;
15+
>x : Symbol(x, Decl(constEnumMergingWithValues4.ts, 6, 7))
16+
}
17+
18+
19+
export = foo
20+
>foo : Symbol(foo, Decl(constEnumMergingWithValues4.ts, 0, 0), Decl(constEnumMergingWithValues4.ts, 3, 1))
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/constEnumMergingWithValues4.ts ===
2+
3+
module foo {
4+
>foo : typeof foo
5+
6+
const enum E { X }
7+
>E : E
8+
>X : E
9+
}
10+
11+
module foo {
12+
>foo : typeof foo
13+
14+
var x = 1;
15+
>x : number
16+
>1 : number
17+
}
18+
19+
20+
export = foo
21+
>foo : typeof foo
22+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//// [constEnumMergingWithValues5.ts]
2+
3+
module foo {
4+
const enum E { X }
5+
}
6+
7+
export = foo
8+
9+
//// [constEnumMergingWithValues5.js]
10+
define(["require", "exports"], function (require, exports) {
11+
var foo;
12+
(function (foo) {
13+
var E;
14+
(function (E) {
15+
E[E["X"] = 0] = "X";
16+
})(E || (E = {}));
17+
})(foo || (foo = {}));
18+
return foo;
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/constEnumMergingWithValues5.ts ===
2+
3+
module foo {
4+
>foo : Symbol(foo, Decl(constEnumMergingWithValues5.ts, 0, 0))
5+
6+
const enum E { X }
7+
>E : Symbol(E, Decl(constEnumMergingWithValues5.ts, 1, 12))
8+
>X : Symbol(E.X, Decl(constEnumMergingWithValues5.ts, 2, 18))
9+
}
10+
11+
export = foo
12+
>foo : Symbol(foo, Decl(constEnumMergingWithValues5.ts, 0, 0))
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/constEnumMergingWithValues5.ts ===
2+
3+
module foo {
4+
>foo : typeof foo
5+
6+
const enum E { X }
7+
>E : E
8+
>X : E
9+
}
10+
11+
export = foo
12+
>foo : typeof foo
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@module: amd
2+
//@filename: m1.ts
3+
4+
function foo() {}
5+
module foo {
6+
const enum E { X }
7+
}
8+
9+
export = foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@module: amd
2+
//@filename: m1.ts
3+
4+
class foo {}
5+
module foo {
6+
const enum E { X }
7+
}
8+
9+
export = foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@module: amd
2+
//@filename: m1.ts
3+
4+
enum foo { A }
5+
module foo {
6+
const enum E { X }
7+
}
8+
9+
export = foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@module: amd
2+
//@filename: m1.ts
3+
4+
module foo {
5+
const enum E { X }
6+
}
7+
8+
module foo {
9+
var x = 1;
10+
}
11+
12+
13+
export = foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@module: amd
2+
//@filename: m1.ts
3+
//@preserveConstEnums: true
4+
5+
module foo {
6+
const enum E { X }
7+
}
8+
9+
export = foo

0 commit comments

Comments
 (0)