@ryuugg/stanza 0.3.2 → 0.4.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 +7 -5
- package/package.json +19 -11
- package/src/launcher +102 -8
- package/src/cli.ts +0 -659
- package/src/compile/darwin-arm64.ts +0 -3
- package/src/compile/darwin-x64.ts +0 -3
- package/src/compile/linux-arm64-musl.ts +0 -3
- package/src/compile/linux-arm64.ts +0 -3
- package/src/compile/linux-x64-musl.ts +0 -3
- package/src/compile/linux-x64.ts +0 -3
- package/src/compiled.ts +0 -9
- package/src/depth.ts +0 -368
- package/src/directories.ts +0 -77
- package/src/engine/attach.ts +0 -62
- package/src/engine/directives.ts +0 -157
- package/src/engine/doc.ts +0 -93
- package/src/engine/edits.ts +0 -79
- package/src/engine/explain.ts +0 -332
- package/src/engine/gaps.ts +0 -357
- package/src/engine/hunks.ts +0 -319
- package/src/engine/index.ts +0 -273
- package/src/engine/model.ts +0 -103
- package/src/engine/rules.ts +0 -103
- package/src/engine/types.ts +0 -38
- package/src/files.ts +0 -975
- package/src/format.ts +0 -159
- package/src/gitignore.ts +0 -280
- package/src/hook.ts +0 -365
- package/src/languages/index.ts +0 -34
- package/src/languages/javascript/ast.ts +0 -261
- package/src/languages/javascript/braces.ts +0 -374
- package/src/languages/javascript/config/biome.ts +0 -112
- package/src/languages/javascript/config/eslint.ts +0 -128
- package/src/languages/javascript/config/evaluate.ts +0 -278
- package/src/languages/javascript/config/find.ts +0 -105
- package/src/languages/javascript/config/glob.ts +0 -149
- package/src/languages/javascript/config/index.ts +0 -48
- package/src/languages/javascript/config/layers.ts +0 -468
- package/src/languages/javascript/config/module.ts +0 -327
- package/src/languages/javascript/config/oxlint.ts +0 -170
- package/src/languages/javascript/config/width.ts +0 -723
- package/src/languages/javascript/deserializers.ts +0 -17
- package/src/languages/javascript/embedded.ts +0 -7
- package/src/languages/javascript/entry.ts +0 -42
- package/src/languages/javascript/facts.ts +0 -294
- package/src/languages/javascript/index.ts +0 -20
- package/src/languages/javascript/lists.ts +0 -64
- package/src/languages/javascript/oracle.ts +0 -191
- package/src/languages/javascript/oxc-raw.d.ts +0 -31
- package/src/languages/javascript/parse.ts +0 -131
- package/src/languages/javascript/raw.ts +0 -95
- package/src/languages/javascript/scan.ts +0 -68
- package/src/languages/language.ts +0 -109
- package/src/native-file.d.ts +0 -4
- package/src/pool.ts +0 -312
- package/src/start.ts +0 -49
- package/src/step.ts +0 -113
- package/src/usage.ts +0 -35
- package/src/worker.ts +0 -4
package/src/pool.ts
DELETED
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
import { availableParallelism } from "node:os";
|
|
2
|
-
import { statSync } from "node:fs";
|
|
3
|
-
import { workerData, type MessagePort } from "node:worker_threads";
|
|
4
|
-
import {
|
|
5
|
-
formatFile,
|
|
6
|
-
mergeOutcomes,
|
|
7
|
-
readText,
|
|
8
|
-
type FileOutcome,
|
|
9
|
-
type FormatRun,
|
|
10
|
-
type Formatted,
|
|
11
|
-
type Input,
|
|
12
|
-
} from "./format.ts";
|
|
13
|
-
import { deepRisk } from "./depth.ts";
|
|
14
|
-
import {
|
|
15
|
-
deserializerFiles,
|
|
16
|
-
embedDeserializers,
|
|
17
|
-
type DeserializerFiles,
|
|
18
|
-
} from "./languages/javascript/deserializers.ts";
|
|
19
|
-
|
|
20
|
-
export const THRESHOLD = 256;
|
|
21
|
-
export const DEFAULT_WORKERS = 3;
|
|
22
|
-
export const CHUNK_SIZE = 64;
|
|
23
|
-
|
|
24
|
-
export interface FileInput extends Omit<Input, "read"> {
|
|
25
|
-
output: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface PoolData<Input, Job> {
|
|
29
|
-
id: number;
|
|
30
|
-
inputs: Input[];
|
|
31
|
-
job: Job;
|
|
32
|
-
chunkSize: number;
|
|
33
|
-
counter: SharedArrayBuffer;
|
|
34
|
-
signal: SharedArrayBuffer;
|
|
35
|
-
claims: SharedArrayBuffer;
|
|
36
|
-
finished: SharedArrayBuffer;
|
|
37
|
-
port: MessagePort;
|
|
38
|
-
addon: string | undefined;
|
|
39
|
-
deserializers: DeserializerFiles;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export type ChunkMessage<Result> =
|
|
43
|
-
| (Result & { chunk: number })
|
|
44
|
-
| { chunk: number | undefined; error: string };
|
|
45
|
-
|
|
46
|
-
export interface PoolTask<Input, Outcome, Result extends { outcomes: (Outcome | null)[] }, Job> {
|
|
47
|
-
worker: string | URL;
|
|
48
|
-
job: Job;
|
|
49
|
-
chunkSize?: number;
|
|
50
|
-
chunk(inputs: Input[]): Result;
|
|
51
|
-
merge(result: Result): void;
|
|
52
|
-
item(input: Input): Outcome;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function workerCount(
|
|
56
|
-
fileCount: number,
|
|
57
|
-
env: Readonly<Record<string, string | undefined>>,
|
|
58
|
-
maximum = DEFAULT_WORKERS,
|
|
59
|
-
): number {
|
|
60
|
-
const override = env.STANZA_WORKERS;
|
|
61
|
-
if (override !== undefined && /^\d+$/.test(override)) {
|
|
62
|
-
const count = Number(override);
|
|
63
|
-
if (count <= 64) return count;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (fileCount < THRESHOLD) return 0;
|
|
67
|
-
|
|
68
|
-
return Math.max(0, Math.min(maximum, availableParallelism() - 1));
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function hardLinked(input: FileInput, run: FormatRun): boolean {
|
|
72
|
-
if (run.mode !== "fix" || !run.write) return false;
|
|
73
|
-
|
|
74
|
-
try {
|
|
75
|
-
return statSync(input.path).nlink > 1;
|
|
76
|
-
} catch {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function tooDeep(path: string, text: string): boolean {
|
|
82
|
-
return deepRisk(text, !/\.[cm]?ts$/.test(path));
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function formatChunk(inputs: FileInput[], run: FormatRun, inWorker: boolean) {
|
|
86
|
-
const warnings = { unread: new Set<string>(), unreadWidth: new Set<string>() };
|
|
87
|
-
const outcomes = inputs.map((input) => {
|
|
88
|
-
if (hardLinked(input, run)) return null;
|
|
89
|
-
|
|
90
|
-
const text = readText(input.path);
|
|
91
|
-
if (inWorker && typeof text === "string" && tooDeep(input.path, text)) return null;
|
|
92
|
-
|
|
93
|
-
return formatFile({ ...input, read: () => text }, run, input.output, warnings);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
return { outcomes, unread: [...warnings.unread], unreadWidth: [...warnings.unreadWidth] };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function acceptChunk<Input, Outcome, Result extends { outcomes: (Outcome | null)[] }, Job>(
|
|
100
|
-
inputCount: number,
|
|
101
|
-
chunkSize: number,
|
|
102
|
-
message: ChunkMessage<Result>,
|
|
103
|
-
chunks: (Outcome | null)[][],
|
|
104
|
-
task: PoolTask<Input, Outcome, Result, Job>,
|
|
105
|
-
errors: string[],
|
|
106
|
-
): void {
|
|
107
|
-
if ("error" in message) {
|
|
108
|
-
errors.push(message.error);
|
|
109
|
-
if (message.chunk === undefined) return;
|
|
110
|
-
|
|
111
|
-
const size = Math.min(chunkSize, inputCount - message.chunk * chunkSize);
|
|
112
|
-
chunks[message.chunk] = Array<null>(size).fill(null);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
chunks[message.chunk] = message.outcomes;
|
|
117
|
-
task.merge(message);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function recoverAbandoned<Outcome>(
|
|
121
|
-
inputCount: number,
|
|
122
|
-
chunkSize: number,
|
|
123
|
-
chunks: (Outcome | null)[][],
|
|
124
|
-
claims: Int32Array,
|
|
125
|
-
stopped: boolean[],
|
|
126
|
-
errors: string[],
|
|
127
|
-
): number {
|
|
128
|
-
let recovered = 0;
|
|
129
|
-
for (let chunk = 0; chunk < claims.length; chunk++) {
|
|
130
|
-
const owner = Atomics.load(claims, chunk);
|
|
131
|
-
if (chunks[chunk] !== undefined || owner === 0 || !stopped[owner - 1]) continue;
|
|
132
|
-
|
|
133
|
-
const size = Math.min(chunkSize, inputCount - chunk * chunkSize);
|
|
134
|
-
chunks[chunk] = Array<null>(size).fill(null);
|
|
135
|
-
errors.push("a worker thread stopped before reporting its files, so main formatted them");
|
|
136
|
-
recovered++;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return recovered;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export function runPooled<Input, Outcome, Result extends { outcomes: (Outcome | null)[] }, Job>(
|
|
143
|
-
inputs: Input[],
|
|
144
|
-
task: PoolTask<Input, Outcome, Result, Job>,
|
|
145
|
-
count: number,
|
|
146
|
-
): { outcomes: Outcome[]; errors: string[] } {
|
|
147
|
-
const { Worker, MessageChannel, receiveMessageOnPort } =
|
|
148
|
-
require("node:worker_threads") as typeof import("node:worker_threads");
|
|
149
|
-
|
|
150
|
-
const counter = new Int32Array(new SharedArrayBuffer(4));
|
|
151
|
-
const signal = new Int32Array(new SharedArrayBuffer(4));
|
|
152
|
-
|
|
153
|
-
const chunkSize = task.chunkSize ?? CHUNK_SIZE;
|
|
154
|
-
const chunkCount = Math.ceil(inputs.length / chunkSize);
|
|
155
|
-
const claims = new Int32Array(new SharedArrayBuffer(4 * chunkCount));
|
|
156
|
-
const finished = new Int32Array(new SharedArrayBuffer(4 * Math.max(count, 1)));
|
|
157
|
-
const chunks: (Outcome | null)[][] = Array(chunkCount);
|
|
158
|
-
|
|
159
|
-
const errors: string[] = [];
|
|
160
|
-
const ports: MessagePort[] = [];
|
|
161
|
-
const deserializers = deserializerFiles();
|
|
162
|
-
for (let index = 0; index < count; index++) {
|
|
163
|
-
const { port1, port2 } = new MessageChannel();
|
|
164
|
-
try {
|
|
165
|
-
const workerData: PoolData<Input, Job> = {
|
|
166
|
-
id: index,
|
|
167
|
-
inputs,
|
|
168
|
-
job: task.job,
|
|
169
|
-
chunkSize,
|
|
170
|
-
counter: counter.buffer as SharedArrayBuffer,
|
|
171
|
-
signal: signal.buffer as SharedArrayBuffer,
|
|
172
|
-
claims: claims.buffer as SharedArrayBuffer,
|
|
173
|
-
finished: finished.buffer as SharedArrayBuffer,
|
|
174
|
-
port: port2,
|
|
175
|
-
addon: process.env.NAPI_RS_NATIVE_LIBRARY_PATH,
|
|
176
|
-
deserializers,
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
const worker = new Worker(task.worker, { workerData, transferList: [port2] });
|
|
180
|
-
worker.on("error", (error: Error) => errors.push(error.message));
|
|
181
|
-
worker.unref();
|
|
182
|
-
ports.push(port1);
|
|
183
|
-
} catch (error: unknown) {
|
|
184
|
-
errors.push(error instanceof Error ? error.message : String(error));
|
|
185
|
-
port1.close();
|
|
186
|
-
port2.close();
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
let completed = 0;
|
|
191
|
-
while (completed < chunkCount) {
|
|
192
|
-
const observed = Atomics.load(signal, 0);
|
|
193
|
-
const stopped = Array.from(finished, (_, index) => Atomics.load(finished, index) === 1);
|
|
194
|
-
|
|
195
|
-
for (const port of ports) {
|
|
196
|
-
let received: ReturnType<typeof receiveMessageOnPort>;
|
|
197
|
-
while ((received = receiveMessageOnPort(port)) !== undefined) {
|
|
198
|
-
const message = received.message as ChunkMessage<Result>;
|
|
199
|
-
acceptChunk(inputs.length, chunkSize, message, chunks, task, errors);
|
|
200
|
-
if (message.chunk !== undefined) completed++;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const chunk = Atomics.add(counter, 0, 1);
|
|
205
|
-
if (chunk < chunkCount) {
|
|
206
|
-
try {
|
|
207
|
-
const result = task.chunk(inputs.slice(chunk * chunkSize, (chunk + 1) * chunkSize));
|
|
208
|
-
acceptChunk(inputs.length, chunkSize, { chunk, ...result }, chunks, task, errors);
|
|
209
|
-
} catch (error: unknown) {
|
|
210
|
-
acceptChunk(
|
|
211
|
-
inputs.length,
|
|
212
|
-
chunkSize,
|
|
213
|
-
{ chunk, error: error instanceof Error ? error.message : String(error) },
|
|
214
|
-
chunks,
|
|
215
|
-
task,
|
|
216
|
-
errors,
|
|
217
|
-
);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
completed++;
|
|
221
|
-
continue;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
completed += recoverAbandoned(inputs.length, chunkSize, chunks, claims, stopped, errors);
|
|
225
|
-
if (completed < chunkCount) Atomics.wait(signal, 0, observed, 50);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const outcomes: Outcome[] = [];
|
|
229
|
-
for (let chunk = 0; chunk < chunks.length; chunk++)
|
|
230
|
-
for (const [offset, outcome] of chunks[chunk]!.entries()) {
|
|
231
|
-
const input = inputs[chunk * chunkSize + offset]!;
|
|
232
|
-
outcomes.push(outcome ?? task.item(input));
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
for (const port of ports) port.close();
|
|
236
|
-
|
|
237
|
-
return { outcomes, errors };
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
export function serveChunks<Input, Job, Result>(work: (inputs: Input[], job: Job) => Result): void {
|
|
241
|
-
const data = workerData as PoolData<Input, Job>;
|
|
242
|
-
const counter = new Int32Array(data.counter);
|
|
243
|
-
const signal = new Int32Array(data.signal);
|
|
244
|
-
const claims = new Int32Array(data.claims);
|
|
245
|
-
const finished = new Int32Array(data.finished);
|
|
246
|
-
function post(message: ChunkMessage<Result>): void {
|
|
247
|
-
data.port.postMessage(message);
|
|
248
|
-
Atomics.add(signal, 0, 1);
|
|
249
|
-
Atomics.notify(signal, 0);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
try {
|
|
253
|
-
if (data.addon !== undefined) process.env.NAPI_RS_NATIVE_LIBRARY_PATH = data.addon;
|
|
254
|
-
embedDeserializers(data.deserializers);
|
|
255
|
-
|
|
256
|
-
const chunkCount = Math.ceil(data.inputs.length / data.chunkSize);
|
|
257
|
-
for (
|
|
258
|
-
let chunk = Atomics.add(counter, 0, 1);
|
|
259
|
-
chunk < chunkCount;
|
|
260
|
-
chunk = Atomics.add(counter, 0, 1)
|
|
261
|
-
) {
|
|
262
|
-
Atomics.store(claims, chunk, data.id + 1);
|
|
263
|
-
|
|
264
|
-
try {
|
|
265
|
-
const result = work(
|
|
266
|
-
data.inputs.slice(chunk * data.chunkSize, (chunk + 1) * data.chunkSize),
|
|
267
|
-
data.job,
|
|
268
|
-
);
|
|
269
|
-
|
|
270
|
-
post({ chunk, ...result });
|
|
271
|
-
} catch (error: unknown) {
|
|
272
|
-
post({ chunk, error: error instanceof Error ? error.message : String(error) });
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
} catch (error: unknown) {
|
|
276
|
-
post({ chunk: undefined, error: error instanceof Error ? error.message : String(error) });
|
|
277
|
-
} finally {
|
|
278
|
-
Atomics.store(finished, data.id, 1);
|
|
279
|
-
Atomics.add(signal, 0, 1);
|
|
280
|
-
Atomics.notify(signal, 0);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
export function formatPooled(
|
|
285
|
-
inputs: FileInput[],
|
|
286
|
-
run: FormatRun,
|
|
287
|
-
count: number,
|
|
288
|
-
): { formatted: Formatted; errors: string[] } {
|
|
289
|
-
const warnings = { unread: new Set<string>(), unreadWidth: new Set<string>() };
|
|
290
|
-
// A compiled Bun worker resolves a bare specifier inside bunfs, not a file URL.
|
|
291
|
-
const specifier = Bun.main.includes("/$bunfs/")
|
|
292
|
-
? "./worker.ts"
|
|
293
|
-
: new URL("./worker.ts", import.meta.url);
|
|
294
|
-
|
|
295
|
-
const { outcomes, errors } = runPooled(
|
|
296
|
-
inputs,
|
|
297
|
-
{
|
|
298
|
-
worker: specifier,
|
|
299
|
-
job: run,
|
|
300
|
-
chunk: (inputs) => formatChunk(inputs, run, false),
|
|
301
|
-
merge(result) {
|
|
302
|
-
for (const path of result.unread) warnings.unread.add(path);
|
|
303
|
-
for (const path of result.unreadWidth) warnings.unreadWidth.add(path);
|
|
304
|
-
},
|
|
305
|
-
item: (input): FileOutcome =>
|
|
306
|
-
formatFile({ ...input, read: () => readText(input.path) }, run, input.output, warnings),
|
|
307
|
-
},
|
|
308
|
-
count,
|
|
309
|
-
);
|
|
310
|
-
|
|
311
|
-
return { formatted: mergeOutcomes(outcomes, warnings), errors };
|
|
312
|
-
}
|
package/src/start.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
|
-
import { hookCall } from "./hook.ts";
|
|
3
|
-
|
|
4
|
-
type Writer = (chunk: string | Uint8Array) => void;
|
|
5
|
-
|
|
6
|
-
export interface Io {
|
|
7
|
-
cwd: string;
|
|
8
|
-
env: Readonly<Record<string, string | undefined>>;
|
|
9
|
-
stdin: () => Uint8Array;
|
|
10
|
-
stdout: Writer;
|
|
11
|
-
stderr: Writer;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function ignoreBrokenPipe(error: NodeJS.ErrnoException): void {
|
|
15
|
-
if (error.code !== "EPIPE") throw error;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export function systemIo(): Io {
|
|
19
|
-
// Under Bun a stream write throws EPIPE into a stack trace and exit 1, where console.log swallowed it.
|
|
20
|
-
process.stdout.on("error", ignoreBrokenPipe);
|
|
21
|
-
process.stderr.on("error", ignoreBrokenPipe);
|
|
22
|
-
|
|
23
|
-
return {
|
|
24
|
-
cwd: process.cwd(),
|
|
25
|
-
env: process.env,
|
|
26
|
-
stdin: () => readFileSync(0),
|
|
27
|
-
stdout: (chunk) => {
|
|
28
|
-
process.stdout.write(chunk);
|
|
29
|
-
},
|
|
30
|
-
stderr: (chunk) => {
|
|
31
|
-
process.stderr.write(chunk);
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export async function start(argv: string[]): Promise<number> {
|
|
37
|
-
const io = systemIo();
|
|
38
|
-
if (argv[0] === "hook") {
|
|
39
|
-
const args = argv.slice(1);
|
|
40
|
-
const call = hookCall(args, io);
|
|
41
|
-
if (typeof call === "number") return call;
|
|
42
|
-
|
|
43
|
-
const { runHookCall } = await import("./cli.ts");
|
|
44
|
-
return runHookCall(call, args, io);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const { main } = await import("./cli.ts");
|
|
48
|
-
return main(argv, io);
|
|
49
|
-
}
|
package/src/step.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { dirname, extname } from "node:path";
|
|
2
|
-
import { isGeneratedHeader } from "./files.ts";
|
|
3
|
-
import { processFile } from "./engine/index.ts";
|
|
4
|
-
import type { Changed } from "./engine/model.ts";
|
|
5
|
-
import { languageOf } from "./languages/index.ts";
|
|
6
|
-
import { FALLBACK_WIDTH, type Width } from "./languages/language.ts";
|
|
7
|
-
import type { Braces, FileResult, Finding, Mode, Options } from "./engine/types.ts";
|
|
8
|
-
|
|
9
|
-
export type Decoded = string | { message: string };
|
|
10
|
-
|
|
11
|
-
export interface StepOptions {
|
|
12
|
-
mode: Mode;
|
|
13
|
-
braces?: Braces | undefined;
|
|
14
|
-
changedLines?: Changed | undefined;
|
|
15
|
-
unread?: Set<string>;
|
|
16
|
-
unreadWidth?: Set<string>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface StepResult {
|
|
20
|
-
findings: Finding[];
|
|
21
|
-
fixed: string | undefined;
|
|
22
|
-
parseError: boolean;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const utf8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
26
|
-
const bom = "";
|
|
27
|
-
|
|
28
|
-
export function decode(bytes: Uint8Array): Decoded {
|
|
29
|
-
try {
|
|
30
|
-
return utf8.decode(bytes);
|
|
31
|
-
} catch {
|
|
32
|
-
return { message: "not valid UTF-8, left untouched" };
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function withoutMark(text: string): string {
|
|
37
|
-
return text.startsWith(bom) ? text.slice(bom.length) : text;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function keepBraces(path: string, braces?: Braces, unread?: Set<string>): boolean {
|
|
41
|
-
if (braces !== undefined) return braces === "off";
|
|
42
|
-
|
|
43
|
-
const setting = languageOf(path).config?.setting(dirname(path), extname(path)) ?? {
|
|
44
|
-
enforced: false,
|
|
45
|
-
unread: [],
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
for (const file of setting.unread) unread?.add(file);
|
|
49
|
-
return setting.enforced;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export function stepSettings(
|
|
53
|
-
path: string,
|
|
54
|
-
options: { braces?: Braces; unread?: Set<string>; unreadWidth?: Set<string> } = {},
|
|
55
|
-
): { keepBraces: boolean; width: Width } {
|
|
56
|
-
const language = languageOf(path);
|
|
57
|
-
const width = language.config?.width(path) ?? FALLBACK_WIDTH;
|
|
58
|
-
for (const file of width.unread) options.unreadWidth?.add(file);
|
|
59
|
-
|
|
60
|
-
return { keepBraces: keepBraces(path, options.braces, options.unread), width };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function fixText(path: string, text: string, mode: Mode, options: Options): FileResult {
|
|
64
|
-
const body = withoutMark(text);
|
|
65
|
-
const mark = text.slice(0, text.length - body.length);
|
|
66
|
-
const result = processFile(path, body, mode, options);
|
|
67
|
-
return { ...result, text: mark + result.text };
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function failure(path: string, rule: "parse" | "error", message: string): StepResult {
|
|
71
|
-
return {
|
|
72
|
-
findings: [{ path, line: 1, col: 1, rule, message, fixable: false }],
|
|
73
|
-
fixed: undefined,
|
|
74
|
-
parseError: true,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export function formatText(path: string, text: Decoded, options: StepOptions): StepResult {
|
|
79
|
-
if (typeof text !== "string") return failure(path, "parse", text.message);
|
|
80
|
-
|
|
81
|
-
try {
|
|
82
|
-
if (isGeneratedHeader(text)) return { findings: [], fixed: undefined, parseError: false };
|
|
83
|
-
|
|
84
|
-
const result = fixText(path, text, options.mode, {
|
|
85
|
-
...stepSettings(path, options),
|
|
86
|
-
...(options.changedLines === undefined ? {} : { changedLines: options.changedLines }),
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
const changed = options.mode === "fix" && !result.parseError && result.text !== text;
|
|
90
|
-
if (changed) {
|
|
91
|
-
const body = withoutMark(result.text);
|
|
92
|
-
const error =
|
|
93
|
-
body === result.lastParse.text
|
|
94
|
-
? result.lastParse.error
|
|
95
|
-
: languageOf(path).parse(path, body).error;
|
|
96
|
-
|
|
97
|
-
if (error)
|
|
98
|
-
return failure(
|
|
99
|
-
path,
|
|
100
|
-
"error",
|
|
101
|
-
`fixed text does not parse (${error.message}), left untouched`,
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return {
|
|
106
|
-
findings: result.findings,
|
|
107
|
-
fixed: changed ? result.text : undefined,
|
|
108
|
-
parseError: result.parseError,
|
|
109
|
-
};
|
|
110
|
-
} catch (error: unknown) {
|
|
111
|
-
return failure(path, "error", String(error));
|
|
112
|
-
}
|
|
113
|
-
}
|
package/src/usage.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
export const bothBraces = "use one of --braces or --no-braces";
|
|
2
|
-
|
|
3
|
-
export const usage =
|
|
4
|
-
"Usage: stanza (--fix | --check) [--changed [--hunks] | --stdin <path> | [--] <paths...>] [--json] [--braces | --no-braces]\n stanza --check --staged [--hunks] [--json] [--braces | --no-braces]\n stanza explain <file>:<line> [--no-braces]\n stanza hook [--braces | --no-braces] [--hunks]";
|
|
5
|
-
|
|
6
|
-
export const flags: (readonly [string, string])[] = [
|
|
7
|
-
["--fix", "apply every deterministic rule in place"],
|
|
8
|
-
["--check", "report only, change nothing"],
|
|
9
|
-
["--changed", "files from `git diff --name-only HEAD` plus untracked files"],
|
|
10
|
-
["--hunks", "with --changed or --staged, only gaps and blocks touching changed lines"],
|
|
11
|
-
["--staged", "the staged content of staged files, for pre-commit"],
|
|
12
|
-
[
|
|
13
|
-
"--stdin <path>",
|
|
14
|
-
"source on stdin; --fix: fixed text on stdout, findings on stderr; --check: findings on stdout",
|
|
15
|
-
],
|
|
16
|
-
["--json", "findings as a JSON array"],
|
|
17
|
-
["--braces", "turn on the braces rule even when lint config turns it off or cannot be read"],
|
|
18
|
-
["--no-braces", "turn off the braces rule, keep the blank line rules"],
|
|
19
|
-
["explain <file>:<line>", "which rule decides the gap or braced body at that line, and why"],
|
|
20
|
-
[
|
|
21
|
-
"hook",
|
|
22
|
-
"Stop and SubagentStop; PostToolUse on Edit, MultiEdit and Codex apply_patch; PreToolUse on Write; reads JSON on stdin",
|
|
23
|
-
],
|
|
24
|
-
["--help", "usage, flags and the rule catalog"],
|
|
25
|
-
["--version", "the version, and for a built binary the commit it was built from"],
|
|
26
|
-
];
|
|
27
|
-
|
|
28
|
-
export function columns(rows: readonly (readonly string[])[]): string[] {
|
|
29
|
-
const widths = rows[0]!.map((_, index) => Math.max(...rows.map((row) => row[index]!.length)));
|
|
30
|
-
return rows.map((row) =>
|
|
31
|
-
row
|
|
32
|
-
.map((cell, index) => (index < row.length - 1 ? cell.padEnd(widths[index]!) : cell))
|
|
33
|
-
.join(" "),
|
|
34
|
-
);
|
|
35
|
-
}
|
package/src/worker.ts
DELETED