@ryuugg/stanza 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.
Files changed (50) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +163 -0
  3. package/package.json +63 -0
  4. package/src/cli.ts +820 -0
  5. package/src/compile/darwin-arm64.ts +3 -0
  6. package/src/compile/darwin-x64.ts +3 -0
  7. package/src/compile/linux-arm64-musl.ts +3 -0
  8. package/src/compile/linux-arm64.ts +3 -0
  9. package/src/compile/linux-x64-musl.ts +3 -0
  10. package/src/compile/linux-x64.ts +3 -0
  11. package/src/compiled.ts +6 -0
  12. package/src/engine/attach.ts +62 -0
  13. package/src/engine/directives.ts +154 -0
  14. package/src/engine/doc.ts +93 -0
  15. package/src/engine/edits.ts +79 -0
  16. package/src/engine/explain.ts +333 -0
  17. package/src/engine/gaps.ts +329 -0
  18. package/src/engine/hunks.ts +320 -0
  19. package/src/engine/index.ts +254 -0
  20. package/src/engine/model.ts +102 -0
  21. package/src/engine/rules.ts +103 -0
  22. package/src/engine/types.ts +37 -0
  23. package/src/files.ts +822 -0
  24. package/src/gitignore.ts +279 -0
  25. package/src/hook.ts +282 -0
  26. package/src/languages/index.ts +32 -0
  27. package/src/languages/javascript/ast.ts +199 -0
  28. package/src/languages/javascript/braces.ts +374 -0
  29. package/src/languages/javascript/config/biome.ts +112 -0
  30. package/src/languages/javascript/config/eslint.ts +128 -0
  31. package/src/languages/javascript/config/evaluate.ts +278 -0
  32. package/src/languages/javascript/config/find.ts +108 -0
  33. package/src/languages/javascript/config/glob.ts +150 -0
  34. package/src/languages/javascript/config/index.ts +48 -0
  35. package/src/languages/javascript/config/layers.ts +425 -0
  36. package/src/languages/javascript/config/module.ts +326 -0
  37. package/src/languages/javascript/config/oxlint.ts +170 -0
  38. package/src/languages/javascript/config/width.ts +715 -0
  39. package/src/languages/javascript/entry.ts +39 -0
  40. package/src/languages/javascript/facts.ts +268 -0
  41. package/src/languages/javascript/index.ts +20 -0
  42. package/src/languages/javascript/lists.ts +49 -0
  43. package/src/languages/javascript/oracle.ts +191 -0
  44. package/src/languages/javascript/parse.ts +58 -0
  45. package/src/languages/javascript/scan.ts +68 -0
  46. package/src/languages/language.ts +109 -0
  47. package/src/launcher +21 -0
  48. package/src/native-file.d.ts +4 -0
  49. package/src/step.ts +108 -0
  50. package/src/usage.ts +30 -0
