Skip to content

Commit 1f7422d

Browse files
fernandopasikljharb
authored andcommitted
[Fix] no-extraneous-dependencies/TypeScript: do not error when importing type from dev dependencies
Fixes #1618.
1 parent 54eb51b commit 1f7422d

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
### Added
99
- [`no-unused-modules`]: consider exported TypeScript interfaces, types and enums ([#1819], thanks [@nicolashenry])
1010

11+
### Fixed
12+
- [`no-extraneous-dependencies`]/TypeScript: do not error when importing type from dev dependencies ([#1820], thanks [@fernandopasik])
13+
1114
## [2.21.2] - 2020-06-09
1215
### Fixed
1316
- [`order`]: avoid a crash on TypeScript’s `export import` syntax ([#1808], thanks [@ljharb])
@@ -704,6 +707,7 @@ for info on changes for earlier releases.
704707

705708
[`memo-parser`]: ./memo-parser/README.md
706709

710+
[#1820]: https://github.com/benmosher/eslint-plugin-import/pull/1820
707711
[#1819]: https://github.com/benmosher/eslint-plugin-import/pull/1819
708712
[#1802]: https://github.com/benmosher/eslint-plugin-import/pull/1802
709713
[#1801]: https://github.com/benmosher/eslint-plugin-import/issues/1801
@@ -1220,3 +1224,4 @@ for info on changes for earlier releases.
12201224
[@Maxim-Mazurok]: https://github.com/Maxim-Mazurok
12211225
[@malykhinvi]: https://github.com/malykhinvi
12221226
[@nicolashenry]: https://github.com/nicolashenry
1227+
[@fernandopasik]: https://github.com/fernandopasik

src/rules/no-extraneous-dependencies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function optDepErrorMessage(packageName) {
111111

112112
function reportIfMissing(context, deps, depsOptions, node, name) {
113113
// Do not report when importing types
114-
if (node.importKind === 'type') {
114+
if (node.importKind === 'type' || (node.parent && node.parent.importKind === 'type')) {
115115
return
116116
}
117117

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"@types/json-schema": "*"
4+
}
5+
}

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test } from '../utils'
1+
import { getTSParsers, test } from '../utils'
22
import * as path from 'path'
33
import * as fs from 'fs'
44

@@ -17,6 +17,7 @@ const packageFileWithSyntaxErrorMessage = (() => {
1717
}
1818
})()
1919
const packageDirWithFlowTyped = path.join(__dirname, '../../files/with-flow-typed')
20+
const packageDirWithTypescriptDevDependencies = path.join(__dirname, '../../files/with-typescript-dev-dependencies')
2021
const packageDirMonoRepoRoot = path.join(__dirname, '../../files/monorepo')
2122
const packageDirMonoRepoWithNested = path.join(__dirname, '../../files/monorepo/packages/nested-package')
2223
const packageDirWithEmpty = path.join(__dirname, '../../files/empty')
@@ -312,3 +313,56 @@ ruleTester.run('no-extraneous-dependencies', rule, {
312313
}),
313314
],
314315
})
316+
317+
describe('TypeScript', function () {
318+
getTSParsers()
319+
.forEach((parser) => {
320+
const parserConfig = {
321+
parser: parser,
322+
settings: {
323+
'import/parsers': { [parser]: ['.ts'] },
324+
'import/resolver': { 'eslint-import-resolver-typescript': true },
325+
},
326+
}
327+
328+
if (parser !== require.resolve('typescript-eslint-parser')) {
329+
ruleTester.run('no-extraneous-dependencies', rule, {
330+
valid: [
331+
test(Object.assign({
332+
code: 'import type { JSONSchema7Type } from "@types/json-schema";',
333+
options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
334+
}, parserConfig)),
335+
],
336+
invalid: [
337+
test(Object.assign({
338+
code: 'import { JSONSchema7Type } from "@types/json-schema";',
339+
options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
340+
errors: [{
341+
message: "'@types/json-schema' should be listed in the project's dependencies, not devDependencies.",
342+
}],
343+
}, parserConfig)),
344+
],
345+
})
346+
} else {
347+
ruleTester.run('no-extraneous-dependencies', rule, {
348+
valid: [],
349+
invalid: [
350+
test(Object.assign({
351+
code: 'import { JSONSchema7Type } from "@types/json-schema";',
352+
options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
353+
errors: [{
354+
message: "'@types/json-schema' should be listed in the project's dependencies, not devDependencies.",
355+
}],
356+
}, parserConfig)),
357+
test(Object.assign({
358+
code: 'import type { JSONSchema7Type } from "@types/json-schema";',
359+
options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
360+
errors: [{
361+
message: "'@types/json-schema' should be listed in the project's dependencies, not devDependencies.",
362+
}],
363+
}, parserConfig)),
364+
],
365+
})
366+
}
367+
})
368+
})

0 commit comments

Comments
 (0)