Skip to content

Commit b2e9bbd

Browse files
committed
feature: @putout/plugin-conditions: apply-consistent-blocks: leading comments
1 parent b73bd36 commit b2e9bbd

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const get = () => {
2+
let a = 0;
3+
4+
if (b) {
5+
// hello
6+
a = 0;
7+
} else if (c) {
8+
// world
9+
const x = 1;
10+
a = create(x);
11+
}
12+
13+
return a;
14+
};

packages/plugin-conditions/lib/apply-consistent-blocks/index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@ export const report = () => `Use consistent blocks`;
1212
export const fix = (path) => {
1313
const paths = getAllNodes(path);
1414

15-
if (isAllBlocks(paths))
15+
if (isAllBlocks(paths)) {
1616
for (const path of paths) {
1717
if (isBlockStatement(path))
1818
continue;
1919

2020
const {node} = path;
2121
replaceWith(path, blockStatement([node]));
2222
}
23-
else
24-
for (const path of paths) {
25-
if (!isBlockStatement(path))
26-
continue;
27-
28-
const [node] = path.node.body;
29-
replaceWith(path, node);
30-
}
23+
24+
return;
25+
}
26+
27+
for (const path of paths) {
28+
if (!isBlockStatement(path))
29+
continue;
30+
31+
const [node] = path.node.body;
32+
replaceWith(path, node);
33+
}
3134
};
3235

3336
function isAllBlocks(paths) {
@@ -80,7 +83,7 @@ export const filter = (path) => {
8083
continue;
8184

8285
if (first.leadingComments?.length)
83-
continue;
86+
return false;
8487
}
8588

8689
blocks.push(is);

packages/plugin-conditions/lib/apply-consistent-blocks/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ test('putout: apply-consistent-blocks: no report: comment', (t) => {
7272
t.end();
7373
});
7474

75+
test('putout: apply-consistent-blocks: no report: comment-else', (t) => {
76+
t.noReport('comment-else');
77+
t.end();
78+
});
79+
7580
test('putout: apply-consistent-blocks: no report after transform: nested', (t) => {
7681
t.noReportAfterTransform('nested');
7782
t.end();

0 commit comments

Comments
 (0)