Skip to content

Commit

Permalink
[Fix] no-unused-modules: consider exported TypeScript interfaces, t…
Browse files Browse the repository at this point in the history
…ypes and enums

Fixes #1680

Co-authored-by: e020873 <nicolas.henry-partner@arcelormittal.com>
Co-authored-by: Jordan Harband <ljharb@gmail.com>
  • Loading branch information
nicolashenry and ljharb committed Jun 11, 2020
1 parent ec5195e commit f064772
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
### Fixed
- [`no-unused-modules`]: consider exported TypeScript interfaces, types and enums ([#1819], thanks [@nicolashenry])

## [2.21.2] - 2020-06-09
### Fixed
Expand Down Expand Up @@ -702,6 +704,7 @@ for info on changes for earlier releases.

[`memo-parser`]: ./memo-parser/README.md

[#1819]: https://github.com/benmosher/eslint-plugin-import/pull/1819
[#1802]: https://github.com/benmosher/eslint-plugin-import/pull/1802
[#1801]: https://github.com/benmosher/eslint-plugin-import/issues/1801
[#1788]: https://github.com/benmosher/eslint-plugin-import/pull/1788
Expand Down Expand Up @@ -1216,3 +1219,4 @@ for info on changes for earlier releases.
[@adjerbetian]: https://github.com/adjerbetian
[@Maxim-Mazurok]: https://github.com/Maxim-Mazurok
[@malykhinvi]: https://github.com/malykhinvi
[@nicolashenry]: https://github.com/nicolashenry
57 changes: 31 additions & 26 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,33 @@ const IMPORT_DEFAULT_SPECIFIER = 'ImportDefaultSpecifier'
const VARIABLE_DECLARATION = 'VariableDeclaration'
const FUNCTION_DECLARATION = 'FunctionDeclaration'
const CLASS_DECLARATION = 'ClassDeclaration'
const INTERFACE_DECLARATION = 'InterfaceDeclaration'
const TYPE_ALIAS = 'TypeAlias'
const TS_INTERFACE_DECLARATION = 'TSInterfaceDeclaration'
const TS_TYPE_ALIAS_DECLARATION = 'TSTypeAliasDeclaration'
const TS_ENUM_DECLARATION = 'TSEnumDeclaration'
const DEFAULT = 'default'

function forEachDeclarationIdentifier(declaration, cb) {
if (declaration) {
if (
declaration.type === FUNCTION_DECLARATION ||
declaration.type === CLASS_DECLARATION ||
declaration.type === INTERFACE_DECLARATION ||
declaration.type === TYPE_ALIAS ||
declaration.type === TS_INTERFACE_DECLARATION ||
declaration.type === TS_TYPE_ALIAS_DECLARATION ||
declaration.type === TS_ENUM_DECLARATION
) {
cb(declaration.id.name)
} else if (declaration.type === VARIABLE_DECLARATION) {
declaration.declarations.forEach(({ id }) => {
cb(id.name)
})
}
}
}

/**
* List of imports per file.
*
Expand Down Expand Up @@ -559,19 +584,9 @@ module.exports = {
}
})
}
if (declaration) {
if (
declaration.type === FUNCTION_DECLARATION ||
declaration.type === CLASS_DECLARATION
) {
newExportIdentifiers.add(declaration.id.name)
}
if (declaration.type === VARIABLE_DECLARATION) {
declaration.declarations.forEach(({ id }) => {
newExportIdentifiers.add(id.name)
})
}
}
forEachDeclarationIdentifier(declaration, (name) => {
newExportIdentifiers.add(name)
})
}
})

Expand Down Expand Up @@ -883,19 +898,9 @@ module.exports = {
node.specifiers.forEach(specifier => {
checkUsage(node, specifier.exported.name)
})
if (node.declaration) {
if (
node.declaration.type === FUNCTION_DECLARATION ||
node.declaration.type === CLASS_DECLARATION
) {
checkUsage(node, node.declaration.id.name)
}
if (node.declaration.type === VARIABLE_DECLARATION) {
node.declaration.declarations.forEach(declaration => {
checkUsage(node, declaration.id.name)
})
}
}
forEachDeclarationIdentifier(node.declaration, (name) => {
checkUsage(node, name)
})
},
}
},
Expand Down
7 changes: 6 additions & 1 deletion tests/files/no-unused-modules/typescript/file-ts-a.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import {b} from './file-ts-b';
import {c} from './file-ts-c';
import {d} from './file-ts-d';
import {e} from './file-ts-e';

export const a = b + 1;
export const a = b + 1 + e.f;
export const a2: c = {};
export const a3: d = {};
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface c {};
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type d = {};
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/typescript/file-ts-e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export enum e { f };
73 changes: 72 additions & 1 deletion tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, testFilePath } from '../utils'
import { test, testFilePath, getTSParsers } from '../utils'
import jsxConfig from '../../../config/react'
import typescriptConfig from '../../../config/typescript'

Expand Down Expand Up @@ -736,10 +736,81 @@ describe('correctly work with Typescript only files', () => {
error(`exported declaration 'b' not used within other modules`),
],
}),
test({
options: unusedExportsTypescriptOptions,
code: `export interface c {};`,
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/typescript/file-ts-c.ts'),
errors: [
error(`exported declaration 'c' not used within other modules`),
],
}),
test({
options: unusedExportsTypescriptOptions,
code: `export type d = {};`,
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/typescript/file-ts-d.ts'),
errors: [
error(`exported declaration 'd' not used within other modules`),
],
}),
],
})
})

context('TypeScript', function () {
getTSParsers().forEach((parser) => {
typescriptRuleTester.run('no-unused-modules', rule, {
valid: [
test({
options: unusedExportsTypescriptOptions,
code: 'import a from "file-ts-a";',
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-a.ts'),
}),
],
invalid: [
test({
options: unusedExportsTypescriptOptions,
code: `export const b = 2;`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-b.ts'),
errors: [
error(`exported declaration 'b' not used within other modules`),
],
}),
test({
options: unusedExportsTypescriptOptions,
code: `export interface c {};`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-c.ts'),
errors: [
error(`exported declaration 'c' not used within other modules`),
],
}),
test({
options: unusedExportsTypescriptOptions,
code: `export type d = {};`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-d.ts'),
errors: [
error(`exported declaration 'd' not used within other modules`),
],
}),
test({
options: unusedExportsTypescriptOptions,
code: `export enum e { f };`,
parser: parser,
filename: testFilePath('./no-unused-modules/typescript/file-ts-e.ts'),
errors: [
error(`exported declaration 'e' not used within other modules`),
],
}),
],
})
})
})

describe('correctly work with JSX only files', () => {
jsxRuleTester.run('no-unused-modules', rule, {
valid: [
Expand Down

0 comments on commit f064772

Please sign in to comment.