Skip to content

Commit

Permalink
feat(getStaticValue): allow RegExp getters (#77)
Browse files Browse the repository at this point in the history
Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
  • Loading branch information
RunDevelopment and ota-meshi committed Mar 17, 2023
1 parent e916558 commit 80b934e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/get-static-value.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ const callPassThrough = new Set([
Object.seal,
])

/** @type {ReadonlyArray<readonly [Function, ReadonlySet<string>]>} */
const getterAllowed = [
[
RegExp,
new Set([
"dotAll",
"flags",
"global",
"hasIndices",
"ignoreCase",
"multiline",
"source",
"sticky",
"unicode",
]),
],
]

/**
* Get the property descriptor.
* @param {object} object The object to get.
Expand Down Expand Up @@ -438,8 +456,19 @@ const operations = Object.freeze({
}
const property = getStaticPropertyNameValue(node, initialScope)

if (property != null && !isGetter(object.value, property.value)) {
return { value: object.value[property.value] }
if (property != null) {
if (!isGetter(object.value, property.value)) {
return { value: object.value[property.value] }
}

for (const [classFn, allowed] of getterAllowed) {
if (
object.value instanceof classFn &&
allowed.has(property.value)
) {
return { value: object.value[property.value] }
}
}
}
}
return null
Expand Down
12 changes: 12 additions & 0 deletions test/get-static-value.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe("The 'getStaticValue' function", () => {
{ code: "[1, 2, 3]", expected: { value: [1, 2, 3] } },
{ code: "[,, 3]", expected: { value: [, , 3] } }, //eslint-disable-line no-sparse-arrays
{ code: "[1, ...[2, 3]]", expected: { value: [1, 2, 3] } },
{ code: "[1, ...[2, 3]].length", expected: { value: 3 } },
{ code: "[1, ...[2, 3]]['1']", expected: { value: 2 } },
{ code: "[0, a]", expected: null },
{ code: "[0, ...a]", expected: null },
{ code: "a = 1 + 2", expected: { value: 3 } },
Expand Down Expand Up @@ -74,6 +76,14 @@ describe("The 'getStaticValue' function", () => {
{ code: "1", expected: { value: 1 } },
{ code: "'hello'", expected: { value: "hello" } },
{ code: "/foo/gu", expected: { value: /foo/gu } },
{ code: "RegExp(/foo/gu)", expected: { value: /foo/gu } },
{ code: "RegExp(/foo/, 'gu')", expected: { value: /foo/gu } },
{ code: "RegExp('foo', 'gu')", expected: { value: /foo/gu } },
{ code: "new RegExp('foo', 'gu')", expected: { value: /foo/gu } },
{ code: "/foo/gu.source", expected: { value: "foo" } },
{ code: "/foo/gu.flags", expected: { value: "gu" } },
{ code: "/foo/gu.unicode", expected: { value: true } },
{ code: "/foo/gu.ignoreCase", expected: { value: false } },
{ code: "true && 1", expected: { value: 1 } },
{ code: "false && a", expected: { value: false } },
{ code: "true || a", expected: { value: true } },
Expand Down Expand Up @@ -160,6 +170,8 @@ describe("The 'getStaticValue' function", () => {
{ code: "String.raw`\\unicode`", expected: { value: "\\unicode" } },
{ code: "`he${a}o`", expected: null }, //eslint-disable-line no-template-curly-in-string
{ code: "x`hello`", expected: null },
{ code: "'abc'.length", expected: { value: 3 } },
{ code: "'abc'[1]", expected: { value: "b" } },
{ code: "' foo '.trim()", expected: { value: "foo" } },
{ code: "' foo '.trim().toUpperCase()", expected: { value: "FOO" } },
{ code: "' foo '.indexOf('f')", expected: { value: 2 } },
Expand Down

0 comments on commit 80b934e

Please sign in to comment.