@wasm-fmt/gofmt 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +11 -0
- package/.gitattributes +5 -0
- package/README.md +7 -9
- package/gen_patch.sh +23 -0
- package/lib.d.ts +2 -4
- package/lib.js +32 -27
- package/package.json +18 -2
- package/test.js +20 -22
- package/vite.js +8 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Check http://editorconfig.org for more information
|
|
2
|
+
# This is the main config file for this project:
|
|
3
|
+
root = true
|
|
4
|
+
|
|
5
|
+
[*]
|
|
6
|
+
charset = utf-8
|
|
7
|
+
trim_trailing_whitespace = true
|
|
8
|
+
end_of_line = lf
|
|
9
|
+
indent_style = space
|
|
10
|
+
insert_final_newline = true
|
|
11
|
+
indent_size = 4
|
package/.gitattributes
ADDED
package/README.md
CHANGED
|
@@ -10,27 +10,25 @@ npm install @wasm-fmt/gofmt
|
|
|
10
10
|
# Usage
|
|
11
11
|
|
|
12
12
|
```JavaScript
|
|
13
|
-
import { format } from '@wasm-fmt/gofmt';
|
|
13
|
+
import init, { format } from '@wasm-fmt/gofmt';
|
|
14
|
+
|
|
15
|
+
await init();
|
|
14
16
|
|
|
15
17
|
const source = `
|
|
16
18
|
package main
|
|
17
19
|
import "fmt"
|
|
18
|
-
func main(){fmt.Println("Hello, 世界")
|
|
20
|
+
func main(){fmt.Println("Hello, 世界")
|
|
21
|
+
}
|
|
19
22
|
`;
|
|
20
23
|
|
|
21
|
-
const formatted =
|
|
24
|
+
const formatted = format(source);
|
|
22
25
|
console.log(formatted);
|
|
23
26
|
```
|
|
24
27
|
|
|
25
28
|
Vite users tip:
|
|
26
29
|
|
|
27
30
|
```JavaScript
|
|
28
|
-
import { format } from '@wasm-fmt/gofmt';
|
|
29
|
-
import wasmUrl from "@wasm-fmt/gofmt/lib.wasm?url";
|
|
30
|
-
|
|
31
|
-
// ...
|
|
32
|
-
|
|
33
|
-
const formatted = await format(source, new URL(wasmUrl, import.meta.url));
|
|
31
|
+
import init, { format } from '@wasm-fmt/gofmt/vite';
|
|
34
32
|
```
|
|
35
33
|
|
|
36
34
|
# Build from source
|
package/gen_patch.sh
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
current_dir=$(pwd)
|
|
2
|
+
tmp_dir=$(mktemp -d)
|
|
3
|
+
|
|
4
|
+
cd $tmp_dir
|
|
5
|
+
git init
|
|
6
|
+
|
|
7
|
+
cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js $tmp_dir/go_wasm.js
|
|
8
|
+
git add -f .
|
|
9
|
+
git commit -m "init"
|
|
10
|
+
|
|
11
|
+
cp $current_dir/go_wasm.js $tmp_dir/go_wasm.js
|
|
12
|
+
git add -f .
|
|
13
|
+
|
|
14
|
+
git diff \
|
|
15
|
+
--cached \
|
|
16
|
+
--no-color \
|
|
17
|
+
--ignore-space-at-eol \
|
|
18
|
+
--no-ext-diff \
|
|
19
|
+
--src-prefix=a/ \
|
|
20
|
+
--dst-prefix=b/ \
|
|
21
|
+
>$current_dir/go_wasm.patch
|
|
22
|
+
|
|
23
|
+
rm -rf $tmp_dir
|
package/lib.d.ts
CHANGED
package/lib.js
CHANGED
|
@@ -3,37 +3,42 @@ const go = new Go();
|
|
|
3
3
|
|
|
4
4
|
let mod;
|
|
5
5
|
|
|
6
|
-
export async function
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
export default async function init(wasm_url) {
|
|
7
|
+
if (!mod) {
|
|
8
|
+
if (!wasm_url) {
|
|
9
|
+
wasm_url = new URL("lib.wasm", import.meta.url);
|
|
10
|
+
}
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
if (typeof wasm_url === "string") {
|
|
13
|
+
wasm_url = new URL(wasm_url);
|
|
14
|
+
}
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
16
|
+
if (
|
|
17
|
+
typeof __webpack_require__ !== "function" &&
|
|
18
|
+
wasm_url.protocol === "file:"
|
|
19
|
+
) {
|
|
20
|
+
const fs = await import("node:fs");
|
|
21
|
+
const bytes = fs.readFileSync(wasm_url);
|
|
22
|
+
mod = new WebAssembly.Module(bytes);
|
|
23
|
+
} else if ("compileStreaming" in WebAssembly) {
|
|
24
|
+
mod = await WebAssembly.compileStreaming(fetch(wasm_url));
|
|
25
|
+
} else {
|
|
26
|
+
const response = await fetch(wasm_url);
|
|
27
|
+
const bytes = await response.arrayBuffer();
|
|
28
|
+
mod = new WebAssembly.Module(bytes);
|
|
29
|
+
}
|
|
26
30
|
}
|
|
27
|
-
|
|
31
|
+
}
|
|
28
32
|
|
|
29
|
-
|
|
30
|
-
|
|
33
|
+
export function format(input) {
|
|
34
|
+
const inst = new WebAssembly.Instance(mod, go.importObject);
|
|
35
|
+
go.run(inst);
|
|
31
36
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
const input_len = go.storeString(input);
|
|
38
|
+
const output_len = inst.exports.format(input_len);
|
|
39
|
+
if (output_len < 0) {
|
|
40
|
+
throw new Error(go.loadString(-output_len));
|
|
41
|
+
}
|
|
37
42
|
|
|
38
|
-
|
|
43
|
+
return go.loadString(output_len);
|
|
39
44
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wasm-fmt/gofmt",
|
|
3
3
|
"author": "magic-akari <akari.ccino@gamil.com>",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.3.0",
|
|
5
5
|
"description": "wasm based gofmt",
|
|
6
6
|
"main": "lib.js",
|
|
7
7
|
"types": "lib.d.ts",
|
|
8
8
|
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./lib.d.ts",
|
|
12
|
+
"default": "./lib.js"
|
|
13
|
+
},
|
|
14
|
+
"./vite": {
|
|
15
|
+
"types": "./lib.d.ts",
|
|
16
|
+
"default": "./vite.js"
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json",
|
|
19
|
+
"./*": "./*"
|
|
20
|
+
},
|
|
9
21
|
"scripts": {
|
|
10
22
|
"go_wasm": "cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js ./go_wasm.js",
|
|
11
23
|
"patch": "git apply ./go_wasm.patch",
|
|
@@ -16,7 +28,11 @@
|
|
|
16
28
|
"engines": {
|
|
17
29
|
"node": ">=16.17.0"
|
|
18
30
|
},
|
|
19
|
-
"keywords": [
|
|
31
|
+
"keywords": [
|
|
32
|
+
"wasm",
|
|
33
|
+
"golang",
|
|
34
|
+
"formatter"
|
|
35
|
+
],
|
|
20
36
|
"license": "MIT",
|
|
21
37
|
"publishConfig": {
|
|
22
38
|
"access": "public"
|
package/test.js
CHANGED
|
@@ -1,31 +1,29 @@
|
|
|
1
|
-
import assert from "node:assert";
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
2
|
import fs from "node:fs/promises";
|
|
3
3
|
import test from "node:test";
|
|
4
|
-
import { format } from "./lib.js";
|
|
4
|
+
import init, { format } from "./lib.js";
|
|
5
|
+
|
|
6
|
+
await init();
|
|
5
7
|
|
|
6
8
|
const files = (await fs.readdir("testdata"))
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
.filter((f) => f.endsWith(".input"))
|
|
10
|
+
.map((f) => {
|
|
11
|
+
return {
|
|
12
|
+
input_name: f,
|
|
13
|
+
golden_name: f.replace(".input", ".golden"),
|
|
14
|
+
};
|
|
15
|
+
});
|
|
14
16
|
|
|
15
17
|
for (const { input_name, golden_name } of files) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
fs.readFile(`testdata/${f}`, "utf-8")
|
|
23
|
-
)
|
|
24
|
-
);
|
|
18
|
+
test(`format ${input_name}`, { skip: input_name[0] === "." }, async () => {
|
|
19
|
+
const [input, expected] = await Promise.all(
|
|
20
|
+
[input_name, golden_name].map((f) =>
|
|
21
|
+
fs.readFile(`testdata/${f}`, "utf-8")
|
|
22
|
+
)
|
|
23
|
+
);
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
const actual = format(input);
|
|
27
26
|
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
);
|
|
27
|
+
assert.equal(actual, expected);
|
|
28
|
+
});
|
|
31
29
|
}
|