Skip to content

Commit fe12961

Browse files
committed
feature: @putout/processor-wasm: rules: remove-useless-args: add
1 parent 27e355a commit fe12961

File tree

13 files changed

+129
-4
lines changed

13 files changed

+129
-4
lines changed

packages/processor-wasm/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ npm i @putout/processor-wasm -D
7373

7474
### remove-unused
7575

76+
```diff
77+
(module
78+
(func $one (result i32)
79+
i32.const 1
80+
)
81+
(func $oneTwo (param $y) (result i32 i32)
82+
- (call $one (local.get $y))
83+
+ (call $one)
84+
i32.const 2
85+
)
86+
)
87+
```
88+
89+
### remove-useless-args
90+
7691
```diff
7792
(module
7893
- (func $answer (result i32)

packages/processor-wasm/lib/lint.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,18 @@ const parseError = ({message}) => ({
8383
},
8484
});
8585

86+
const parsePath = (path) => path.path || path;
87+
8688
function convertPlaces(rule, rawPlaces, plugin) {
8789
const places = [];
8890

8991
for (const path of rawPlaces) {
90-
const {line, column} = path.node.loc.start;
92+
const currentPath = parsePath(path);
93+
94+
const {line, column} = currentPath.node.loc?.start || {
95+
line: 0,
96+
column: 0,
97+
};
9198

9299
places.push({
93100
rule: `${rule} (wasm)`,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import * as convertGetLocalToLocalGet from './convert-get-local-to-local-get/index.js';
22
import * as convertSetLocalToLocalSet from './convert-set-local-to-local-set/index.js';
33
import * as applyNesting from './apply-nesting/index.js';
4+
import * as removeUselessArgs from './remove-useless-args/index.js';
5+
import * as removeUnused from './remove-unused/index.js';
46

57
export const rules = [
68
['convert-get-local-to-local-get', convertGetLocalToLocalGet],
79
['convert-set-local-to-local-set', convertSetLocalToLocalSet],
810
['apply-nesting', applyNesting],
11+
['remove-useless-args', removeUselessArgs],
12+
['remove-unused', removeUnused],
913
];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(module
2+
(func $one (result i32)
3+
(i32.const 1)
4+
)
5+
(func $oneTwo (result i32) (result i32)
6+
(call $one)
7+
(i32.const 2)
8+
)
9+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(module
2+
(func $one (result i32)
3+
i32.const 1
4+
)
5+
(func $oneTwo (param $y) (result i32 i32)
6+
(call $one (local.get $y))
7+
i32.const 2
8+
)
9+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const {entries} = Object;
2+
3+
export const report = ({path}) => `Avoid useless arguments in '${path.node.index.value}' call`;
4+
5+
export const fix = ({path, length}) => {
6+
path.node.instrArgs.length = length;
7+
};
8+
9+
export const find = (ast, {push, traverse}) => {
10+
const funcs = {};
11+
const calls = {};
12+
13+
traverse(ast, {
14+
Func(path) {
15+
const {value} = path.node.name;
16+
const {params} = path.node.signature;
17+
18+
funcs[value] = params;
19+
},
20+
CallInstruction(path) {
21+
const {index, instrArgs} = path.node;
22+
const {value} = index;
23+
24+
calls[value] = [path, instrArgs];
25+
},
26+
});
27+
28+
for (const [name, [path, args]] of entries(calls)) {
29+
const {length} = funcs[name];
30+
31+
if (length < args.length)
32+
push({
33+
path,
34+
length,
35+
});
36+
}
37+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {createTest} from '#test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['remove-useless-args', plugin],
7+
],
8+
});
9+
10+
test('putout: processor-wasm: remove-useless-args: report', (t) => {
11+
t.report('remove-useless-args', `Avoid useless arguments in 'one' call`);
12+
t.end();
13+
});
14+
15+
test('putout: processor-wasm: remove-useless-args: transform', (t) => {
16+
t.transform('remove-useless-args');
17+
t.end();
18+
});

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
(module
22
;; this is simple function that adds a couple of parameters
33

4-
(func (param $a i32) (param $b i32)
5-
(i32.add (get.local $b) (get.local $a))
6-
)
74
;; this statement exports the function to the host environment
85

96
(export "add" (func $add))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(module
2+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(func $answer (result i32)
3+
i32.const 0x2A
4+
)
5+
)

0 commit comments

Comments
 (0)