From 1f7422d2e06e4f6accd68031e5768dd38a7ac18a Mon Sep 17 00:00:00 2001 From: Fernando Pasik Date: Fri, 12 Jun 2020 16:04:52 +0100 Subject: [PATCH] [Fix] `no-extraneous-dependencies`/TypeScript: do not error when importing type from dev dependencies Fixes #1618. --- CHANGELOG.md | 5 ++ src/rules/no-extraneous-dependencies.js | 2 +- .../package.json | 5 ++ tests/src/rules/no-extraneous-dependencies.js | 56 ++++++++++++++++++- 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/files/with-typescript-dev-dependencies/package.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cb0223be..6ad6bc43b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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]) @@ -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 @@ -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 diff --git a/src/rules/no-extraneous-dependencies.js b/src/rules/no-extraneous-dependencies.js index 03c45526c..366a684c4 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/src/rules/no-extraneous-dependencies.js @@ -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 } diff --git a/tests/files/with-typescript-dev-dependencies/package.json b/tests/files/with-typescript-dev-dependencies/package.json new file mode 100644 index 000000000..e17fbd977 --- /dev/null +++ b/tests/files/with-typescript-dev-dependencies/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "@types/json-schema": "*" + } +} diff --git a/tests/src/rules/no-extraneous-dependencies.js b/tests/src/rules/no-extraneous-dependencies.js index 97279d853..a3ddb0742 100644 --- a/tests/src/rules/no-extraneous-dependencies.js +++ b/tests/src/rules/no-extraneous-dependencies.js @@ -1,4 +1,4 @@ -import { test } from '../utils' +import { getTSParsers, test } from '../utils' import * as path from 'path' import * as fs from 'fs' @@ -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') @@ -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)), + ], + }) + } + }) +})