Skip to content

Commit f12447e

Browse files
ZamiellSukkaW
andauthored
feat: whitelist option for no-extraneous-dependencies (import-js#142)
Co-authored-by: SukkaW <[email protected]>
1 parent 34e1334 commit f12447e

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

.changeset/light-apples-rhyme.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-import-x": minor
3+
---
4+
5+
Add new option "whitelist" for rule "no-extraneous-dependencies"

docs/rules/no-extraneous-dependencies.md

+19-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Modules have to be installed for this rule to work.
99

1010
## Options
1111

12-
This rule supports the following options:
12+
### Dependency Options
1313

1414
`devDependencies`: If set to `false`, then the rule will show an error when `devDependencies` are imported. Defaults to `true`.
1515
Type imports are ignored by default.
@@ -34,27 +34,41 @@ You can also use an array of globs instead of literal booleans:
3434

3535
When using an array of globs, the setting will be set to `true` (no errors reported) if the name of the file being linted (i.e. not the imported file/module) matches a single glob in the array, and `false` otherwise.
3636

37+
### Other Options
38+
39+
#### `includeInternal` & `includeTypes`
40+
3741
There are 2 boolean options to opt into checking extra imports that are normally ignored: `includeInternal`, which enables the checking of internal modules, and `includeTypes`, which enables checking of type imports in TypeScript.
3842

3943
```js
4044
"import-x/no-extraneous-dependencies": ["error", {"includeInternal": true, "includeTypes": true}]
4145
```
4246

43-
Also there is one more option called `packageDir`, this option is to specify the path to the folder containing package.json.
47+
#### `packageDir`
48+
49+
The `packageDir` option is to specify the path to the folder containing package.json.
4450

4551
If provided as a relative path string, will be computed relative to the current working directory at linter execution time. If this is not ideal (does not work with some editor integrations), consider using `__dirname` to provide a path relative to your configuration.
4652

4753
```js
48-
"import-x/no-extraneous-dependencies": ["error", {"packageDir": './some-dir/'}]
54+
"import-x/no-extraneous-dependencies": ["error", {"packageDir": "./some-dir/"}]
4955
// or
50-
"import-x/no-extraneous-dependencies": ["error", {"packageDir": path.join(__dirname, 'some-dir')}]
56+
"import-x/no-extraneous-dependencies": ["error", {"packageDir": path.join(__dirname, "some-dir")}]
5157
```
5258

5359
It may also be an array of multiple paths, to support monorepos or other novel project
5460
folder layouts:
5561

5662
```js
57-
"import-x/no-extraneous-dependencies": ["error", {"packageDir": ['./some-dir/', './root-pkg']}]
63+
"import-x/no-extraneous-dependencies": ["error", {"packageDir": ["./some-dir/", "./root-pkg"]}]
64+
```
65+
66+
#### `whitelist`
67+
68+
The `whitelist` option is an optional string array to specify the names of packages that this rule should ignore.
69+
70+
```js
71+
"import-x/no-extraneous-dependencies": ["error", {"whitelist": ["foo", "bar"]}]
5872
```
5973

6074
## Rule Details

src/rules/no-extraneous-dependencies.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ function reportIfMissing(
215215
depsOptions: DepsOptions,
216216
node: TSESTree.Node,
217217
name: string,
218+
whitelist: Set<string> | undefined,
218219
) {
219220
// Do not report when importing types unless option is enabled
220221
if (
@@ -292,6 +293,10 @@ function reportIfMissing(
292293

293294
const packageName = realPackageName || importPackageName
294295

296+
if (whitelist?.has(packageName)) {
297+
return
298+
}
299+
295300
if (declarationStatus.isInDevDeps && !depsOptions.allowDevDeps) {
296301
context.report({
297302
node,
@@ -342,6 +347,7 @@ type Options = {
342347
bundledDependencies?: boolean
343348
includeInternal?: boolean
344349
includeTypes?: boolean
350+
whitelist?: string[]
345351
}
346352

347353
type MessageId =
@@ -370,6 +376,7 @@ export = createRule<[Options?], MessageId>({
370376
packageDir: { type: ['string', 'array'] },
371377
includeInternal: { type: ['boolean'] },
372378
includeTypes: { type: ['boolean'] },
379+
whitelist: { type: ['array'] },
373380
},
374381
additionalProperties: false,
375382
},
@@ -408,7 +415,14 @@ export = createRule<[Options?], MessageId>({
408415
return {
409416
...moduleVisitor(
410417
(source, node) => {
411-
reportIfMissing(context, deps, depsOptions, node, source.value)
418+
reportIfMissing(
419+
context,
420+
deps,
421+
depsOptions,
422+
node,
423+
source.value,
424+
options.whitelist ? new Set(options.whitelist) : undefined,
425+
)
412426
},
413427
{ commonjs: true },
414428
),

test/rules/no-extraneous-dependencies.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ ruleTester.run('no-extraneous-dependencies', rule, {
220220
},
221221
},
222222
}),
223+
224+
test({
225+
code: 'import "not-a-dependency"',
226+
filename: path.join(packageDirMonoRepoRoot, 'foo.js'),
227+
options: [
228+
{ packageDir: packageDirMonoRepoRoot, whitelist: ['not-a-dependency'] },
229+
],
230+
}),
223231
],
224232
invalid: [
225233
test({

0 commit comments

Comments
 (0)