Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions lib/util/check-restricted.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"use strict"

const path = require("path")
const { Minimatch } = require("minimatch")
const globrex = require("globrex")

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

/**
* Check if matched or not.
* @param {Minimatch} matcher The matcher.
* @param {globrex.Results} matcher The globrex "matcher".
* @param {boolean} absolute The flag that the matcher is for absolute paths.
* @param {import('./import-target.js')} importee The importee information.
*/
function match(matcher, absolute, { filePath, name }) {
if (absolute) {
return filePath != null && matcher.match(filePath)
return filePath != null && matcher.regex.test(filePath)
}
return matcher.match(name)
return matcher.regex.test(name)
}

/** Restriction. */
Expand All @@ -40,11 +40,7 @@ class Restriction {
const pattern = negate ? raw.slice(1) : raw
const absolute = path.isAbsolute(pattern)

const posix = pattern.replace(/\\/g, "/")
const matcher = new Minimatch(posix, {
allowWindowsEscape: true,
dot: true,
})
const matcher = globrex(pattern, { globstar: true })
return { absolute, matcher, negate }
})

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"eslint-plugin-es-x": "^7.8.0",
"get-tsconfig": "^4.8.1",
"globals": "^15.11.0",
"globrex": "^0.1.2",
"ignore": "^5.3.2",
"minimatch": "^9.0.5",
"semver": "^7.6.3",
Expand All @@ -31,6 +32,7 @@
"@eslint/js": "^9.14.0",
"@types/eslint": "^9.6.1",
"@types/estree": "^1.0.6",
"@types/globrex": "^0.1.4",
"@types/node": "^20.17.5",
"@typescript-eslint/parser": "^8.26.1",
"@typescript-eslint/typescript-estree": "^8.26.1",
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/no-restricted-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ new RuleTester().run("no-restricted-require", rule, {
code: 'require("../foo");',
options: [[{ name: path.resolve(__dirname, "foo") }]],
},
{
code: 'require("foo/bar/baz")',
options: [["foo/*"]],
},
],
invalid: [
{
Expand Down Expand Up @@ -200,5 +204,16 @@ new RuleTester().run("no-restricted-require", rule, {
},
],
},
{
filename: path.resolve(__dirname, "lib/sub/test.js"),
code: 'require("../../foo");',
options: [[{ name: "**/foo" }]],
errors: [
{
messageId: "restricted",
data: { name: "../../foo", customMessage: "" },
},
],
},
],
})
Loading