Skip to content

Commit 20fb6cc

Browse files
authored
Fix false positive for require.resolve (#135)
* Fix false positive for require.resolve * Update src/rules/no-invalid.ts
1 parent 339e037 commit 20fb6cc

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/utils/ast/js/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const UNKNOWN = Symbol("unknown value")
2626
type TUnknown = typeof UNKNOWN
2727
const EMPTY_MAP = Object.freeze(new Map())
2828
const UNKNOWN_PATH_DATA: SubPathData = { data: UNKNOWN, children: EMPTY_MAP }
29+
const UNKNOWN_STRING_PATH_DATA: SubPathData = {
30+
data: "UNKNOWN",
31+
children: EMPTY_MAP,
32+
}
2933
export type PathData = {
3034
key:
3135
| [number, number]
@@ -440,6 +444,14 @@ const VISITORS = {
440444
): SubPathData {
441445
const evalData = getStaticValue(context, node)
442446
if (!evalData) {
447+
if (
448+
node.callee.type === "MemberExpression" &&
449+
node.callee.object.type === "Identifier" &&
450+
node.callee.object.name === "require" &&
451+
getStaticPropertyName(node.callee, context) === "resolve"
452+
) {
453+
return UNKNOWN_STRING_PATH_DATA
454+
}
443455
return UNKNOWN_PATH_DATA
444456
}
445457
return {
@@ -475,7 +487,7 @@ const VISITORS = {
475487
for (const e of node.expressions) {
476488
const data = getPathData(e, context)
477489
if (data.data === UNKNOWN) {
478-
return UNKNOWN_PATH_DATA
490+
return UNKNOWN_STRING_PATH_DATA
479491
}
480492
expressions.push(data.data)
481493
}

tests/src/rules/no-invalid.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ tester.run(
1818
"no-invalid",
1919
{},
2020
{
21+
valid: [
22+
{
23+
filename: path.join(__dirname, ".eslintrc.js"),
24+
code: 'module.exports = { "extends": [ require.resolve("eslint-config-foo") ] }',
25+
parser: require.resolve("espree"),
26+
options: [
27+
{
28+
schemas: [
29+
{
30+
fileMatch: ["tests/src/rules/.eslintrc.js"],
31+
schema: "https://json.schemastore.org/eslintrc",
32+
},
33+
],
34+
useSchemastoreCatalog: false,
35+
},
36+
],
37+
},
38+
],
2139
invalid: [
2240
{
2341
filename: ".eslintrc.json",
@@ -40,7 +58,8 @@ tester.run(
4058
},
4159
{
4260
filename: path.join(__dirname, ".eslintrc.js"),
43-
code: '{ "extends": [ 42 ] }',
61+
code: 'module.exports = { "extends": [ 42 ] }',
62+
parser: require.resolve("espree"),
4463
options: [
4564
{
4665
schemas: [

0 commit comments

Comments
 (0)