Skip to content

feat: add excludeSourcePatterns option to import-access/jsdoc rule#125

Merged
uhyo merged 2 commits intouhyo:masterfrom
ygkn:exclude-source-patterns
Apr 14, 2025
Merged

feat: add excludeSourcePatterns option to import-access/jsdoc rule#125
uhyo merged 2 commits intouhyo:masterfrom
ygkn:exclude-source-patterns

Conversation

@ygkn
Copy link
Copy Markdown
Contributor

@ygkn ygkn commented Apr 14, 2025

Closes #124

This PR adds a new excludeSourcePatterns option to the import-access/jsdoc rule.

Implementation

  • Added excludeSourcePatterns option to JSDocRuleOptions
  • Enhanced checkSymbolImportability to check source paths against patterns
  • Added documentation and tests

The change is fully backward compatible as it's an optional feature.

Example

// .eslintrc.js
{
  "rules": {
    "import-access/jsdoc": ["error", {
      "defaultImportability": "package",
      "excludeSourcePatterns": [
        ".next/**/*",
        "**/*.generated.*"
      ]
    }]
  }
}

Verification

We can verify this PR works as expected using the minimal reproduction repository from issue #124:

git clone https://github.com/ygkn/eslint-plugin-import-access-do-not-work-with-next.git
cd eslint-plugin-import-access-do-not-work-with-next

# Run `npm run build` at this repo checkouted this pr
npm install -D file:/path/to/local/clone/of/this/pr

Add option to eslint.config.mjs:

    {
      rules: {
        "import-access/jsdoc": [
          "error",
          {
            defaultImportability: "package",
+           excludeSourcePatterns: [
+             ".next/**/*",
+           ],
          },
        ],
      },
    },

We get only expected errors.

❯ npm run lint

> lint
> next lint


./src/app/test-import.tsx
4:10  Error: Cannot import a package-private export 'formatDate'  import-access/jsdoc

info  - Need to disable some ESLint rules? Learn more here: https://nextjs.org/docs/app/api-reference/config/eslint#disabling-rules

Dependencies

Added minimatch (v10.0.1) for glob pattern matching. This is already used in the ecosystem:

  • ESLint itself uses minimatch (v3.1.2)
  • TypeScript-ESLint uses minimatch in its parser
❯ npm ls minimatch
eslint-plugin-import-access@2.2.2
├─┬ @typescript-eslint/typescript-estree@8.4.0
│ └── minimatch@9.0.5
├─┬ babel-jest@29.7.0
│ └─┬ babel-plugin-istanbul@6.1.1
│   └─┬ test-exclude@6.0.0
│     ├─┬ glob@7.2.3
│     │ └── minimatch@3.1.2
│     └── minimatch@3.1.2
├─┬ eslint@8.57.0
│ ├─┬ @eslint/eslintrc@2.1.4
│ │ └── minimatch@3.1.2
│ ├─┬ @humanwhocodes/config-array@0.11.14
│ │ └── minimatch@3.1.2
│ └── minimatch@3.1.2
└── minimatch@10.0.1

Alternative Approach

If adding minimatch as a dependency is a concern, we could implement a regex-based approach instead:

// Using regex patterns instead of glob patterns
excludeSourceRegexps?: RegExp[];

// Usage in checkSymbolImportability:
if (packageOptions.excludeSourceRegexps?.some(regex => regex.test(relativePath))) {
  // Skip importability check
  return;
}

This would avoid the external dependency, though glob patterns are more user-friendly and consistent with ESLint's existing pattern syntax.

Copy link
Copy Markdown
Owner

@uhyo uhyo left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! I'd like to ask a light improvement in documentation.

Tests are failing but I'm not sure it is related to this PR... 😨

@ygkn ygkn requested a review from uhyo April 14, 2025 13:06
@uhyo
Copy link
Copy Markdown
Owner

uhyo commented Apr 14, 2025

Thank you! I'll take a look at broken CI so I can release this quickly.

@uhyo uhyo merged commit 8d71a52 into uhyo:master Apr 14, 2025
0 of 67 checks passed
@uhyo
Copy link
Copy Markdown
Owner

uhyo commented May 14, 2025

@ygkn Hello 👋 sorry for being very late, but version 3.0.0-beta.0 includes this feature. Could you test this if you have time? Thanks!

@ygkn
Copy link
Copy Markdown
Contributor Author

ygkn commented May 14, 2025

@uhyo It works! Thank you!

Here is the test:
https://github.com/ygkn/eslint-plugin-import-access-do-not-work-with-next/blob/upgrade-to-3.0.0-beta.0/eslint.config.mjs#L36

@uhyo
Copy link
Copy Markdown
Owner

uhyo commented May 15, 2025

@ygkn Great! Thank you. I released 3.0.0 that includes this feature.

@ygkn ygkn deleted the exclude-source-patterns branch May 16, 2025 04:31
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.

feature proposal: Add Option to Exclude Source Path Patterns from import-access Rule

2 participants