Skip to content

Commit ab0181d

Browse files
s-h-a-d-o-wljharb
authored andcommitted
[New] no-unused-modules: Support destructuring
1 parent d31d615 commit ab0181d

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1111
- [`no-internal-modules`]: Add `forbid` option ([#1846], thanks [@guillaumewuip])
1212
- add [`no-relative-packages`] ([#1860], [#966], thanks [@tapayne88] [@panrafal])
1313
- add [`no-import-module-exports`] rule: report import declarations with CommonJS exports ([#804], thanks [@kentcdodds] and [@ttmarek])
14+
- [`no-unused-modules`]: Support destructuring assignment for `export`. ([#1997], thanks [@s-h-a-d-o-w])
1415

1516
### Fixed
1617
- [`export`]/TypeScript: properly detect export specifiers as children of a TS module block ([#1889], thanks [@andreubotella])
@@ -760,6 +761,7 @@ for info on changes for earlier releases.
760761

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

764+
[#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997
763765
[#1993]: https://github.com/benmosher/eslint-plugin-import/pull/1993
764766
[#1983]: https://github.com/benmosher/eslint-plugin-import/pull/1983
765767
[#1974]: https://github.com/benmosher/eslint-plugin-import/pull/1974
@@ -1344,3 +1346,4 @@ for info on changes for earlier releases.
13441346
[@christianvuerings]: https://github.com/christianvuerings
13451347
[@devongovett]: https://github.com/devongovett
13461348
[@dwardu]: https://github.com/dwardu
1349+
[@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
@@ -970,3 +970,23 @@ describe('ignore flow types', () => {
970970
invalid: [],
971971
});
972972
});
973+
974+
describe('support (nested) destructuring assignment', () => {
975+
ruleTester.run('no-unused-modules', rule, {
976+
valid: [
977+
test({
978+
options: unusedExportsOptions,
979+
code: 'import {a, b} from "./destructuring-b";',
980+
parser: require.resolve('babel-eslint'),
981+
filename: testFilePath('./no-unused-modules/destructuring-a.js'),
982+
}),
983+
test({
984+
options: unusedExportsOptions,
985+
code: 'const obj = {a: 1, dummy: {b: 2}}; export const {a, dummy: {b}} = obj;',
986+
parser: require.resolve('babel-eslint'),
987+
filename: testFilePath('./no-unused-modules/destructuring-b.js'),
988+
}),
989+
],
990+
invalid: [],
991+
});
992+
});

0 commit comments

Comments
 (0)