Skip to content

Commit ba7a459

Browse files
committed
feature: @putout/plugin-remove-useless-variables: assignment: add
1 parent f8dd769 commit ba7a459

File tree

10 files changed

+177
-1
lines changed

10 files changed

+177
-1
lines changed

packages/plugin-remove-useless-variables/.putout.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"match": {
3-
"README.md": {
3+
"*.md": {
4+
"putout/declare": "off",
45
"promises/remove-useless-async": "off"
56
}
67
}

packages/plugin-remove-useless-variables/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ npm i @putout/plugin-remove-useless-variables -D
1313

1414
## Rules
1515

16+
-[assignment](#assignmentn);
1617
-[declaration](#declaration);
1718
-[destruct](#destruct);
1819
-[duplicate](#duplicate);
@@ -24,6 +25,7 @@ npm i @putout/plugin-remove-useless-variables -D
2425
```json
2526
{
2627
"rules": {
28+
"remove-useless-variables/assignment": "on",
2729
"remove-useless-variables/rename": "on",
2830
"remove-useless-variables/remove": "on",
2931
"remove-useless-variables/destruct": "on",
@@ -35,6 +37,22 @@ npm i @putout/plugin-remove-useless-variables -D
3537
}
3638
```
3739

40+
## assignment
41+
42+
Checkout in 🐊[**Putout Editor**](https://putout.cloudcmd.io/#/gist/a2f7fe5e2c294443576f95dce6fde67e/342492c8b9de8e36e469c8f543fc0f2b7d2df73c).
43+
44+
### ❌ Example of incorrect code
45+
46+
```js
47+
while (!(files = readDirectory(parentDir)).length) {}
48+
```
49+
50+
### ✅ Example of correct code
51+
52+
```js
53+
while (!readDirectory(parentDir).length) {}
54+
```
55+
3856
## rename
3957

4058
### ❌ Example of incorrect code
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function notReferenced() {
2+
let files = [];
3+
4+
while (!readDirectory(parentDir)) {}}
5+
6+
function referenced() {
7+
let files = [];
8+
9+
while (!(files = readDirectory(parentDir)).length) {}
10+
11+
console.log(files);
12+
}
13+
14+
function notIdentifier() {
15+
let files = [];
16+
17+
while (!(files.x = readDirectory(parentDir)).length) {}}
18+
19+
function notDeclared() {
20+
while (!(files = readDirectory(parentDir)).length) {}
21+
22+
console.log(files);
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function notReferenced() {
2+
let files = [];
3+
4+
while (!(files = readDirectory(parentDir)).length) {
5+
}
6+
}
7+
8+
function referenced() {
9+
let files = [];
10+
11+
while (!(files = readDirectory(parentDir)).length) {
12+
}
13+
14+
console.log(files);
15+
}
16+
17+
function notIdentifier() {
18+
let files = [];
19+
20+
while (!(files.x = readDirectory(parentDir)).length) {
21+
}
22+
}
23+
24+
function notDeclared() {
25+
while (!(files = readDirectory(parentDir)).length) {
26+
}
27+
console.log(files);
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const {types, operator} = require('putout');
4+
5+
const {getBinding} = operator;
6+
const {isIdentifier} = types;
7+
8+
module.exports.report = () => `Avoid useless assign`;
9+
10+
module.exports.match = () => ({
11+
'(__a = __b).__c': ({__a}, path) => {
12+
if (!isIdentifier(__a))
13+
return false;
14+
15+
const binding = getBinding(path, __a.name);
16+
17+
if (!binding)
18+
return false;
19+
20+
const {references} = binding;
21+
22+
return !references;
23+
},
24+
});
25+
26+
module.exports.replace = () => ({
27+
'(__a = __b).__c': '__b',
28+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const {createTest} = require('@putout/test');
4+
const plugin = require('.');
5+
6+
const test = createTest(__dirname, {
7+
plugins: [
8+
['assignment', plugin],
9+
],
10+
});
11+
12+
test('remove-useless-variables: assignment: report', (t) => {
13+
t.report('assignment', `Avoid useless assign`);
14+
t.end();
15+
});
16+
17+
test('remove-useless-variables: assignment: transform', (t) => {
18+
t.transform('assignment');
19+
t.end();
20+
});

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ const remove = require('./remove');
55
const destruct = require('./destruct');
66
const declaration = require('./declaration');
77
const duplicate = require('./duplicate');
8+
const assignment = require('./assignment');
89

910
module.exports.rules = {
1011
rename,
1112
remove,
1213
destruct,
1314
declaration,
1415
duplicate,
16+
assignment,
1517
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function notReferenced() {
2+
let files = [];
3+
4+
while (!readDirectory(parentDir)) {}}
5+
6+
function referenced() {
7+
let files = [];
8+
9+
while (!(files = readDirectory(parentDir)).length) {}
10+
11+
console.log(files);
12+
}
13+
14+
function notIdentifier() {
15+
let files = [];
16+
17+
while (!(files.x = readDirectory(parentDir)).length) {}}
18+
19+
function notDeclared() {
20+
while (!(files = readDirectory(parentDir)).length) {}
21+
22+
console.log(files);
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function notReferenced() {
2+
let files = [];
3+
4+
while (!(files = readDirectory(parentDir)).length) {
5+
}
6+
}
7+
8+
function referenced() {
9+
let files = [];
10+
11+
while (!(files = readDirectory(parentDir)).length) {
12+
}
13+
14+
console.log(files);
15+
}
16+
17+
function notIdentifier() {
18+
let files = [];
19+
20+
while (!(files.x = readDirectory(parentDir)).length) {
21+
}
22+
}
23+
24+
function notDeclared() {
25+
while (!(files = readDirectory(parentDir)).length) {
26+
}
27+
console.log(files);
28+
}

packages/plugin-remove-useless-variables/test/remove-useless-variables.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ test('plugin-remove-useless-variables: transform: duplicate', (t) => {
7878
t.transform('duplicate');
7979
t.end();
8080
});
81+
82+
test('plugin-remove-useless-variables: transform: assignment', (t) => {
83+
t.transform('assignment');
84+
t.end();
85+
});

0 commit comments

Comments
 (0)