Skip to content

Commit f5078b3

Browse files
committed
feature: @putout/plugin-remove-useless-contiue: remove only ContinueStatement
1 parent 2f5449c commit f5078b3

File tree

9 files changed

+54
-10
lines changed

9 files changed

+54
-10
lines changed

packages/plugin-convert-for-each-to-for-of/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"devDependencies": {
3232
"@putout/plugin-convert-comparison-to-boolean": "*",
3333
"@putout/plugin-convert-const-to-let": "*",
34+
"@putout/plugin-remove-useless-continue": "*",
3435
"@putout/plugin-remove-useless-variables": "*",
3536
"@putout/test": "^5.0.0",
3637
"c8": "^7.5.0",

packages/plugin-convert-for-each-to-for-of/test/convert-for-each-to-for-of.js

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const convertForEachToForOf = require('..');
66
const convertComparisonToBoolean = require('@putout/plugin-convert-comparison-to-boolean');
77
const removeUselessVariables = require('@putout/plugin-remove-useless-variables');
88
const convertConstToLet = require('@putout/plugin-convert-const-to-let');
9+
const removeUselessContinue = require('@putout/plugin-remove-useless-continue');
910

1011
const test = createTest(__dirname, {
1112
'convert-for-each-to-for-of': convertForEachToForOf,
@@ -106,6 +107,13 @@ test('plugin-convert-for-each-to-for-of: transform: this i', (t) => {
106107
t.end();
107108
});
108109

110+
test('plugin-convert-for-each-to-for-of: transform: end-return', (t) => {
111+
t.transform('end-return', {
112+
'remove-useless-continue': removeUselessContinue,
113+
});
114+
t.end();
115+
});
116+
109117
test('plugin-convert-for-each-to-for-of: transform: not-constant', (t) => {
110118
t.transform('not-const', {
111119
convertConstToLet,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let a = 0;
2+
3+
for (const b of list) {
4+
a += b;
5+
a;
6+
}
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let a = 0;
2+
3+
list.forEach((b) => {
4+
a += b;
5+
return a;
6+
});
7+

packages/plugin-remove-useless-continue/lib/remove-useless-continue.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
'use strict';
22

3-
const {EmptyElement} = require('putout').types;
3+
const {operator} = require('putout');
4+
5+
const {remove} = operator;
46

57
const isLoop = (path) => path.isLoop() || path.parentPath.isLoop();
68

7-
module.exports.report = () => 'Useless continue should be avoided';
9+
module.exports.report = () => `Avoid useless 'continue'`;
810

911
module.exports.fix = (path) => {
10-
path.parentPath.node.body = EmptyElement;
12+
remove(path);
1113
};
1214

1315
module.exports.traverse = ({push}) => ({
@@ -23,7 +25,7 @@ module.exports.traverse = ({push}) => ({
2325
}
2426

2527
if (parentPath.isBlockStatement()) {
26-
push(parentPath);
28+
push(path);
2729
return;
2830
}
2931
},
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
for (i = 0; i < 5; i++)
2-
;
1+
for (i = 0; i < 5; i++) {}
32

43
for (i = 0; i < 5; i++)
5-
;
4+
{}
65

76
while (x)
8-
;
7+
{}
98

109
for (const a of [])
11-
;
10+
{}
1211

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let a = 0;
2+
3+
for (const b of list) {
4+
a += b;
5+
}
6+
7+
fn(a);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let a = 0;
2+
3+
for (const b of list) {
4+
a += b;
5+
continue;
6+
}
7+
8+
fn(a);

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const test = createTest(__dirname, {
88
});
99

1010
test('plugin-remove-useless-continue: report', (t) => {
11-
t.report('continue', 'Useless continue should be avoided');
11+
t.report('continue', `Avoid useless 'continue'`);
1212
t.end();
1313
});
1414

@@ -22,6 +22,11 @@ test('plugin-remove-useless-continue: transform', (t) => {
2222
t.end();
2323
});
2424

25+
test('plugin-remove-useless-continue: transform: for-of', (t) => {
26+
t.transform('for-of');
27+
t.end();
28+
});
29+
2530
test('plugin-remove-useless-continue: no transform: no-continue', (t) => {
2631
t.noTransform('no-continue');
2732
t.end();

0 commit comments

Comments
 (0)