Skip to content

Commit f3bc7a4

Browse files
fiskerota-meshi
andauthored
fix: fix crash when evaluating Symbol.prototype in getStringIfConstant (#182)
Co-authored-by: Yosuke Ota <[email protected]>
1 parent 324b445 commit f3bc7a4

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/get-string-if-constant.mjs

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,15 @@ export function getStringIfConstant(node, initialScope = null) {
1818
}
1919

2020
const evaluated = getStaticValue(node, initialScope)
21-
return evaluated && String(evaluated.value)
21+
22+
if (evaluated) {
23+
// `String(Symbol.prototype)` throws error
24+
try {
25+
return String(evaluated.value)
26+
} catch {
27+
// No op
28+
}
29+
}
30+
31+
return null
2232
}

test/get-string-if-constant.mjs

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ describe("The 'getStringIfConstant' function", () => {
6060
{ code: "let id = 'abc'; id = 'foo'; id", expected: null },
6161
{ code: "var id = 'abc'; id = 'foo'; id", expected: null },
6262
{ code: "const id = otherId; id", expected: null },
63+
{ code: "Symbol.prototype", expected: null },
6364
]) {
6465
it(`should return ${JSON.stringify(expected)} from ${code}`, () => {
6566
const linter = newCompatLinter()
6667

6768
let actual = null
6869
linter.verify(code, {
69-
languageOptions: { ecmaVersion: 2020 },
70+
languageOptions: {
71+
ecmaVersion: 2020,
72+
globals: { Symbol: "readonly" },
73+
},
7074
rules: { "test/test": "error" },
7175
plugins: {
7276
test: {

0 commit comments

Comments
 (0)