@@ -0,0 +1,48 @@
1
+ import { resolve } from "node:path";
2
+ import { biome } from "./biome.ts";
3
+ import { flat, legacy } from "./eslint.ts";
4
+ import { realDirectory } from "./find.ts";
5
+ import type { Decision, Reader } from "./layers.ts";
6
+ import { oxlint } from "./oxlint.ts";
7
+
8
+ const READERS: Reader[] = [flat, legacy, biome, oxlint];
9
+
10
+ export interface BracesSetting {
11
+ enforced: boolean;
12
+ unread: string[];
13
+ }
14
+
15
+ const cache = new Map<string, BracesSetting>();
16
+
17
+ export function braceDecisions(dir: string, extension?: string): Decision[] {
18
+ dir = realDirectory(resolve(dir));
19
+ return READERS.map((reader) => reader.decide(dir, extension)).filter(
20
+ (decision) => decision !== null,
21
+ );
22
+ }
23
+
24
+ export function bracesSetting(dir: string, extension?: string): BracesSetting {
25
+ const requested = `${dir}\0${extension ?? ""}`;
26
+ const cached = cache.get(requested);
27
+ if (cached !== undefined) return cached;
28
+
29
+ let result: BracesSetting = { enforced: true, unread: [] };
30
+ try {
31
+ const decisions = braceDecisions(dir, extension);
32
+ const enforced = decisions.some((decision) => decision.setting !== "off");
33
+ const unread = decisions.some((decision) => decision.setting === "on")
34
+ ? []
35
+ : decisions
36
+ .filter((decision) => decision.setting === "unknown")
37
+ .flatMap((decision) => decision.files);
38
+
39
+ result = { enforced, unread };
40
+ } catch {}
41
+
42
+ cache.set(requested, result);
43
+ return result;
44
+ }
45
+
46
+ export function bracesEnforced(dir: string, extension?: string): boolean {
47
+ return bracesSetting(dir, extension).enforced;
48
+ }
@@ -0,0 +1,425 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { basename, dirname, relative, sep } from "node:path";
3
+ import type { Node } from "oxc-parser";
4
+ import { walk } from "../ast.ts";
5
+ import {
6
+ UNKNOWN,
7
+ NO_SETTING,
8
+ type Value,
9
+ evaluate,
10
+ exported,
11
+ known,
12
+ object,
13
+ origin,
14
+ property,
15
+ } from "./evaluate.ts";
16
+ import { configDirectories, resolveModule, type Loader } from "./find.ts";
17
+ import { globReach } from "./glob.ts";
18
+ import { moduleAt, type ConfigModule } from "./module.ts";
19
+
20
+ export type Setting = "on" | "off" | "unknown";
21
+ export type Reach = "all" | "none" | "some";
22
+ export type Coverage = Reach | ((dir: string, extension?: string) => Reach);
23
+ type Family = "flat" | "legacy" | "biome" | "oxlint";
24
+
25
+ export interface Layer {
26
+ readonly reach: Coverage;
27
+ readonly setting: Setting;
28
+ }
29
+
30
+ export interface Decision {
31
+ family: Family;
32
+ setting: Setting;
33
+ files: string[];
34
+ }
35
+
36
+ export interface Reader {
37
+ family: Family;
38
+ files: readonly string[];
39
+ candidate?(file: string): boolean;
40
+ backstop: readonly string[];
41
+ backstopImports: boolean;
42
+ layers(value: Value, context: Context): Layer[];
43
+ decide(dir: string, extension: string | undefined): Decision | null;
44
+ }
45
+
46
+ export interface Tree {
47
+ scope(value: Value, context: Context): Coverage;
48
+ own(value: Value, context: Context): Layer[];
49
+ extend(entry: string, parent: Value, context: Context): Layer[];
50
+ }
51
+
52
+ export type Mode = "anchored" | "legacy" | "biome1";
53
+
54
+ export interface Found {
55
+ setting: Setting | null;
56
+ root: Value;
57
+ file: string;
58
+ }
59
+
60
+ export interface Context {
61
+ reader: Reader;
62
+ file: string;
63
+ shared: boolean;
64
+ chain: string[];
65
+ consumed: Set<Node>;
66
+ ancestors: Value[];
67
+ modules: Set<ConfigModule>;
68
+ work: {
69
+ count: number;
70
+ exceeded: boolean;
71
+ cycles: number;
72
+ memo: Map<Value, Map<string, Layer[]>>;
73
+ };
74
+ }
75
+
76
+ export const UNKNOWN_LAYER: Layer = { reach: "all", setting: "unknown" };
77
+ const EXPANSION_BUDGET = 100000;
78
+
79
+ const layerCache = new Map<string, { layers: Layer[]; root: Value }>();
80
+
81
+ export function lastLayers(layers: Layer[]): Layer[] {
82
+ const seen = new Set<Layer>();
83
+ const result: Layer[] = [];
84
+ for (let index = layers.length - 1; index >= 0; index--) {
85
+ const layer = layers[index]!;
86
+ if (seen.has(layer)) continue;
87
+
88
+ seen.add(layer);
89
+ result.push(layer);
90
+ }
91
+
92
+ return result.reverse();
93
+ }
94
+
95
+ export function expanded(
96
+ value: Value,
97
+ context: Context,
98
+ phase: string,
99
+ produce: () => Layer[],
100
+ ): Layer[] {
101
+ const work = context.work;
102
+ if (work.exceeded) return [UNKNOWN_LAYER];
103
+
104
+ const key = JSON.stringify([context.file, context.shared, context.chain, phase]);
105
+ const cached = work.memo.get(value)?.get(key);
106
+ if (cached) return cached;
107
+
108
+ const cycles = work.cycles;
109
+ const result = produce();
110
+
111
+ work.count += result.length + 1;
112
+ if (work.count > EXPANSION_BUDGET) work.exceeded = true;
113
+ if (work.exceeded) return [UNKNOWN_LAYER];
114
+
115
+ if (work.cycles === cycles) {
116
+ let entries = work.memo.get(value);
117
+ if (!entries) {
118
+ entries = new Map();
119
+ work.memo.set(value, entries);
120
+ }
121
+
122
+ entries.set(key, result);
123
+ }
124
+
125
+ return result;
126
+ }
127
+
128
+ export function levelOf(value: Value): Setting {
129
+ const seen = new Set<Value>();
130
+ while (!seen.has(value)) {
131
+ if (value === "off" || value === 0) return "off";
132
+ if (typeof value === "string" || typeof value === "number") return "on";
133
+
134
+ seen.add(value);
135
+
136
+ if (Array.isArray(value)) value = value[0];
137
+ else if (object(value)) value = property(value, "level");
138
+ else return "unknown";
139
+ }
140
+
141
+ return "unknown";
142
+ }
143
+
144
+ function intersect(left: Reach, right: Reach): Reach {
145
+ if (left === "none" || right === "none") return "none";
146
+ return left === "all" ? right : right === "all" ? left : "some";
147
+ }
148
+
149
+ function reachAt(coverage: Coverage, dir: string, extension?: string): Reach {
150
+ return typeof coverage === "function" ? coverage(dir, extension) : coverage;
151
+ }
152
+
153
+ export function intersectCoverage(left: Coverage, right: Coverage): Coverage {
154
+ if (left === "all") return right;
155
+ if (right === "all") return left;
156
+ if (left === "none" || right === "none") return "none";
157
+ return (dir, extension) =>
158
+ intersect(reachAt(left, dir, extension), reachAt(right, dir, extension));
159
+ }
160
+
161
+ function fold(layers: Layer[], dir: string, extension?: string): Setting | null {
162
+ let setting: Setting | null = null;
163
+ for (const layer of layers) {
164
+ const reach = reachAt(layer.reach, dir, extension);
165
+ if (reach === "all" || (reach === "some" && layer.setting !== "off")) setting = layer.setting;
166
+ }
167
+
168
+ return setting;
169
+ }
170
+
171
+ export function read(value: Value, key: string, context: Context): Value {
172
+ const node = origin(value, key);
173
+ if (node) context.consumed.add(node);
174
+ return property(value, key);
175
+ }
176
+
177
+ export function patterns(value: Value, context: Context, mode: Mode): Coverage {
178
+ if (value === undefined) return "all";
179
+ if (!Array.isArray(value) || !value.every((item): item is string => typeof item === "string"))
180
+ return "some";
181
+
182
+ if (context.shared) return "some";
183
+ if (value.length === 0) return "none";
184
+ if (mode === "biome1") return "some";
185
+
186
+ const anchor = dirname(context.file);
187
+ return (dir, extension) =>
188
+ globReach(value, relative(anchor, dir).split(sep).join("/"), mode === "legacy", extension);
189
+ }
190
+
191
+ export function excluding(
192
+ reach: Coverage,
193
+ exclusion: Value,
194
+ context: Context,
195
+ mode: Mode,
196
+ ): Coverage {
197
+ if (exclusion === undefined) return reach;
198
+
199
+ const excluded = patterns(exclusion, context, mode);
200
+ return (dir, extension) => {
201
+ const included = reachAt(reach, dir, extension);
202
+ return included === "all" && reachAt(excluded, dir, extension) !== "none" ? "some" : included;
203
+ };
204
+ }
205
+
206
+ function inherit(entry: Value, parent: Value, context: Context, hooks: Tree): Layer[] {
207
+ if (typeof entry !== "string") return context.reader.layers(entry, { ...context, shared: true });
208
+
209
+ const preset = known(entry);
210
+ if (preset !== undefined) return context.reader.layers(preset, { ...context, shared: true });
211
+
212
+ return hooks.extend(entry, parent, context);
213
+ }
214
+
215
+ export function resolved(specifier: string, context: Context, loader: Loader): Layer[] {
216
+ const file = resolveModule(specifier, context.file, loader);
217
+ return file ? fileLayers(file, { ...context, shared: true }).layers : [UNKNOWN_LAYER];
218
+ }
219
+
220
+ export function tree(value: Value, context: Context, hooks: Tree): Layer[] {
221
+ if (value === NO_SETTING) return [];
222
+ if (context.work.exceeded) return [UNKNOWN_LAYER];
223
+ if (context.ancestors.includes(value)) {
224
+ context.work.cycles++;
225
+ return [UNKNOWN_LAYER];
226
+ }
227
+
228
+ return expanded(value, context, "tree", () => {
229
+ const next: Context = { ...context, ancestors: [...context.ancestors, value] };
230
+ if (Array.isArray(value)) return lastLayers(value.flatMap((item) => tree(item, next, hooks)));
231
+ if (!object(value)) return [UNKNOWN_LAYER];
232
+
233
+ const reach = hooks.scope(value, next);
234
+ const inherited = property(value, "extends");
235
+ const entries: Value[] =
236
+ inherited === undefined ? [] : Array.isArray(inherited) ? inherited : [inherited];
237
+
238
+ const result = entries.flatMap((entry) => inherit(entry, value, next, hooks));
239
+ result.push(...hooks.own(value, next));
240
+
241
+ const scoped = lastLayers(result).map((layer) => ({
242
+ ...layer,
243
+ reach: intersectCoverage(layer.reach, reach),
244
+ }));
245
+
246
+ const overrides = property(value, "overrides");
247
+ if (overrides !== undefined) {
248
+ const items: Value[] = Array.isArray(overrides) ? overrides : [UNKNOWN];
249
+ scoped.push(
250
+ ...lastLayers(items.flatMap((item) => tree(item, next, hooks))).map((layer) => ({
251
+ ...layer,
252
+ reach: intersectCoverage(reach, layer.reach),
253
+ })),
254
+ );
255
+ }
256
+
257
+ return scoped;
258
+ });
259
+ }
260
+
261
+ function yamlSetting(text: string): Setting | null {
262
+ const lines = text.split(/\r?\n/).map((line) => line.replace(/\s#.*$/, "").trimEnd());
263
+ const index = lines.findIndex((line) => /^\s*["']?curly["']?\s*:/.test(line));
264
+
265
+ if (lines.some((line, at) => at !== index && /\bcurly\b/.test(line))) return "unknown";
266
+
267
+ let setting: Setting | null = null;
268
+ if (index !== -1) {
269
+ let value = lines[index]!.replace(/^[^:]*:\s*/, "");
270
+ if (value === "")
271
+ value =
272
+ lines
273
+ .slice(index + 1)
274
+ .find((line) => line.trim() !== "")
275
+ ?.trim() ?? "";
276
+
277
+ const first = value
278
+ .replace(/[[\]]/g, "")
279
+ .replace(/^[-\s]+/, "")
280
+ .split(",")[0]!
281
+ .trim()
282
+ .replace(/^["']|["']$/g, "");
283
+
284
+ setting = first === "off" || first === "0" ? "off" : "on";
285
+ }
286
+
287
+ return setting !== "on" && lines.some((line) => /^(extends|overrides)\s*:/.test(line))
288
+ ? "unknown"
289
+ : setting;
290
+ }
291
+
292
+ function backstop(module: ConfigModule, context: Context, result: Layer[]): void {
293
+ if (!module.program) return;
294
+
295
+ walk(module.program, (node) => {
296
+ if (node.type !== "Property" || context.consumed.has(node)) return;
297
+
298
+ const key =
299
+ node.key.type === "Identifier"
300
+ ? node.key.name
301
+ : node.key.type === "Literal"
302
+ ? node.key.value
303
+ : null;
304
+
305
+ if (
306
+ typeof key === "string" &&
307
+ context.reader.backstop.includes(key) &&
308
+ levelOf(evaluate(node.value, module, context.chain)) !== "off"
309
+ )
310
+ result.push(UNKNOWN_LAYER);
311
+ });
312
+ }
313
+
314
+ export function fileLayers(file: string, context: Context): { layers: Layer[]; root: Value } {
315
+ const module = moduleAt(file);
316
+ if (!module || context.chain.includes(module.file) || context.chain.length >= 32)
317
+ return { layers: [UNKNOWN_LAYER], root: undefined };
318
+
319
+ const key = JSON.stringify([file, dirname(file), context.reader.family, context.shared]);
320
+ const cached = layerCache.get(key);
321
+ if (cached) return cached;
322
+
323
+ const cycles = context.work.cycles;
324
+ const result = readFileLayers(file, module, context);
325
+ if (!context.work.exceeded && context.work.cycles === cycles) layerCache.set(key, result);
326
+ return result;
327
+ }
328
+
329
+ const manifestConfigs = new Map<string, Value>();
330
+
331
+ export function eslintConfig(manifest: string): Value {
332
+ if (manifestConfigs.has(manifest)) return manifestConfigs.get(manifest);
333
+
334
+ let config: Value;
335
+ try {
336
+ const text = readFileSync(manifest, "utf8").replace(/^\uFEFF/, "");
337
+ config = property(JSON.parse(text) as Value, "eslintConfig");
338
+ } catch {
339
+ config = UNKNOWN;
340
+ }
341
+
342
+ manifestConfigs.set(manifest, config);
343
+ return config;
344
+ }
345
+
346
+ function readFileLayers(
347
+ file: string,
348
+ module: ConfigModule,
349
+ context: Context,
350
+ ): { layers: Layer[]; root: Value } {
351
+ const next = { ...context, file, chain: [...context.chain, module.file] };
352
+ next.modules.add(module);
353
+
354
+ if (module.yaml) {
355
+ const setting = yamlSetting(module.text);
356
+ return {
357
+ layers: setting === null ? [] : [{ reach: "all", setting }],
358
+ root: /^root:\s*true\s*$/m.test(module.text),
359
+ };
360
+ }
361
+
362
+ const manifest = context.reader.family === "legacy" && basename(file) === "package.json";
363
+ const value = manifest ? eslintConfig(file) : exported(module.file, "default", context.chain);
364
+
365
+ const result = [...context.reader.layers(value, next)];
366
+ if (manifest) return { layers: result, root: property(value, "root") };
367
+
368
+ if (context.reader.backstopImports)
369
+ for (const candidate of next.modules)
370
+ backstop(candidate, { ...next, file: candidate.file, chain: [] }, result);
371
+ else backstop(module, next, result);
372
+
373
+ return { layers: result, root: property(value, "root") };
374
+ }
375
+
376
+ export function configAt(
377
+ file: string,
378
+ dir: string,
379
+ extension: string | undefined,
380
+ reader: Reader,
381
+ ): Found {
382
+ const work = {
383
+ count: 0,
384
+ exceeded: false,
385
+ cycles: 0,
386
+ memo: new Map<Value, Map<string, Layer[]>>(),
387
+ };
388
+
389
+ const result = fileLayers(file, {
390
+ reader,
391
+ file,
392
+ shared: false,
393
+ chain: [],
394
+ consumed: new Set(),
395
+ ancestors: [],
396
+ modules: new Set(),
397
+ work,
398
+ });
399
+
400
+ return {
401
+ setting: work.exceeded ? "unknown" : fold(result.layers, dir, extension),
402
+ root: result.root,
403
+ file,
404
+ };
405
+ }
406
+
407
+ export function decided(reader: Reader, found: Found | null): Decision | null {
408
+ return found && found.setting !== null
409
+ ? { family: reader.family, setting: found.setting, files: [found.file] }
410
+ : null;
411
+ }
412
+
413
+ export function everyConfig(dir: string, extension: string | undefined, reader: Reader): Found[] {
414
+ return [...configDirectories(dir, reader)].flatMap((files) =>
415
+ files.map((file) => configAt(file, dir, extension, reader)),
416
+ );
417
+ }
418
+
419
+ // ESLint 9 uses the cwd config; ESLint 10 uses the nearest config.
420
+ export function enforcing(found: Found[]): Found | null {
421
+ const nearest = found.find((item) => item.setting !== null);
422
+ return (
423
+ found.find((item) => item.setting === "on" || item.setting === "unknown") ?? nearest ?? null
424
+ );
425
+ }