Skip to content

Commit cc8399d

Browse files
author
Andy
authored
Escape string literal before looking it up in enum's symbol table (#17441)
1 parent b080aa9 commit cc8399d

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21517,7 +21517,7 @@ namespace ts {
2151721517
else {
2151821518
const argument = ex.argumentExpression;
2151921519
Debug.assert(isLiteralExpression(argument));
21520-
name = (argument as LiteralExpression).text as __String; // TODO: GH#17348
21520+
name = escapeLeadingUnderscores((argument as LiteralExpression).text);
2152121521
}
2152221522
return evaluateEnumMember(expr, type.symbol, name);
2152321523
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [underscoreEscapedNameInEnum.ts]
2+
enum E {
3+
"__foo" = 1,
4+
bar = E["__foo"] + 1
5+
}
6+
7+
8+
//// [underscoreEscapedNameInEnum.js]
9+
var E;
10+
(function (E) {
11+
E[E["__foo"] = 1] = "__foo";
12+
E[E["bar"] = 2] = "bar";
13+
})(E || (E = {}));
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/compiler/underscoreEscapedNameInEnum.ts ===
2+
enum E {
3+
>E : Symbol(E, Decl(underscoreEscapedNameInEnum.ts, 0, 0))
4+
5+
"__foo" = 1,
6+
bar = E["__foo"] + 1
7+
>bar : Symbol(E.bar, Decl(underscoreEscapedNameInEnum.ts, 1, 16))
8+
>E : Symbol(E, Decl(underscoreEscapedNameInEnum.ts, 0, 0))
9+
}
10+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/underscoreEscapedNameInEnum.ts ===
2+
enum E {
3+
>E : E
4+
5+
"__foo" = 1,
6+
>1 : 1
7+
8+
bar = E["__foo"] + 1
9+
>bar : E
10+
>E["__foo"] + 1 : number
11+
>E["__foo"] : E
12+
>E : typeof E
13+
>"__foo" : "__foo"
14+
>1 : 1
15+
}
16+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum E {
2+
"__foo" = 1,
3+
bar = E["__foo"] + 1
4+
}

0 commit comments

Comments
 (0)