Skip to content

Commit 80b934e

Browse files
feat(getStaticValue): allow RegExp getters (#77)
Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
1 parent e916558 commit 80b934e

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/get-static-value.mjs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,24 @@ const callPassThrough = new Set([
170170
Object.seal,
171171
])
172172

173+
/** @type {ReadonlyArray<readonly [Function, ReadonlySet<string>]>} */
174+
const getterAllowed = [
175+
[
176+
RegExp,
177+
new Set([
178+
"dotAll",
179+
"flags",
180+
"global",
181+
"hasIndices",
182+
"ignoreCase",
183+
"multiline",
184+
"source",
185+
"sticky",
186+
"unicode",
187+
]),
188+
],
189+
]
190+
173191
/**
174192
* Get the property descriptor.
175193
* @param {object} object The object to get.
@@ -438,8 +456,19 @@ const operations = Object.freeze({
438456
}
439457
const property = getStaticPropertyNameValue(node, initialScope)
440458

441-
if (property != null && !isGetter(object.value, property.value)) {
442-
return { value: object.value[property.value] }
459+
if (property != null) {
460+
if (!isGetter(object.value, property.value)) {
461+
return { value: object.value[property.value] }
462+
}
463+
464+
for (const [classFn, allowed] of getterAllowed) {
465+
if (
466+
object.value instanceof classFn &&
467+
allowed.has(property.value)
468+
) {
469+
return { value: object.value[property.value] }
470+
}
471+
}
443472
}
444473
}
445474
return null

test/get-static-value.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ describe("The 'getStaticValue' function", () => {
99
{ code: "[1, 2, 3]", expected: { value: [1, 2, 3] } },
1010
{ code: "[,, 3]", expected: { value: [, , 3] } }, //eslint-disable-line no-sparse-arrays
1111
{ code: "[1, ...[2, 3]]", expected: { value: [1, 2, 3] } },
12+
{ code: "[1, ...[2, 3]].length", expected: { value: 3 } },
13+
{ code: "[1, ...[2, 3]]['1']", expected: { value: 2 } },
1214
{ code: "[0, a]", expected: null },
1315
{ code: "[0, ...a]", expected: null },
1416
{ code: "a = 1 + 2", expected: { value: 3 } },
@@ -74,6 +76,14 @@ describe("The 'getStaticValue' function", () => {
7476
{ code: "1", expected: { value: 1 } },
7577
{ code: "'hello'", expected: { value: "hello" } },
7678
{ code: "/foo/gu", expected: { value: /foo/gu } },
79+
{ code: "RegExp(/foo/gu)", expected: { value: /foo/gu } },
80+
{ code: "RegExp(/foo/, 'gu')", expected: { value: /foo/gu } },
81+
{ code: "RegExp('foo', 'gu')", expected: { value: /foo/gu } },
82+
{ code: "new RegExp('foo', 'gu')", expected: { value: /foo/gu } },
83+
{ code: "/foo/gu.source", expected: { value: "foo" } },
84+
{ code: "/foo/gu.flags", expected: { value: "gu" } },
85+
{ code: "/foo/gu.unicode", expected: { value: true } },
86+
{ code: "/foo/gu.ignoreCase", expected: { value: false } },
7787
{ code: "true && 1", expected: { value: 1 } },
7888
{ code: "false && a", expected: { value: false } },
7989
{ code: "true || a", expected: { value: true } },
@@ -160,6 +170,8 @@ describe("The 'getStaticValue' function", () => {
160170
{ code: "String.raw`\\unicode`", expected: { value: "\\unicode" } },
161171
{ code: "`he${a}o`", expected: null }, //eslint-disable-line no-template-curly-in-string
162172
{ code: "x`hello`", expected: null },
173+
{ code: "'abc'.length", expected: { value: 3 } },
174+
{ code: "'abc'[1]", expected: { value: "b" } },
163175
{ code: "' foo '.trim()", expected: { value: "foo" } },
164176
{ code: "' foo '.trim().toUpperCase()", expected: { value: "FOO" } },
165177
{ code: "' foo '.indexOf('f')", expected: { value: 2 } },

0 commit comments

Comments
 (0)