Skip to content

fix(44477): confuse comment emit when const multi enum has same key #44545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3368,13 +3368,9 @@ namespace ts {
const substitute = typeof constantValue === "string" ? factory.createStringLiteral(constantValue) : factory.createNumericLiteral(constantValue);
if (!compilerOptions.removeComments) {
const originalNode = getOriginalNode(node, isAccessExpression);
const propertyName = isPropertyAccessExpression(originalNode)
? declarationNameToString(originalNode.name)
: safeMultiLineComment(getTextOfNode(originalNode.argumentExpression));

addSyntheticTrailingComment(substitute, SyntaxKind.MultiLineCommentTrivia, ` ${propertyName} `);
addSyntheticTrailingComment(substitute, SyntaxKind.MultiLineCommentTrivia, ` ${safeMultiLineComment(getTextOfNode(originalNode))} `);
}

return substitute;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/amdModuleConstEnumUsage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ define(["require", "exports"], function (require, exports) {
function User() {
}
User.prototype.method = function (input) {
if (0 /* A */ === input) { }
if (0 /* CharCode.A */ === input) { }
};
return User;
}());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function foo(x) {
var y = x; // Ok
}
foo(5);
foo(0 /* A */);
foo(0 /* E.A */);
var A = /** @class */ (function () {
function A() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ function foo1() {
})(E || (E = {}));
}
function foo2() {
return 0 /* A */;
return 0 /* E.A */;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function foo1() {
})(E || (E = {}));
}
function foo2() {
return 0 /* A */;
return 0 /* E.A */;
var E;
(function (E) {
E[E["A"] = 0] = "A";
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/constEnum3.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ f2('bar')
//// [constEnum3.js]
function f1(f) { }
function f2(f) { }
f1(0 /* foo */);
f1(1 /* bar */);
f1(0 /* TestType.foo */);
f1(1 /* TestType.bar */);
f2('foo');
f2('bar');
2 changes: 1 addition & 1 deletion tests/baselines/reference/constEnumExternalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ define(["require", "exports"], function (require, exports) {
define(["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
var v = 100 /* V */;
var v = 100 /* A.V */;
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ exports.fooFunc = fooFunc;
exports.__esModule = true;
function check(x) {
switch (x) {
case 0 /* Some */:
case 0 /* Foo.ConstFooEnum.Some */:
break;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = Foo.ConstEnumOnlyModule;
exports.__esModule = true;
function check(x) {
switch (x) {
case 0 /* Some */:
case 0 /* Foo.ConstFooEnum.Some */:
break;
}
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/constEnumNoEmitReexport.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ exports.__esModule = true;
//// [Usage1.js]
"use strict";
exports.__esModule = true;
0 /* Foo */;
0 /* Foo */;
0 /* MyConstEnum1.Foo */;
0 /* MyConstEnum2.Foo */;
//// [Usage2.js]
"use strict";
exports.__esModule = true;
0 /* Foo */;
0 /* MyConstEnum.Foo */;
//// [Usage3.js]
"use strict";
exports.__esModule = true;
0 /* Foo */;
0 /* MyConstEnum.Foo */;
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ StillEnum.Foo;
//// [usages.js]
"use strict";
exports.__esModule = true;
0 /* Foo */;
0 /* Foo */;
0 /* Foo */;
0 /* MyConstEnum.Foo */;
0 /* AlsoEnum.Foo */;
0 /* StillEnum.Foo */;
2 changes: 1 addition & 1 deletion tests/baselines/reference/constEnumOnlyModuleMerging.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ var Outer;
var B;
(function (B) {
var O = Outer;
var x = 0 /* X */;
var x = 0 /* O.A.X */;
var y = O.x;
})(B || (B = {}));
12 changes: 6 additions & 6 deletions tests/baselines/reference/constEnumPropertyAccess1.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class C {
var o = {
1: true
};
var a = 1 /* A */;
var a1 = 1 /* "A" */;
var g = o[1 /* A */];
var a = 1 /* G.A */;
var a1 = 1 /* G["A"] */;
var g = o[1 /* G.A */];
class C {
[1 /* A */]() { }
get [2 /* B */]() {
[1 /* G.A */]() { }
get [2 /* G.B */]() {
return true;
}
set [2 /* B */](x) { }
set [2 /* G.B */](x) { }
}


Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/constEnumPropertyAccess2.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ G.B = 3;
// than a property access that selects one of the enum's members
// Error from referring constant enum in any other context than a property access
var z = G;
var z1 = G[1 /* A */];
var z1 = G[1 /* G.A */];
var g;
g = "string";
function foo(x) { }
2 /* B */ = 3;
2 /* G.B */ = 3;


//// [constEnumPropertyAccess2.d.ts]
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/constEnumSyntheticNodesComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ function assert(x) {
}
function verify(a) {
switch (a) {
case 0 /* A */:
case 0 /* En.A */:
return assert(a);
case 1 /* "B" */:
case 1 /* En["B"] */:
return assert(a);
case 2 /* `C` */:
case 2 /* En[`C`] */:
return assert(a);
case 3 /* "\u{44}" */:
case 3 /* En["\u{44}"] */:
return assert(a);
}
}
24 changes: 12 additions & 12 deletions tests/baselines/reference/constEnumToStringWithComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ let c1 = Foo["C"].toString();


//// [constEnumToStringWithComments.js]
var x0 = 100 /* X */.toString();
var x1 = 100 /* "X" */.toString();
var y0 = 0.5 /* Y */.toString();
var y1 = 0.5 /* "Y" */.toString();
var z0 = 2 /* Z */.toString();
var z1 = 2 /* "Z" */.toString();
var a0 = -1 /* A */.toString();
var a1 = -1 /* "A" */.toString();
var b0 = -1.5 /* B */.toString();
var b1 = -1.5 /* "B" */.toString();
var c0 = -1 /* C */.toString();
var c1 = -1 /* "C" */.toString();
var x0 = 100 /* Foo.X */.toString();
var x1 = 100 /* Foo["X"] */.toString();
var y0 = 0.5 /* Foo.Y */.toString();
var y1 = 0.5 /* Foo["Y"] */.toString();
var z0 = 2 /* Foo.Z */.toString();
var z1 = 2 /* Foo["Z"] */.toString();
var a0 = -1 /* Foo.A */.toString();
var a1 = -1 /* Foo["A"] */.toString();
var b0 = -1.5 /* Foo.B */.toString();
var b1 = -1.5 /* Foo["B"] */.toString();
var c0 = -1 /* Foo.C */.toString();
var c1 = -1 /* Foo["C"] */.toString();
88 changes: 44 additions & 44 deletions tests/baselines/reference/constEnums.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,72 +192,72 @@ var A2;
})(A2 || (A2 = {}));
var I2 = A2.B;
function foo0(e) {
if (e === 1 /* V1 */) {
if (e === 1 /* I.V1 */) {
}
else if (e === 101 /* V2 */) {
else if (e === 101 /* I.V2 */) {
}
}
function foo1(e) {
if (e === 10 /* V1 */) {
if (e === 10 /* I1.C.E.V1 */) {
}
else if (e === 110 /* V2 */) {
else if (e === 110 /* I1.C.E.V2 */) {
}
}
function foo2(e) {
if (e === 10 /* V1 */) {
if (e === 10 /* I2.C.E.V1 */) {
}
else if (e === 110 /* V2 */) {
else if (e === 110 /* I2.C.E.V2 */) {
}
}
function foo(x) {
switch (x) {
case 0 /* A */:
case 1 /* B */:
case 10 /* C */:
case 1 /* D */:
case 1 /* E */:
case 1 /* F */:
case 1 /* G */:
case -2 /* H */:
case 0 /* I */:
case 0 /* J */:
case -6 /* K */:
case -2 /* L */:
case 2 /* M */:
case 2 /* N */:
case 0 /* O */:
case 0 /* P */:
case 1 /* PQ */:
case -1 /* Q */:
case 0 /* R */:
case 0 /* S */:
case 11 /* "T" */:
case 11 /* `U` */:
case 11 /* V */:
case 11 /* W */:
case 100 /* W1 */:
case 100 /* W2 */:
case 100 /* W3 */:
case 11 /* W4 */:
case 0 /* Enum1.A */:
case 1 /* Enum1.B */:
case 10 /* Enum1.C */:
case 1 /* Enum1.D */:
case 1 /* Enum1.E */:
case 1 /* Enum1.F */:
case 1 /* Enum1.G */:
case -2 /* Enum1.H */:
case 0 /* Enum1.I */:
case 0 /* Enum1.J */:
case -6 /* Enum1.K */:
case -2 /* Enum1.L */:
case 2 /* Enum1.M */:
case 2 /* Enum1.N */:
case 0 /* Enum1.O */:
case 0 /* Enum1.P */:
case 1 /* Enum1.PQ */:
case -1 /* Enum1.Q */:
case 0 /* Enum1.R */:
case 0 /* Enum1.S */:
case 11 /* Enum1["T"] */:
case 11 /* Enum1[`U`] */:
case 11 /* Enum1.V */:
case 11 /* Enum1.W */:
case 100 /* Enum1.W1 */:
case 100 /* Enum1.W2 */:
case 100 /* Enum1.W3 */:
case 11 /* Enum1.W4 */:
break;
}
}
function bar(e) {
switch (e) {
case 1 /* V1 */: return 1;
case 101 /* V2 */: return 1;
case 64 /* V3 */: return 1;
case 1 /* A.B.C.E.V1 */: return 1;
case 101 /* A.B.C.E.V2 */: return 1;
case 64 /* A.B.C.E.V3 */: return 1;
}
}
function baz(c) {
switch (c) {
case 0 /* "//" */:
case 1 /* "/*" */:
case 2 /* "*_/" */:
case 3 /* "///" */:
case 4 /* "#" */:
case 5 /* "<!--" */:
case 6 /* "-->" */:
case 0 /* Comments["//"] */:
case 1 /* Comments["/*"] */:
case 2 /* Comments["*_/"] */:
case 3 /* Comments["///"] */:
case 4 /* Comments["#"] */:
case 5 /* Comments["<!--"] */:
case 6 /* Comments["-->"] */:
break;
}
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/constIndexedAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ let n3 = test[numbersNotConst.one];
var test;
var s = test[0];
var n = test[1];
var s1 = test[0 /* zero */];
var n1 = test[1 /* one */];
var s2 = test[0 /* "zero" */];
var n2 = test[1 /* "one" */];
var s1 = test[0 /* numbers.zero */];
var n1 = test[1 /* numbers.one */];
var s2 = test[0 /* numbers["zero"] */];
var n2 = test[1 /* numbers["one"] */];
var numbersNotConst;
(function (numbersNotConst) {
numbersNotConst[numbersNotConst["zero"] = 0] = "zero";
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/constantEnumAssert.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ var foo2 = { a: E2.a };
var foo3 = { a: E1.a };
var foo4 = { a: E2.a };
var foo5 = { a: E3.a };
var foo6 = { a: 0 /* a */ };
var foo6 = { a: 0 /* E4.a */ };
var foo7 = { a: E5.a };
var foo8 = { a: E1.a };
var foo9 = { a: E2.a };
var foo10 = { a: E3.a };
var foo11 = { a: 0 /* a */ };
var foo11 = { a: 0 /* E4.a */ };
var foo12 = { a: E5.a };
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ var A = /** @class */ (function () {
A.prototype.getA = function () {
var _a;
return _a = {},
_a["123123" /* Test1 */] = '123',
_a["12312312312" /* Test2 */] = '123',
_a["123123" /* TestEnum.Test1 */] = '123',
_a["12312312312" /* TestEnum.Test2 */] = '123',
_a;
};
return A;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ var E;
})(E || (E = {}));
function foo1(...a) { }
foo1(1, 2, 3, E.a);
foo1(1, 2, 3, 0 /* a */, E.b);
foo1(1, 2, 3, 0 /* E1.a */, E.b);
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ function foo1() {
}
}
foo1(1, 2, 3, E.a);
foo1(1, 2, 3, 0 /* a */, E.b);
foo1(1, 2, 3, 0 /* E1.a */, E.b);
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ var E;
})(E || (E = {}));
function foo1(...a) { }
foo1(1, 2, 3, E.a);
foo1(1, 2, 3, 0 /* a */, E.b);
foo1(1, 2, 3, 0 /* E1.a */, E.b);
4 changes: 2 additions & 2 deletions tests/baselines/reference/discriminantPropertyCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ function onlyPlus(arg) {
function func3(value) {
if (value.type !== undefined) {
switch (value.type) {
case 1 /* bar1 */:
case 1 /* BarEnum.bar1 */:
break;
case 2 /* bar2 */:
case 2 /* BarEnum.bar2 */:
break;
default:
never(value.type);
Expand Down
Loading