Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] no-unused-modules: handle ClassDeclaration #1371

Merged
merged 1 commit into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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