Skip to content

Commit 2b3673e

Browse files
committed
feature: @putout/plugin-variables: remove-unused
1 parent 37a9fda commit 2b3673e

File tree

252 files changed

+386
-492
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

252 files changed

+386
-492
lines changed

README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ So, if you want to convert it to [`ESM`](https://developer.mozilla.org/en-US/doc
296296
`putout index.js --disable-all` will find next errors:
297297

298298
```sh
299-
1:4 error 'unused' is defined but never used remove-unused-variables
300-
7:23 error 'a' is defined but never used remove-unused-variables
299+
1:4 error 'unused' is defined but never used variables/remove-unused
300+
7:23 error 'a' is defined but never used variables/remove-unused
301301
3:0 error Use arrow function convert-to-arrow-function
302302
1:0 error Add missing 'use strict' directive on top of CommonJS nodejs/dd-missing-strict-cmode
303303
8:4 error Reject is useless in async functions, use throw instead promises/convert-reject-to-throw
@@ -310,7 +310,7 @@ It will create config file `.putout.json`:
310310
```json
311311
{
312312
"rules": {
313-
"remove-unused-variables": "off",
313+
"variables/remove-unused": "off",
314314
"convert-to-arrow-function": "off",
315315
"nodejs/add-missing-strict-mode": "off",
316316
"promises/convert-reject-to-throw": "off",
@@ -325,7 +325,7 @@ Then running `putout index.js --enable nodejs/convert-commonjs-to-esm` will upda
325325
```diff
326326
{
327327
"rules": {
328-
"remove-unused-variables": "off",
328+
"variables/remove-unused": "off",
329329
"convert-to-arrow-function": "off",
330330
"nodejs/add-missing-strict-mode": "off",
331331
"promises/convert-reject-to-throw": "off",
@@ -541,12 +541,16 @@ const source = `
541541

542542
#### Plugins
543543

544-
🐊**Putout** supports dynamic loading of plugins from `node_modules`. Let's consider example of using the [remove-unused-variables](https://github.com/coderaiser/putout/tree/master/packages/plugin-remove-unused-variables/README.md#readme) plugin:
544+
🐊**Putout** supports dynamic loading of plugins from `node_modules`. Let's consider example of using the [variables/remove-unused](https://github.com/coderaiser/putout/tree/master/packages/plugin-variables/README.md#remove-unused) plugin:
545545

546546
```js
547547
putout(source, {
548+
rules: {
549+
'variables': 'off',
550+
'variables/remove-unused': 'on',
551+
},
548552
plugins: [
549-
'remove-unused-variables',
553+
'variables',
550554
],
551555
});
552556

@@ -568,16 +572,20 @@ In the following example redundant variables are found without making changes to
568572
```js
569573
putout(source, {
570574
fix: false,
575+
rules: {
576+
'variables': 'off',
577+
'variables/remove-unused': 'on',
578+
},
571579
plugins: [
572-
'remove-unused-variables',
580+
'variables',
573581
],
574582
});
575583

576584
// returns
577585
({
578586
code: '\n' + ` const hello = 'world';\n` + ` const hi = 'there';\n` + ' \n' + ' console.log(hello);\n',
579587
places: [{
580-
rule: 'remove-unused-variables',
588+
rule: 'variables/remove-unused',
581589
message: '"hi" is defined but never used',
582590
position: {
583591
line: 3,
@@ -2159,7 +2167,6 @@ It has a lot of plugins divided by groups:
21592167

21602168
| Package | Version |
21612169
|--------|-------|
2162-
| [`@putout/plugin-remove-unused-variables`](/packages/plugin-remove-unused-variables#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-unused-variables.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-unused-variables) |
21632170
| [`@putout/plugin-remove-unreferenced-variables`](/packages/plugin-remove-unreferenced-variables#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-unreferenced-variables.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-unreferenced-variables) |
21642171
| [`@putout/plugin-remove-duplicate-keys`](/packages/plugin-remove-duplicate-keys#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-duplicate-keys.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-duplicate-keys) |
21652172
| [`@putout/plugin-remove-duplicate-case`](/packages/plugin-remove-duplicate-case#readme) | [![npm](https://img.shields.io/npm/v/@putout/plugin-remove-duplicate-case.svg?maxAge=86400)](https://www.npmjs.com/package/@putout/plugin-remove-duplicate-case) |
@@ -2362,7 +2369,7 @@ You can disable rules using `"off"`, or enable them (in `match` section) using `
23622369
```json
23632370
{
23642371
"rules": {
2365-
"remove-unused-variables": "off"
2372+
"variables/remove-unused": "off"
23662373
}
23672374
}
23682375
```
@@ -2372,7 +2379,7 @@ Or pass options using `rules` section:
23722379
```json
23732380
{
23742381
"rules": {
2375-
"remove-unused-variables": ["on", {
2382+
"variables/remove-unused": ["on", {
23762383
"exclude": "const global = __"
23772384
}]
23782385
}
@@ -2387,7 +2394,7 @@ Pass an array when you have a couple templates to exclude:
23872394
```json
23882395
{
23892396
"rules": {
2390-
"remove-unused-variables": ["on", {
2397+
"variables/remove-unused": ["on", {
23912398
"exclude": [
23922399
"VariableDeclaration"
23932400
]
@@ -2670,7 +2677,7 @@ If you don't want to publish a **plugin** you developed, you can pass it to 🐊
26702677
```js
26712678
putout('const a = 5', {
26722679
plugins: [
2673-
['remove-unused-variables', require('@putout/plugin-remove-unused-variables')],
2680+
['variables', require('@putout/plugin-variables')],
26742681
],
26752682
});
26762683
```
@@ -2814,7 +2821,7 @@ Just create `.babelrc.json` file with configuration you need.
28142821
"plugins": [
28152822
["putout", {
28162823
"rules": {
2817-
"remove-unused-variables": "off"
2824+
"variables/remove-unused": "off"
28182825
}
28192826
}]
28202827
]

packages/engine-loader/lib/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {validateRulesRelations} = require('./index');
77

88
test('engine-loader: load-plugins', (t) => {
99
const result = loadPlugins({
10-
pluginNames: ['remove-unused-variables'],
10+
pluginNames: ['remove-debugger'],
1111
});
1212

1313
t.equal(result.length, 1);

packages/engine-loader/test/load-plugins.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const fixture = readFixtures(['shebang', 'shebang-fix']);
1919

2020
test('putout: loader: user plugin', (t) => {
2121
const {_findPath} = Module;
22-
const rmVars = 'remove-unused-variables';
22+
const rmVars = 'remove-debugger';
2323

2424
const rmUnusedVars = require(`@putout/plugin-${rmVars}`);
2525

@@ -39,7 +39,7 @@ test('putout: loader: user plugin', (t) => {
3939
return _findPath(name, paths);
4040
});
4141

42-
const {code} = putout(`const t = 'hello'`, {
42+
const {code} = putout(`debugger`, {
4343
loadPlugins,
4444
plugins: [rmVars],
4545
});
@@ -63,15 +63,15 @@ test('putout: loader: can not find', (t) => {
6363
});
6464

6565
test('putout: loader: function', (t) => {
66-
const rmVars = 'remove-unused-variables';
66+
const rmVars = 'remove-debugger';
6767
const rmVarsPlugin = require(`@putout/plugin-${rmVars}`);
6868

6969
mockRequire(`@putout/plugin-${rmVars}`, null);
7070

7171
reRequire('..');
7272
const putout = reRequire('putout');
7373

74-
const {code} = putout(`const t = 'hello'`, {
74+
const {code} = putout(`debugger;`, {
7575
plugins: [{
7676
[rmVars]: rmVarsPlugin,
7777
}],
@@ -84,15 +84,15 @@ test('putout: loader: function', (t) => {
8484
});
8585

8686
test('putout: loader: function: rules', (t) => {
87-
const rmVars = 'remove-unused-variables';
87+
const rmVars = 'remove-debugger';
8888
const rmVarsPlugin = require(`@putout/plugin-${rmVars}`);
8989

9090
mockRequire(`@putout/plugin-${rmVars}`, null);
9191

9292
reRequire('..');
9393
const putout = reRequire('putout');
9494

95-
const {code} = putout(`const t = 'hello'`, {
95+
const {code} = putout(`debugger`, {
9696
plugins: [{
9797
'brand-new-rule': {
9898
rules: {
@@ -109,15 +109,15 @@ test('putout: loader: function: rules', (t) => {
109109
});
110110

111111
test('putout: loader: disabled rule', (t) => {
112-
const rmVars = 'remove-unused-variables';
112+
const rmVars = 'remove-debugger';
113113
const rmVarsPlugin = require(`@putout/plugin-${rmVars}`);
114114

115115
mockRequire(`@putout/plugin-${rmVars}`, null);
116116

117117
reRequire('..');
118118
const putout = reRequire('putout');
119119

120-
const {code} = putout(`const t = 'hello'`, {
120+
const {code} = putout(`debugger`, {
121121
rules: {
122122
[rmVars]: false,
123123
},
@@ -128,7 +128,7 @@ test('putout: loader: disabled rule', (t) => {
128128

129129
stopAll();
130130

131-
t.equal(code, `const t = 'hello';\n`);
131+
t.equal(code, `debugger;\n`);
132132
t.end();
133133
});
134134

@@ -155,37 +155,37 @@ test('putout: loader: disabled rule from multi rule plugin', (t) => {
155155
});
156156

157157
test('putout: loader: plugins: array', (t) => {
158-
const rmVars = 'remove-unused-variables';
158+
const rmVars = 'remove-debugger';
159159
const rmVarsPlugin = require(`@putout/plugin-${rmVars}`);
160160

161161
mockRequire(`@putout/plugin-${rmVars}`, null);
162162

163163
reRequire('..');
164164
const putout = reRequire('putout');
165165

166-
const {code} = putout(`const t = 'hello'`, {
166+
const {code} = putout(`debugger;`, {
167167
rules: {
168168
[rmVars]: false,
169169
},
170170
plugins: [
171-
['remove-unused-variables', rmVarsPlugin],
171+
['remove-debugger', rmVarsPlugin],
172172
],
173173
});
174174

175175
stopAll();
176176

177-
t.equal(code, `const t = 'hello';\n`);
177+
t.equal(code, `debugger;\n`);
178178
t.end();
179179
});
180180

181181
test('putout: loader: nested rules', (t) => {
182182
const {code} = putout(fixture.shebang, {
183183
rules: {
184184
'putout/convert-babel-types': 'off',
185-
'remove-unused-variables': 'off',
185+
'variables': 'off',
186186
},
187187
plugins: [
188-
'remove-unused-variables',
188+
'variables',
189189
'putout',
190190
],
191191
});

packages/engine-parser/lib/parsers/hermes.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const fixture = readFixtures(__dirname, ['hermes', 'hermes-fix']);
1010
test('putout: parser: hermes', (t) => {
1111
const {code} = putout(fixture.hermes, {
1212
parser: 'hermes',
13-
plugins: ['remove-unused-variables'],
13+
plugins: ['variables'],
1414
});
1515

1616
const expected = fixture.hermesFix;

packages/engine-parser/lib/printers/babel.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ test('putout: parser: print: printer: babel: preserve format: trim', (t) => {
6262
});
6363

6464
transform(ast, source, {
65-
plugins: ['remove-unused-variables'],
65+
plugins: ['variables'],
6666
});
6767

6868
const result = print(ast, {
@@ -89,7 +89,7 @@ test('putout: parser: print: printer: babel: preserve format: align-spaces: off'
8989
});
9090

9191
transform(ast, source, {
92-
plugins: ['remove-unused-variables'],
92+
plugins: ['variables'],
9393
});
9494

9595
const result = print(ast, {

packages/engine-parser/test/parser.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const fixture = readFixtures([
4646
test('putout: parser: export default declaration: acorn', (t) => {
4747
const {code} = putout(fixture.exportDefaultDeclaration, {
4848
parser: 'acorn',
49-
plugins: ['remove-unused-variables'],
49+
plugins: ['variables'],
5050
});
5151

5252
const expected = fixture.exportDefaultDeclarationFix;
@@ -58,7 +58,7 @@ test('putout: parser: export default declaration: acorn', (t) => {
5858
test('putout: parser: acorn: parens-typescript', (t) => {
5959
const {code} = putout(fixture.parensTypescript, {
6060
parser: 'acorn',
61-
plugins: ['remove-unused-variables'],
61+
plugins: ['variables'],
6262
});
6363

6464
const expected = fixture.parensTypescriptFix;
@@ -70,7 +70,7 @@ test('putout: parser: acorn: parens-typescript', (t) => {
7070
test('putout: parser: export default declaration: esprima', (t) => {
7171
const {code} = putout(fixture.exportDefaultDeclaration, {
7272
parser: 'esprima',
73-
plugins: ['remove-unused-variables'],
73+
plugins: ['variables'],
7474
});
7575

7676
const expected = fixture.exportDefaultDeclarationFix;
@@ -82,7 +82,7 @@ test('putout: parser: export default declaration: esprima', (t) => {
8282
test('putout: parser: export default declaration: custom parser', (t) => {
8383
const [e] = tryCatch(putout, fixture.exportDefaultDeclaration, {
8484
parser: 'custom',
85-
plugins: ['remove-unused-variables'],
85+
plugins: ['variables'],
8686
});
8787

8888
t.match(e.message, `Cannot find module 'custom'`);
@@ -105,7 +105,7 @@ test('putout: parser: use strict: parser: espree: debugger', (t) => {
105105
test('putout: parser: export default declaration: tenko: export default', (t) => {
106106
const {code} = putout(fixture.exportDefaultDeclaration, {
107107
parser: 'tenko',
108-
plugins: ['remove-unused-variables'],
108+
plugins: ['variables'],
109109
});
110110

111111
const expected = fixture.exportDefaultDeclarationFix;
@@ -116,7 +116,7 @@ test('putout: parser: export default declaration: tenko: export default', (t) =>
116116

117117
test('putout: parser: export default declaration: tenko: throw', (t) => {
118118
const {code} = putout(fixture.throw, {
119-
plugins: ['remove-unused-variables'],
119+
plugins: ['variables'],
120120
});
121121

122122
const expected = '\n';
@@ -176,7 +176,7 @@ test('putout: parser: generate', (t) => {
176176

177177
test('putout: parser: flow', (t) => {
178178
const {code} = putout(fixture.flow, {
179-
plugins: ['remove-unused-variables'],
179+
plugins: ['variables'],
180180
});
181181

182182
const expected = fixture.flowFix;
@@ -202,7 +202,7 @@ test('putout: parser: broken', (t) => {
202202
test('putout: parser: typescript', (t) => {
203203
const {code} = putout(fixture.typescript, {
204204
isTS: true,
205-
plugins: ['remove-unused-variables'],
205+
plugins: ['variables'],
206206
});
207207

208208
const expected = fixture.typescriptFix;
@@ -214,7 +214,7 @@ test('putout: parser: typescript', (t) => {
214214
test('putout: parser: decorator', (t) => {
215215
const {code} = putout(fixture.decorator, {
216216
isTS: true,
217-
plugins: ['remove-unused-variables'],
217+
plugins: ['variables'],
218218
});
219219

220220
const expected = fixture.decorator;
@@ -226,7 +226,7 @@ test('putout: parser: decorator', (t) => {
226226
test('putout: parser: decorator-legacy', (t) => {
227227
const {code} = putout(fixture.decoratorLegacy, {
228228
isTS: true,
229-
plugins: ['remove-unused-variables'],
229+
plugins: ['variables'],
230230
});
231231

232232
const expected = fixture.decoratorLegacy;
@@ -263,7 +263,7 @@ test('putout: parser: jsx: not react', (t) => {
263263

264264
test('putout: parser: strict mode', (t) => {
265265
const {code} = putout(fixture.strictMode, {
266-
plugins: ['remove-unused-variables'],
266+
plugins: ['variables'],
267267
});
268268

269269
t.equal(code, fixture.strictModeFix);
@@ -439,7 +439,7 @@ test('putout: parser: esprima', (t) => {
439439
test('putout: parser: printer: babel', (t) => {
440440
const {code} = putout(fixture.printerBabel, {
441441
printer: 'babel',
442-
plugins: ['remove-unused-variables'],
442+
plugins: ['variables'],
443443
});
444444

445445
const expected = fixture.printerBabelFix;

0 commit comments

Comments
 (0)