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

[Fix] no-extraneous-dependencies/TypeScript: do not error when importing type from dev dependencies #1820

Merged
merged 1 commit into from
Jun 14, 2020
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Added
- [`no-unused-modules`]: consider exported TypeScript interfaces, types and enums ([#1819], thanks [@nicolashenry])

### Fixed
- [`no-extraneous-dependencies`]/TypeScript: do not error when importing type from dev dependencies ([#1820], thanks [@fernandopasik])

## [2.21.2] - 2020-06-09
### Fixed
- [`order`]: avoid a crash on TypeScript’s `export import` syntax ([#1808], thanks [@ljharb])
Expand Down Expand Up @@ -704,6 +707,7 @@ for info on changes for earlier releases.

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

[#1820]: https://github.com/benmosher/eslint-plugin-import/pull/1820
[#1819]: https://github.com/benmosher/eslint-plugin-import/pull/1819
[#1802]: https://github.com/benmosher/eslint-plugin-import/pull/1802
[#1801]: https://github.com/benmosher/eslint-plugin-import/issues/1801
Expand Down Expand Up @@ -1220,3 +1224,4 @@ for info on changes for earlier releases.
[@Maxim-Mazurok]: https://github.com/Maxim-Mazurok
[@malykhinvi]: https://github.com/malykhinvi
[@nicolashenry]: https://github.com/nicolashenry
[@fernandopasik]: https://github.com/fernandopasik
2 changes: 1 addition & 1 deletion src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function optDepErrorMessage(packageName) {

function reportIfMissing(context, deps, depsOptions, node, name) {
// Do not report when importing types
if (node.importKind === 'type') {
if (node.importKind === 'type' || (node.parent && node.parent.importKind === 'type')) {
return
}

Expand Down
5 changes: 5 additions & 0 deletions tests/files/with-typescript-dev-dependencies/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"@types/json-schema": "*"
}
}
56 changes: 55 additions & 1 deletion tests/src/rules/no-extraneous-dependencies.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from '../utils'
import { getTSParsers, test } from '../utils'
import * as path from 'path'
import * as fs from 'fs'

Expand All @@ -17,6 +17,7 @@ const packageFileWithSyntaxErrorMessage = (() => {
}
})()
const packageDirWithFlowTyped = path.join(__dirname, '../../files/with-flow-typed')
const packageDirWithTypescriptDevDependencies = path.join(__dirname, '../../files/with-typescript-dev-dependencies')
const packageDirMonoRepoRoot = path.join(__dirname, '../../files/monorepo')
const packageDirMonoRepoWithNested = path.join(__dirname, '../../files/monorepo/packages/nested-package')
const packageDirWithEmpty = path.join(__dirname, '../../files/empty')
Expand Down Expand Up @@ -312,3 +313,56 @@ ruleTester.run('no-extraneous-dependencies', rule, {
}),
],
})

describe('TypeScript', function () {
getTSParsers()
.forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
}

if (parser !== require.resolve('typescript-eslint-parser')) {
ruleTester.run('no-extraneous-dependencies', rule, {
valid: [
test(Object.assign({
code: 'import type { JSONSchema7Type } from "@types/json-schema";',
options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
}, parserConfig)),
],
invalid: [
test(Object.assign({
code: 'import { JSONSchema7Type } from "@types/json-schema";',
options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
errors: [{
message: "'@types/json-schema' should be listed in the project's dependencies, not devDependencies.",
}],
}, parserConfig)),
],
})
} else {
ruleTester.run('no-extraneous-dependencies', rule, {
valid: [],
invalid: [
test(Object.assign({
code: 'import { JSONSchema7Type } from "@types/json-schema";',
options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
errors: [{
message: "'@types/json-schema' should be listed in the project's dependencies, not devDependencies.",
}],
}, parserConfig)),
test(Object.assign({
code: 'import type { JSONSchema7Type } from "@types/json-schema";',
options: [{packageDir: packageDirWithTypescriptDevDependencies, devDependencies: false }],
errors: [{
message: "'@types/json-schema' should be listed in the project's dependencies, not devDependencies.",
}],
}, parserConfig)),
],
})
}
})
})