Skip to content

feat(no-extraneous-dependencies): add exclude option to rule #3198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## [Unreleased]

### Added
- add `exclude` option to `import/no-extraneous-dependencies` to allow excluding dependencies from rule. ([#3198], thanks [@sf0rman])

## [2.32.0] - 2025-06-20

### Added
Expand Down
15 changes: 14 additions & 1 deletion docs/rules/no-extraneous-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Type imports are ignored by default.

`bundledDependencies`: If set to `false`, then the rule will show an error when `bundledDependencies` are imported. Defaults to `true`.

`exclude`: If set, then the rule will exclude errors for the matched patterns. Defaults to `undefined`.

You can set the options like this:

```js
Expand Down Expand Up @@ -57,6 +59,12 @@ folder layouts:
"import/no-extraneous-dependencies": ["error", {"packageDir": ['./some-dir/', './root-pkg']}]
```

You can also exclude errors for specific import paths to support packages that provide its components as nested dependencies.

```js
"import/no-extraneous-dependencies": ["error", {"exclude": ['@scope/package']}]
```

## Rule Details

Given the following `package.json`:
Expand All @@ -69,7 +77,8 @@ Given the following `package.json`:
"builtin-modules": "^1.1.1",
"lodash.cond": "^4.2.0",
"lodash.find": "^4.2.0",
"pkg-up": "^1.0.0"
"pkg-up": "^1.0.0",
"radix-ui": "^1.4.2",
},
"devDependencies": {
"ava": "^0.13.0",
Expand Down Expand Up @@ -132,6 +141,10 @@ import type { MyType } from 'foo';

/* eslint import/no-extraneous-dependencies: ["error", {"peerDependencies": true}] */
import react from 'react';

/* eslint import/no-extraneous-dependencies: ["error", {"exclude": ['@radix-ui/react-*']}] */
import { Alert } from "@radix-ui/react-alert-dialog";
import { Button } from "@radix-ui/react-button";
```

## When Not To Use It
Expand Down
17 changes: 17 additions & 0 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ function checkDependencyDeclaration(deps, packageName, declarationStatus) {
}), newDeclarationStatus);
}

function isInExcludeList(packageName, exclude) {
if (!exclude) {
return false;
}

if (Array.isArray(exclude)) {
return exclude.some((pattern) => minimatch(packageName, pattern));
}
return minimatch(packageName, exclude);
Comment on lines +181 to +188
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (!exclude) {
return false;
}
if (Array.isArray(exclude)) {
return exclude.some((pattern) => minimatch(packageName, pattern));
}
return minimatch(packageName, exclude);
return [].concat(exclude || []).some((pattern) => minimatch(packageName, pattern));

}

function reportIfMissing(context, deps, depsOptions, node, name) {
// Do not report when importing types unless option is enabled
if (
Expand All @@ -200,6 +211,10 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
return;
}

if (isInExcludeList(name, depsOptions.exclude)) {
return;
}

const resolved = resolve(name, context);
if (!resolved) { return; }

Expand Down Expand Up @@ -277,6 +292,7 @@ module.exports = {
packageDir: { type: ['string', 'array'] },
includeInternal: { type: ['boolean'] },
includeTypes: { type: ['boolean'] },
exclude: { type: ['string', 'array'] },
Copy link
Member

Choose a reason for hiding this comment

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

for the array, let's ensure it's unique, and that the items can only be of type string

},
additionalProperties: false,
},
Expand All @@ -295,6 +311,7 @@ module.exports = {
allowBundledDeps: testConfig(options.bundledDependencies, filename) !== false,
verifyInternalDeps: !!options.includeInternal,
verifyTypeImports: !!options.includeTypes,
exclude: options.exclude,
};

return moduleVisitor((source, node) => {
Expand Down
31 changes: 31 additions & 0 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,37 @@ ruleTester.run('no-extraneous-dependencies', rule, {
},
},
}),

test({
code: `import "excluded-package";`,
options: [{ exclude: 'excluded-package' }],
}),

test({
code: `
import "excluded-package";
import x from "another-package";
`,
options: [{ exclude: ['excluded-package', 'another-package'] }],
}),

test({
code: `import "@scope/excluded-package";`,
options: [{ exclude: '@scope/excluded-*' }],
}),

test({
code: `import { item } from "@scope/excluded-package";`,
options: [{ exclude: '@scope/excluded-*' }],
}),

test({
code: `
import { item } from "@scope/some-package";
import { a, b } from "@scope/another-package"
`,
options: [{ exclude: '@scope/*' }],
}),
],
invalid: [
test({
Expand Down
Loading