Skip to content

Commit 21cde20

Browse files
committed
refactor: @putout/plugin-remove-nested-blocks: simplify
1 parent d10d224 commit 21cde20

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

packages/plugin-remove-nested-blocks/lib/remove-nested-blocks.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@ const {isReturnStatement} = types;
55
const {replaceWithMultiple} = operator;
66
const {keys} = Object;
77

8-
const isIntersect = (bindingsA, path) => {
9-
if (path.removed)
10-
return false;
11-
12-
path.scope.crawl();
13-
14-
const keysA = keys(bindingsA);
15-
16-
for (const key of keysA) {
17-
if (path.scope.hasBinding(key))
18-
return true;
19-
}
20-
21-
return false;
22-
};
23-
248
module.exports.report = () => 'Avoid nested blocks';
259

2610
module.exports.fix = (path) => {
@@ -47,12 +31,13 @@ module.exports.filter = (path) => {
4731
if (isSwitch)
4832
return false;
4933

50-
const is = !isIntersect(bindings, path.parentPath);
51-
5234
if (!parentPath.isBlockStatement() && !parentPath.isProgram())
5335
return false;
5436

55-
return is || path.container.length === 1;
37+
if (!isIntersect(parentPath, bindings))
38+
return true;
39+
40+
return path.container.length === 1;
5641
};
5742

5843
function isReturnWithoutArg(path) {
@@ -63,3 +48,15 @@ function isReturnWithoutArg(path) {
6348

6449
return !prevPath.node.argument;
6550
}
51+
52+
const isIntersect = (path, bindings) => {
53+
path.scope.crawl();
54+
55+
for (const key of keys(bindings)) {
56+
if (path.scope.hasBinding(key))
57+
return true;
58+
}
59+
60+
return false;
61+
};
62+

0 commit comments

Comments
 (0)