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

[flow] no-unused-modules: add flow type support #1542

Merged
merged 1 commit into from Dec 5, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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
@@ -0,0 +1 @@
import { type FooType } from './flow-2';
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/flow-1.js
@@ -0,0 +1,2 @@
// @flow strict
export type Bar = number;
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/flow-2.js
@@ -0,0 +1,2 @@
// @flow strict
export type FooType = string;
32 changes: 32 additions & 0 deletions tests/src/rules/no-unused-modules.js
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`),
],
}),
],
})
})