Skip to content
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

feat(IgnoreImports): allow ignored imports in no-extraneous-dependency #2557

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions docs/rules/no-extraneous-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ folder layouts:
"import/no-extraneous-dependencies": ["error", {"packageDir": ['./some-dir/', './root-pkg']}]
```

There is `ignoreImports` options, it can be used to ignore some files or directories.

```js
"import/no-extraneous-dependencies": ["error", {"ignoreImports": ['./some-root-dir/', '__mocks__']}]
```

## Rule Details

Given the following `package.json`:
Expand Down
14 changes: 14 additions & 0 deletions src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ function reportIfMissing(context, deps, depsOptions, node, name) {

if (
declarationStatus.isInDeps ||
depsOptions.allowIgnoreImports ||
(depsOptions.allowDevDeps && declarationStatus.isInDevDeps) ||
(depsOptions.allowPeerDeps && declarationStatus.isInPeerDeps) ||
(depsOptions.allowOptDeps && declarationStatus.isInOptDeps) ||
Expand All @@ -216,6 +217,7 @@ function reportIfMissing(context, deps, depsOptions, node, name) {

if (
declarationStatus.isInDeps ||
depsOptions.allowIgnoreImports ||
(depsOptions.allowDevDeps && declarationStatus.isInDevDeps) ||
(depsOptions.allowPeerDeps && declarationStatus.isInPeerDeps) ||
(depsOptions.allowOptDeps && declarationStatus.isInOptDeps) ||
Expand Down Expand Up @@ -250,6 +252,16 @@ function testConfig(config, filename) {
));
}

function testIgnoreImports(config, filename) {
if (!config) {
return false
}
return config.some(c => (
filename.includes(value.substr(0, 2) === './' ? value.replace('.', process.cwd()) : value)
Comment on lines +259 to +260
Copy link
Member

Choose a reason for hiding this comment

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

where does value come from here?

also, let's use slice over substr.

))
}


module.exports = {
meta: {
type: 'problem',
Expand All @@ -268,6 +280,7 @@ module.exports = {
'packageDir': { 'type': ['string', 'array'] },
'includeInternal': { 'type': ['boolean'] },
'includeTypes': { 'type': ['boolean'] },
'ignoreImports': { 'type': ['array'] },
},
'additionalProperties': false,
},
Expand All @@ -280,6 +293,7 @@ module.exports = {
const deps = getDependencies(context, options.packageDir) || extractDepFields({});

const depsOptions = {
allowIgnoreImports: testIgnoreImports(options.ignoreImports, filename) !== false,
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
allowIgnoreImports: testIgnoreImports(options.ignoreImports, filename) !== false,
ignoreImports: testIgnoreImports(options.ignoreImports, filename) !== false,

allowDevDeps: testConfig(options.devDependencies, filename) !== false,
allowOptDeps: testConfig(options.optionalDependencies, filename) !== false,
allowPeerDeps: testConfig(options.peerDependencies, filename) !== false,
Expand Down