Skip to content

Commit 06d426c

Browse files
authored
Fix const enum comments (#47990)
* chore: add test case * fix: const enum comment * fix: replace all unsafe pattern * chore: run regex only if element access
1 parent f82d0cb commit 06d426c

File tree

5 files changed

+352
-155
lines changed

5 files changed

+352
-155
lines changed

src/compiler/transformers/ts.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -3355,6 +3355,10 @@ namespace ts {
33553355
return substituteConstantValue(node);
33563356
}
33573357

3358+
function safeMultiLineComment(value: string): string {
3359+
return value.replace(/\*\//g, "*_/");
3360+
}
3361+
33583362
function substituteConstantValue(node: PropertyAccessExpression | ElementAccessExpression): LeftHandSideExpression {
33593363
const constantValue = tryGetConstEnumValue(node);
33603364
if (constantValue !== undefined) {
@@ -3366,7 +3370,7 @@ namespace ts {
33663370
const originalNode = getOriginalNode(node, isAccessExpression);
33673371
const propertyName = isPropertyAccessExpression(originalNode)
33683372
? declarationNameToString(originalNode.name)
3369-
: getTextOfNode(originalNode.argumentExpression);
3373+
: safeMultiLineComment(getTextOfNode(originalNode.argumentExpression));
33703374

33713375
addSyntheticTrailingComment(substitute, SyntaxKind.MultiLineCommentTrivia, ` ${propertyName} `);
33723376
}

tests/baselines/reference/constEnums.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ const enum Enum1 {
3838
W5 = Enum1[`V`],
3939
}
4040

41+
const enum Comments {
42+
"//",
43+
"/*",
44+
"*/",
45+
"///",
46+
"#",
47+
"<!--",
48+
"-->",
49+
}
4150

4251
module A {
4352
export module B {
@@ -153,7 +162,21 @@ function bar(e: A.B.C.E): number {
153162
case A.B.C.E.V2: return 1;
154163
case A.B.C.E.V3: return 1;
155164
}
156-
}
165+
}
166+
167+
function baz(c: Comments) {
168+
switch (c) {
169+
case Comments["//"]:
170+
case Comments["/*"]:
171+
case Comments["*/"]:
172+
case Comments["///"]:
173+
case Comments["#"]:
174+
case Comments["<!--"]:
175+
case Comments["-->"]:
176+
break;
177+
}
178+
}
179+
157180

158181
//// [constEnums.js]
159182
var A2;
@@ -226,3 +249,15 @@ function bar(e) {
226249
case 64 /* V3 */: return 1;
227250
}
228251
}
252+
function baz(c) {
253+
switch (c) {
254+
case 0 /* "//" */:
255+
case 1 /* "/*" */:
256+
case 2 /* "*_/" */:
257+
case 3 /* "///" */:
258+
case 4 /* "#" */:
259+
case 5 /* "<!--" */:
260+
case 6 /* "-->" */:
261+
break;
262+
}
263+
}

0 commit comments

Comments
 (0)