Skip to content

Commit

Permalink
[fix] no-unused-modules: handle ClassDeclaration
Browse files Browse the repository at this point in the history
Fixes #1368
  • Loading branch information
golopot committed May 29, 2019
1 parent ca4bf95 commit 8b55e8f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const IMPORT_NAMESPACE_SPECIFIER = 'ImportNamespaceSpecifier'
const IMPORT_DEFAULT_SPECIFIER = 'ImportDefaultSpecifier'
const VARIABLE_DECLARATION = 'VariableDeclaration'
const FUNCTION_DECLARATION = 'FunctionDeclaration'
const CLASS_DECLARATION = 'ClassDeclaration'
const DEFAULT = 'default'

let preparationDone = false
Expand Down Expand Up @@ -390,7 +391,10 @@ module.exports = {
})
}
if (declaration) {
if (declaration.type === FUNCTION_DECLARATION) {
if (
declaration.type === FUNCTION_DECLARATION ||
declaration.type === CLASS_DECLARATION
) {
newExportIdentifiers.add(declaration.id.name)
}
if (declaration.type === VARIABLE_DECLARATION) {
Expand Down Expand Up @@ -712,7 +716,10 @@ module.exports = {
checkUsage(node, specifier.exported.name)
})
if (node.declaration) {
if (node.declaration.type === FUNCTION_DECLARATION) {
if (
node.declaration.type === FUNCTION_DECLARATION ||
node.declaration.type === CLASS_DECLARATION
) {
checkUsage(node, node.declaration.id.name)
}
if (node.declaration.type === VARIABLE_DECLARATION) {
Expand Down
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/file-0.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { e } from './file-e'
import { e2 } from './file-e'
import { h2 } from './file-h'
import * as l from './file-l'
import {q} from './file-q'
export * from './file-n'
export { default, o0, o3 } from './file-o'
export { p } from './file-p'
3 changes: 3 additions & 0 deletions tests/files/no-unused-modules/file-q.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class q {
q0() {}
}
9 changes: 9 additions & 0 deletions tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ ruleTester.run('no-unused-modules', rule, {
code: 'const a = 1; const b = 2; export { a, b }'}),
test({ options: missingExportsOptions,
code: 'const a = 1; export default a'}),
test({ options: missingExportsOptions,
code: 'export class Foo {}'}),
],
invalid: [
test({
Expand Down Expand Up @@ -67,6 +69,9 @@ ruleTester.run('no-unused-modules', rule, {
test({ options: unusedExportsOptions,
code: 'export function d() { return 4 }',
filename: testFilePath('./no-unused-modules/file-d.js')}),
test({ options: unusedExportsOptions,
code: 'export class q { q0() {} }',
filename: testFilePath('./no-unused-modules/file-q.js')}),
test({ options: unusedExportsOptions,
code: 'const e0 = 5; export { e0 as e }',
filename: testFilePath('./no-unused-modules/file-e.js')}),
Expand Down Expand Up @@ -132,6 +137,10 @@ ruleTester.run('no-unused-modules', rule, {
code: 'export function j() { return 4 }',
filename: testFilePath('./no-unused-modules/file-j.js'),
errors: [error(`exported declaration 'j' not used within other modules`)]}),
test({ options: unusedExportsOptions,
code: 'export class q { q0() {} }',
filename: testFilePath('./no-unused-modules/file-q.js'),
errors: [error(`exported declaration 'q' not used within other modules`)]}),
test({ options: unusedExportsOptions,
code: 'const k0 = 5; export { k0 as k }',
filename: testFilePath('./no-unused-modules/file-k.js'),
Expand Down

0 comments on commit 8b55e8f

Please sign in to comment.