From b12d871bb62da9f64bbe48cfbffef326598c8bf9 Mon Sep 17 00:00:00 2001 From: Kevin Grandon Date: Sun, 24 Dec 2017 16:35:52 -0500 Subject: [PATCH] Ignore import statements when using 'import type' I was running into this issue and was curious if you all would entertain PRs for typing support. Refs #931 --- src/rules/no-extraneous-dependencies.js | 6 ++++++ .../with-flow-typed/flow-typed/npm/myflowtyped_v1.x.x.js | 4 ++++ tests/files/with-flow-typed/package.json | 3 +++ tests/src/rules/no-extraneous-dependencies.js | 6 ++++++ 4 files changed, 19 insertions(+) create mode 100644 tests/files/with-flow-typed/flow-typed/npm/myflowtyped_v1.x.x.js create mode 100644 tests/files/with-flow-typed/package.json diff --git a/src/rules/no-extraneous-dependencies.js b/src/rules/no-extraneous-dependencies.js index d3e8d0405a..b19482831a 100644 --- a/src/rules/no-extraneous-dependencies.js +++ b/src/rules/no-extraneous-dependencies.js @@ -55,9 +55,15 @@ function optDepErrorMessage(packageName) { } function reportIfMissing(context, deps, depsOptions, node, name) { + // Do not report when importing types + if (node.importKind === 'type') { + return + } + if (importType(name, context) !== 'external') { return } + const resolved = resolve(name, context) if (!resolved) { return diff --git a/tests/files/with-flow-typed/flow-typed/npm/myflowtyped_v1.x.x.js b/tests/files/with-flow-typed/flow-typed/npm/myflowtyped_v1.x.x.js new file mode 100644 index 0000000000..2917b03f51 --- /dev/null +++ b/tests/files/with-flow-typed/flow-typed/npm/myflowtyped_v1.x.x.js @@ -0,0 +1,4 @@ +// flow-typed signature: ____ +// flow-typed version: ____/myflowtyped_v1.x.x/flow_>=v0.33.x + +declare module 'myflowtyped' {} diff --git a/tests/files/with-flow-typed/package.json b/tests/files/with-flow-typed/package.json new file mode 100644 index 0000000000..18a1e415e5 --- /dev/null +++ b/tests/files/with-flow-typed/package.json @@ -0,0 +1,3 @@ +{ + "dependencies": {} +} diff --git a/tests/src/rules/no-extraneous-dependencies.js b/tests/src/rules/no-extraneous-dependencies.js index 2177831153..a8817931b1 100644 --- a/tests/src/rules/no-extraneous-dependencies.js +++ b/tests/src/rules/no-extraneous-dependencies.js @@ -14,6 +14,7 @@ const packageFileWithSyntaxErrorMessage = (() => { return error.message } })() +const packageDirWithFlowTyped = path.join(__dirname, '../../files/with-flow-typed') ruleTester.run('no-extraneous-dependencies', rule, { valid: [ @@ -69,6 +70,11 @@ ruleTester.run('no-extraneous-dependencies', rule, { code: 'import "doctrine"', options: [{packageDir: path.join(__dirname, '../../../')}], }), + test({ + code: 'import type MyType from "myflowtyped";', + options: [{packageDir: packageDirWithFlowTyped}], + parser: 'babel-eslint', + }), ], invalid: [ test({