Skip to content

Commit 1f3fb2e

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

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

Diff for: CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2525
- [`extensions`]/[`no-cycle`]/[`no-extraneous-dependencies`]: Correct module real path resolution ([#1696], thanks [@paztis])
2626
- [`no-named-default`]: ignore Flow import type and typeof ([#1983], thanks [@christianvuerings])
2727
- [`no-extraneous-dependencies`]: Exclude flow `typeof` imports ([#1534], thanks [@devongovett])
28+
- [`no-unused-modules`]: Support destructuring assignment for `export`. ([#1997], thanks [@s-h-a-d-o-w])
2829

2930
### Changed
3031
- [Generic Import Callback] Make callback for all imports once in rules ([#1237], thanks [@ljqx])
@@ -759,6 +760,7 @@ for info on changes for earlier releases.
759760

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

763+
[#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997
762764
[#1983]: https://github.com/benmosher/eslint-plugin-import/pull/1983
763765
[#1974]: https://github.com/benmosher/eslint-plugin-import/pull/1974
764766
[#1958]: https://github.com/benmosher/eslint-plugin-import/pull/1958
@@ -1340,4 +1342,5 @@ for info on changes for earlier releases.
13401342
[@panrafal]: https://github.com/panrafal
13411343
[@ttmarek]: https://github.com/ttmarek
13421344
[@christianvuerings]: https://github.com/christianvuerings
1343-
[@devongovett]: https://github.com/devongovett
1345+
[@devongovett]: https://github.com/devongovett
1346+
[@s-h-a-d-o-w]: https://github.com/s-h-a-d-o-w

Diff for: src/rules/no-unused-modules.js

+12-2
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
}

Diff for: tests/files/no-unused-modules/destructuring-a.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import {a, b} from "./destructuring-b";

Diff for: tests/files/no-unused-modules/destructuring-b.js

+2
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;

Diff for: tests/src/rules/no-unused-modules.js

+20
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)