Skip to content

Commit 34719e8

Browse files
authored
fix(no-restricted-require): Handle .. paths (#458)
1 parent 76f8150 commit 34719e8

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

lib/util/check-restricted.js

Lines changed: 5 additions & 9 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. */
@@ -40,11 +40,7 @@ class Restriction {
4040
const pattern = negate ? raw.slice(1) : raw
4141
const absolute = path.isAbsolute(pattern)
4242

43-
const posix = pattern.replace(/\\/g, "/")
44-
const matcher = new Minimatch(posix, {
45-
allowWindowsEscape: true,
46-
dot: true,
47-
})
43+
const matcher = globrex(pattern, { globstar: true })
4844
return { absolute, matcher, negate }
4945
})
5046

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ new RuleTester().run("no-restricted-require", rule, {
5454
code: 'require("../foo");',
5555
options: [[{ name: path.resolve(__dirname, "foo") }]],
5656
},
57+
{
58+
code: 'require("foo/bar/baz")',
59+
options: [["foo/*"]],
60+
},
5761
],
5862
invalid: [
5963
{
@@ -200,5 +204,16 @@ new RuleTester().run("no-restricted-require", rule, {
200204
},
201205
],
202206
},
207+
{
208+
filename: path.resolve(__dirname, "lib/sub/test.js"),
209+
code: 'require("../../foo");',
210+
options: [[{ name: "**/foo" }]],
211+
errors: [
212+
{
213+
messageId: "restricted",
214+
data: { name: "../../foo", customMessage: "" },
215+
},
216+
],
217+
},
203218
],
204219
})

0 commit comments

Comments
 (0)