Skip to content

Commit 04bc1b9

Browse files
committed
[Fix] no-unused-modules: Support destructuring
1 parent f2db74a commit 04bc1b9

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/rules/no-unused-modules.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @author René Fermann
55
*/
66

7-
import Exports from '../ExportMap';
7+
import Exports, { recursivePatternCapture } from '../ExportMap';
88
import { getFileExtensions } from 'eslint-module-utils/ignore';
99
import resolve from 'eslint-module-utils/resolve';
1010
import docsUrl from '../docsUrl';
@@ -63,6 +63,8 @@ const IMPORT_DEFAULT_SPECIFIER = 'ImportDefaultSpecifier';
6363
const VARIABLE_DECLARATION = 'VariableDeclaration';
6464
const FUNCTION_DECLARATION = 'FunctionDeclaration';
6565
const CLASS_DECLARATION = 'ClassDeclaration';
66+
const IDENTIFIER = 'Identifier';
67+
const OBJECT_PATTERN = 'ObjectPattern';
6668
const TS_INTERFACE_DECLARATION = 'TSInterfaceDeclaration';
6769
const TS_TYPE_ALIAS_DECLARATION = 'TSTypeAliasDeclaration';
6870
const TS_ENUM_DECLARATION = 'TSEnumDeclaration';
@@ -80,7 +82,15 @@ function forEachDeclarationIdentifier(declaration, cb) {
8082
cb(declaration.id.name);
8183
} else if (declaration.type === VARIABLE_DECLARATION) {
8284
declaration.declarations.forEach(({ id }) => {
83-
cb(id.name);
85+
if(id.type === OBJECT_PATTERN) {
86+
recursivePatternCapture(id, (pattern) => {
87+
if (pattern.type === IDENTIFIER) {
88+
cb(pattern.name);
89+
}
90+
});
91+
} else {
92+
cb(id.name);
93+
}
8494
});
8595
}
8696
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import {a, b} from "./destructuring-b";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const obj = {a: 1, dummy: {b: 2}};
2+
export const {a, dummy: {b}} = obj;

tests/src/rules/no-unused-modules.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,3 +965,23 @@ describe('ignore flow types', () => {
965965
invalid: [],
966966
});
967967
});
968+
969+
describe('support (nested) destructuring assignment', () => {
970+
ruleTester.run('no-unused-modules', rule, {
971+
valid: [
972+
test({
973+
options: unusedExportsOptions,
974+
code: 'import {a, b} from "./destructuring-b";',
975+
parser: require.resolve('babel-eslint'),
976+
filename: testFilePath('./no-unused-modules/destructuring-a.js'),
977+
}),
978+
test({
979+
options: unusedExportsOptions,
980+
code: 'const obj = {a: 1, dummy: {b: 2}}; export const {a, dummy: {b}} = obj;',
981+
parser: require.resolve('babel-eslint'),
982+
filename: testFilePath('./no-unused-modules/destructuring-b.js'),
983+
}),
984+
],
985+
invalid: [],
986+
});
987+
});

0 commit comments

Comments
 (0)