Skip to content

Commit 587cca2

Browse files
committed
🐛 fix getStringIfConstant to handle literals correctly
1 parent c119e83 commit 587cca2

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/get-string-if-constant.js

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import { getStaticValue } from "./get-static-value"
77
* @returns {string|null} The value of the node, or `null`.
88
*/
99
export function getStringIfConstant(node, initialScope = null) {
10+
// Handle the literals that the platform doesn't support natively.
11+
if (node.type === "Literal" && node.value === null) {
12+
if (node.regex) {
13+
return `/${node.regex.pattern}/${node.regex.flags}`
14+
}
15+
if (node.bigint) {
16+
return node.bigint
17+
}
18+
}
19+
1020
const evaluated = getStaticValue(node, initialScope)
1121
return evaluated && String(evaluated.value)
1222
}

test/get-string-if-constant.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe("The 'getStringIfConstant' function", () => {
1919
{ code: "`aaa${id}bbb`", expected: null }, //eslint-disable-line no-template-curly-in-string
2020
{ code: "1 + 2", expected: "3" },
2121
{ code: "'a' + 'b'", expected: "ab" },
22+
{ code: "/(?<a>\\w+)\\k<a>/gu", expected: "/(?<a>\\w+)\\k<a>/gu" },
2223
]) {
2324
it(`should return ${JSON.stringify(expected)} from ${code}`, () => {
2425
const linter = new eslint.Linter()

0 commit comments

Comments
 (0)