Skip to content

Commit

Permalink
🐛 fix getStringIfConstant to handle literals correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Aug 20, 2019
1 parent c119e83 commit 587cca2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/get-string-if-constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import { getStaticValue } from "./get-static-value"
* @returns {string|null} The value of the node, or `null`.
*/
export function getStringIfConstant(node, initialScope = null) {
// Handle the literals that the platform doesn't support natively.
if (node.type === "Literal" && node.value === null) {
if (node.regex) {
return `/${node.regex.pattern}/${node.regex.flags}`
}
if (node.bigint) {
return node.bigint
}
}

const evaluated = getStaticValue(node, initialScope)
return evaluated && String(evaluated.value)
}
1 change: 1 addition & 0 deletions test/get-string-if-constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe("The 'getStringIfConstant' function", () => {
{ code: "`aaa${id}bbb`", expected: null }, //eslint-disable-line no-template-curly-in-string
{ code: "1 + 2", expected: "3" },
{ code: "'a' + 'b'", expected: "ab" },
{ code: "/(?<a>\\w+)\\k<a>/gu", expected: "/(?<a>\\w+)\\k<a>/gu" },
]) {
it(`should return ${JSON.stringify(expected)} from ${code}`, () => {
const linter = new eslint.Linter()
Expand Down

0 comments on commit 587cca2

Please sign in to comment.