Skip to content

Commit f9d7e2b

Browse files
committed
[flow] no-unused-modules: add flow type support
1 parent 112a0bf commit f9d7e2b

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

src/rules/no-unused-modules.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const VARIABLE_DECLARATION = 'VariableDeclaration'
4242
const FUNCTION_DECLARATION = 'FunctionDeclaration'
4343
const CLASS_DECLARATION = 'ClassDeclaration'
4444
const DEFAULT = 'default'
45+
const TYPE_ALIAS = 'TypeAlias'
4546

4647
let preparationDone = false
4748
const importList = new Map()
@@ -391,6 +392,7 @@ module.exports = {
391392
}
392393

393394
exports = exportList.get(file)
395+
console.log('file: ', file)
394396

395397
// special case: export * from
396398
const exportAll = exports.get(EXPORT_ALL_DECLARATION)
@@ -409,8 +411,10 @@ module.exports = {
409411
}
410412

411413
const exportStatement = exports.get(exportedValue)
414+
console.log('exportStatement: ', exportStatement)
412415

413416
const value = exportedValue === IMPORT_DEFAULT_SPECIFIER ? DEFAULT : exportedValue
417+
console.log('value: ', value)
414418

415419
if (typeof exportStatement !== 'undefined'){
416420
if (exportStatement.whereUsed.size < 1) {
@@ -463,7 +467,8 @@ module.exports = {
463467
if (declaration) {
464468
if (
465469
declaration.type === FUNCTION_DECLARATION ||
466-
declaration.type === CLASS_DECLARATION
470+
declaration.type === CLASS_DECLARATION ||
471+
declaration.type === TYPE_ALIAS
467472
) {
468473
newExportIdentifiers.add(declaration.id.name)
469474
}
@@ -788,7 +793,8 @@ module.exports = {
788793
if (node.declaration) {
789794
if (
790795
node.declaration.type === FUNCTION_DECLARATION ||
791-
node.declaration.type === CLASS_DECLARATION
796+
node.declaration.type === CLASS_DECLARATION ||
797+
node.declaration.type === TYPE_ALIAS
792798
) {
793799
checkUsage(node, node.declaration.id.name)
794800
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { type FooType } from './flow-2';
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @flow strict
2+
export type Bar = number;
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// @flow strict
2+
export type FooType = string;

tests/src/rules/no-unused-modules.js

+32
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,35 @@ describe('do not report unused export for files mentioned in package.json', () =
638638
],
639639
})
640640
})
641+
642+
describe('correctly report flow types', () => {
643+
ruleTester.run('no-unused-modules', rule, {
644+
valid: [
645+
test({
646+
options: unusedExportsOptions,
647+
code: 'import { type FooType } from "./flow-2";',
648+
parser: require.resolve('babel-eslint'),
649+
filename: testFilePath('./no-unused-modules/flow-0.js'),
650+
}),
651+
test({
652+
options: unusedExportsOptions,
653+
code: `// @flow strict
654+
export type FooType = string;`,
655+
parser: require.resolve('babel-eslint'),
656+
filename: testFilePath('./no-unused-modules/flow-2.js'),
657+
}),
658+
],
659+
invalid: [
660+
test({
661+
options: unusedExportsOptions,
662+
code: `// @flow strict
663+
export type Bar = string;`,
664+
parser: require.resolve('babel-eslint'),
665+
filename: testFilePath('./no-unused-modules/flow-1.js'),
666+
errors: [
667+
error(`exported declaration 'Bar' not used within other modules`),
668+
],
669+
}),
670+
],
671+
})
672+
})

0 commit comments

Comments
 (0)