@wasm-fmt/gofmt 0.4.0 → 0.4.2

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Build](https://github.com/wasm-fmt/gofmt/actions/workflows/build.yml/badge.svg)](https://github.com/wasm-fmt/gofmt/actions/workflows/build.yml)
1
+ [![Test](https://github.com/wasm-fmt/gofmt/actions/workflows/test.yml/badge.svg)](https://github.com/wasm-fmt/gofmt/actions/workflows/test.yml)
2
2
  [![npm](https://img.shields.io/npm/v/@wasm-fmt/gofmt)](https://www.npmjs.com/package/@wasm-fmt/gofmt)
3
3
 
4
4
  # Install
@@ -40,8 +40,8 @@ git clone https://github.com/wasm-fmt/gofmt.git
40
40
  # 2. install TinyGo https://tinygo.org/getting-started/install/
41
41
 
42
42
  # 3. build
43
- npm run build
43
+ pnpm build
44
44
 
45
45
  # 4. test
46
- npm run test
46
+ pnpm run /^test:/
47
47
  ```
@@ -1,9 +1,9 @@
1
1
  export type InitInput =
2
- | RequestInfo
3
- | URL
4
- | Response
5
- | BufferSource
6
- | WebAssembly.Module;
2
+ | RequestInfo
3
+ | URL
4
+ | Response
5
+ | BufferSource
6
+ | WebAssembly.Module;
7
7
 
8
8
  export default function init(wasm_url?: InitInput): Promise<void>;
9
9
  export declare function format(input: string): string;
@@ -3,11 +3,10 @@
3
3
  // license that can be found in the LICENSE file.
4
4
  //
5
5
  // This file has been modified for use by the TinyGo compiler.
6
-
7
6
  const encoder = new TextEncoder("utf-8");
8
7
  const decoder = new TextDecoder("utf-8");
9
8
 
10
- export class Go {
9
+ class Go {
11
10
  constructor() {
12
11
  this._callbackTimeouts = new Map();
13
12
  this._nextCallbackTimeoutID = 1;
@@ -356,3 +355,72 @@
356
355
  };
357
356
  }
358
357
  }
358
+ /**
359
+ * ================== End of wasm_exec.js ==================
360
+ */
361
+ /**/let wasm;
362
+ /**/export default async function init(input) {
363
+ /**/ if (wasm) {
364
+ /**/ await wasm;
365
+ /**/ return;
366
+ /**/ }
367
+ /**/ const go = new Go();
368
+ /**/ wasm = load(input, go.importObject);
369
+ /**/ wasm = await wasm;
370
+ /**/ go.run(wasm.instance);
371
+ /**/}
372
+ /**/
373
+ /**/async function load(module, importObject) {
374
+ /**/ switch (typeof module) {
375
+ /**/ case "undefined":
376
+ /**/ module = "gofmt.wasm";
377
+ /**/ case "string":
378
+ /**/ module = new URL(module, import.meta.url);
379
+ /**/ }
380
+ /**/ if (module instanceof URL || module instanceof Request) {
381
+ /**/ if (
382
+ /**/ typeof __webpack_require__ !== "function" &&
383
+ /**/ module.protocol === "file:"
384
+ /**/ ) {
385
+ /**/ const fs = await import("node:fs/promises");
386
+ /**/ module = await fs.readFile(module);
387
+ /**/ } else {
388
+ /**/ module = await fetch(module);
389
+ /**/ }
390
+ /**/ }
391
+ /**/ if (module instanceof Response) {
392
+ /**/ if ("instantiateStreaming" in WebAssembly) {
393
+ /**/ try {
394
+ /**/ return await WebAssembly.instantiateStreaming(
395
+ /**/ module,
396
+ /**/ importObject
397
+ /**/ );
398
+ /**/ } catch (e) {
399
+ /**/ if (module.headers.get("Content-Type") != "application/wasm") {
400
+ /**/ console.warn(
401
+ /**/ "`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",
402
+ /**/ e
403
+ /**/ );
404
+ /**/ } else {
405
+ /**/ throw e;
406
+ /**/ }
407
+ /**/ }
408
+ /**/ }
409
+ /**/ const bytes = await module.arrayBuffer();
410
+ /**/ return await WebAssembly.instantiate(bytes, importObject);
411
+ /**/ }
412
+ /**/ const instance = await WebAssembly.instantiate(module, importObject);
413
+ /**/ if (instance instanceof WebAssembly.Instance) {
414
+ /**/ return { instance, module };
415
+ /**/ }
416
+ /**/ return instance;
417
+ /**/}
418
+ /**/
419
+ /**/export function format(input) {
420
+ /**/ const [err, result] = wasm.instance.format(input);
421
+ /**/ if (err) {
422
+ /**/ throw new Error(result);
423
+ /**/ }
424
+ /**/ return result;
425
+ /**/}
426
+ /**/
package/package.json CHANGED
@@ -1,50 +1,51 @@
1
1
  {
2
- "name": "@wasm-fmt/gofmt",
3
- "description": "A wasm based golang formatter",
4
- "author": "magic-akari <akari.ccino@gamil.com>",
5
- "version": "0.4.0",
6
- "license": "MIT",
7
- "keywords": [
8
- "wasm",
9
- "golang",
10
- "formatter"
11
- ],
12
- "repository": {
13
- "type": "git",
14
- "url": "https://github.com/wasm-fmt/gofmt"
15
- },
16
- "homepage": "https://github.com/wasm-fmt/gofmt",
17
- "bugs": {
18
- "url": "https://github.com/wasm-fmt/gofmt/issues"
19
- },
20
- "type": "module",
21
- "main": "lib.js",
22
- "module": "lib.js",
23
- "types": "lib.d.ts",
24
- "exports": {
25
- ".": {
26
- "types": "./lib.d.ts",
27
- "default": "./lib.js"
28
- },
29
- "./vite": {
30
- "types": "./lib.d.ts",
31
- "default": "./vite.js"
32
- },
33
- "./package.json": "./package.json",
34
- "./*": "./*"
35
- },
36
- "scripts": {
37
- "go_wasm": "cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js ./go_wasm.js",
38
- "patch": "git apply ./go_wasm.patch",
39
- "build": "tinygo build -o=lib.wasm -target=wasm -no-debug -stack-size=24kb ./src/lib.go",
40
- "postbuild": "npm run go_wasm && npm run patch",
41
- "test": "node --test"
42
- },
43
- "engines": {
44
- "node": ">=16.17.0"
45
- },
46
- "publishConfig": {
47
- "access": "public",
48
- "provenance": true
49
- }
2
+ "name": "@wasm-fmt/gofmt",
3
+ "description": "A wasm based golang formatter",
4
+ "author": "magic-akari <akari.ccino@gamil.com>",
5
+ "version": "0.4.2",
6
+ "license": "MIT",
7
+ "keywords": [
8
+ "wasm",
9
+ "golang",
10
+ "formatter"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/wasm-fmt/gofmt"
15
+ },
16
+ "homepage": "https://github.com/wasm-fmt/gofmt",
17
+ "bugs": {
18
+ "url": "https://github.com/wasm-fmt/gofmt/issues"
19
+ },
20
+ "type": "module",
21
+ "main": "gofmt.js",
22
+ "module": "gofmt.js",
23
+ "types": "gofmt.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./gofmt.d.ts",
27
+ "default": "./gofmt.js"
28
+ },
29
+ "./vite": {
30
+ "types": "./gofmt.d.ts",
31
+ "default": "./vite.js"
32
+ },
33
+ "./package.json": "./package.json",
34
+ "./*": "./*"
35
+ },
36
+ "scripts": {
37
+ "gofmt": "cp $(tinygo env TINYGOROOT)/targets/wasm_exec.js ./gofmt.js",
38
+ "patch": "git apply ./gofmt.patch",
39
+ "build": "tinygo build -o=gofmt.wasm -target=wasm -no-debug -stack-size=24kb ./src/lib.go",
40
+ "postbuild": "npm run gofmt && npm run patch",
41
+ "test:node": "node --test test_node",
42
+ "test:deno": "deno test test_deno --allow-read",
43
+ "test:bun": "bun test test_bun"
44
+ },
45
+ "engines": {
46
+ "node": ">=16.17.0"
47
+ },
48
+ "publishConfig": {
49
+ "access": "public"
50
+ }
50
51
  }
