Skip to content

Commit 1e6a4f7

Browse files
committed
Handle .. paths in no-restricted-require
no-restricted-require uses Minimatch to match the given names from the rule's configuration to the require statements it evaluates. As stated in #151, this behavior does not match `..` to the `**` glob pattern. This is considered as intended behavior from Minimatch (see isaacs/minimatch#230), but also means that the rule's behavior differs from eslint's deprecated no-restricted-modules rule it's supposed to replace. Using [`globrex`](https://www.npmjs.com/package/globrex) can fix this behavior. Fixes #151
1 parent 8185617 commit 1e6a4f7

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

lib/util/check-restricted.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"use strict"
66

77
const path = require("path")
8-
const { Minimatch } = require("minimatch")
8+
const globrex = require("globrex")
99

1010
/** @typedef {import("../util/import-target")} ImportTarget */
1111
/**
@@ -16,15 +16,15 @@ const { Minimatch } = require("minimatch")
1616

1717
/**
1818
* Check if matched or not.
19-
* @param {Minimatch} matcher The matcher.
19+
* @param {globrex.Results} matcher The globrex "matcher".
2020
* @param {boolean} absolute The flag that the matcher is for absolute paths.
2121
* @param {import('./import-target.js')} importee The importee information.
2222
*/
2323
function match(matcher, absolute, { filePath, name }) {
2424
if (absolute) {
25-
return filePath != null && matcher.match(filePath)
25+
return filePath != null && matcher.regex.test(filePath)
2626
}
27-
return matcher.match(name)
27+
return matcher.regex.test(name)
2828
}
2929

3030
/** Restriction. */
@@ -41,10 +41,7 @@ class Restriction {
4141
const absolute = path.isAbsolute(pattern)
4242

4343
const posix = pattern.replace(/\\/g, "/")
44-
const matcher = new Minimatch(posix, {
45-
allowWindowsEscape: true,
46-
dot: true,
47-
})
44+
const matcher = globrex(posix)
4845
return { absolute, matcher, negate }
4946
})
5047

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"eslint-plugin-es-x": "^7.8.0",
2323
"get-tsconfig": "^4.8.1",
2424
"globals": "^15.11.0",
25+
"globrex": "^0.1.2",
2526
"ignore": "^5.3.2",
2627
"minimatch": "^9.0.5",
2728
"semver": "^7.6.3",
@@ -31,6 +32,7 @@
3132
"@eslint/js": "^9.14.0",
3233
"@types/eslint": "^9.6.1",
3334
"@types/estree": "^1.0.6",
35+
"@types/globrex": "^0.1.4",
3436
"@types/node": "^20.17.5",
3537
"@typescript-eslint/parser": "^8.26.1",
3638
"@typescript-eslint/typescript-estree": "^8.26.1",

tests/lib/rules/no-restricted-require.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,16 @@ new RuleTester().run("no-restricted-require", rule, {
200200
},
201201
],
202202
},
203+
{
204+
filename: path.resolve(__dirname, "lib/sub/test.js"),
205+
code: 'require("../../foo");',
206+
options: [[{ name: "**/foo" }]],
207+
errors: [
208+
{
209+
messageId: "restricted",
210+
data: { name: "../../foo", customMessage: "" },
211+
},
212+
],
213+
},
203214
],
204215
})

0 commit comments

Comments
 (0)