diff --git a/src/rules/no-unused-modules.js b/src/rules/no-unused-modules.js index 9cd7814754..598b8c7461 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() @@ -391,6 +392,7 @@ module.exports = { } exports = exportList.get(file) + console.log('file: ', file) // special case: export * from const exportAll = exports.get(EXPORT_ALL_DECLARATION) @@ -409,8 +411,10 @@ module.exports = { } const exportStatement = exports.get(exportedValue) + console.log('exportStatement: ', exportStatement) const value = exportedValue === IMPORT_DEFAULT_SPECIFIER ? DEFAULT : exportedValue + console.log('value: ', value) if (typeof exportStatement !== 'undefined'){ if (exportStatement.whereUsed.size < 1) { @@ -463,7 +467,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 +793,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 0000000000..46bda68794 --- /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 0000000000..bb7266d3ce --- /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 0000000000..0cbb836a6d --- /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 792748cd86..fed0f39bb5 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`), + ], + }), + ], + }) +})