Skip to content

Commit

Permalink
[flow] no-unused-modules: add flow type support
Browse files Browse the repository at this point in the history
  • Loading branch information
rfermann authored and ljharb committed Nov 17, 2019
1 parent 14c71a3 commit 05085bb
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/flow-0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { type FooType } from './flow-2';
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/flow-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @flow strict
export type Bar = number;
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/flow-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @flow strict
export type FooType = string;
32 changes: 32 additions & 0 deletions tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`),
],
}),
],
})
})

0 comments on commit 05085bb

Please sign in to comment.