@wasm-fmt/clang-format 21.1.7 → 22.1.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/README.md CHANGED
@@ -18,15 +18,13 @@ npx jsr add @fmt/clang-format
18
18
 
19
19
  ## CLI
20
20
 
21
- This repository contains two executable files, namely clang-format and git-clang-format.
21
+ This repository contains 3 executable files, namely `clang-format`, `git-clang-format` and `clang-format-diff`.
22
22
  For more information, please refer to https://clang.llvm.org/docs/ClangFormat.html
23
23
 
24
- ## API
24
+ ## Node.js / Deno / Bun / Bundler
25
25
 
26
- ```JavaScript
27
- import init, { format } from "@wasm-fmt/clang-format";
28
-
29
- await init();
26
+ ```javascript
27
+ import { format } from "@wasm-fmt/clang-format";
30
28
 
31
29
  const source = `
32
30
  #include <iostream>
@@ -38,9 +36,9 @@ return 0;}
38
36
 
39
37
  // JSON representation of Clang-Format Style Options
40
38
  const config = JSON.stringify({
41
- BasedOnStyle: "Chromium",
42
- IndentWidth: 4,
43
- ColumnLimit: 80,
39
+ BasedOnStyle: "Chromium",
40
+ IndentWidth: 4,
41
+ ColumnLimit: 80,
44
42
  });
45
43
 
46
44
  // or YAML representation of Clang-Format Style Options which is used in `.clang-format` file
@@ -55,11 +53,7 @@ ColumnLimit: 80
55
53
  // or the preset name
56
54
  const config3 = "Chromium";
57
55
 
58
- const formatted = format(
59
- source,
60
- "main.cc",
61
- config,
62
- );
56
+ const formatted = format(source, "main.cc", config);
63
57
 
64
58
  console.log(formatted);
65
59
  ```
@@ -72,6 +66,53 @@ The third argument of `format` is a Clang-Format Style Options, which can be one
72
66
 
73
67
  See [Clang-Format Style Options](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for more information.
74
68
 
69
+ ## Web
70
+
71
+ For web environments, you need to initialize WASM module manually:
72
+
73
+ ```javascript
74
+ import init, { format } from "@wasm-fmt/clang-format/web";
75
+
76
+ await init();
77
+
78
+ const source = `
79
+ #include <iostream>
80
+ using namespace std;
81
+ auto main() -> int{
82
+ std::cout << "Hello World!" << std::endl;
83
+ return 0;}
84
+ `;
85
+
86
+ const formatted = format(source, "main.cc", "Chromium");
87
+ console.log(formatted);
88
+ ```
89
+
90
+ ### Vite
91
+
92
+ ```JavaScript
93
+ import init, { format } from "@wasm-fmt/clang-format/vite";
94
+
95
+ await init();
96
+ // ...
97
+ ```
98
+
99
+ ## Entry Points
100
+
101
+ - `.` - Auto-detects environment (Node.js uses node, Webpack uses bundler, default is ESM)
102
+ - `./node` - Node.js environment (no init required)
103
+ - `./esm` - ESM environments like Deno (no init required)
104
+ - `./bundler` - Bundlers like Webpack (no init required)
105
+ - `./web` - Web browsers (requires manual init)
106
+ - `./vite` - Vite bundler (requires manual init)
107
+
108
+ # How does it work?
109
+
110
+ [Clang-Format] is a tool to format C/C++/Java/JavaScript/TypeScript/Objective-C/Protobuf/C# code.
111
+
112
+ This package is a WebAssembly build of Clang-Format, with a JavaScript wrapper.
113
+
114
+ [Clang-Format]: https://clang.llvm.org/docs/ClangFormat.html
115
+
75
116
  # Build from source
76
117
 
77
118
  1. Install [LLVM](https://llvm.org/docs/GettingStarted.html) and [Clang](https://clang.llvm.org/get_started.html) (version 18 or later).
@@ -0,0 +1,114 @@
1
+ let wasm;
2
+
3
+ export function set_wasm(_wasm) {
4
+ wasm = _wasm;
5
+ assert_init = () => {};
6
+ }
7
+
8
+ const registry = typeof FinalizationRegistry === "undefined" ? { register: () => {}, unregister: () => {} }
9
+ : new FinalizationRegistry((impl) => {
10
+ impl.delete();
11
+ });
12
+
13
+ export class ClangFormat {
14
+ constructor() {
15
+ assert_init();
16
+ this._impl = new wasm.ClangFormat();
17
+ registry.register(this, this._impl, this);
18
+ }
19
+
20
+ with_style(style) {
21
+ this._impl.with_style(style);
22
+ return this;
23
+ }
24
+
25
+ with_fallback_style(style) {
26
+ this._impl.with_fallback_style(style);
27
+ return this;
28
+ }
29
+
30
+ format(content, filename = "<stdin>") {
31
+ const result = this._impl.format(content, filename);
32
+ return unwrap(result) ?? content;
33
+ }
34
+
35
+ format_range(content, offset, length, filename = "<stdin>") {
36
+ const result = this._impl.format_range(content, filename, offset, length);
37
+ return unwrap(result) ?? content;
38
+ }
39
+
40
+ format_line(content, from, to, filename = "<stdin>") {
41
+ const result = this._impl.format_line(content, filename, from, to);
42
+ return unwrap(result) ?? content;
43
+ }
44
+
45
+ static version() {
46
+ assert_init();
47
+ return wasm.ClangFormat.version();
48
+ }
49
+
50
+ static dump_config({ style = "file", filename = "<stdin>", code = "" } = {}) {
51
+ assert_init();
52
+ const result = wasm.ClangFormat.dump_config(style, filename, code);
53
+ return unwrap(result);
54
+ }
55
+
56
+ [Symbol.dispose]() {
57
+ if (this._impl) {
58
+ registry.unregister(this);
59
+ this._impl.delete();
60
+ this._impl = null;
61
+ }
62
+ }
63
+ }
64
+
65
+ function assert_init() { throw new Error("uninit"); }
66
+
67
+ function unwrap(result) {
68
+ const { status, content } = result;
69
+ if (status === wasm.ResultStatus.Error) {
70
+ throw Error(content);
71
+ }
72
+ if (status === wasm.ResultStatus.Unchanged) {
73
+ return null;
74
+ }
75
+ return content;
76
+ }
77
+
78
+ export function version() { return ClangFormat.version(); }
79
+
80
+ export function dump_config(args) { return ClangFormat.dump_config(args); }
81
+
82
+ export function format(content, filename = "<stdin>", style = "LLVM") {
83
+ const formatter = new ClangFormat();
84
+ try {
85
+ return formatter.with_style(style).format(content, filename);
86
+ } finally {
87
+ formatter[Symbol.dispose]();
88
+ }
89
+ }
90
+
91
+ export function format_line_range(content, from, to, filename = "<stdin>", style = "LLVM") {
92
+ const formatter = new ClangFormat().with_style(style);
93
+ try {
94
+ if (from < 1) {
95
+ throw Error("start line should be at least 1");
96
+ }
97
+ if (from > to) {
98
+ throw Error("start line should not exceed end line");
99
+ }
100
+
101
+ return formatter.format_line(content, from, to, filename);
102
+ } finally {
103
+ formatter[Symbol.dispose]();
104
+ }
105
+ }
106
+
107
+ export function format_byte_range(content, offset, length, filename = "<stdin>", style = "LLVM") {
108
+ const formatter = new ClangFormat().with_style(style);
109
+ try {
110
+ return formatter.format_range(content, offset, length, filename);
111
+ } finally {
112
+ formatter[Symbol.dispose]();
113
+ }
114
+ }