Skip to content

Commit

Permalink
[Fix] no-extraneous-dependencies/TypeScript: do not error when impo…
Browse files Browse the repository at this point in the history
…rting inline type from dev dependencies
  • Loading branch information
AndyOGo authored and ljharb committed Mar 14, 2023
1 parent 0ae35c0 commit 9799567
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`no-duplicates`]: remove duplicate identifiers in duplicate imports ([#2577], thanks [@joe-matsec])
- [`consistent-type-specifier-style`]: fix accidental removal of comma in certain cases ([#2754], thanks [@bradzacher])
- [Perf] `ExportMap`: Improve `ExportMap.for` performance on larger codebases ([#2756], thanks [@leipert])
- [`no-extraneous-dependencies`]/TypeScript: do not error when importing inline type from dev dependencies ([#1820], thanks [@andyogo])

### Changed
- [Docs] [`no-duplicates`]: fix example schema ([#2684], thanks [@simmo])
Expand Down Expand Up @@ -1070,6 +1071,7 @@ for info on changes for earlier releases.
[#2756]: https://github.com/import-js/eslint-plugin-import/pull/2756
[#2754]: https://github.com/import-js/eslint-plugin-import/pull/2754
[#2748]: https://github.com/import-js/eslint-plugin-import/pull/2748
[#2735]: https://github.com/import-js/eslint-plugin-import/pull/2735
[#2699]: https://github.com/import-js/eslint-plugin-import/pull/2699
[#2664]: https://github.com/import-js/eslint-plugin-import/pull/2664
[#2613]: https://github.com/import-js/eslint-plugin-import/pull/2613
Expand Down Expand Up @@ -1625,6 +1627,7 @@ for info on changes for earlier releases.
[@alexgorbatchev]: https://github.com/alexgorbatchev
[@andreubotella]: https://github.com/andreubotella
[@AndrewLeedham]: https://github.com/AndrewLeedham
[@andyogo]: https://github.com/andyogo
[@aravindet]: https://github.com/aravindet
[@arvigeus]: https://github.com/arvigeus
[@asapach]: https://github.com/asapach
Expand Down
1 change: 1 addition & 0 deletions docs/rules/no-extraneous-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Modules have to be installed for this rule to work.
This rule supports the following options:

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

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

Expand Down
7 changes: 6 additions & 1 deletion src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ function reportIfMissing(context, deps, depsOptions, node, name) {
// Do not report when importing types unless option is enabled
if (
!depsOptions.verifyTypeImports &&
(node.importKind === 'type' || node.importKind === 'typeof')
(node.importKind === 'type' || node.importKind === 'typeof' ||
(
Array.isArray(node.specifiers) &&
node.specifiers.length &&
node.specifiers.every((specifier) => specifier.importKind === 'type' || specifier.importKind === 'typeof'))
)
) {
return;
}
Expand Down
30 changes: 28 additions & 2 deletions tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ describe('TypeScript', () => {

test(Object.assign({
code: 'import type T from "a";',
options: [{
options: [{
packageDir: packageDirWithTypescriptDevDependencies,
devDependencies: false,
includeTypes: true,
Expand All @@ -464,6 +464,16 @@ typescriptRuleTester.run('no-extraneous-dependencies typescript type imports', r
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
parser: parsers.BABEL_OLD,
}),
test({
code: 'import { type MyType } from "not-a-dependency";',
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
parser: parsers.BABEL_OLD,
}),
test({
code: 'import { type MyType, type OtherType } from "not-a-dependency";',
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
parser: parsers.BABEL_OLD,
}),
],
invalid: [
test({
Expand All @@ -476,13 +486,29 @@ typescriptRuleTester.run('no-extraneous-dependencies typescript type imports', r
}],
}),
test({
code: `import type { Foo } from 'not-a-dependency'`,
code: `import type { Foo } from 'not-a-dependency';`,
options: [{ includeTypes: true }],
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
parser: parsers.BABEL_OLD,
errors: [{
message: `'not-a-dependency' should be listed in the project's dependencies. Run 'npm i -S not-a-dependency' to add it`,
}],
}),
test({
code: 'import Foo, { type MyType } from "not-a-dependency";',
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
parser: parsers.BABEL_OLD,
errors: [{
message: `'not-a-dependency' should be listed in the project's dependencies. Run 'npm i -S not-a-dependency' to add it`,
}],
}),
test({
code: 'import { type MyType, Foo } from "not-a-dependency";',
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
parser: parsers.BABEL_OLD,
errors: [{
message: `'not-a-dependency' should be listed in the project's dependencies. Run 'npm i -S not-a-dependency' to add it`,
}],
}),
],
});

0 comments on commit 9799567

Please sign in to comment.