diff --git a/src/get-string-if-constant.mjs b/src/get-string-if-constant.mjs index ab03363..9d2e569 100644 --- a/src/get-string-if-constant.mjs +++ b/src/get-string-if-constant.mjs @@ -18,5 +18,15 @@ export function getStringIfConstant(node, initialScope = null) { } const evaluated = getStaticValue(node, initialScope) - return evaluated && String(evaluated.value) + + if (evaluated) { + // `String(Symbol.prototype)` throws error + try { + return String(evaluated.value) + } catch { + // No op + } + } + + return null } diff --git a/test/get-string-if-constant.mjs b/test/get-string-if-constant.mjs index 5d8cba0..04ae3ed 100644 --- a/test/get-string-if-constant.mjs +++ b/test/get-string-if-constant.mjs @@ -49,6 +49,7 @@ describe("The 'getStringIfConstant' function", () => { { code: "let id = 'abc'; id = 'foo'; id", expected: null }, { code: "var id = 'abc'; id = 'foo'; id", expected: null }, { code: "const id = otherId; id", expected: null }, + { code: "Symbol.prototype", expected: null }, ]) { it(`should return ${JSON.stringify(expected)} from ${code}`, () => { const linter = new eslint.Linter() @@ -63,6 +64,7 @@ describe("The 'getStringIfConstant' function", () => { }, })) linter.verify(code, { + globals: { Symbol: "readonly" }, parserOptions: { ecmaVersion: 2020 }, rules: { test: "error" }, })