Skip to content

Commit 7e74584

Browse files
Merge pull request #161 from CodeshiftCommunity/putout
Adds remove-unused-vars via putout
2 parents 96387e3 + 4f7b5b5 commit 7e74584

File tree

14 files changed

+5316
-34828
lines changed

14 files changed

+5316
-34828
lines changed

.changeset/tidy-bobcats-lick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codeshift/mod-javascript': patch
3+
---
4+
5+
Adds remove-unused-vars from putout as javascript preset

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
plugin_packages/
44
.vscode/
55
.parcel-cache/
6+
package-lock.json
67

78
# Generated website pages
89
website/docs/registry-generated/*

community/javascript/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
"devDependencies": {
1818
"@codeshift/cli": "^0.14.1",
1919
"@codeshift/test-utils": "^0.3.1",
20+
"@putout/plugin-remove-unused-variables": "^5.1.0",
2021
"@types/jest": "^29.0.0",
2122
"@types/node": "^16.11.0",
2223
"jest": "^29.0.0",
2324
"prettier": "^2.0.0",
25+
"putout": "^29.5.1",
2426
"ts-jest": "^29.0.0",
2527
"typescript": "^4.5.5"
2628
},

community/javascript/src/codeshift.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ module.exports = {
88
'remove-debugger': require.resolve('./remove-debugger/transform'),
99
'sort-object-props': require.resolve('./sort-object-props/transform'),
1010
'var-to-let': require.resolve('./var-to-let/transform'),
11+
'remove-unused-vars': require.resolve('./remove-unused-vars/transform'),
1112
},
1213
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# javascript#remove-unused-vars
2+
3+
Codemods for javascript#remove-unused-vars
4+
5+
Detects and removes unused variables in JavaScript code.
6+
7+
_Credit:_ [https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-unused-variables](https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-unused-variables#readme)
8+
9+
```js
10+
/* INPUT */
11+
const x = 1;
12+
const y = 2;
13+
console.log(y);
14+
15+
/* OUTPUT */
16+
const y = 2;
17+
console.log(y);
18+
```
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import { applyTransform } from '@codeshift/test-utils';
2+
import * as transformer from './transform';
3+
4+
describe('javascript#remove-unused-vars transform', () => {
5+
it('should remove unused variables', async () => {
6+
const result = await applyTransform(
7+
transformer,
8+
`
9+
const x = 1;
10+
const y = 2;
11+
console.log(y);
12+
`,
13+
);
14+
15+
expect(result).toMatchInlineSnapshot(`
16+
"const y = 2;
17+
console.log(y);"
18+
`);
19+
});
20+
21+
it('should not remove used variables', async () => {
22+
const result = await applyTransform(
23+
transformer,
24+
`
25+
const x = 1;
26+
const y = 2;
27+
console.log(x + y);
28+
`,
29+
);
30+
expect(result).toMatchInlineSnapshot(`
31+
"const x = 1;
32+
const y = 2;
33+
console.log(x + y);"
34+
`);
35+
});
36+
37+
it('should remove unused variable in nested scope', async () => {
38+
const result = await applyTransform(
39+
transformer,
40+
`
41+
{
42+
if (true) {
43+
const a = 1;
44+
}
45+
}`,
46+
);
47+
48+
expect(result).toMatchInlineSnapshot(`
49+
"{
50+
if (true) {}
51+
}"
52+
`);
53+
});
54+
55+
it('should remove unused function in nested scope', async () => {
56+
const result = await applyTransform(
57+
transformer,
58+
`
59+
function foo() {
60+
if (true) {
61+
const a = 1;
62+
}
63+
}`,
64+
);
65+
66+
expect(result).toMatchInlineSnapshot(`""`);
67+
});
68+
69+
it('should remove unused variable in for loop', async () => {
70+
const result = await applyTransform(
71+
transformer,
72+
`
73+
for (let i = 0; i < 10; i++) {
74+
const a = i;
75+
}
76+
`,
77+
);
78+
79+
expect(result).toMatchInlineSnapshot(`"for (let i = 0; i < 10; i++) {}"`);
80+
});
81+
82+
it('should remove unused variable in destructuring assignment', async () => {
83+
const result = await applyTransform(
84+
transformer,
85+
`
86+
const { a, b } = { a: 1, b: 2 };
87+
console.log(a);
88+
`,
89+
);
90+
91+
expect(result).toMatchInlineSnapshot(`
92+
"const {
93+
a
94+
} = { a: 1, b: 2 };
95+
console.log(a);"
96+
`);
97+
});
98+
99+
it('should remove unused function argument', async () => {
100+
const result = await applyTransform(
101+
transformer,
102+
`
103+
function foo(a, b) {
104+
console.log(a);
105+
}
106+
foo(1);
107+
`,
108+
);
109+
110+
expect(result).toMatchInlineSnapshot(`
111+
"function foo(a) {
112+
console.log(a);
113+
}
114+
foo(1);"
115+
`);
116+
});
117+
118+
it('should not remove used variable in conditional', async () => {
119+
const result = await applyTransform(
120+
transformer,
121+
`
122+
let a;
123+
if (true) {
124+
a = 1;
125+
}
126+
console.log(a);
127+
`,
128+
);
129+
130+
expect(result).toMatchInlineSnapshot(`
131+
"let a;
132+
if (true) {
133+
a = 1;
134+
}
135+
console.log(a);"
136+
`);
137+
});
138+
139+
it('should remove unused function declaration', async () => {
140+
const result = await applyTransform(
141+
transformer,
142+
`
143+
function foo() {
144+
console.log('Hello, world!');
145+
}
146+
`,
147+
);
148+
149+
expect(result).toMatchInlineSnapshot(`""`);
150+
});
151+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { FileInfo } from 'jscodeshift';
2+
// @ts-expect-error
3+
import putout from 'putout';
4+
// @ts-expect-error
5+
import removeUnusedVariables from '@putout/plugin-remove-unused-variables';
6+
7+
export default function transformer(file: FileInfo) {
8+
const output = putout(file.source, {
9+
plugins: [['remove-unused-variables', removeUnusedVariables]],
10+
});
11+
12+
return output.code;
13+
}

0 commit comments

Comments
 (0)