Skip to content

Commit c0efe51

Browse files
committed
feature: remove-unused-variables: ExportNamedDeclaraiton: ObjectPattern
1 parent 5a3771c commit c0efe51

File tree

4 files changed

+92
-46
lines changed

4 files changed

+92
-46
lines changed

packages/plugin-remove-unused-variables/lib/get-vars/get-vars.js

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ const {
1313
traverseTemplateLiteral,
1414
} = require('./traverse');
1515

16-
const {isKeyword} = operator;
16+
const {createExportNamedDeclaration} = require('./visitors/export-named-declaration.js');
1717
const {assign} = Object;
18+
const {isKeyword} = operator;
1819

1920
const {
2021
isAssignmentPattern,
@@ -25,8 +26,6 @@ const {
2526
isObjectExpression,
2627
isFunctionDeclaration,
2728
isArrayExpression,
28-
isArrayPattern,
29-
isVariableDeclaration,
3029
isRestElement,
3130
} = types;
3231

@@ -513,49 +512,9 @@ module.exports = ({use, declare, addParams}) => {
513512
if (isObjectExpression(declaration))
514513
return traverseObj(declarationPath.get('properties'));
515514
},
516-
517-
ExportNamedDeclaration(path) {
518-
const declarationPath = path.get('declaration');
519-
const {declaration, specifiers} = path.node;
520-
521-
if (declarationPath.isFunctionDeclaration())
522-
return use(path, declaration.id.name);
523-
524-
if (declarationPath.isClassDeclaration())
525-
return use(path, declaration.id.name);
526-
527-
// typescript
528-
if (declarationPath.isTSInterfaceDeclaration())
529-
return use(path, declaration.id.name);
530-
531-
if (declarationPath.isTSTypeAliasDeclaration())
532-
return use(path, declaration.id.name);
533-
534-
if (isVariableDeclaration(declaration)) {
535-
const {declarations} = declaration;
536-
537-
for (const {id} of declarations) {
538-
/* istanbul ignore else */
539-
if (isIdentifier(id))
540-
use(path, id.name);
541-
542-
if (isArrayPattern(id))
543-
for (const element of id.elements) {
544-
if (isIdentifier(element))
545-
use(path, element.name);
546-
}
547-
}
548-
549-
return;
550-
}
551-
552-
for (const {local} of specifiers) {
553-
/* istanbul ignore else */
554-
if (isIdentifier(local))
555-
use(path, local.name);
556-
}
557-
},
558-
515+
ExportNamedDeclaration: createExportNamedDeclaration({
516+
use,
517+
}),
559518
Function(path) {
560519
const {node, parentPath} = path;
561520
const {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'use strict';
2+
3+
const {types} = require('putout');
4+
const {
5+
isArrayPattern,
6+
isRestElement,
7+
isObjectPattern,
8+
isIdentifier,
9+
isVariableDeclaration,
10+
} = types;
11+
12+
module.exports.createExportNamedDeclaration = ({use}) => (path) => {
13+
const declarationPath = path.get('declaration');
14+
const {declaration, specifiers} = path.node;
15+
16+
if (declarationPath.isFunctionDeclaration())
17+
return use(path, declaration.id.name);
18+
19+
if (declarationPath.isClassDeclaration())
20+
return use(path, declaration.id.name);
21+
22+
// typescript
23+
if (declarationPath.isTSInterfaceDeclaration())
24+
return use(path, declaration.id.name);
25+
26+
if (declarationPath.isTSTypeAliasDeclaration())
27+
return use(path, declaration.id.name);
28+
29+
if (isVariableDeclaration(declaration)) {
30+
const {declarations} = declaration;
31+
32+
for (const {id} of declarations) {
33+
/* istanbul ignore else */
34+
if (isIdentifier(id))
35+
use(path, id.name);
36+
37+
if (isObjectPattern(id))
38+
for (const property of id.properties) {
39+
if (isRestElement(property) && isIdentifier(property.argument)) {
40+
use(path, property.argument.name);
41+
continue;
42+
}
43+
44+
if (isIdentifier(property.value))
45+
use(path, property.value.name);
46+
}
47+
48+
if (isArrayPattern(id))
49+
for (const element of id.elements)
50+
if (isIdentifier(element))
51+
use(path, element.name);
52+
}
53+
54+
return;
55+
}
56+
57+
for (const {local} of specifiers) {
58+
/* istanbul ignore else */
59+
if (isIdentifier(local))
60+
use(path, local.name);
61+
}
62+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const {rules, ...spread} = putout;

packages/plugin-remove-unused-variables/test/get-vars.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const fixture = readFixtures([
3636
'destr-fn-vars',
3737
'decorator',
3838
'do-while',
39+
'export-const',
3940
'export-default-function',
4041
'export-default-anonymous-function',
4142
'export-default-class',
@@ -814,6 +815,29 @@ test('remove-unused-variables: get-vars: import', (t) => {
814815
t.end();
815816
});
816817

818+
test('remove-unused-variables: get-vars: export const', (t) => {
819+
const ast = parse(fixture.exportConst);
820+
const result = getVars(ast);
821+
822+
const expected = [{
823+
putout: {
824+
declared: false,
825+
used: true,
826+
},
827+
rules: {
828+
declared: true,
829+
used: true,
830+
},
831+
spread: {
832+
declared: true,
833+
used: true,
834+
},
835+
}];
836+
837+
t.deepEqual(result, expected);
838+
t.end();
839+
});
840+
817841
test('remove-unused-variables: get-vars: export default function', (t) => {
818842
const ast = parse(fixture.exportDefaultFunction);
819843
const result = getVars(ast);

0 commit comments

Comments
 (0)