Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: eslint-community/eslint-utils
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.5.0
Choose a base ref
...
head repository: eslint-community/eslint-utils
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.5.1
Choose a head ref
  • 1 commit
  • 2 files changed
  • 2 contributors

Commits on Mar 13, 2025

  1. fix: fix crash when evaluating Symbol.prototype in `getStringIfCons…

    …tant` (#182)
    
    Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
    fisker and ota-meshi authored Mar 13, 2025
    Copy the full SHA
    f3bc7a4 View commit details
Showing with 16 additions and 2 deletions.
  1. +11 −1 src/get-string-if-constant.mjs
  2. +5 −1 test/get-string-if-constant.mjs
12 changes: 11 additions & 1 deletion src/get-string-if-constant.mjs
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 5 additions & 1 deletion test/get-string-if-constant.mjs
Original file line number Diff line number Diff line change
@@ -60,13 +60,17 @@ 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 = newCompatLinter()

let actual = null
linter.verify(code, {
languageOptions: { ecmaVersion: 2020 },
languageOptions: {
ecmaVersion: 2020,
globals: { Symbol: "readonly" },
},
rules: { "test/test": "error" },
plugins: {
test: {