worsier 0.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 ADDED
@@ -0,0 +1,16 @@
1
+ # Worsier
2
+
3
+ A configurable JavaScript, TypeScript, JSX, and TSX formatter powered by Rust.
4
+
5
+ ```sh
6
+ pnpm add -D worsier
7
+ pnpm exec worsier --init
8
+ pnpm exec worsier --check .
9
+ pnpm exec worsier --write .
10
+ ```
11
+
12
+ ```js
13
+ import { format } from 'worsier'
14
+
15
+ const output = await format('example.ts', 'const value={answer:42};', {})
16
+ ```
package/bin/worsier.js ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+
3
+ import('../dist/binding.js')
4
+ .then(({ loadBinding }) => loadBinding().runCli(process.argv.slice(2)))
5
+ .then((exitCode) => {
6
+ process.exitCode = exitCode
7
+ })
8
+ .catch((error) => {
9
+ console.error(error instanceof Error ? error.message : error)
10
+ process.exitCode = 2
11
+ })
@@ -0,0 +1,298 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "title": "FormatConfig",
4
+ "type": "object",
5
+ "properties": {
6
+ "$schema": {
7
+ "type": [
8
+ "string",
9
+ "null"
10
+ ]
11
+ },
12
+ "arrays": {
13
+ "$ref": "#/$defs/ArrayConfig",
14
+ "default": {
15
+ "elementLayout": "auto",
16
+ "layout": "auto",
17
+ "objectElements": "inherit"
18
+ }
19
+ },
20
+ "arrowParentheses": {
21
+ "$ref": "#/$defs/ArrowParentheses",
22
+ "default": "always"
23
+ },
24
+ "bracketSpacing": {
25
+ "type": "boolean",
26
+ "default": true
27
+ },
28
+ "finalNewline": {
29
+ "type": "boolean",
30
+ "default": true
31
+ },
32
+ "ignorePatterns": {
33
+ "type": "array",
34
+ "default": [],
35
+ "items": {
36
+ "type": "string"
37
+ }
38
+ },
39
+ "imports": {
40
+ "$ref": "#/$defs/ImportConfig",
41
+ "default": {
42
+ "specifierLayout": "auto"
43
+ }
44
+ },
45
+ "indentStyle": {
46
+ "$ref": "#/$defs/IndentStyle",
47
+ "default": "space"
48
+ },
49
+ "indentWidth": {
50
+ "type": "integer",
51
+ "format": "uint8",
52
+ "default": 2,
53
+ "maximum": 24,
54
+ "minimum": 0
55
+ },
56
+ "lineEnding": {
57
+ "$ref": "#/$defs/LineEnding",
58
+ "default": "preserve"
59
+ },
60
+ "lineWidth": {
61
+ "type": "integer",
62
+ "format": "uint16",
63
+ "default": 100,
64
+ "maximum": 320,
65
+ "minimum": 1
66
+ },
67
+ "objects": {
68
+ "$ref": "#/$defs/ObjectConfig",
69
+ "default": {
70
+ "layout": "auto",
71
+ "propertyLayout": "auto",
72
+ "whenArrayElement": "inherit"
73
+ }
74
+ },
75
+ "quoteStyle": {
76
+ "$ref": "#/$defs/QuoteStyle",
77
+ "default": "single"
78
+ },
79
+ "semicolons": {
80
+ "$ref": "#/$defs/Semicolons",
81
+ "default": "always"
82
+ },
83
+ "statementSpacing": {
84
+ "type": "array",
85
+ "default": [],
86
+ "items": {
87
+ "$ref": "#/$defs/StatementSpacingRule"
88
+ }
89
+ },
90
+ "trailingCommas": {
91
+ "$ref": "#/$defs/TrailingCommas",
92
+ "default": "multiline"
93
+ },
94
+ "verifyAst": {
95
+ "type": "boolean",
96
+ "default": true
97
+ }
98
+ },
99
+ "additionalProperties": false,
100
+ "$defs": {
101
+ "ArrayConfig": {
102
+ "type": "object",
103
+ "properties": {
104
+ "elementLayout": {
105
+ "$ref": "#/$defs/CollectionItemLayout",
106
+ "default": "auto"
107
+ },
108
+ "layout": {
109
+ "$ref": "#/$defs/CollectionLayout",
110
+ "default": "auto"
111
+ },
112
+ "objectElements": {
113
+ "$ref": "#/$defs/ArrayObjectLayout",
114
+ "default": "inherit"
115
+ }
116
+ },
117
+ "additionalProperties": false
118
+ },
119
+ "ArrayObjectLayout": {
120
+ "type": "string",
121
+ "enum": [
122
+ "inherit",
123
+ "onePerLine"
124
+ ]
125
+ },
126
+ "ArrowParentheses": {
127
+ "type": "string",
128
+ "enum": [
129
+ "always",
130
+ "asNeeded"
131
+ ]
132
+ },
133
+ "CollectionItemLayout": {
134
+ "type": "string",
135
+ "enum": [
136
+ "auto",
137
+ "preserve",
138
+ "onePerLine"
139
+ ]
140
+ },
141
+ "CollectionLayout": {
142
+ "type": "string",
143
+ "enum": [
144
+ "auto",
145
+ "preserve",
146
+ "singleLine",
147
+ "multiLine"
148
+ ]
149
+ },
150
+ "ImportConfig": {
151
+ "type": "object",
152
+ "properties": {
153
+ "specifierLayout": {
154
+ "$ref": "#/$defs/CollectionItemLayout",
155
+ "default": "auto"
156
+ }
157
+ },
158
+ "additionalProperties": false
159
+ },
160
+ "IndentStyle": {
161
+ "type": "string",
162
+ "enum": [
163
+ "space",
164
+ "tab"
165
+ ]
166
+ },
167
+ "LineEnding": {
168
+ "type": "string",
169
+ "enum": [
170
+ "preserve",
171
+ "lf",
172
+ "crlf"
173
+ ]
174
+ },
175
+ "LineShape": {
176
+ "type": "string",
177
+ "enum": [
178
+ "any",
179
+ "singleLine",
180
+ "multiLine"
181
+ ]
182
+ },
183
+ "ObjectArrayLayout": {
184
+ "type": "string",
185
+ "enum": [
186
+ "inherit",
187
+ "multiLine"
188
+ ]
189
+ },
190
+ "ObjectConfig": {
191
+ "type": "object",
192
+ "properties": {
193
+ "layout": {
194
+ "$ref": "#/$defs/CollectionLayout",
195
+ "default": "auto"
196
+ },
197
+ "propertyLayout": {
198
+ "$ref": "#/$defs/CollectionItemLayout",
199
+ "default": "auto"
200
+ },
201
+ "whenArrayElement": {
202
+ "$ref": "#/$defs/ObjectArrayLayout",
203
+ "default": "inherit"
204
+ }
205
+ },
206
+ "additionalProperties": false
207
+ },
208
+ "QuoteStyle": {
209
+ "type": "string",
210
+ "enum": [
211
+ "single",
212
+ "double"
213
+ ]
214
+ },
215
+ "Semicolons": {
216
+ "type": "string",
217
+ "enum": [
218
+ "always",
219
+ "asNeeded"
220
+ ]
221
+ },
222
+ "StatementKind": {
223
+ "type": "string",
224
+ "enum": [
225
+ "any",
226
+ "import",
227
+ "export",
228
+ "const",
229
+ "let",
230
+ "var",
231
+ "function",
232
+ "class",
233
+ "type",
234
+ "interface",
235
+ "enum",
236
+ "namespace",
237
+ "other"
238
+ ]
239
+ },
240
+ "StatementScope": {
241
+ "type": "string",
242
+ "enum": [
243
+ "any",
244
+ "topLevel",
245
+ "block"
246
+ ]
247
+ },
248
+ "StatementSelector": {
249
+ "type": "object",
250
+ "properties": {
251
+ "kind": {
252
+ "$ref": "#/$defs/StatementKind",
253
+ "default": "any"
254
+ },
255
+ "lineShape": {
256
+ "$ref": "#/$defs/LineShape",
257
+ "default": "any"
258
+ }
259
+ },
260
+ "additionalProperties": false
261
+ },
262
+ "StatementSpacingRule": {
263
+ "type": "object",
264
+ "properties": {
265
+ "blankLines": {
266
+ "type": "integer",
267
+ "format": "uint8",
268
+ "maximum": 255,
269
+ "minimum": 0
270
+ },
271
+ "next": {
272
+ "$ref": "#/$defs/StatementSelector"
273
+ },
274
+ "previous": {
275
+ "$ref": "#/$defs/StatementSelector"
276
+ },
277
+ "scope": {
278
+ "$ref": "#/$defs/StatementScope",
279
+ "default": "any"
280
+ }
281
+ },
282
+ "additionalProperties": false,
283
+ "required": [
284
+ "previous",
285
+ "next",
286
+ "blankLines"
287
+ ]
288
+ },
289
+ "TrailingCommas": {
290
+ "type": "string",
291
+ "enum": [
292
+ "none",
293
+ "multiline",
294
+ "all"
295
+ ]
296
+ }
297
+ }
298
+ }
@@ -0,0 +1,5 @@
1
+ export interface NativeBinding {
2
+ format(fileName: string, sourceText: string, configJson: string): Promise<string>;
3
+ runCli(args: string[]): Promise<number>;
4
+ }
5
+ export declare function loadBinding(): NativeBinding;
@@ -0,0 +1,39 @@
1
+ import { createRequire } from 'node:module';
2
+ const require = createRequire(import.meta.url);
3
+ const platformPackages = {
4
+ 'darwin-arm64': 'worsier-darwin-arm64',
5
+ 'darwin-x64': 'worsier-darwin-x64',
6
+ 'linux-arm64-gnu': 'worsier-linux-arm64-gnu',
7
+ 'linux-arm64-musl': 'worsier-linux-arm64-musl',
8
+ 'linux-x64-gnu': 'worsier-linux-x64-gnu',
9
+ 'linux-x64-musl': 'worsier-linux-x64-musl',
10
+ 'win32-arm64-msvc': 'worsier-win32-arm64-msvc',
11
+ 'win32-x64-msvc': 'worsier-win32-x64-msvc'
12
+ };
13
+ let binding;
14
+ function libc() {
15
+ if (process.platform !== 'linux') {
16
+ return 'gnu';
17
+ }
18
+ const report = process.report?.getReport();
19
+ return report?.header?.glibcVersionRuntime ? 'gnu' : 'musl';
20
+ }
21
+ export function loadBinding() {
22
+ if (binding) {
23
+ return binding;
24
+ }
25
+ const suffix = process.platform === 'linux' ? `-${libc()}` : process.platform === 'win32' ? '-msvc' : '';
26
+ const target = `${process.platform}-${process.arch}${suffix}`;
27
+ const packageName = platformPackages[target];
28
+ if (!packageName) {
29
+ throw new Error(`Worsier does not support ${target}.`);
30
+ }
31
+ try {
32
+ binding = require(packageName);
33
+ }
34
+ catch (error) {
35
+ const reason = error instanceof Error ? error.message : String(error);
36
+ throw new Error(`Failed to load the Worsier native package ${packageName}: ${reason}`, { cause: error });
37
+ }
38
+ return binding;
39
+ }
@@ -0,0 +1,3 @@
1
+ import type { FormatConfig } from './types.js';
2
+ export type { FormatConfig } from './types.js';
3
+ export declare function format(fileName: string, sourceText: string, config: FormatConfig): Promise<string>;
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
1
+ import { loadBinding } from './binding.js';
2
+ export async function format(fileName, sourceText, config) {
3
+ const configJson = JSON.stringify(config);
4
+ try {
5
+ return await loadBinding().format(fileName, sourceText, configJson);
6
+ }
7
+ catch (error) {
8
+ if (error instanceof Error) {
9
+ const match = /^\[([A-Z_]+)\]\s*/.exec(error.message);
10
+ if (match) {
11
+ Object.defineProperty(error, 'code', {
12
+ configurable: true,
13
+ enumerable: true,
14
+ value: match[1]
15
+ });
16
+ error.message = error.message.slice(match[0].length);
17
+ }
18
+ }
19
+ throw error;
20
+ }
21
+ }
@@ -0,0 +1,42 @@
1
+ export interface FormatConfig {
2
+ $schema?: string | null;
3
+ arrays?: ArrayConfig;
4
+ arrowParentheses?: 'always' | 'asNeeded';
5
+ bracketSpacing?: boolean;
6
+ finalNewline?: boolean;
7
+ ignorePatterns?: string[];
8
+ imports?: ImportConfig;
9
+ indentStyle?: 'space' | 'tab';
10
+ indentWidth?: number;
11
+ lineEnding?: 'preserve' | 'lf' | 'crlf';
12
+ lineWidth?: number;
13
+ objects?: ObjectConfig;
14
+ quoteStyle?: 'single' | 'double';
15
+ semicolons?: 'always' | 'asNeeded';
16
+ statementSpacing?: StatementSpacingRule[];
17
+ trailingCommas?: 'none' | 'multiline' | 'all';
18
+ verifyAst?: boolean;
19
+ }
20
+ export interface ArrayConfig {
21
+ elementLayout?: 'auto' | 'preserve' | 'onePerLine';
22
+ layout?: 'auto' | 'preserve' | 'singleLine' | 'multiLine';
23
+ objectElements?: 'inherit' | 'onePerLine';
24
+ }
25
+ export interface ImportConfig {
26
+ specifierLayout?: 'auto' | 'preserve' | 'onePerLine';
27
+ }
28
+ export interface ObjectConfig {
29
+ layout?: 'auto' | 'preserve' | 'singleLine' | 'multiLine';
30
+ propertyLayout?: 'auto' | 'preserve' | 'onePerLine';
31
+ whenArrayElement?: 'inherit' | 'multiLine';
32
+ }
33
+ export interface StatementSpacingRule {
34
+ blankLines: number;
35
+ next: StatementSelector;
36
+ previous: StatementSelector;
37
+ scope?: 'any' | 'topLevel' | 'block';
38
+ }
39
+ export interface StatementSelector {
40
+ kind?: 'any' | 'import' | 'export' | 'const' | 'let' | 'var' | 'function' | 'class' | 'type' | 'interface' | 'enum' | 'namespace' | 'other';
41
+ lineShape?: 'any' | 'singleLine' | 'multiLine';
42
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ // Generated from the Rust FormatConfig JSON Schema. Do not edit.
2
+ export {};
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "worsier",
3
+ "version": "0.1.0",
4
+ "description": "A configurable JavaScript and TypeScript formatter powered by Rust",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Perdolique/worsier.git",
9
+ "directory": "packages/npm"
10
+ },
11
+ "type": "module",
12
+ "engines": {
13
+ "node": "^20.19.0 || >=22.12.0"
14
+ },
15
+ "exports": {
16
+ ".": {
17
+ "types": "./dist/index.d.ts",
18
+ "import": "./dist/index.js"
19
+ },
20
+ "./configuration_schema.json": "./configuration_schema.json"
21
+ },
22
+ "bin": {
23
+ "worsier": "bin/worsier.js"
24
+ },
25
+ "files": [
26
+ "bin",
27
+ "dist",
28
+ "configuration_schema.json",
29
+ "README.md"
30
+ ],
31
+ "scripts": {
32
+ "build": "tsc -p tsconfig.json",
33
+ "build:native": "napi build --manifest-path ../../crates/napi/Cargo.toml --output-dir ../../npm/linux-x64-gnu --platform --release --no-js",
34
+ "test": "node --test test/*.test.mjs",
35
+ "test:packed": "node ../../scripts/test-packed-package.mjs"
36
+ },
37
+ "napi": {
38
+ "binaryName": "worsier",
39
+ "targets": [
40
+ "aarch64-apple-darwin",
41
+ "aarch64-pc-windows-msvc",
42
+ "aarch64-unknown-linux-gnu",
43
+ "aarch64-unknown-linux-musl",
44
+ "x86_64-apple-darwin",
45
+ "x86_64-pc-windows-msvc",
46
+ "x86_64-unknown-linux-gnu",
47
+ "x86_64-unknown-linux-musl"
48
+ ]
49
+ },
50
+ "optionalDependencies": {
51
+ "worsier-darwin-arm64": "0.1.0",
52
+ "worsier-win32-arm64-msvc": "0.1.0",
53
+ "worsier-linux-arm64-gnu": "0.1.0",
54
+ "worsier-linux-arm64-musl": "0.1.0",
55
+ "worsier-darwin-x64": "0.1.0",
56
+ "worsier-win32-x64-msvc": "0.1.0",
57
+ "worsier-linux-x64-gnu": "0.1.0",
58
+ "worsier-linux-x64-musl": "0.1.0"
59
+ },
60
+ "devDependencies": {
61
+ "@types/node": "26.2.0",
62
+ "typescript": "7.0.2"
63
+ }
64
+ }