Skip to content

Commit d4d863d

Browse files
committed
feature: @putout/plugin-remove-unused-private-fields: use before declare
1 parent 79eb6ff commit d4d863d

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ const {traverse, operator} = require('putout');
44

55
const {remove} = operator;
66

7-
module.exports.report = ({name}) => `private field "#${name}" declared by not used`;
7+
module.exports.report = ({name}) => `Private field "#${name}" declared by not used`;
88

99
module.exports.find = (ast) => {
1010
const vars = [];
1111
const addVar = addVariable(vars);
1212
const rmVar = removeVariable(vars);
13+
const list = [];
1314

1415
traverseClass(ast, {
1516
ClassPrivateProperty(path) {
@@ -27,7 +28,7 @@ module.exports.find = (ast) => {
2728
const propertyPath = parentPath.get('property');
2829

2930
if (propertyPath.isPrivateName()) {
30-
rmVar(path, propertyPath.node.id.name);
31+
list.push([path, propertyPath.node.id.name]);
3132
return;
3233
}
3334

@@ -43,11 +44,15 @@ module.exports.find = (ast) => {
4344
const keyPath = propertyPath.get('key');
4445

4546
if (keyPath.isPrivateName())
46-
rmVar(path, keyPath.node.id.name);
47+
list.push([path, keyPath.node.id.name]);
4748
}
4849
},
4950
});
5051

52+
for (const [path, name] of list) {
53+
rmVar(path, name);
54+
}
55+
5156
return Object.values(vars);
5257
};
5358

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export class DrawCircle {
2+
constructor(
3+
el: HTMLElement,
4+
radius: number,
5+
options: SpeccerOptionsInterface
6+
) {
7+
this.#init(el, radius, options);
8+
}
9+
10+
#init(el: HTMLElement, radius: number, options: SpeccerOptionsInterface) {
11+
if (!el || !radius || !options) {
12+
throw new Error('Missing inputs el or radius or options');
13+
}
14+
}
15+
}
16+

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const test = createTest(__dirname, {
1010
});
1111

1212
test('plugin-remove-unused-private-fields: report: class', (t) => {
13-
t.report('class', 'private field "#world" declared by not used');
13+
t.report('class', 'Private field "#world" declared by not used');
1414
t.end();
1515
});
1616

@@ -38,3 +38,8 @@ test('plugin-remove-unused-private-fields: no transform: destructuring', (t) =>
3838
t.noTransform('destructuring');
3939
t.end();
4040
});
41+
42+
test('plugin-remove-unused-private-fields: no report: private', (t) => {
43+
t.noReport('private');
44+
t.end();
45+
});

0 commit comments

Comments
 (0)