Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
sf0rman wants to merge2 commits intoimport-js:main
base:main
Choose a base branch
Loading
fromsf0rman:add-exclude-to-no-extraneous-dependencies
Open
Show file tree
Hide file tree
Changes fromall 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 deletionsCHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletiondocs/rules/no-extraneous-dependencies.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionssrc/rules/no-extraneous-dependencies.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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){
returnfalse;
}
if(Array.isArray(exclude)){
returnexclude.some((pattern)=>minimatch(packageName,pattern));
}
returnminimatch(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 DownExpand 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 deletionstests/src/rules/no-extraneous-dependencies.js
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp