Skip to content

Commit 8295aed

Browse files
committed
fix: @putout/processor-wasm: rules: apply-nesting: exclude couple
1 parent 592be77 commit 8295aed

File tree

7 files changed

+70
-9
lines changed

7 files changed

+70
-9
lines changed

packages/processor-wasm/lib/rules/apply-nesting/fixture/apply-nesting-fix.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
;; this is simple function that adds a couple of parameters
33

44
(func (param $a i32) (param $b i32)
5-
(i32.add (get.local $a) (get.local $b))
5+
(i32.add (get.local $b) (get.local $a))
66
)
77
;; this statement exports the function to the host environment
88

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(func $Moore (param $upLeft i32) (param $up i32) (param $upRight i32) (param $right i32) (param $downRight i32) (param $down i32) (param $downLeft i32) (param $left i32) (result i32)
2+
(local.get $upLeft)
3+
(local.get $up)
4+
(local.get $upRight)
5+
(local.get $right)
6+
(local.get $downRight)
7+
(local.get $down)
8+
(i32.add (local.get $left) (local.get $downLeft))
9+
(i32.add)
10+
(i32.add)
11+
(i32.add)
12+
(i32.add)
13+
(i32.add)
14+
(i32.add)
15+
)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(func $Moore (export "Moore")
2+
(param $upLeft i32) (param $up i32) (param $upRight i32)
3+
(param $right i32) (param $downRight i32) (param $down i32)
4+
(param $downLeft i32) (param $left i32)
5+
(result i32)
6+
7+
(local.get $upLeft)
8+
(local.get $up)
9+
(local.get $upRight)
10+
(local.get $right)
11+
(local.get $downRight)
12+
(local.get $down)
13+
(local.get $downLeft)
14+
(local.get $left)
15+
16+
(i32.add) (i32.add) (i32.add) (i32.add)
17+
(i32.add) (i32.add) (i32.add)
18+
)
Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,47 @@
1+
const INSTRUCTIONS = ['add'];
2+
13
export const report = (path) => {
24
const {id, object} = path.node;
35
return `Apply nesting for '${object}.${id}'`;
46
};
57

68
export const fix = (path) => {
7-
const {parentPath, parentKey} = path;
8-
const body = parentPath.node[parentKey];
9+
const body = getBody(path);
910
const index = body.indexOf(path.node);
10-
const first = body[index - 2];
11-
const second = body[index - 1];
1211

13-
path.node.args = [first, second];
12+
path.node.args = getArgs(path);
1413
body.splice(index - 2, 2);
1514
};
1615

1716
export const traverse = ({push}) => ({
1817
Instr(path) {
1918
const {id, args} = path.node;
2019

21-
if (id === 'add' && !args.length)
20+
if (args.length)
21+
return;
22+
23+
const prev = getPrevSibling(path);
24+
25+
if (INSTRUCTIONS.includes(prev.id))
26+
return;
27+
28+
if (INSTRUCTIONS.includes(id))
2229
push(path);
2330
},
2431
});
32+
33+
function getArgs(path) {
34+
const first = getPrevSibling(path, 1);
35+
const second = getPrevSibling(path, 2);
36+
37+
return [first, second];
38+
}
39+
40+
function getPrevSibling(path, index = 1) {
41+
const body = getBody(path);
42+
const currentIndex = body.indexOf(path.node);
43+
44+
return body[currentIndex - index];
45+
}
46+
47+
const getBody = ({parentPath, parentKey}) => parentPath.node[parentKey];

packages/processor-wasm/lib/rules/apply-nesting/index.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ test('putout: processor-wasm: apply-nesting: no report: nested', (t) => {
2121
t.noReport('nested');
2222
t.end();
2323
});
24+
25+
test('putout: processor-wasm: apply-nesting: transform: couple', (t) => {
26+
t.transform('couple');
27+
t.end();
28+
});

packages/processor-wasm/test/fixture/apply-nesting-fix.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
;; this is simple function that adds a couple of parameters
33

44
(func (param $a i32) (param $b i32)
5-
(i32.add (get.local $a) (get.local $b))
5+
(i32.add (get.local $b) (get.local $a))
66
)
77
;; this statement exports the function to the host environment
88

packages/processor-wasm/test/fixture/set-local-fix.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
;; this is simple function that adds a couple of parameters
33

44
(func $add (param $a i32) (param $b i32) (result i32)
5-
(i32.add (local.set $a) (local.set $b))
5+
(i32.add (local.set $b) (local.set $a))
66
)
77
;; this statement exports the function to the host environment
88

0 commit comments

Comments
 (0)