Skip to content

Conversation

@dword-design
Copy link
Contributor

@dword-design dword-design commented Jun 4, 2025

When passing a cwd to the linter instance, the matchers of no-extraneous-dependencies don't work and don't respect the root folder. Fixed by incorporating the cwd into the minimatch call.


Important

Fixes no-extraneous-dependencies rule to respect cwd by updating testConfig to include cwd in minimatch call.

  • Behavior:
    • Fixes no-extraneous-dependencies rule to respect cwd when set.
    • Updates testConfig function to include cwd in minimatch call.
  • Functions:
    • Modifies testConfig in no-extraneous-dependencies.ts to accept cwd parameter and use it in minimatch.
    • Updates calls to testConfig in createRule to pass context.cwd.

This description was created by Ellipsis for 3ca67f2. You can customize this summary. It will automatically update as commits are pushed.

Summary by CodeRabbit

  • Bug Fixes
    • Improved file matching accuracy by enhancing pattern resolution relative to the current working directory.
    • Updated glob matching to disable comment parsing, resulting in more precise exception and ignore pattern handling across multiple rules.

@coderabbitai
Copy link

coderabbitai bot commented Jun 4, 2025

Walkthrough

The testConfig and testIsAllow functions were refactored to accept a RuleContext object instead of a filename string. Glob matching was enhanced by resolving patterns relative to context.cwd with options like windowsPathsNoEscape and nocomment enabled. Calls to these functions were updated accordingly. Additionally, minimatch calls in two other rules were modified to include { nocomment: true } in their options.

Changes

File(s) Change Summary
src/rules/no-extraneous-dependencies.ts, src/rules/no-unassigned-import.ts Updated testConfig and testIsAllow signatures to take context: RuleContext; enhanced glob matching by resolving patterns relative to context.cwd with options { nocomment: true, windowsPathsNoEscape: true }; updated call sites to pass context.
src/rules/no-import-module-exports.ts, src/rules/no-namespace.ts Added { nocomment: true } option to minimatch calls for exception and ignore glob matching respectively.

Possibly related PRs

Suggested labels

bug

Poem

A bunny hopped through code today,
Adjusted paths along the way.
With cwd now set just right,
Globs resolve without a fight.
No more bugs in dependency land—
The rule now works as planned!
🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/rules/no-extraneous-dependencies.ts

Oops! Something went wrong! :(

ESLint: 9.28.0

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/lib/index.js' imported from /eslint.config.js
at finalizeResolution (node:internal/modules/esm/resolve:274:11)
at moduleResolve (node:internal/modules/esm/resolve:859:10)
at defaultResolve (node:internal/modules/esm/resolve:983:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:799:12)
at #cachedDefaultResolve (node:internal/modules/esm/loader:723:25)
at ModuleLoader.resolve (node:internal/modules/esm/loader:706:38)
at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:307:38)
at #link (node:internal/modules/esm/module_job:170:49)

src/rules/no-import-module-exports.ts

Oops! Something went wrong! :(

ESLint: 9.28.0

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/lib/index.js' imported from /eslint.config.js
at finalizeResolution (node:internal/modules/esm/resolve:274:11)
at moduleResolve (node:internal/modules/esm/resolve:859:10)
at defaultResolve (node:internal/modules/esm/resolve:983:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:799:12)
at #cachedDefaultResolve (node:internal/modules/esm/loader:723:25)
at ModuleLoader.resolve (node:internal/modules/esm/loader:706:38)
at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:307:38)
at #link (node:internal/modules/esm/module_job:170:49)

src/rules/no-namespace.ts

