Skip to content

Commit 9799567

Browse files
AndyOGoljharb
authored andcommitted
[Fix] no-extraneous-dependencies/TypeScript: do not error when importing inline type from dev dependencies
1 parent 0ae35c0 commit 9799567

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1010
- [`no-duplicates`]: remove duplicate identifiers in duplicate imports ([#2577], thanks [@joe-matsec])
1111
- [`consistent-type-specifier-style`]: fix accidental removal of comma in certain cases ([#2754], thanks [@bradzacher])
1212
- [Perf] `ExportMap`: Improve `ExportMap.for` performance on larger codebases ([#2756], thanks [@leipert])
13+
- [`no-extraneous-dependencies`]/TypeScript: do not error when importing inline type from dev dependencies ([#1820], thanks [@andyogo])
1314

1415
### Changed
1516
- [Docs] [`no-duplicates`]: fix example schema ([#2684], thanks [@simmo])
@@ -1070,6 +1071,7 @@ for info on changes for earlier releases.
10701071
[#2756]: https://github.com/import-js/eslint-plugin-import/pull/2756
10711072
[#2754]: https://github.com/import-js/eslint-plugin-import/pull/2754
10721073
[#2748]: https://github.com/import-js/eslint-plugin-import/pull/2748
1074+
[#2735]: https://github.com/import-js/eslint-plugin-import/pull/2735
10731075
[#2699]: https://github.com/import-js/eslint-plugin-import/pull/2699
10741076
[#2664]: https://github.com/import-js/eslint-plugin-import/pull/2664
10751077
[#2613]: https://github.com/import-js/eslint-plugin-import/pull/2613
@@ -1625,6 +1627,7 @@ for info on changes for earlier releases.
16251627
[@alexgorbatchev]: https://github.com/alexgorbatchev
16261628
[@andreubotella]: https://github.com/andreubotella
16271629
[@AndrewLeedham]: https://github.com/AndrewLeedham
1630+
[@andyogo]: https://github.com/andyogo
16281631
[@aravindet]: https://github.com/aravindet
16291632
[@arvigeus]: https://github.com/arvigeus
16301633
[@asapach]: https://github.com/asapach

docs/rules/no-extraneous-dependencies.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Modules have to be installed for this rule to work.
1212
This rule supports the following options:
1313

1414
`devDependencies`: If set to `false`, then the rule will show an error when `devDependencies` are imported. Defaults to `true`.
15+
Type imports are ignored by default.
1516

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

src/rules/no-extraneous-dependencies.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
178178
// Do not report when importing types unless option is enabled
179179
if (
180180
!depsOptions.verifyTypeImports &&
181-
(node.importKind === 'type' || node.importKind === 'typeof')
181+
(node.importKind === 'type' || node.importKind === 'typeof' ||
182+
(
183+
Array.isArray(node.specifiers) &&
184+
node.specifiers.length &&
185+
node.specifiers.every((specifier) => specifier.importKind === 'type' || specifier.importKind === 'typeof'))
186+
)
182187
) {
183188
return;
184189
}

tests/src/rules/no-extraneous-dependencies.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ describe('TypeScript', () => {
438438

439439
test(Object.assign({
440440
code: 'import type T from "a";',
441-
options: [{
441+
options: [{
442442
packageDir: packageDirWithTypescriptDevDependencies,
443443
devDependencies: false,
444444
includeTypes: true,
@@ -464,6 +464,16 @@ typescriptRuleTester.run('no-extraneous-dependencies typescript type imports', r
464464
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
465465
parser: parsers.BABEL_OLD,
466466
}),
467+
test({
468+
code: 'import { type MyType } from "not-a-dependency";',
469+
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
470+
parser: parsers.BABEL_OLD,
471+
}),
472+
test({
473+
code: 'import { type MyType, type OtherType } from "not-a-dependency";',
474+
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
475+
parser: parsers.BABEL_OLD,
476+
}),
467477
],
468478
invalid: [
469479
test({
@@ -476,13 +486,29 @@ typescriptRuleTester.run('no-extraneous-dependencies typescript type imports', r
476486
}],
477487
}),
478488
test({
479-
code: `import type { Foo } from 'not-a-dependency'`,
489+
code: `import type { Foo } from 'not-a-dependency';`,
480490
options: [{ includeTypes: true }],
481491
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
482492
parser: parsers.BABEL_OLD,
483493
errors: [{
484494
message: `'not-a-dependency' should be listed in the project's dependencies. Run 'npm i -S not-a-dependency' to add it`,
485495
}],
486496
}),
497+
test({
498+
code: 'import Foo, { type MyType } from "not-a-dependency";',
499+
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
500+
parser: parsers.BABEL_OLD,
501+
errors: [{
502+
message: `'not-a-dependency' should be listed in the project's dependencies. Run 'npm i -S not-a-dependency' to add it`,
503+
}],
504+
}),
505+
test({
506+
code: 'import { type MyType, Foo } from "not-a-dependency";',
507+
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
508+
parser: parsers.BABEL_OLD,
509+
errors: [{
510+
message: `'not-a-dependency' should be listed in the project's dependencies. Run 'npm i -S not-a-dependency' to add it`,
511+
}],
512+
}),
487513
],
488514
});

0 commit comments

Comments
 (0)