diff --git a/lib/util/check-restricted.js b/lib/util/check-restricted.js index 5a68fedc..c41e5486 100644 --- a/lib/util/check-restricted.js +++ b/lib/util/check-restricted.js @@ -5,7 +5,7 @@ "use strict" const path = require("path") -const { Minimatch } = require("minimatch") +const globrex = require("globrex") /** @typedef {import("../util/import-target")} ImportTarget */ /** @@ -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. */ @@ -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 } }) diff --git a/package.json b/package.json index 1a68bd27..4200bc24 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/tests/lib/rules/no-restricted-require.js b/tests/lib/rules/no-restricted-require.js index 1c63c825..41654155 100644 --- a/tests/lib/rules/no-restricted-require.js +++ b/tests/lib/rules/no-restricted-require.js @@ -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: [ { @@ -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: "" }, + }, + ], + }, ], })