From 05085bbdafa624d8cf6a765b9e078c41c931679b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fermann?= Date: Sun, 17 Nov 2019 10:47:01 +0100 Subject: [PATCH] [flow] `no-unused-modules`: add flow type support --- CHANGELOG.md | 2 ++ src/rules/no-unused-modules.js | 7 ++++-- tests/files/no-unused-modules/flow-0.js | 1 + tests/files/no-unused-modules/flow-1.js | 2 ++ tests/files/no-unused-modules/flow-2.js | 2 ++ tests/src/rules/no-unused-modules.js | 32 +++++++++++++++++++++++++ 6 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tests/files/no-unused-modules/flow-0.js create mode 100644 tests/files/no-unused-modules/flow-1.js create mode 100644 tests/files/no-unused-modules/flow-2.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 6639a77c1..fc51048f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel - [`no-namespace`]: Make rule fixable ([#1401], thanks [@TrevorBurnham]) - support `parseForESLint` from custom parser ([#1435], thanks [@JounQin]) - [`no-extraneous-dependencies`]: Implement support for [bundledDependencies](https://npm.github.io/using-pkgs-docs/package-json/types/bundleddependencies.html) ([#1436], thanks [@schmidsi])) +- [`no-unused-modules`]: add flow type support ([#1542], thanks [@rfermann]) ### Fixed - [`default`]: make error message less confusing ([#1470], thanks [@golopot]) @@ -620,6 +621,7 @@ for info on changes for earlier releases. [`memo-parser`]: ./memo-parser/README.md [#1551]: https://github.com/benmosher/eslint-plugin-import/pull/1551 +[#1542]: https://github.com/benmosher/eslint-plugin-import/pull/1542 [#1521]: https://github.com/benmosher/eslint-plugin-import/pull/1521 [#1519]: https://github.com/benmosher/eslint-plugin-import/pull/1519 [#1507]: https://github.com/benmosher/eslint-plugin-import/pull/1507 diff --git a/src/rules/no-unused-modules.js b/src/rules/no-unused-modules.js index 860a73d62..ccb16fd95 100644 --- a/src/rules/no-unused-modules.js +++ b/src/rules/no-unused-modules.js @@ -42,6 +42,7 @@ const VARIABLE_DECLARATION = 'VariableDeclaration' const FUNCTION_DECLARATION = 'FunctionDeclaration' const CLASS_DECLARATION = 'ClassDeclaration' const DEFAULT = 'default' +const TYPE_ALIAS = 'TypeAlias' let preparationDone = false const importList = new Map() @@ -463,7 +464,8 @@ module.exports = { if (declaration) { if ( declaration.type === FUNCTION_DECLARATION || - declaration.type === CLASS_DECLARATION + declaration.type === CLASS_DECLARATION || + declaration.type === TYPE_ALIAS ) { newExportIdentifiers.add(declaration.id.name) } @@ -788,7 +790,8 @@ module.exports = { if (node.declaration) { if ( node.declaration.type === FUNCTION_DECLARATION || - node.declaration.type === CLASS_DECLARATION + node.declaration.type === CLASS_DECLARATION || + node.declaration.type === TYPE_ALIAS ) { checkUsage(node, node.declaration.id.name) } diff --git a/tests/files/no-unused-modules/flow-0.js b/tests/files/no-unused-modules/flow-0.js new file mode 100644 index 000000000..46bda6879 --- /dev/null +++ b/tests/files/no-unused-modules/flow-0.js @@ -0,0 +1 @@ +import { type FooType } from './flow-2'; diff --git a/tests/files/no-unused-modules/flow-1.js b/tests/files/no-unused-modules/flow-1.js new file mode 100644 index 000000000..bb7266d3c --- /dev/null +++ b/tests/files/no-unused-modules/flow-1.js @@ -0,0 +1,2 @@ +// @flow strict +export type Bar = number; diff --git a/tests/files/no-unused-modules/flow-2.js b/tests/files/no-unused-modules/flow-2.js new file mode 100644 index 000000000..0cbb836a6 --- /dev/null +++ b/tests/files/no-unused-modules/flow-2.js @@ -0,0 +1,2 @@ +// @flow strict +export type FooType = string; diff --git a/tests/src/rules/no-unused-modules.js b/tests/src/rules/no-unused-modules.js index 792748cd8..fed0f39bb 100644 --- a/tests/src/rules/no-unused-modules.js +++ b/tests/src/rules/no-unused-modules.js @@ -638,3 +638,35 @@ describe('do not report unused export for files mentioned in package.json', () = ], }) }) + +describe('correctly report flow types', () => { + ruleTester.run('no-unused-modules', rule, { + valid: [ + test({ + options: unusedExportsOptions, + code: 'import { type FooType } from "./flow-2";', + parser: require.resolve('babel-eslint'), + filename: testFilePath('./no-unused-modules/flow-0.js'), + }), + test({ + options: unusedExportsOptions, + code: `// @flow strict + export type FooType = string;`, + parser: require.resolve('babel-eslint'), + filename: testFilePath('./no-unused-modules/flow-2.js'), + }), + ], + invalid: [ + test({ + options: unusedExportsOptions, + code: `// @flow strict + export type Bar = string;`, + parser: require.resolve('babel-eslint'), + filename: testFilePath('./no-unused-modules/flow-1.js'), + errors: [ + error(`exported declaration 'Bar' not used within other modules`), + ], + }), + ], + }) +})