Skip to content

Commit 25abc61

Browse files
committed
feature: @putout/processor-wasm: apply-nesting (xtuc/webassemblyjs#1208)
1 parent 4e69573 commit 25abc61

File tree

20 files changed

+189
-29
lines changed

20 files changed

+189
-29
lines changed

packages/processor-wasm/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ npm i @putout/processor-wasm -D
5151
+local.set $a
5252
```
5353

54+
### apply-nesting
55+
56+
#### ❌ Example of incorrect code
57+
58+
```wast
59+
(func (param $a i32) (param $b i32)
60+
(get_local $a)
61+
(get_local $b)
62+
(i32.add)
63+
)
64+
```
65+
66+
#### ✅ Example of correct code
67+
68+
```wast
69+
(func (param $a i32) (param $b i32)
70+
(i32.add (get_local $a) (get_local $b))
71+
)
72+
```
73+
5474
## License
5575

5676
MIT

packages/processor-wasm/lib/lint.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import tryCatch from 'try-catch';
22
import {traverse} from '@webassemblyjs/ast';
33
import {parse} from '@webassemblyjs/wast-parser';
44
import {print} from '@webassemblyjs/wast-printer';
5-
import {rules} from './rules/index.js';
65

7-
export const lint = (source, {fix = true} = {}) => {
6+
const {entries} = Object;
7+
const {isArray} = Array;
8+
9+
export const lint = (source, overrides = {}) => {
10+
const {fix = true, plugins} = overrides;
811
const [error, ast] = tryCatch(parse, source);
912

1013
if (error)
@@ -16,7 +19,7 @@ export const lint = (source, {fix = true} = {}) => {
1619

1720
const allPlaces = [];
1821

19-
for (const [rule, plugin] of rules) {
22+
for (const [rule, plugin] of parsePlugins(plugins)) {
2023
const places = [];
2124
const push = places.push.bind(places);
2225

@@ -43,6 +46,21 @@ export const lint = (source, {fix = true} = {}) => {
4346
};
4447
};
4548

49+
function parsePlugins(plugins) {
50+
const result = [];
51+
52+
for (const plugin of plugins) {
53+
if (isArray(plugin)) {
54+
result.push(plugin);
55+
continue;
56+
}
57+
58+
result.push(...entries(plugin));
59+
}
60+
61+
return result;
62+
}
63+
4664
const parseError = ({message}) => ({
4765
rule: 'wasm-parser-error (wasm)',
4866
message,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
;; this is simple function that adds a couple of parameters
3+
4+
(func (param $a i32) (param $b i32)
5+
(i32.add (get.local $a) (get.local $b))
6+
)
7+
;; this statement exports the function to the host environment
8+
9+
(export "add" (func $add))
10+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
;; this is simple function that adds a couple of parameters
3+
(func (param $a i32) (param $b i32)
4+
(get.local $a)
5+
(get.local $b)
6+
(i32.add)
7+
)
8+
;; this statement exports the function to the host environment
9+
(export "add" (func $add))
10+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module
2+
;; this is simple function that adds a couple of parameters
3+
4+
(func (param $a i32) (param $b i32)
5+
(i32.add (get.local $a) (get.local $b))
6+
)
7+
;; this statement exports the function to the host environment
8+
9+
(export "add" (func $add))
10+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const report = (path) => {
2+
const {id, object} = path.node;
3+
return `Apply nesting for '${object}.${id}'`;
4+
};
5+
6+
export const fix = (path) => {
7+
const {parentPath, parentKey} = path;
8+
const body = parentPath.node[parentKey];
9+
const index = body.indexOf(path.node);
10+
const first = body[index - 2];
11+
const second = body[index - 1];
12+
13+
path.node.args = [first, second];
14+
body.splice(index - 2, 2);
15+
};
16+
17+
export const traverse = ({push}) => ({
18+
Instr(path) {
19+
const {id, args} = path.node;
20+
21+
if (id === 'add' && !args.length)
22+
push(path);
23+
},
24+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {createTest} from '#test';
2+
import * as plugin from './index.js';
3+
4+
const test = createTest(import.meta.url, {
5+
plugins: [
6+
['apply-nesting', plugin],
7+
],
8+
});
9+
10+
test('putout: processor-wasm: apply-nesting: report', (t) => {
11+
t.report('apply-nesting', `Apply nesting for 'i32.add'`);
12+
t.end();
13+
});
14+
15+
test('putout: processor-wasm: apply-nesting: transform', (t) => {
16+
t.transform('apply-nesting');
17+
t.end();
18+
});
19+
20+
test('putout: processor-wasm: apply-nesting: no report: nested', (t) => {
21+
t.noReport('nested');
22+
t.end();
23+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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';
3+
import * as applyNesting from './apply-nesting/index.js';
34

45
export const rules = [
56
['convert-get-local-to-local-get', convertGetLocalToLocalGet],
67
['convert-set-local-to-local-set', convertSetLocalToLocalSet],
8+
['apply-nesting', applyNesting],
79
];

packages/processor-wasm/lib/wasm.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {lint} from './lint.js';
2+
import {rules as plugins} from './rules/index.js';
23

34
export const files = [
45
'*.wat',
@@ -8,6 +9,7 @@ export const files = [
89
export const find = (source) => {
910
const result = lint(source, {
1011
fix: false,
12+
plugins,
1113
});
1214

1315
return result.places;
@@ -16,6 +18,7 @@ export const find = (source) => {
1618
export const fix = (source) => {
1719
const {code} = lint(source, {
1820
fix: true,
21+
plugins,
1922
});
2023

2124
return code;

packages/processor-wasm/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"eslint-plugin-putout": "^26.0.0",
5050
"madrun": "^11.0.0",
5151
"nodemon": "^3.0.1",
52-
"putout": "*"
52+
"putout": "*",
53+
"supertape": "^11.1.0"
5354
},
5455
"imports": {
5556
"#test": {

0 commit comments

Comments
 (0)