From c4bfb426d08319dba13bda6d2e517bfe2feb4c61 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 4 Jan 2024 17:20:48 +0800 Subject: [PATCH 1/5] Add test --- test/get-string-if-constant.mjs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/get-string-if-constant.mjs b/test/get-string-if-constant.mjs index 5d8cba0..5445cb1 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" }, }) From 9599f7e684ea3feefde1902903e68d1995747ada Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 4 Jan 2024 17:24:51 +0800 Subject: [PATCH 2/5] Fix --- src/get-string-if-constant.mjs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/get-string-if-constant.mjs b/src/get-string-if-constant.mjs index ab03363..9d15702 100644 --- a/src/get-string-if-constant.mjs +++ b/src/get-string-if-constant.mjs @@ -18,5 +18,13 @@ export function getStringIfConstant(node, initialScope = null) { } const evaluated = getStaticValue(node, initialScope) - return evaluated && String(evaluated.value) + + if (evaluated) { + // `String(Symbol.prototype)` throw errors + try { + return String(evaluated.value); + } catch {} + } + + return null; } From 5348984cdfbca00551b23102b3ae64c11bcff973 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 4 Jan 2024 17:26:43 +0800 Subject: [PATCH 3/5] Format --- src/get-string-if-constant.mjs | 4 ++-- test/get-string-if-constant.mjs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/get-string-if-constant.mjs b/src/get-string-if-constant.mjs index 9d15702..e05bc8d 100644 --- a/src/get-string-if-constant.mjs +++ b/src/get-string-if-constant.mjs @@ -22,9 +22,9 @@ export function getStringIfConstant(node, initialScope = null) { if (evaluated) { // `String(Symbol.prototype)` throw errors try { - return String(evaluated.value); + return String(evaluated.value) } catch {} } - return null; + return null } diff --git a/test/get-string-if-constant.mjs b/test/get-string-if-constant.mjs index 5445cb1..04ae3ed 100644 --- a/test/get-string-if-constant.mjs +++ b/test/get-string-if-constant.mjs @@ -49,7 +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}, + { code: "Symbol.prototype", expected: null }, ]) { it(`should return ${JSON.stringify(expected)} from ${code}`, () => { const linter = new eslint.Linter() @@ -64,7 +64,7 @@ describe("The 'getStringIfConstant' function", () => { }, })) linter.verify(code, { - globals: {Symbol: 'readonly'}, + globals: { Symbol: "readonly" }, parserOptions: { ecmaVersion: 2020 }, rules: { test: "error" }, }) From 411e4a03c6dfa16dc3acba3dbf239a760cf604c9 Mon Sep 17 00:00:00 2001 From: fisker Date: Thu, 4 Jan 2024 17:28:37 +0800 Subject: [PATCH 4/5] Linting --- src/get-string-if-constant.mjs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/get-string-if-constant.mjs b/src/get-string-if-constant.mjs index e05bc8d..709d515 100644 --- a/src/get-string-if-constant.mjs +++ b/src/get-string-if-constant.mjs @@ -23,7 +23,9 @@ export function getStringIfConstant(node, initialScope = null) { // `String(Symbol.prototype)` throw errors try { return String(evaluated.value) - } catch {} + } catch { + // No op + } } return null From 3dac2cf4f16b305f40e3d2174b90cdb6719badea Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 4 Jan 2024 17:42:00 +0800 Subject: [PATCH 5/5] Update get-string-if-constant.mjs --- src/get-string-if-constant.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/get-string-if-constant.mjs b/src/get-string-if-constant.mjs index 709d515..9d2e569 100644 --- a/src/get-string-if-constant.mjs +++ b/src/get-string-if-constant.mjs @@ -20,7 +20,7 @@ export function getStringIfConstant(node, initialScope = null) { const evaluated = getStaticValue(node, initialScope) if (evaluated) { - // `String(Symbol.prototype)` throw errors + // `String(Symbol.prototype)` throws error try { return String(evaluated.value) } catch {