Skip to content

Commit

Permalink
consider exported TypeScript interfaces, types and enums
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolashenry committed Jun 11, 2020
1 parent f53e872 commit 98ea37e
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 4 deletions.
2 changes: 2 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 ([#1680], thanks [@nicolashenry])

## [2.21.2] - 2020-06-09
### Fixed
Expand Down
19 changes: 17 additions & 2 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ 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'

/**
Expand Down Expand Up @@ -562,7 +567,12 @@ module.exports = {
if (declaration) {
if (
declaration.type === FUNCTION_DECLARATION ||
declaration.type === CLASS_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
) {
newExportIdentifiers.add(declaration.id.name)
}
Expand Down Expand Up @@ -886,7 +896,12 @@ module.exports = {
if (node.declaration) {
if (
node.declaration.type === FUNCTION_DECLARATION ||
node.declaration.type === CLASS_DECLARATION
node.declaration.type === CLASS_DECLARATION ||
node.declaration.type === INTERFACE_DECLARATION ||
node.declaration.type === TYPE_ALIAS ||
node.declaration.type === TS_INTERFACE_DECLARATION ||
node.declaration.type === TS_TYPE_ALIAS_DECLARATION ||
node.declaration.type === TS_ENUM_DECLARATION
) {
checkUsage(node, node.declaration.id.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 98ea37e

Please sign in to comment.