Skip to content

Commit

Permalink
[Fix] no-unused-modules: Support destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
s-h-a-d-o-w committed Mar 15, 2021
1 parent e871a9a commit c73b4ce
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`extensions`]/[`no-cycle`]/[`no-extraneous-dependencies`]: Correct module real path resolution ([#1696], thanks [@paztis])
- [`no-named-default`]: ignore Flow import type and typeof ([#1983], thanks [@christianvuerings])
- [`no-extraneous-dependencies`]: Exclude flow `typeof` imports ([#1534], thanks [@devongovett])
- [`no-unused-modules`]: Support destructuring assignment for `export`. ([#1997], thanks [@s-h-a-d-o-w])

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

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

[#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997
[#1983]: https://github.com/benmosher/eslint-plugin-import/pull/1983
[#1974]: https://github.com/benmosher/eslint-plugin-import/pull/1974
[#1958]: https://github.com/benmosher/eslint-plugin-import/pull/1958
Expand Down Expand Up @@ -1340,4 +1342,5 @@ for info on changes for earlier releases.
[@panrafal]: https://github.com/panrafal
[@ttmarek]: https://github.com/ttmarek
[@christianvuerings]: https://github.com/christianvuerings
[@devongovett]: https://github.com/devongovett
[@devongovett]: https://github.com/devongovett
[@s-h-a-d-o-w]: https://github.com/s-h-a-d-o-w
14 changes: 12 additions & 2 deletions src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @author René Fermann
*/

import Exports from '../ExportMap';
import Exports, { recursivePatternCapture } from '../ExportMap';
import { getFileExtensions } from 'eslint-module-utils/ignore';
import resolve from 'eslint-module-utils/resolve';
import docsUrl from '../docsUrl';
Expand Down Expand Up @@ -63,6 +63,8 @@ const IMPORT_DEFAULT_SPECIFIER = 'ImportDefaultSpecifier';
const VARIABLE_DECLARATION = 'VariableDeclaration';
const FUNCTION_DECLARATION = 'FunctionDeclaration';
const CLASS_DECLARATION = 'ClassDeclaration';
const IDENTIFIER = 'Identifier';
const OBJECT_PATTERN = 'ObjectPattern';
const TS_INTERFACE_DECLARATION = 'TSInterfaceDeclaration';
const TS_TYPE_ALIAS_DECLARATION = 'TSTypeAliasDeclaration';
const TS_ENUM_DECLARATION = 'TSEnumDeclaration';
Expand All @@ -80,7 +82,15 @@ function forEachDeclarationIdentifier(declaration, cb) {
cb(declaration.id.name);
} else if (declaration.type === VARIABLE_DECLARATION) {
declaration.declarations.forEach(({ id }) => {
cb(id.name);
if(id.type === OBJECT_PATTERN) {
recursivePatternCapture(id, (pattern) => {
if (pattern.type === IDENTIFIER) {
cb(pattern.name);
}
});
} else {
cb(id.name);
}
});
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/files/no-unused-modules/destructuring-a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import {a, b} from "./destructuring-b";
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/destructuring-b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const obj = {a: 1, dummy: {b: 2}};
export const {a, dummy: {b}} = obj;
20 changes: 20 additions & 0 deletions tests/src/rules/no-unused-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,3 +970,23 @@ describe('ignore flow types', () => {
invalid: [],
});
});

describe('support (nested) destructuring assignment', () => {
ruleTester.run('no-unused-modules', rule, {
valid: [
test({
options: unusedExportsOptions,
code: 'import {a, b} from "./destructuring-b";',
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/destructuring-a.js'),
}),
test({
options: unusedExportsOptions,
code: 'const obj = {a: 1, dummy: {b: 2}}; export const {a, dummy: {b}} = obj;',
parser: require.resolve('babel-eslint'),
filename: testFilePath('./no-unused-modules/destructuring-b.js'),
}),
],
invalid: [],
});
});

0 comments on commit c73b4ce

Please sign in to comment.