Skip to content

Commit 8e213c7

Browse files
committed
feature: @putout/plugin-for-of: remove-useless-variables: check for VariableDeclarator before getting init
1 parent 9124c54 commit 8e213c7

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
for (const file of list) {}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
for (a of b) {
2-
const {b, c, x} = a;
1+
console.log(filename);
2+
const result = {};
3+
4+
for (const file of list) {
5+
console.log(file);
36
}

packages/plugin-for-of/lib/remove-useless-variables/index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ module.exports.traverse = ({push, options}) => ({
3636
const [referencePath] = referencePaths;
3737
const {parentPath} = referencePath;
3838

39-
const isSameName = parentPath.get('init').isIdentifier({
40-
name,
41-
});
42-
43-
if (!isSameName)
39+
if (!parentPath.isVariableDeclarator())
4440
return;
4541

4642
const idPath = parentPath.get('id');

packages/plugin-for-of/lib/remove-useless-variables/index.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
'use strict';
22

33
const {createTest} = require('@putout/test');
4-
const plugin = require('.');
4+
5+
const removeUnusedVariables = require('@putout/plugin-remove-unused-variables');
6+
const removeConsole = require('@putout/plugin-remove-console');
7+
58
const removeUselessArrayFrom = require('../remove-useless-array-from');
9+
const plugin = require('.');
610

711
const test = createTest(__dirname, {
812
plugins: [
@@ -55,6 +59,14 @@ test('putout: plugin-for-of: remove-useless-variables no transform: nested', (t)
5559
t.end();
5660
});
5761

62+
test('putout: plugin-for-of: remove-useless-variables no report: no-declaration', (t) => {
63+
t.transform('no-declaration', {
64+
'remove-console': removeConsole,
65+
'remove-unused-variables': removeUnusedVariables,
66+
});
67+
t.end();
68+
});
69+
5870
test('putout: plugin-for-of: remove-useless-variables transform with: array-from', (t) => {
5971
t.transform('array-from', {
6072
'remove-useless-array-from': removeUselessArrayFrom,

packages/plugin-for-of/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@putout/eslint-flat": "^2.0.0",
3636
"@putout/plugin-conditions": "*",
3737
"@putout/plugin-convert-const-to-let": "*",
38+
"@putout/plugin-remove-console": "*",
3839
"@putout/plugin-remove-empty": "*",
3940
"@putout/plugin-remove-nested-blocks": "*",
4041
"@putout/plugin-remove-unreferenced-variables": "*",

0 commit comments

Comments
 (0)