@ryuugg/stanza 0.3.0 → 0.3.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/package.json +2 -2
- package/src/cli.ts +35 -142
- package/src/compile/darwin-arm64.ts +1 -1
- package/src/compile/darwin-x64.ts +1 -1
- package/src/compile/linux-arm64-musl.ts +1 -1
- package/src/compile/linux-arm64.ts +1 -1
- package/src/compile/linux-x64-musl.ts +1 -1
- package/src/compile/linux-x64.ts +1 -1
- package/src/compiled.ts +3 -0
- package/src/depth.ts +368 -0
- package/src/directories.ts +77 -0
- package/src/engine/gaps.ts +26 -2
- package/src/engine/index.ts +43 -23
- package/src/engine/types.ts +2 -1
- package/src/files.ts +176 -22
- package/src/format.ts +159 -0
- package/src/hook.ts +32 -6
- package/src/languages/javascript/ast.ts +57 -16
- package/src/languages/javascript/config/find.ts +7 -10
- package/src/languages/javascript/config/index.ts +1 -1
- package/src/languages/javascript/config/layers.ts +50 -6
- package/src/languages/javascript/config/module.ts +3 -2
- package/src/languages/javascript/config/width.ts +32 -26
- package/src/languages/javascript/deserializers.ts +17 -0
- package/src/languages/javascript/embedded.ts +7 -0
- package/src/languages/javascript/oxc-raw.d.ts +31 -0
- package/src/languages/javascript/parse.ts +74 -1
- package/src/languages/javascript/raw.ts +95 -0
- package/src/pool.ts +312 -0
- package/src/step.ts +6 -1
- package/src/usage.ts +5 -2
- package/src/worker.ts +4 -0
package/src/format.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
accessSync,
|
|
4
|
+
chmodSync,
|
|
5
|
+
constants,
|
|
6
|
+
readFileSync,
|
|
7
|
+
realpathSync,
|
|
8
|
+
renameSync,
|
|
9
|
+
type Stats,
|
|
10
|
+
statSync,
|
|
11
|
+
unlinkSync,
|
|
12
|
+
writeFileSync,
|
|
13
|
+
} from "node:fs";
|
|
14
|
+
import { dirname, join } from "node:path";
|
|
15
|
+
import { errorCode } from "./files.ts";
|
|
16
|
+
import type { Changed } from "./engine/model.ts";
|
|
17
|
+
import { compareFindings, type Braces, type Finding, type Mode } from "./engine/types.ts";
|
|
18
|
+
import { decode, type Decoded, formatText } from "./step.ts";
|
|
19
|
+
|
|
20
|
+
export interface Input {
|
|
21
|
+
path: string;
|
|
22
|
+
read: () => Decoded;
|
|
23
|
+
changedLines?: Changed | undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface FormatRun {
|
|
27
|
+
mode: Mode;
|
|
28
|
+
braces: Braces | undefined;
|
|
29
|
+
write: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface Warnings {
|
|
33
|
+
unread: Set<string>;
|
|
34
|
+
unreadWidth: Set<string>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface FileOutcome {
|
|
38
|
+
path: string;
|
|
39
|
+
findings: Finding[];
|
|
40
|
+
failed: boolean;
|
|
41
|
+
rewritten?: string;
|
|
42
|
+
kept?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface Formatted extends Warnings {
|
|
46
|
+
findings: Finding[];
|
|
47
|
+
failed: boolean;
|
|
48
|
+
rewritten: string[];
|
|
49
|
+
kept: Map<string, string>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function readText(path: string): Decoded {
|
|
53
|
+
try {
|
|
54
|
+
return decode(readFileSync(path));
|
|
55
|
+
} catch (error: unknown) {
|
|
56
|
+
return { message: String(error) };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function removeQuietly(path: string): void {
|
|
61
|
+
try {
|
|
62
|
+
unlinkSync(path);
|
|
63
|
+
} catch {}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function replaceFile(real: string, text: string, target: Stats): boolean {
|
|
67
|
+
const temp = join(dirname(real), `.stanza-${randomUUID()}.tmp`);
|
|
68
|
+
try {
|
|
69
|
+
writeFileSync(temp, text, { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
70
|
+
} catch (error: unknown) {
|
|
71
|
+
removeQuietly(temp);
|
|
72
|
+
if (["EACCES", "EPERM"].includes(errorCode(error))) return false;
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
const created = statSync(temp);
|
|
78
|
+
if (created.uid !== target.uid || created.gid !== target.gid) {
|
|
79
|
+
removeQuietly(temp);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
chmodSync(temp, target.mode & 0o7777);
|
|
84
|
+
renameSync(temp, real);
|
|
85
|
+
return true;
|
|
86
|
+
} catch (error: unknown) {
|
|
87
|
+
removeQuietly(temp);
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function writeInPlace(real: string, text: string): void {
|
|
93
|
+
const original = readFileSync(real);
|
|
94
|
+
try {
|
|
95
|
+
writeFileSync(real, text, "utf8");
|
|
96
|
+
} catch (error: unknown) {
|
|
97
|
+
try {
|
|
98
|
+
writeFileSync(real, original);
|
|
99
|
+
} catch {}
|
|
100
|
+
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function writeFixed(path: string, text: string, output: string): Finding | undefined {
|
|
106
|
+
try {
|
|
107
|
+
const real = realpathSync(path);
|
|
108
|
+
const target = statSync(real);
|
|
109
|
+
accessSync(real, constants.W_OK);
|
|
110
|
+
|
|
111
|
+
if (target.nlink > 1 || !replaceFile(real, text, target)) writeInPlace(real, text);
|
|
112
|
+
} catch (error: unknown) {
|
|
113
|
+
return { path: output, line: 1, col: 1, rule: "write", message: String(error), fixable: false };
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function formatFile(
|
|
118
|
+
input: Input,
|
|
119
|
+
run: FormatRun,
|
|
120
|
+
output: string,
|
|
121
|
+
warnings: Warnings,
|
|
122
|
+
): FileOutcome {
|
|
123
|
+
const result = formatText(input.path, input.read(), {
|
|
124
|
+
mode: run.mode,
|
|
125
|
+
braces: run.braces,
|
|
126
|
+
changedLines: input.changedLines,
|
|
127
|
+
...warnings,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const outcome: FileOutcome = {
|
|
131
|
+
path: input.path,
|
|
132
|
+
findings: result.findings.map((finding) => ({ ...finding, path: output })),
|
|
133
|
+
failed: result.parseError,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
if (result.fixed === undefined) return outcome;
|
|
137
|
+
if (!run.write) return { ...outcome, kept: result.fixed };
|
|
138
|
+
|
|
139
|
+
const unwritten = writeFixed(input.path, result.fixed, output);
|
|
140
|
+
if (unwritten) return { ...outcome, findings: [...outcome.findings, unwritten], failed: true };
|
|
141
|
+
|
|
142
|
+
return { ...outcome, rewritten: output };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function mergeOutcomes(outcomes: FileOutcome[], warnings: Warnings): Formatted {
|
|
146
|
+
const findings: Finding[] = [];
|
|
147
|
+
const rewritten: string[] = [];
|
|
148
|
+
const kept = new Map<string, string>();
|
|
149
|
+
|
|
150
|
+
let failed = false;
|
|
151
|
+
for (const outcome of outcomes) {
|
|
152
|
+
findings.push(...outcome.findings);
|
|
153
|
+
failed ||= outcome.failed;
|
|
154
|
+
if (outcome.rewritten !== undefined) rewritten.push(outcome.rewritten);
|
|
155
|
+
if (outcome.kept !== undefined) kept.set(outcome.path, outcome.kept);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return { findings: findings.sort(compareFindings), failed, rewritten, kept, ...warnings };
|
|
159
|
+
}
|
package/src/hook.ts
CHANGED
|
@@ -52,6 +52,22 @@ export function hookCall(args: string[], io: Io): HookCall | number {
|
|
|
52
52
|
if (input.event === "edit") return input.paths.some(isCandidateFile) ? input : 0;
|
|
53
53
|
if (input.stopHookActive) return 0;
|
|
54
54
|
|
|
55
|
+
if ("agentTranscriptPath" in input) {
|
|
56
|
+
if (input.agentTranscriptPath === undefined) return 0;
|
|
57
|
+
|
|
58
|
+
let text: string;
|
|
59
|
+
try {
|
|
60
|
+
text = readFileSync(input.agentTranscriptPath, "utf8");
|
|
61
|
+
} catch {
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const written = writtenFilesInTranscript(text);
|
|
66
|
+
if (written === undefined || ![...written].some(isCandidateFile)) return 0;
|
|
67
|
+
|
|
68
|
+
return { event: "stop", cwd: input.cwd, written };
|
|
69
|
+
}
|
|
70
|
+
|
|
55
71
|
const written =
|
|
56
72
|
input.transcriptPath === undefined ? undefined : writtenFiles(input.transcriptPath);
|
|
57
73
|
|
|
@@ -67,7 +83,11 @@ export function hookInput(
|
|
|
67
83
|
text: string,
|
|
68
84
|
fallbackCwd: string,
|
|
69
85
|
):
|
|
70
|
-
| {
|
|
86
|
+
| ({
|
|
87
|
+
event: "stop";
|
|
88
|
+
cwd: string;
|
|
89
|
+
stopHookActive: boolean;
|
|
90
|
+
} & ({ transcriptPath: string | undefined } | { agentTranscriptPath: string | undefined }))
|
|
71
91
|
| {
|
|
72
92
|
event: "write";
|
|
73
93
|
cwd: string;
|
|
@@ -118,20 +138,26 @@ export function hookInput(
|
|
|
118
138
|
};
|
|
119
139
|
}
|
|
120
140
|
|
|
121
|
-
const
|
|
141
|
+
const transcriptField = event === "SubagentStop" ? "agent_transcript_path" : "transcript_path";
|
|
142
|
+
|
|
143
|
+
const transcriptPath = transcriptField in input ? input[transcriptField] : undefined;
|
|
122
144
|
if (transcriptPath != null && typeof transcriptPath !== "string")
|
|
123
|
-
return { error:
|
|
145
|
+
return { error: `${transcriptField} must be a string` };
|
|
124
146
|
|
|
125
147
|
const stopHookActive = "stop_hook_active" in input ? input.stop_hook_active : undefined;
|
|
126
148
|
if (stopHookActive != null && typeof stopHookActive !== "boolean")
|
|
127
149
|
return { error: "stop_hook_active must be a boolean" };
|
|
128
150
|
|
|
129
|
-
|
|
130
|
-
event: "stop",
|
|
151
|
+
const stop = {
|
|
152
|
+
event: "stop" as const,
|
|
131
153
|
cwd: dir,
|
|
132
154
|
stopHookActive: stopHookActive ?? false,
|
|
133
|
-
transcriptPath: transcriptPath == null ? undefined : resolve(fallbackCwd, transcriptPath),
|
|
134
155
|
};
|
|
156
|
+
|
|
157
|
+
const path = transcriptPath == null ? undefined : resolve(fallbackCwd, transcriptPath);
|
|
158
|
+
return event === "SubagentStop"
|
|
159
|
+
? { ...stop, agentTranscriptPath: path }
|
|
160
|
+
: { ...stop, transcriptPath: path };
|
|
135
161
|
}
|
|
136
162
|
|
|
137
163
|
const events = new Set(["Stop", "SubagentStop", "PreToolUse", "PostToolUse"]);
|
|
@@ -36,11 +36,39 @@ export function children(node: Node): [string, Node][] {
|
|
|
36
36
|
return result;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
function
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
function pushValueReverse(
|
|
40
|
+
value: unknown,
|
|
41
|
+
parent: Node,
|
|
42
|
+
nodes: Node[],
|
|
43
|
+
parents: (Node | null)[],
|
|
44
|
+
): void {
|
|
45
|
+
if (Array.isArray(value)) {
|
|
46
|
+
for (let index = value.length - 1; index >= 0; index--) {
|
|
47
|
+
const child: unknown = value[index];
|
|
48
|
+
if (!isNode(child)) continue;
|
|
49
|
+
|
|
50
|
+
nodes.push(child);
|
|
51
|
+
parents.push(parent);
|
|
52
|
+
}
|
|
53
|
+
} else if (isNode(value)) {
|
|
54
|
+
nodes.push(value);
|
|
55
|
+
parents.push(parent);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function pushChildrenReverse(node: Node, nodes: Node[], parents: (Node | null)[]): void {
|
|
60
|
+
const keys = visitorKeys[node.type];
|
|
61
|
+
if (keys) {
|
|
62
|
+
const fields = node as unknown as Record<string, unknown>;
|
|
63
|
+
for (let index = keys.length - 1; index >= 0; index--)
|
|
64
|
+
pushValueReverse(fields[keys[index]!], node, nodes, parents);
|
|
65
|
+
} else {
|
|
66
|
+
const entries = Object.entries(node);
|
|
67
|
+
for (let index = entries.length - 1; index >= 0; index--) {
|
|
68
|
+
const [key, value] = entries[index]!;
|
|
69
|
+
if (key === "parent") continue;
|
|
70
|
+
pushValueReverse(value, node, nodes, parents);
|
|
71
|
+
}
|
|
44
72
|
}
|
|
45
73
|
}
|
|
46
74
|
|
|
@@ -49,22 +77,35 @@ export function walk(
|
|
|
49
77
|
enter: (node: Node, parent: Node | null) => void,
|
|
50
78
|
leave?: (node: Node, parent: Node | null) => void,
|
|
51
79
|
): void {
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
80
|
+
const nodes: Node[] = [root];
|
|
81
|
+
const parents: (Node | null)[] = [null];
|
|
82
|
+
if (!leave) {
|
|
83
|
+
while (nodes.length > 0) {
|
|
84
|
+
const node = nodes.pop()!;
|
|
85
|
+
const parent = parents.pop()!;
|
|
86
|
+
enter(node, parent);
|
|
87
|
+
pushChildrenReverse(node, nodes, parents);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const leaveIndexes: number[] = [];
|
|
94
|
+
while (nodes.length > 0) {
|
|
95
|
+
const node = nodes.pop()!;
|
|
96
|
+
const parent = parents.pop()!;
|
|
97
|
+
if (leaveIndexes.at(-1) === nodes.length) {
|
|
98
|
+
leaveIndexes.pop();
|
|
99
|
+
leave(node, parent);
|
|
58
100
|
continue;
|
|
59
101
|
}
|
|
60
102
|
|
|
61
103
|
enter(node, parent);
|
|
62
|
-
|
|
63
|
-
|
|
104
|
+
leaveIndexes.push(nodes.length);
|
|
105
|
+
nodes.push(node);
|
|
106
|
+
parents.push(parent);
|
|
64
107
|
|
|
65
|
-
|
|
66
|
-
eachChild(node, (_key, child) => stack.push({ node: child, parent: node, leaving: false }));
|
|
67
|
-
reverseFrom(stack, first);
|
|
108
|
+
pushChildrenReverse(node, nodes, parents);
|
|
68
109
|
}
|
|
69
110
|
}
|
|
70
111
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { readFileSync, realpathSync, statSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { holds } from "../../../directories.ts";
|
|
3
4
|
|
|
4
5
|
const suffixes = ["", ".json", ".jsonc", ".js", ".cjs", ".mjs", ".ts", ".mts", ".cts", "/index.js"];
|
|
5
6
|
|
|
@@ -76,12 +77,6 @@ export function resolveModule(specifier: string, from: string, loader: Loader):
|
|
|
76
77
|
}
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
export function realDirectory(dir: string): string {
|
|
80
|
-
if (existsSync(dir)) return realpathSync(dir);
|
|
81
|
-
const parent = dirname(dir);
|
|
82
|
-
return parent === dir ? dir : join(realDirectory(parent), basename(dir));
|
|
83
|
-
}
|
|
84
|
-
|
|
85
80
|
export function* configDirectories(
|
|
86
81
|
dir: string,
|
|
87
82
|
reader: { family: string; files: readonly string[]; candidate?(file: string): boolean },
|
|
@@ -91,9 +86,11 @@ export function* configDirectories(
|
|
|
91
86
|
|
|
92
87
|
let files = directoryFiles.get(key);
|
|
93
88
|
if (!files) {
|
|
89
|
+
const current = dir;
|
|
94
90
|
files = reader.files
|
|
95
|
-
.
|
|
96
|
-
.
|
|
91
|
+
.filter((name) => holds(current, name))
|
|
92
|
+
.map((name) => join(current, name))
|
|
93
|
+
.filter((file) => reader.candidate?.(file) ?? true);
|
|
97
94
|
|
|
98
95
|
directoryFiles.set(key, files);
|
|
99
96
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
2
|
import { biome } from "./biome.ts";
|
|
3
3
|
import { flat, legacy } from "./eslint.ts";
|
|
4
|
-
import { realDirectory } from "
|
|
4
|
+
import { realDirectory } from "../../../directories.ts";
|
|
5
5
|
import type { Decision, Reader } from "./layers.ts";
|
|
6
6
|
import { oxlint } from "./oxlint.ts";
|
|
7
7
|
|
|
@@ -70,13 +70,16 @@ export interface Context {
|
|
|
70
70
|
exceeded: boolean;
|
|
71
71
|
cycles: number;
|
|
72
72
|
memo: Map<Value, Map<string, Layer[]>>;
|
|
73
|
+
charged: Set<string>;
|
|
74
|
+
reaching: Set<string>[];
|
|
73
75
|
};
|
|
74
76
|
}
|
|
75
77
|
|
|
76
78
|
export const UNKNOWN_LAYER: Layer = { reach: "all", setting: "unknown" };
|
|
77
79
|
const EXPANSION_BUDGET = 100000;
|
|
78
80
|
|
|
79
|
-
const layerCache = new Map<string, { layers: Layer[]; root: Value }>();
|
|
81
|
+
const layerCache = new Map<string, { layers: Layer[]; root: Value; reached: Set<string> }>();
|
|
82
|
+
const exclusiveCost = new Map<string, number>();
|
|
80
83
|
|
|
81
84
|
export function lastLayers(layers: Layer[]): Layer[] {
|
|
82
85
|
const seen = new Set<Layer>();
|
|
@@ -311,6 +314,19 @@ function backstop(module: ConfigModule, context: Context, result: Layer[]): void
|
|
|
311
314
|
});
|
|
312
315
|
}
|
|
313
316
|
|
|
317
|
+
function charge(keys: Iterable<string>, work: Context["work"]): void {
|
|
318
|
+
for (const key of keys) {
|
|
319
|
+
for (const reached of work.reaching) reached.add(key);
|
|
320
|
+
|
|
321
|
+
if (work.charged.has(key)) continue;
|
|
322
|
+
|
|
323
|
+
work.charged.add(key);
|
|
324
|
+
work.count += exclusiveCost.get(key)!;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (work.count > EXPANSION_BUDGET) work.exceeded = true;
|
|
328
|
+
}
|
|
329
|
+
|
|
314
330
|
export function fileLayers(file: string, context: Context): { layers: Layer[]; root: Value } {
|
|
315
331
|
const module = moduleAt(file);
|
|
316
332
|
if (!module || context.chain.includes(module.file) || context.chain.length >= 32)
|
|
@@ -318,11 +334,37 @@ export function fileLayers(file: string, context: Context): { layers: Layer[]; r
|
|
|
318
334
|
|
|
319
335
|
const key = JSON.stringify([file, dirname(file), context.reader.family, context.shared]);
|
|
320
336
|
const cached = layerCache.get(key);
|
|
321
|
-
if (cached)
|
|
337
|
+
if (cached) {
|
|
338
|
+
charge(cached.reached, context.work);
|
|
339
|
+
return cached;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const work = context.work;
|
|
343
|
+
for (const reached of work.reaching) reached.add(key);
|
|
344
|
+
|
|
345
|
+
const reached = new Set([key]);
|
|
346
|
+
work.reaching.push(reached);
|
|
347
|
+
const cycles = work.cycles;
|
|
348
|
+
const count = work.count;
|
|
349
|
+
const charged = new Set(work.charged);
|
|
350
|
+
|
|
351
|
+
let result: { layers: Layer[]; root: Value };
|
|
352
|
+
try {
|
|
353
|
+
result = readFileLayers(file, module, context);
|
|
354
|
+
} finally {
|
|
355
|
+
work.reaching.pop();
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
if (!work.exceeded && work.cycles === cycles) {
|
|
359
|
+
let cost = work.count - count;
|
|
360
|
+
for (const entry of work.charged)
|
|
361
|
+
if (entry !== key && !charged.has(entry)) cost -= exclusiveCost.get(entry)!;
|
|
362
|
+
|
|
363
|
+
layerCache.set(key, { ...result, reached });
|
|
364
|
+
exclusiveCost.set(key, cost);
|
|
365
|
+
work.charged.add(key);
|
|
366
|
+
}
|
|
322
367
|
|
|
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
368
|
return result;
|
|
327
369
|
}
|
|
328
370
|
|
|
@@ -378,11 +420,13 @@ export function configAt(
|
|
|
378
420
|
extension: string | undefined,
|
|
379
421
|
reader: Reader,
|
|
380
422
|
): Found {
|
|
381
|
-
const work = {
|
|
423
|
+
const work: Context["work"] = {
|
|
382
424
|
count: 0,
|
|
383
425
|
exceeded: false,
|
|
384
426
|
cycles: 0,
|
|
385
427
|
memo: new Map<Value, Map<string, Layer[]>>(),
|
|
428
|
+
charged: new Set<string>(),
|
|
429
|
+
reaching: [],
|
|
386
430
|
};
|
|
387
431
|
|
|
388
432
|
const result = fileLayers(file, {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { readFileSync
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { realPath } from "../../../directories.ts";
|
|
2
3
|
import type { Node, Program } from "oxc-parser";
|
|
3
4
|
import { boundNames, children, declared, referenceChild, walk } from "../ast.ts";
|
|
4
5
|
import { parse } from "../parse.ts";
|
|
@@ -294,7 +295,7 @@ function indexModule(module: ConfigModule): void {
|
|
|
294
295
|
|
|
295
296
|
export function moduleAt(file: string): ConfigModule | null {
|
|
296
297
|
try {
|
|
297
|
-
file =
|
|
298
|
+
file = realPath(file);
|
|
298
299
|
if (modules.has(file)) return modules.get(file) ?? null;
|
|
299
300
|
|
|
300
301
|
const text = readFileSync(file, "utf8");
|
|
@@ -2,7 +2,8 @@ import { existsSync, readFileSync } from "node:fs";
|
|
|
2
2
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
3
3
|
import type { Width } from "../../language.ts";
|
|
4
4
|
import { UNKNOWN, type Value, exported, object, property } from "./evaluate.ts";
|
|
5
|
-
import {
|
|
5
|
+
import { holds, invocationMap, realDirectory } from "../../../directories.ts";
|
|
6
|
+
import { configDirectories } from "./find.ts";
|
|
6
7
|
import { moduleAt } from "./module.ts";
|
|
7
8
|
|
|
8
9
|
type Formatter = "oxfmt" | "prettier" | "biome";
|
|
@@ -72,7 +73,7 @@ const FILES = [
|
|
|
72
73
|
|
|
73
74
|
const parsed = new Map<string, Read>();
|
|
74
75
|
const directories = new Map<string, Directory>();
|
|
75
|
-
const dependencies =
|
|
76
|
+
const dependencies = invocationMap<string, Formatter[]>();
|
|
76
77
|
const globs = new Map<string, Bun.Glob>();
|
|
77
78
|
|
|
78
79
|
const editorGlobs = new Map<string, { regex: RegExp; ranges: [bigint, bigint][] }>();
|
|
@@ -261,27 +262,32 @@ function directory(dir: string, files: string[]): Directory {
|
|
|
261
262
|
}
|
|
262
263
|
|
|
263
264
|
function dependent(dir: string): Formatter[] {
|
|
264
|
-
const
|
|
265
|
-
if (cached) return cached;
|
|
265
|
+
const visited: string[] = [];
|
|
266
266
|
|
|
267
|
+
let found: Formatter[] = [];
|
|
267
268
|
let current = dir;
|
|
268
269
|
while (true) {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
270
|
+
const cached = dependencies.get(current);
|
|
271
|
+
if (cached) {
|
|
272
|
+
found = cached;
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
visited.push(current);
|
|
277
|
+
|
|
278
|
+
if (holds(current, "package.json"))
|
|
279
|
+
try {
|
|
280
|
+
const manifest = JSON.parse(readFileSync(join(current, "package.json"), "utf8")) as Record<
|
|
281
|
+
string,
|
|
282
|
+
Record<string, unknown>
|
|
283
|
+
>;
|
|
284
|
+
|
|
285
|
+
const names = { ...manifest.dependencies, ...manifest.devDependencies };
|
|
286
|
+
if ("oxfmt" in names) found.push("oxfmt");
|
|
287
|
+
if ("@biomejs/biome" in names) found.push("biome");
|
|
288
|
+
if ("prettier" in names) found.push("prettier");
|
|
289
|
+
if (found.length) break;
|
|
290
|
+
} catch {}
|
|
285
291
|
|
|
286
292
|
const parent = dirname(current);
|
|
287
293
|
if (parent === current) break;
|
|
@@ -289,8 +295,8 @@ function dependent(dir: string): Formatter[] {
|
|
|
289
295
|
current = parent;
|
|
290
296
|
}
|
|
291
297
|
|
|
292
|
-
dependencies.set(
|
|
293
|
-
return
|
|
298
|
+
for (const directory of visited) dependencies.set(directory, found);
|
|
299
|
+
return found;
|
|
294
300
|
}
|
|
295
301
|
|
|
296
302
|
function number(value: Value): number | undefined {
|
|
@@ -586,7 +592,7 @@ function editorconfig(
|
|
|
586
592
|
const file = join(dir, ".editorconfig");
|
|
587
593
|
|
|
588
594
|
let root = false;
|
|
589
|
-
if (
|
|
595
|
+
if (holds(dir, ".editorconfig")) {
|
|
590
596
|
const editor = parsedEditor(file);
|
|
591
597
|
if (!editor) unread.push(file);
|
|
592
598
|
else {
|
|
@@ -612,10 +618,10 @@ function editorconfig(
|
|
|
612
618
|
// oxfmt 0.70 reads only the nearest .editorconfig file.
|
|
613
619
|
if (
|
|
614
620
|
biomeDir ||
|
|
615
|
-
(nearest &&
|
|
621
|
+
(nearest && holds(dir, ".editorconfig")) ||
|
|
616
622
|
root ||
|
|
617
|
-
|
|
618
|
-
|
|
623
|
+
holds(dir, ".git") ||
|
|
624
|
+
holds(dir, ".hg")
|
|
619
625
|
)
|
|
620
626
|
break;
|
|
621
627
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { dirname, join } from "node:path";
|
|
2
|
+
|
|
3
|
+
export interface DeserializerFiles {
|
|
4
|
+
js: string;
|
|
5
|
+
ts: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
let embedded: DeserializerFiles | undefined;
|
|
9
|
+
export function embedDeserializers(files: DeserializerFiles): void {
|
|
10
|
+
embedded = files;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function deserializerFiles(): DeserializerFiles {
|
|
14
|
+
if (embedded) return embedded;
|
|
15
|
+
const generated = join(dirname(require.resolve("oxc-parser")), "generated", "deserialize");
|
|
16
|
+
return { js: join(generated, "js.js"), ts: join(generated, "ts.js") };
|
|
17
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import js from "oxc-parser/src-js/generated/deserialize/js.js" with { type: "file" };
|
|
2
|
+
import ts from "oxc-parser/src-js/generated/deserialize/ts.js" with { type: "file" };
|
|
3
|
+
import { embedDeserializers } from "./deserializers.ts";
|
|
4
|
+
|
|
5
|
+
export function embedCompiledDeserializers(): void {
|
|
6
|
+
embedDeserializers({ js, ts });
|
|
7
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
declare module "oxc-parser/src-js/bindings.js" {
|
|
2
|
+
import type { ParserOptions } from "oxc-parser";
|
|
3
|
+
|
|
4
|
+
export function getBufferOffset(buffer: Uint8Array): number;
|
|
5
|
+
export function rawTransferSupported(): boolean;
|
|
6
|
+
export function parseRawSync(
|
|
7
|
+
path: string,
|
|
8
|
+
block: Uint8Array,
|
|
9
|
+
sourceStartPos: number,
|
|
10
|
+
sourceByteLen: number,
|
|
11
|
+
options: ParserOptions,
|
|
12
|
+
): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare module "oxc-parser/src-js/generated/constants.js" {
|
|
16
|
+
export const ACTIVE_SIZE: number;
|
|
17
|
+
export const BLOCK_ALIGN: number;
|
|
18
|
+
export const BLOCK_SIZE: number;
|
|
19
|
+
export const BUFFER_SIZE: number;
|
|
20
|
+
export const IS_TS_FLAG_POS: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare module "oxc-parser/src-js/generated/deserialize/js.js" {
|
|
24
|
+
const path: string;
|
|
25
|
+
export default path;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare module "oxc-parser/src-js/generated/deserialize/ts.js" {
|
|
29
|
+
const path: string;
|
|
30
|
+
export default path;
|
|
31
|
+
}
|