Skip to content

Commit

Permalink
[New] 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 authored and ljharb committed Feb 28, 2021
1 parent d31d615 commit ab0181d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
- [`no-internal-modules`]: Add `forbid` option ([#1846], thanks [@guillaumewuip])
- add [`no-relative-packages`] ([#1860], [#966], thanks [@tapayne88] [@panrafal])
- add [`no-import-module-exports`] rule: report import declarations with CommonJS exports ([#804], thanks [@kentcdodds] and [@ttmarek])
- [`no-unused-modules`]: Support destructuring assignment for `export`. ([#1997], thanks [@s-h-a-d-o-w])

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

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

[#1997]: https://github.com/benmosher/eslint-plugin-import/pull/1997
[#1993]: https://github.com/benmosher/eslint-plugin-import/pull/1993
[#1983]: https://github.com/benmosher/eslint-plugin-import/pull/1983
[#1974]: https://github.com/benmosher/eslint-plugin-import/pull/1974
Expand Down Expand Up @@ -1344,3 +1346,4 @@ for info on changes for earlier releases.
[@christianvuerings]: https://github.com/christianvuerings
[@devongovett]: https://github.com/devongovett
[@dwardu]: https://github.com/dwardu
[@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
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
@@ -0,0 +1 @@
import {a, b} from "./destructuring-b";
2 changes: 2 additions & 0 deletions tests/files/no-unused-modules/destructuring-b.js
@@ -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
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 ab0181d

Please sign in to comment.