Skip to content
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

Fix crash when evaluating Symbol.prototype #182

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion src/get-string-if-constant.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions test/get-string-if-constant.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -63,6 +64,7 @@ describe("The 'getStringIfConstant' function", () => {
},
}))
linter.verify(code, {
globals: { Symbol: "readonly" },
parserOptions: { ecmaVersion: 2020 },
rules: { test: "error" },
})
Expand Down