Skip to content

Commit 07c7093

Browse files
committed
feature: @putout/plugin-remove-unused-private-fields: convert to Traverser
1 parent 9e9974a commit 07c7093

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

packages/plugin-remove-unused-private-fields/lib/remove-unused-private-fields.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
'use strict';
22

3-
const {traverse, operator} = require('putout');
4-
3+
const {operator} = require('putout');
54
const {remove} = operator;
65

76
module.exports.report = ({name}) => `Private field "#${name}" declared by not used`;
87

9-
module.exports.find = (ast) => {
8+
module.exports.fix = ({path}) => {
9+
remove(path);
10+
};
11+
12+
module.exports.traverse = ({push}) => {
1013
const vars = [];
1114
const addVar = addVariable(vars);
1215
const rmVar = removeVariable(vars);
1316
const list = [];
1417

15-
traverseClass(ast, {
18+
return {
1619
ClassPrivateProperty(path) {
1720
const keyPath = path.get('key');
1821
addVar(path, keyPath.node.id.name);
@@ -22,7 +25,6 @@ module.exports.find = (ast) => {
2225
const keyPath = path.get('key');
2326
addVar(path, keyPath.node.id.name);
2427
},
25-
2628
ThisExpression(path) {
2729
const {parentPath} = path;
2830
const propertyPath = parentPath.get('property');
@@ -47,17 +49,24 @@ module.exports.find = (ast) => {
4749
list.push([path, keyPath.node.id.name]);
4850
}
4951
},
50-
});
51-
52-
for (const [path, name] of list) {
53-
rmVar(path, name);
54-
}
55-
56-
return Object.values(vars);
57-
};
58-
59-
module.exports.fix = ({path}) => {
60-
remove(path);
52+
Program: {
53+
exit() {
54+
for (const [path, name] of list) {
55+
rmVar(path, name);
56+
}
57+
58+
for (const {name, path} of Object.values(vars)) {
59+
if (!path.node)
60+
continue;
61+
62+
push({
63+
name,
64+
path,
65+
});
66+
}
67+
},
68+
},
69+
};
6170
};
6271

6372
function findClassName(path) {
@@ -84,11 +93,3 @@ const removeVariable = (vars) => (path, name) => {
8493

8594
delete vars[id];
8695
};
87-
88-
function traverseClass(ast, visitor) {
89-
traverse(ast, {
90-
'ClassDeclaration|ClassExpression'(path) {
91-
path.traverse(visitor);
92-
},
93-
});
94-
}

0 commit comments

Comments
 (0)