Skip to content

Conversation

@mureinik
Copy link

@mureinik mureinik commented Jul 25, 2025

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 can fix this behavior.

Side notes:

  • I see @aladdin-add self-assigned this issue back in 2024, but I'm assuming this assignment is no longer relevant. If it is, let me know, and I'll close this MR.
  • eslint-plugin-n also uses Minimatch in get-convert-path.js. If this PR is accepted, maybe's it's worth considering changing that one too, so there's only only glob-handling dependency to rely on ("worth" as in "I can contribute this change if everyone think this is a good idea")

Fixes #151

@mureinik mureinik force-pushed the check-restricted-globrex branch from 82d1c18 to 1e6a4f7 Compare July 25, 2025 14:18
@mureinik
Copy link
Author

mureinik commented Jul 25, 2025

Note: The failure in "CI / Lint (pull_request)" (i.e., https://github.com/eslint-community/eslint-plugin-n/actions/runs/16524228429/job/46733273822?pr=458) seems to be unrelated to this PR.
The failures relate to files this PR didn't touch, and in fact, if you run CI on the master branch, you'll get the same errors.

Update:
The root cause of this error is #459. PR #460 is a suggestion to fix it, and once it (or some other fix) is merged, this PR should be rebased to get a clean CI run.

@mureinik mureinik force-pushed the check-restricted-globrex branch 2 times, most recently from 1658957 to b160a35 Compare July 25, 2025 14:29
@mureinik mureinik changed the title Handle .. paths in no-restricted-require fix(no-restricted-require): Handle .. paths Jul 25, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the no-restricted-require rule to properly handle .. paths in glob patterns by replacing Minimatch with globrex for pattern matching. This aligns the rule's behavior with ESLint's deprecated no-restricted-modules rule, ensuring that patterns like **/foo correctly match require statements with relative paths containing ...

Key changes:

  • Replaces Minimatch with globrex for glob pattern matching
  • Adds test case to verify **/foo pattern matches ../../foo require statement
  • Updates dependencies to include globrex and its TypeScript types

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
lib/util/check-restricted.js Replaces Minimatch with globrex for pattern matching logic
package.json Adds globrex and @types/globrex dependencies
tests/lib/rules/no-restricted-require.js Adds test case for .. path matching with glob patterns

Copy link

@aladdin-add aladdin-add left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks! Would like @scagood to take a look before merging.

@aladdin-add aladdin-add requested a review from scagood July 25, 2025 22:10
@aladdin-add aladdin-add force-pushed the check-restricted-globrex branch from b160a35 to 8a91cfe Compare July 25, 2025 22:14
Copy link

@scagood scagood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve in general, having read through some of the tests in globrex I do wonder if this is a breaking change?

Personally I think we should enable globstar.


Do you think we should also replace the minimatch with globrex at
lib/util/get-convert-path.js:7?

no-restricted-require uses Minimatch to match the given names from the
rule's configuration to the require statements it evaluates.

As stated in eslint-community#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 eslint-community#151
@mureinik mureinik force-pushed the check-restricted-globrex branch from 8a91cfe to 2b925cd Compare July 26, 2025 19:21
@mureinik
Copy link
Author

mureinik commented Jul 26, 2025

I approve in general, having read through some of the tests in globrex I do wonder if this is a breaking change?

Personally I think we should enable globstar.

@scagood Shoot - you're right.
I originally thought that enabling globstar broke some of the tests, but that was a misdiagnosis on my part (had a different mistake in an earlier version of this implementation).
I added a test at the end of the valid section that passes with the minimatch implementation, but fails without enabling globstar. Enabling it makes the test pass.

Do you think we should also replace the minimatch with globrex at lib/util/get-convert-path.js:7?

I think we should - I actually mentioned it at the top of the PR:

eslint-plugin-n also uses Minimatch in get-convert-path.js. If this PR is accepted, maybe's it's worth considering changing that one too, so there's only only glob-handling dependency to rely on ("worth" as in "I can contribute this change if everyone think this is a good idea")

I just wanted to see if this PR gets a consensus around it before piling on more work.
I can make the change there too, either in this PR or in a separate one, whatever y'all think is best.

@aladdin-add aladdin-add merged commit 34719e8 into eslint-community:master Jul 27, 2025
29 checks passed
@mureinik mureinik deleted the check-restricted-globrex branch July 27, 2025 08:03
mureinik added a commit to mureinik/eslint-plugin-n that referenced this pull request Jul 27, 2025
Follow up on eslint-community#458 - as suggested in the PR discussion, replace minimatch
with globrex across the board, so we ship only one pattern matching
dependency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: n/no-restricted-require does not work for relative imports (as opposed to no-restricted-modules from ESLint)

3 participants