package/vite.js CHANGED
@@ -1,8 +1,8 @@
1
- import init from "./lib.js";
2
- import wasm_url from "./lib.wasm?url";
1
+ import init from "./gofmt.js";
2
+ import wasm_url from "./gofmt.wasm?url";
3
3
 
4
4
  export default function (input = wasm_url) {
5
- return init(input);
5
+ return init(input);
6
6
  }
7
7
 
8
- export * from "./lib.js";
8
+ export * from "./gofmt.js";
package/.editorconfig DELETED
@@ -1,11 +0,0 @@
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 DELETED
@@ -1,5 +0,0 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
3
- *.js linguist-detectable=false
4
- *.mjs linguist-detectable=false
5
- *.sh linguist-detectable=false
package/.node-version DELETED
@@ -1 +0,0 @@
1
- v18.17.0
package/gen_patch.sh DELETED
@@ -1,23 +0,0 @@
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.js DELETED
@@ -1,75 +0,0 @@
1
- import { Go } from "./go_wasm.js";
2
- const go = new Go();
3
-
4
- let wasm;
5
-
6
- export default async function init(input) {
7
- if (wasm) {
8
- await wasm;
9
- return;
10
- }
11
-
12
- wasm = load(input, go.importObject);
13
- wasm = await wasm;
14
- go.run(wasm.instance);
15
- }
16
-
17
- async function load(module, importObject) {
18
- switch (typeof module) {
19
- case "undefined":
20
- module = "lib.wasm";
21
- case "string":
22
- module = new URL(module, import.meta.url);
23
- }
24
-
25
- if (module instanceof URL || module instanceof Request) {
26
- if (
27
- typeof __webpack_require__ !== "function" &&
28
- module.protocol === "file:"
29
- ) {
30
- const fs = await import("node:fs/promises");
31
- module = await fs.readFile(module);
32
- } else {
33
- module = await fetch(module);
34
- }
35
- }
36
-
37
- if (module instanceof Response) {
38
- if ("instantiateStreaming" in WebAssembly) {
39
- try {
40
- return await WebAssembly.instantiateStreaming(
41
- module,
42
- importObject
43
- );
44
- } catch (e) {
45
- if (module.headers.get("Content-Type") != "application/wasm") {
46
- console.warn(
47
- "`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n",
48
- e
49
- );
50
- } else {
51
- throw e;
52
- }
53
- }
54
- }
55
-
56
- const bytes = await module.arrayBuffer();
57
- return await WebAssembly.instantiate(bytes, importObject);
58
- }
59
- const instance = await WebAssembly.instantiate(module, importObject);
60
-
61
- if (instance instanceof WebAssembly.Instance) {
62
- return { instance, module };
63
- }
64
-
65
- return instance;
66
- }
67
-
68
- export function format(input) {
69
- const [err, result] = wasm.instance.format(input);
70
- if (err) {
71
- throw new Error(result);
72
- }
73
-
74
- return result;
75
- }
package/src/lib.go DELETED
@@ -1,23 +0,0 @@
1
- package main
2
-
3
- import (
4
- "go/format"
5
- "syscall/js"
6
- )
7
-
8
- func Format(this js.Value, args []js.Value) any {
9
- input := ([]byte)(args[0].String())
10
-
11
- output, err := format.Source(input)
12
- if err != nil {
13
- return []any{true, err.Error()}
14
- }
15
-
16
- return []any{false, string(output)}
17
- }
18
-
19
- func main() {
20
- done := make(chan bool)
21
- js.Global().Set("format", js.FuncOf(Format))
22
- <-done
23
- }
package/test.js DELETED
@@ -1,29 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import fs from "node:fs/promises";
3
- import test from "node:test";
4
- import init, { format } from "./lib.js";
5
-
6
- await init();
7
-
8
- const files = (await fs.readdir("testdata"))
9
- .filter((f) => f.endsWith(".input"))
10
- .map((f) => {
11
- return {
12
- input_name: f,
13
- golden_name: f.replace(".input", ".golden"),
14
- };
15
- });
16
-
17
- for (const { input_name, golden_name } of files) {
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
- );
24
-
25
- const actual = format(input);
26
-
27
- assert.equal(actual, expected);
28
- });
29
- }
File without changes