Oops! Something went wrong! :(

ESLint: 9.28.0

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/lib/index.js' imported from /eslint.config.js
at finalizeResolution (node:internal/modules/esm/resolve:274:11)
at moduleResolve (node:internal/modules/esm/resolve:859:10)
at defaultResolve (node:internal/modules/esm/resolve:983:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:799:12)
at #cachedDefaultResolve (node:internal/modules/esm/loader:723:25)
at ModuleLoader.resolve (node:internal/modules/esm/loader:706:38)
at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:307:38)
at #link (node:internal/modules/esm/module_job:170:49)

  • 1 others

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 90ec1f1 and 3d94167.

📒 Files selected for processing (6)
  • .changeset/rude-cherries-fly.md (1 hunks)
  • .changeset/serious-mails-provide.md (1 hunks)
  • src/rules/no-extraneous-dependencies.ts (2 hunks)
  • src/rules/no-import-module-exports.ts (1 hunks)
  • src/rules/no-namespace.ts (1 hunks)
  • src/rules/no-unassigned-import.ts (3 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (9)
src/rules/no-import-module-exports.ts (1)

115-117: LGTM: Correct addition of nocomment option.

The addition of { nocomment: true } to the minimatch call correctly disables comment processing in glob patterns, which aligns with the standardization effort across rules in this plugin.

src/rules/no-namespace.ts (1)

50-53: LGTM: Consistent addition of nocomment option.

The addition of nocomment: true alongside the existing matchBase: true option is correctly implemented and maintains consistency with other rules in this plugin.

src/rules/no-extraneous-dependencies.ts (2)

331-349: LGTM: Correct implementation of cwd-aware glob matching.

The refactoring correctly:

  • Updates the function signature to accept RuleContext instead of separate parameters
  • Extracts filename from context.physicalFilename inside the function
  • Adds proper context.cwd resolution for glob patterns with windowsPathsNoEscape: true
  • Maintains backward compatibility with existing absolute path resolution

The implementation properly addresses the PR objective of respecting context.cwd for glob pattern matching.


412-416: LGTM: Consistent function call updates.

All calls to testConfig have been correctly updated to pass the context object instead of the filename string, maintaining consistency across all dependency type checks.

src/rules/no-unassigned-import.ts (3)

8-17: LGTM: Correct function signature update.

The function signature change to accept RuleContext instead of a filename string, along with extracting the filename from context.physicalFilename, follows the established pattern and correctly enables cwd-aware glob matching.


28-31: LGTM: Proper cwd-aware glob resolution.

The addition of the new minimatch call with path.resolve(context.cwd, glob) correctly implements context-aware path resolution with appropriate options (nocomment: true and windowsPathsNoEscape: true).


79-79: LGTM: Function call updated consistently.

The call to testIsAllow has been correctly updated to pass the entire context object instead of extracting the filename separately.

.changeset/serious-mails-provide.md (1)

1-6: Approve changeset for no-import-module-exports and no-namespace update
The changeset follows the standard format and clearly outlines enabling nocomment: true for the specified rules.

.changeset/rude-cherries-fly.md (1)

1-6: Approve changeset for context.cwd fix in dependency rules
This entry is well-structured and accurately describes adding context.cwd support for no-extraneous-dependencies and no-unassigned-import.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Important

Looks good to me! 👍

Reviewed everything up to 3ca67f2 in 1 minute and 19 seconds. Click for details.
  • Reviewed 39 lines of code in 1 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. src/rules/no-extraneous-dependencies.ts:331
  • Draft comment:
    Consider adding an explicit type annotation for the destructured parameter 'cwd' (e.g., { cwd: string }) in the testConfig function to improve clarity and type-safety.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 20% vs. threshold = 50% The suggestion is reasonable since explicit types can improve code clarity and catch errors. However, TypeScript can likely infer the type correctly from context.cwd usage. The change is minor and not critical for correctness. The code will work fine either way since TypeScript's type inference is reliable here. The comment is technically correct and follows good TypeScript practices. Adding explicit types can help catch errors earlier and improve code readability. While explicit types are good practice, this case is not critical since TypeScript can infer the type and the code works correctly without it. The comment doesn't point out an actual problem that needs fixing. The comment should be removed as it suggests a minor stylistic improvement rather than fixing a real issue. The code works correctly as is.
2. src/rules/no-extraneous-dependencies.ts:339
  • Draft comment:
    Verify that switching from path.resolve(c) to path.join(cwd, c) yields the intended resolution behavior; ensure that 'cwd' is an absolute path to avoid mismatches in glob patterns.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% The comment is asking the PR author to verify the behavior of a code change, which violates the rule against asking for confirmation or verification of intention. It also suggests ensuring a condition (that cwd is an absolute path), which is not allowed. Therefore, this comment should be removed.
3. src/rules/no-extraneous-dependencies.ts:407
  • Draft comment:
    When passing { cwd: context.cwd } into testConfig for depsOptions, ensure that context.cwd is always defined; consider adding a fallback (e.g., process.cwd()) if it's not set.
  • Reason this comment was not posted:
    Decided after close inspection that this draft comment was likely wrong and/or not actionable: usefulness confidence = 10% vs. threshold = 50% ESLint's RuleContext always provides a cwd property - it's a core part of ESLint's API. The cwd is needed for ESLint to function at all, so it will never be undefined. The comment is suggesting defensive programming for a case that can't actually occur in practice. I could be wrong about ESLint's API guarantees. Maybe there are edge cases where context.cwd could be undefined. Even if there were edge cases, ESLint itself would fail before reaching this code if cwd was undefined, as it's needed for basic ESLint functionality. The comment should be deleted as it suggests handling an impossible case - context.cwd is guaranteed to be defined by ESLint's core functionality.

Workflow ID: wflow_6SLcdT4MeWM9SSAw

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/rules/no-extraneous-dependencies.ts (1)

340-340: The path resolution fix looks correct, but document the additional minimatch option.

The change from path.resolve(c) to path.join(cwd, c) correctly addresses the core issue by resolving paths relative to the provided cwd. However, the addition of { windowsPathsNoEscape: true } should be documented as it changes the minimatch behavior.

Consider adding a comment explaining why the windowsPathsNoEscape: true option was added:

      minimatch(filename, path.join(cwd, c), { windowsPathsNoEscape: true }),
+      // windowsPathsNoEscape: true prevents escaping backslashes on Windows paths
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 74a16c5 and 3ca67f2.

📒 Files selected for processing (1)
  • src/rules/no-extraneous-dependencies.ts (3 hunks)
🔇 Additional comments (1)
src/rules/no-extraneous-dependencies.ts (1)

407-412: Function calls consistently updated with context.cwd.

All calls to testConfig are properly updated to pass the context's cwd, ensuring consistent behavior across different dependency types.

@changeset-bot
Copy link

changeset-bot bot commented Jun 4, 2025

🦋 Changeset detected

Latest commit: 3d94167

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-import-x Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 4, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@JounQin JounQin force-pushed the fix-no-extraneous-dependencies-cwd branch from 67dea2b to 6456a7d Compare June 5, 2025 12:46
@JounQin JounQin changed the title fix: fix no-extraneous-dependencies when cwd is set fix: take context.cwd into account for several glob options Jun 5, 2025
@JounQin JounQin changed the title fix: take context.cwd into account for several glob options fix: take context.cwd into account for glob options of several rules Jun 5, 2025
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jun 5, 2025

Open in StackBlitz

npm i https://pkg.pr.new/eslint-plugin-import-x@371

commit: 3d94167

@JounQin
Copy link
Member

JounQin commented Jun 5, 2025

@coderabbitai full review

@coderabbitai
Copy link

coderabbitai bot commented Jun 5, 2025

✅ Actions performed

Full review triggered.

@JounQin JounQin merged commit 4f97a1a into un-ts:master Jun 5, 2025
46 checks passed
@dword-design dword-design deleted the fix-no-extraneous-dependencies-cwd branch June 5, 2025 16:35
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.

2 participants