@ryuugg/stanza 0.3.3 → 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 -684
- 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 -372
- 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 -337
- package/src/engine/gaps.ts +0 -361
- 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 -104
- package/src/engine/types.ts +0 -38
- package/src/files.ts +0 -1186
- package/src/format.ts +0 -159
- package/src/gitignore.ts +0 -280
- package/src/hook.ts +0 -380
- 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 -473
- package/src/start.ts +0 -49
- package/src/step.ts +0 -113
- package/src/usage.ts +0 -35
- package/src/worker.ts +0 -5
|
@@ -1,723 +0,0 @@
|
|
|
1
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
-
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
3
|
-
import type { Width } from "../../language.ts";
|
|
4
|
-
import { UNKNOWN, type Value, exported, object, property } from "./evaluate.ts";
|
|
5
|
-
import { holds, invocationMap, realDirectory } from "../../../directories.ts";
|
|
6
|
-
import { configDirectories } from "./find.ts";
|
|
7
|
-
import { moduleAt } from "./module.ts";
|
|
8
|
-
|
|
9
|
-
type Formatter = "oxfmt" | "prettier" | "biome";
|
|
10
|
-
type Config = { file: string; formatter: Formatter; value: Value };
|
|
11
|
-
type Directory = { configs: Config[]; unread: string[] };
|
|
12
|
-
type Read = { kind: "config"; config: Config } | { kind: "skip" } | { kind: "unread" };
|
|
13
|
-
type Setting = { columns?: number; tab?: number; editorconfig: boolean };
|
|
14
|
-
type Editor = {
|
|
15
|
-
root: boolean;
|
|
16
|
-
sections: { pattern: string | null; values: Record<string, string> }[];
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
type FormatKeys = {
|
|
20
|
-
width: string;
|
|
21
|
-
tab: string;
|
|
22
|
-
editorconfig?: string;
|
|
23
|
-
files: string;
|
|
24
|
-
legacyFiles?: string;
|
|
25
|
-
options: string;
|
|
26
|
-
defaultWidth: number;
|
|
27
|
-
basenameOnly: boolean;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const formats: Record<Formatter, FormatKeys> = {
|
|
31
|
-
oxfmt: {
|
|
32
|
-
width: "printWidth",
|
|
33
|
-
tab: "tabWidth",
|
|
34
|
-
files: "files",
|
|
35
|
-
options: "options",
|
|
36
|
-
defaultWidth: 100,
|
|
37
|
-
basenameOnly: true,
|
|
38
|
-
},
|
|
39
|
-
prettier: {
|
|
40
|
-
width: "printWidth",
|
|
41
|
-
tab: "tabWidth",
|
|
42
|
-
files: "files",
|
|
43
|
-
options: "options",
|
|
44
|
-
defaultWidth: 80,
|
|
45
|
-
basenameOnly: true,
|
|
46
|
-
},
|
|
47
|
-
biome: {
|
|
48
|
-
width: "lineWidth",
|
|
49
|
-
tab: "indentWidth",
|
|
50
|
-
editorconfig: "useEditorconfig",
|
|
51
|
-
files: "includes",
|
|
52
|
-
legacyFiles: "include",
|
|
53
|
-
options: "formatter",
|
|
54
|
-
defaultWidth: 80,
|
|
55
|
-
basenameOnly: false,
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const FILES = [
|
|
60
|
-
".oxfmtrc.json",
|
|
61
|
-
".oxfmtrc.jsonc",
|
|
62
|
-
...["ts", "mts", "cts", "js", "mjs", "cjs"].map((ext) => `oxfmt.config.${ext}`),
|
|
63
|
-
"biome.json",
|
|
64
|
-
"biome.jsonc",
|
|
65
|
-
".prettierrc",
|
|
66
|
-
".prettierrc.toml",
|
|
67
|
-
...["json", "json5", "yaml", "yml", "js", "cjs", "mjs", "ts", "cts", "mts"].map(
|
|
68
|
-
(ext) => `.prettierrc.${ext}`,
|
|
69
|
-
),
|
|
70
|
-
...["js", "cjs", "mjs", "ts", "cts", "mts"].map((ext) => `prettier.config.${ext}`),
|
|
71
|
-
"package.json",
|
|
72
|
-
];
|
|
73
|
-
|
|
74
|
-
const parsed = new Map<string, Read>();
|
|
75
|
-
const directories = new Map<string, Directory>();
|
|
76
|
-
const dependencies = invocationMap<string, Formatter[]>();
|
|
77
|
-
const globs = new Map<string, Bun.Glob>();
|
|
78
|
-
|
|
79
|
-
const editorGlobs = new Map<string, { regex: RegExp; ranges: [bigint, bigint][] }>();
|
|
80
|
-
const editors = new Map<string, Editor | null>();
|
|
81
|
-
const widths = new Map<string, Width>();
|
|
82
|
-
|
|
83
|
-
function family(file: string): Formatter {
|
|
84
|
-
const name = basename(file);
|
|
85
|
-
return name.startsWith("biome.")
|
|
86
|
-
? "biome"
|
|
87
|
-
: name.startsWith("oxfmt.") || name.startsWith(".oxfmt")
|
|
88
|
-
? "oxfmt"
|
|
89
|
-
: "prettier";
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function scalar(value: string): Value {
|
|
93
|
-
const text = value.trim().replace(/\s+#.*$/, "");
|
|
94
|
-
if (/^\d+$/.test(text)) return Number(text);
|
|
95
|
-
if (text === "true" || text === "false") return text === "true";
|
|
96
|
-
if (/^(["']).*\1$/.test(text)) return text.slice(1, -1);
|
|
97
|
-
return text;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function flat(text: string, separator: ":" | "="): Value {
|
|
101
|
-
const value: Record<string, Value> = {};
|
|
102
|
-
for (const line of text.split(/\r?\n/)) {
|
|
103
|
-
if (!line.trim() || /^\s*#/.test(line)) continue;
|
|
104
|
-
|
|
105
|
-
const match =
|
|
106
|
-
separator === ":"
|
|
107
|
-
? /^(?:([\w-]+)|"([\w-]+)"|'([\w-]+)')\s*:\s*(.*)$/.exec(line)
|
|
108
|
-
: /^([\w-]+)\s*=\s*(.*)$/.exec(line);
|
|
109
|
-
|
|
110
|
-
const key = separator === ":" ? (match?.[1] ?? match?.[2] ?? match?.[3]) : match?.[1];
|
|
111
|
-
const raw = separator === ":" ? match?.[4] : match?.[2];
|
|
112
|
-
if (!key || key === "overrides" || !raw) return UNKNOWN;
|
|
113
|
-
|
|
114
|
-
value[key] = scalar(raw);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return value;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function readable(file: string): Read {
|
|
121
|
-
const cached = parsed.get(file);
|
|
122
|
-
if (cached) return cached;
|
|
123
|
-
|
|
124
|
-
let value: Value = UNKNOWN;
|
|
125
|
-
const name = basename(file);
|
|
126
|
-
const formatter = family(file);
|
|
127
|
-
try {
|
|
128
|
-
if (name === ".prettierrc.toml") value = flat(readFileSync(file, "utf8"), "=");
|
|
129
|
-
else if (/\.ya?ml$/.test(name)) value = flat(readFileSync(file, "utf8"), ":");
|
|
130
|
-
else if (name === ".prettierrc") {
|
|
131
|
-
const module = moduleAt(file);
|
|
132
|
-
value = module?.program
|
|
133
|
-
? exported(file, "default")
|
|
134
|
-
: module
|
|
135
|
-
? flat(module.text, ":")
|
|
136
|
-
: UNKNOWN;
|
|
137
|
-
} else if (name === "package.json") value = property(exported(file, "default"), "prettier");
|
|
138
|
-
else value = exported(file, "default");
|
|
139
|
-
|
|
140
|
-
if (formatter === "biome" && object(value)) value = mergedBiome(file, value, new Set());
|
|
141
|
-
} catch {}
|
|
142
|
-
|
|
143
|
-
const result: Read =
|
|
144
|
-
value === undefined || (formatter === "biome" && object(value) && !biomeActive(value))
|
|
145
|
-
? { kind: "skip" }
|
|
146
|
-
: object(value)
|
|
147
|
-
? { kind: "config", config: { file, formatter, value } }
|
|
148
|
-
: { kind: "unread" };
|
|
149
|
-
|
|
150
|
-
parsed.set(file, result);
|
|
151
|
-
return result;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function biomeActive(value: Value): boolean {
|
|
155
|
-
if (!object(value)) return true;
|
|
156
|
-
|
|
157
|
-
const formatter = property(value, "formatter");
|
|
158
|
-
if (property(formatter, "enabled") === false) return false;
|
|
159
|
-
if (property(property(property(value, "javascript"), "formatter"), "enabled") === false)
|
|
160
|
-
return false;
|
|
161
|
-
|
|
162
|
-
if (formatter !== undefined || property(property(value, "javascript"), "formatter") !== undefined)
|
|
163
|
-
return true;
|
|
164
|
-
|
|
165
|
-
const overrides = property(value, "overrides");
|
|
166
|
-
if (overrides === UNKNOWN) return true;
|
|
167
|
-
return (
|
|
168
|
-
Array.isArray(overrides) &&
|
|
169
|
-
overrides.some(
|
|
170
|
-
(entry) =>
|
|
171
|
-
object(entry) &&
|
|
172
|
-
(property(entry, "formatter") !== undefined ||
|
|
173
|
-
property(property(entry, "javascript"), "formatter") !== undefined),
|
|
174
|
-
)
|
|
175
|
-
);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function mergedBiome(file: string, value: Value, seen: Set<string>): Value {
|
|
179
|
-
if (!object(value) || seen.has(file)) return UNKNOWN;
|
|
180
|
-
|
|
181
|
-
const entries = property(value, "extends");
|
|
182
|
-
if (entries === undefined) return value;
|
|
183
|
-
|
|
184
|
-
const list = typeof entries === "string" ? [entries] : entries;
|
|
185
|
-
if (!Array.isArray(list) || !list.every((entry) => typeof entry === "string")) return UNKNOWN;
|
|
186
|
-
|
|
187
|
-
seen.add(file);
|
|
188
|
-
|
|
189
|
-
let merged: Value = {};
|
|
190
|
-
for (const entry of list as string[]) {
|
|
191
|
-
let target: string | undefined;
|
|
192
|
-
if (entry === "//") {
|
|
193
|
-
let dir = dirname(file);
|
|
194
|
-
while (dirname(dir) !== dir && !target) {
|
|
195
|
-
dir = dirname(dir);
|
|
196
|
-
|
|
197
|
-
for (const name of ["biome.json", "biome.jsonc"]) {
|
|
198
|
-
const candidate = join(dir, name);
|
|
199
|
-
if (existsSync(candidate)) {
|
|
200
|
-
target = candidate;
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
} else if (entry.startsWith(".") || isAbsolute(entry) || /\.jsonc?$/.test(entry))
|
|
206
|
-
target = resolve(dirname(file), entry);
|
|
207
|
-
|
|
208
|
-
if (!target || !/\.jsonc?$/.test(target) || !existsSync(target)) return UNKNOWN;
|
|
209
|
-
|
|
210
|
-
const parent = mergedBiome(target, exported(target, "default"), seen);
|
|
211
|
-
if (!object(parent)) return UNKNOWN;
|
|
212
|
-
|
|
213
|
-
merged = mergeBiome(merged, parent);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
seen.delete(file);
|
|
217
|
-
return mergeBiome(merged, value);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function mergeBiome(base: Value, next: Value): Value {
|
|
221
|
-
const left = base as Record<string, Value>;
|
|
222
|
-
const right = next as Record<string, Value>;
|
|
223
|
-
const javascript = property(right, "javascript");
|
|
224
|
-
const previous = property(left, "javascript");
|
|
225
|
-
const result = { ...left, ...right };
|
|
226
|
-
if (property(left, "formatter") !== undefined || property(right, "formatter") !== undefined)
|
|
227
|
-
result.formatter = {
|
|
228
|
-
...(property(left, "formatter") as object),
|
|
229
|
-
...(property(right, "formatter") as object),
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
if (previous !== undefined || javascript !== undefined) {
|
|
233
|
-
result.javascript = { ...(previous as object), ...(javascript as object) };
|
|
234
|
-
if (
|
|
235
|
-
property(previous, "formatter") !== undefined ||
|
|
236
|
-
property(javascript, "formatter") !== undefined
|
|
237
|
-
)
|
|
238
|
-
(result.javascript as Record<string, Value>).formatter = {
|
|
239
|
-
...(property(previous, "formatter") as object),
|
|
240
|
-
...(property(javascript, "formatter") as object),
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
return result;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
function directory(dir: string, files: string[]): Directory {
|
|
248
|
-
const cached = directories.get(dir);
|
|
249
|
-
if (cached) return cached;
|
|
250
|
-
|
|
251
|
-
const configs: Config[] = [];
|
|
252
|
-
const unread: string[] = [];
|
|
253
|
-
for (const file of files) {
|
|
254
|
-
const read = readable(file);
|
|
255
|
-
if (read.kind === "config") configs.push(read.config);
|
|
256
|
-
if (read.kind === "unread") unread.push(file);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const found = { configs, unread };
|
|
260
|
-
directories.set(dir, found);
|
|
261
|
-
return found;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
function dependent(dir: string): Formatter[] {
|
|
265
|
-
const visited: string[] = [];
|
|
266
|
-
|
|
267
|
-
let found: Formatter[] = [];
|
|
268
|
-
let current = dir;
|
|
269
|
-
while (true) {
|
|
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 {}
|
|
291
|
-
|
|
292
|
-
const parent = dirname(current);
|
|
293
|
-
if (parent === current) break;
|
|
294
|
-
|
|
295
|
-
current = parent;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
for (const directory of visited) dependencies.set(directory, found);
|
|
299
|
-
return found;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
function number(value: Value): number | undefined {
|
|
303
|
-
return typeof value === "number" && Number.isInteger(value) && value > 0 ? value : undefined;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
function match(pattern: string, file: string, root: string, basenameOnly = true): boolean {
|
|
307
|
-
const path =
|
|
308
|
-
basenameOnly && !pattern.includes("/")
|
|
309
|
-
? basename(file)
|
|
310
|
-
: relative(root, file).split(sep).join("/");
|
|
311
|
-
|
|
312
|
-
let glob = globs.get(pattern);
|
|
313
|
-
if (!glob) {
|
|
314
|
-
glob = new Bun.Glob(pattern);
|
|
315
|
-
globs.set(pattern, glob);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
return glob.match(path);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function orderedIncludes(entries: string[], file: string, root: string): boolean {
|
|
322
|
-
let included = false;
|
|
323
|
-
for (const entry of entries) {
|
|
324
|
-
const bangs = /^!+/.exec(entry)?.[0].length ?? 0;
|
|
325
|
-
const pattern = entry.slice(bangs);
|
|
326
|
-
const matches =
|
|
327
|
-
match(pattern, file, root, false) ||
|
|
328
|
-
(bangs > 0 && !pattern.endsWith("/**") && match(`${pattern}/**`, file, root, false));
|
|
329
|
-
|
|
330
|
-
if (matches) included = bangs === 0;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
return included;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
function patterns(value: Value): string[] | null {
|
|
337
|
-
if (typeof value === "string") return [value];
|
|
338
|
-
return Array.isArray(value) && value.every((item) => typeof item === "string")
|
|
339
|
-
? (value as string[])
|
|
340
|
-
: null;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
function unknown(value: Value): boolean {
|
|
344
|
-
if (value === UNKNOWN) return true;
|
|
345
|
-
if (Array.isArray(value)) return value.some(unknown);
|
|
346
|
-
return object(value) && Object.values(value).some(unknown);
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
function scoped(config: Config, path: string): Value {
|
|
350
|
-
let value = config.value;
|
|
351
|
-
const overrides = property(value, "overrides");
|
|
352
|
-
if (overrides === UNKNOWN || (overrides !== undefined && !Array.isArray(overrides)))
|
|
353
|
-
return UNKNOWN;
|
|
354
|
-
|
|
355
|
-
if (!Array.isArray(overrides)) return value;
|
|
356
|
-
|
|
357
|
-
const keys = formats[config.formatter];
|
|
358
|
-
for (const entry of overrides) {
|
|
359
|
-
if (!object(entry)) return UNKNOWN;
|
|
360
|
-
|
|
361
|
-
const current = property(entry, keys.files);
|
|
362
|
-
const files = patterns(
|
|
363
|
-
current ?? (keys.legacyFiles ? property(entry, keys.legacyFiles) : undefined),
|
|
364
|
-
);
|
|
365
|
-
|
|
366
|
-
const options = property(entry, keys.options);
|
|
367
|
-
const javascript = property(entry, "javascript");
|
|
368
|
-
const excluded = patterns(property(entry, "excludeFiles"));
|
|
369
|
-
if (
|
|
370
|
-
!files ||
|
|
371
|
-
unknown(options) ||
|
|
372
|
-
unknown(javascript) ||
|
|
373
|
-
(property(entry, "excludeFiles") !== undefined && !excluded) ||
|
|
374
|
-
(options !== undefined && !object(options)) ||
|
|
375
|
-
(javascript !== undefined && !object(javascript))
|
|
376
|
-
)
|
|
377
|
-
return UNKNOWN;
|
|
378
|
-
|
|
379
|
-
const root = dirname(config.file);
|
|
380
|
-
const legacy = keys.legacyFiles && current === undefined;
|
|
381
|
-
const included =
|
|
382
|
-
keys.options === "formatter" && !legacy
|
|
383
|
-
? orderedIncludes(files, path, root)
|
|
384
|
-
: files.some((pattern) =>
|
|
385
|
-
match(
|
|
386
|
-
legacy && !pattern.includes("/") ? `**/${pattern}` : pattern,
|
|
387
|
-
path,
|
|
388
|
-
root,
|
|
389
|
-
keys.basenameOnly,
|
|
390
|
-
),
|
|
391
|
-
);
|
|
392
|
-
|
|
393
|
-
if (!included || excluded?.some((pattern) => match(pattern, path, root))) continue;
|
|
394
|
-
if (unknown(property(value, "formatter")) || unknown(property(value, "javascript")))
|
|
395
|
-
return UNKNOWN;
|
|
396
|
-
|
|
397
|
-
value =
|
|
398
|
-
keys.options === "formatter"
|
|
399
|
-
? {
|
|
400
|
-
...(value as object),
|
|
401
|
-
formatter: {
|
|
402
|
-
...(property(value, "formatter") as object),
|
|
403
|
-
...(options as object),
|
|
404
|
-
},
|
|
405
|
-
javascript: {
|
|
406
|
-
...(property(value, "javascript") as object),
|
|
407
|
-
...(javascript as object),
|
|
408
|
-
formatter: {
|
|
409
|
-
...(property(property(value, "javascript"), "formatter") as object),
|
|
410
|
-
...(property(javascript, "formatter") as object),
|
|
411
|
-
},
|
|
412
|
-
},
|
|
413
|
-
}
|
|
414
|
-
: { ...(value as object), ...(options as object) };
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
return value;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
function setting(config: Config, path: string): Setting | "skip" | null {
|
|
421
|
-
const value = scoped(config, path);
|
|
422
|
-
if (value === UNKNOWN) return null;
|
|
423
|
-
if (config.formatter === "biome" && !biomeActive(value)) return "skip";
|
|
424
|
-
|
|
425
|
-
const keys = formats[config.formatter];
|
|
426
|
-
const format = keys.options === "formatter" ? property(value, "formatter") : value;
|
|
427
|
-
const javascript = property(property(value, "javascript"), "formatter");
|
|
428
|
-
const width = property(javascript, keys.width);
|
|
429
|
-
const baseWidth = property(format, keys.width);
|
|
430
|
-
|
|
431
|
-
const tab = property(javascript, keys.tab);
|
|
432
|
-
const baseTab = property(format, keys.tab);
|
|
433
|
-
const editorconfig = keys.editorconfig ? property(format, keys.editorconfig) : undefined;
|
|
434
|
-
if ([width, baseWidth, tab, baseTab, editorconfig].includes(UNKNOWN)) return null;
|
|
435
|
-
|
|
436
|
-
if (config.formatter === "biome") {
|
|
437
|
-
const files = property(value, "files");
|
|
438
|
-
if (files === UNKNOWN) return null;
|
|
439
|
-
|
|
440
|
-
const includes = property(files, "includes");
|
|
441
|
-
const include = property(files, "include");
|
|
442
|
-
const ignore = property(files, "ignore");
|
|
443
|
-
if ([includes, include, ignore].includes(UNKNOWN)) return null;
|
|
444
|
-
|
|
445
|
-
const root = dirname(config.file);
|
|
446
|
-
const v2 = includes === undefined ? undefined : patterns(includes);
|
|
447
|
-
const v1 = include === undefined ? undefined : patterns(include);
|
|
448
|
-
const ignored = ignore === undefined ? undefined : patterns(ignore);
|
|
449
|
-
if (
|
|
450
|
-
(includes !== undefined && !v2) ||
|
|
451
|
-
(include !== undefined && !v1) ||
|
|
452
|
-
(ignore !== undefined && !ignored)
|
|
453
|
-
)
|
|
454
|
-
return null;
|
|
455
|
-
|
|
456
|
-
if (v2 && !orderedIncludes(v2, path, root)) return "skip";
|
|
457
|
-
if (
|
|
458
|
-
v1 &&
|
|
459
|
-
!v1.some((pattern) =>
|
|
460
|
-
match(!pattern.includes("/") ? `**/${pattern}` : pattern, path, root, false),
|
|
461
|
-
)
|
|
462
|
-
)
|
|
463
|
-
return "skip";
|
|
464
|
-
|
|
465
|
-
if (
|
|
466
|
-
ignored?.some((pattern) =>
|
|
467
|
-
match(!pattern.includes("/") ? `**/${pattern}` : pattern, path, root, false),
|
|
468
|
-
)
|
|
469
|
-
)
|
|
470
|
-
return "skip";
|
|
471
|
-
|
|
472
|
-
const formatterIncludes = property(format, "includes");
|
|
473
|
-
if (formatterIncludes === UNKNOWN) return null;
|
|
474
|
-
|
|
475
|
-
const scopedFiles = formatterIncludes === undefined ? undefined : patterns(formatterIncludes);
|
|
476
|
-
if (formatterIncludes !== undefined && !scopedFiles) return null;
|
|
477
|
-
if (scopedFiles && !orderedIncludes(scopedFiles, path, root)) return "skip";
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
const dimensions = {
|
|
481
|
-
columns: number(width) ?? number(baseWidth),
|
|
482
|
-
tab: number(tab) ?? number(baseTab),
|
|
483
|
-
};
|
|
484
|
-
|
|
485
|
-
return {
|
|
486
|
-
...dimensions,
|
|
487
|
-
editorconfig: !keys.editorconfig || editorconfig === true,
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
function parsedEditor(file: string): Editor | null {
|
|
492
|
-
if (editors.has(file)) return editors.get(file)!;
|
|
493
|
-
|
|
494
|
-
try {
|
|
495
|
-
const sections: Editor["sections"] = [{ pattern: null, values: {} }];
|
|
496
|
-
for (const line of readFileSync(file, "utf8").split(/\r?\n/)) {
|
|
497
|
-
const section = /^\s*\[(.+)\]\s*$/.exec(line);
|
|
498
|
-
if (section) {
|
|
499
|
-
sections.push({ pattern: section[1]!, values: {} });
|
|
500
|
-
continue;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
const pair = /^\s*([\w_]+)\s*=\s*([^#;]*)/.exec(line);
|
|
504
|
-
if (pair) sections.at(-1)!.values[pair[1]!.toLowerCase()] = pair[2]!.trim().toLowerCase();
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
const result = { root: sections[0]!.values.root === "true", sections };
|
|
508
|
-
editors.set(file, result);
|
|
509
|
-
return result;
|
|
510
|
-
} catch {
|
|
511
|
-
editors.set(file, null);
|
|
512
|
-
return null;
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
function editorPattern(pattern: string): { regex: RegExp; ranges: [bigint, bigint][] } {
|
|
517
|
-
const cached = editorGlobs.get(pattern);
|
|
518
|
-
if (cached) return cached;
|
|
519
|
-
|
|
520
|
-
const source = pattern.replace(/^\//, "");
|
|
521
|
-
const ranges: [bigint, bigint][] = [];
|
|
522
|
-
const compile = (part: string): string => {
|
|
523
|
-
let result = "";
|
|
524
|
-
for (let index = 0; index < part.length; index++) {
|
|
525
|
-
const char = part[index]!;
|
|
526
|
-
if (char === "*")
|
|
527
|
-
if (part[index + 1] === "*") {
|
|
528
|
-
result += part[index + 2] === "/" ? "(?:.*/)?" : ".*";
|
|
529
|
-
index += part[index + 2] === "/" ? 2 : 1;
|
|
530
|
-
} else result += "[^/]*";
|
|
531
|
-
else if (char === "?") result += "[^/]";
|
|
532
|
-
else if (char === "[") {
|
|
533
|
-
const end = part.indexOf("]", index + 1);
|
|
534
|
-
if (end < 0) result += "\\[";
|
|
535
|
-
else {
|
|
536
|
-
const content = part.slice(index + 1, end);
|
|
537
|
-
result += `[${content.startsWith("!") ? `^${content.slice(1)}` : content}]`;
|
|
538
|
-
index = end;
|
|
539
|
-
}
|
|
540
|
-
} else if (char === "{") {
|
|
541
|
-
const end = part.indexOf("}", index + 1);
|
|
542
|
-
if (end < 0) result += "\\{";
|
|
543
|
-
else {
|
|
544
|
-
const content = part.slice(index + 1, end);
|
|
545
|
-
const range = /^([+-]?\d+)\.\.([+-]?\d+)$/.exec(content);
|
|
546
|
-
if (range) {
|
|
547
|
-
ranges.push([BigInt(range[1]!), BigInt(range[2]!)]);
|
|
548
|
-
result += "(-?(?:0|[1-9]\\d*))";
|
|
549
|
-
} else result += `(?:${content.split(",").map(compile).join("|")})`;
|
|
550
|
-
|
|
551
|
-
index = end;
|
|
552
|
-
}
|
|
553
|
-
} else result += /[\\^$+?.()|{}]/.test(char) ? `\\${char}` : char;
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
return result;
|
|
557
|
-
};
|
|
558
|
-
|
|
559
|
-
const compiled = { regex: new RegExp(`^${compile(source)}$`), ranges };
|
|
560
|
-
editorGlobs.set(pattern, compiled);
|
|
561
|
-
return compiled;
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
function editorMatch(pattern: string, file: string, root: string): boolean {
|
|
565
|
-
const target = pattern.includes("/") ? relative(root, file).split(sep).join("/") : basename(file);
|
|
566
|
-
|
|
567
|
-
const { regex, ranges } = editorPattern(pattern);
|
|
568
|
-
const match = regex.exec(target);
|
|
569
|
-
if (!match) return false;
|
|
570
|
-
|
|
571
|
-
return ranges.every(([first, last], index) => {
|
|
572
|
-
const captured = match[index + 1];
|
|
573
|
-
if (captured === undefined) return true;
|
|
574
|
-
|
|
575
|
-
const value = BigInt(captured);
|
|
576
|
-
return value >= (first < last ? first : last) && value <= (first > last ? first : last);
|
|
577
|
-
});
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
function editorconfig(
|
|
581
|
-
path: string,
|
|
582
|
-
unread: string[],
|
|
583
|
-
biomeDir?: string,
|
|
584
|
-
nearest = false,
|
|
585
|
-
): { columns?: number; tab?: number; file?: string } {
|
|
586
|
-
const values: { columns?: number; tab?: number; file?: string } = {};
|
|
587
|
-
|
|
588
|
-
let sawWidth = false;
|
|
589
|
-
let sawTab = false;
|
|
590
|
-
let dir = biomeDir ?? dirname(path);
|
|
591
|
-
while (true) {
|
|
592
|
-
const file = join(dir, ".editorconfig");
|
|
593
|
-
|
|
594
|
-
let root = false;
|
|
595
|
-
if (holds(dir, ".editorconfig")) {
|
|
596
|
-
const editor = parsedEditor(file);
|
|
597
|
-
if (!editor) unread.push(file);
|
|
598
|
-
else {
|
|
599
|
-
root = editor.root;
|
|
600
|
-
const local: Record<string, string> = {};
|
|
601
|
-
for (const section of editor.sections)
|
|
602
|
-
if (section.pattern === null || editorMatch(section.pattern, path, dir))
|
|
603
|
-
Object.assign(local, section.values);
|
|
604
|
-
|
|
605
|
-
if (!sawWidth && local.max_line_length !== undefined) {
|
|
606
|
-
sawWidth = true;
|
|
607
|
-
values.columns = number(Number(local.max_line_length));
|
|
608
|
-
if (values.columns !== undefined) values.file = file;
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
if (!sawTab && (local.tab_width !== undefined || local.indent_size !== undefined)) {
|
|
612
|
-
sawTab = true;
|
|
613
|
-
values.tab = number(Number(local.tab_width)) ?? number(Number(local.indent_size));
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
// oxfmt 0.70 reads only the nearest .editorconfig file.
|
|
619
|
-
if (
|
|
620
|
-
biomeDir ||
|
|
621
|
-
(nearest && holds(dir, ".editorconfig")) ||
|
|
622
|
-
root ||
|
|
623
|
-
holds(dir, ".git") ||
|
|
624
|
-
holds(dir, ".hg")
|
|
625
|
-
)
|
|
626
|
-
break;
|
|
627
|
-
|
|
628
|
-
const parent = dirname(dir);
|
|
629
|
-
if (parent === dir) break;
|
|
630
|
-
|
|
631
|
-
dir = parent;
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
return values;
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
export function formatterWidth(path: string): Width {
|
|
638
|
-
const dir = realDirectory(dirname(resolve(path)));
|
|
639
|
-
const file = join(dir, basename(path));
|
|
640
|
-
const cached = widths.get(file);
|
|
641
|
-
if (cached) return cached;
|
|
642
|
-
|
|
643
|
-
const unread: string[] = [];
|
|
644
|
-
const dependency = dependent(dir);
|
|
645
|
-
|
|
646
|
-
let choices: { config: Config; setting: Setting }[] = [];
|
|
647
|
-
let current = dir;
|
|
648
|
-
for (const files of configDirectories(dir, { family: "width", files: FILES })) {
|
|
649
|
-
const found = directory(current, files);
|
|
650
|
-
unread.push(...found.unread);
|
|
651
|
-
const valid: typeof choices = [];
|
|
652
|
-
for (const config of found.configs) {
|
|
653
|
-
const scoped = setting(config, file);
|
|
654
|
-
if (scoped === null) unread.push(config.file);
|
|
655
|
-
else if (scoped !== "skip") valid.push({ config, setting: scoped });
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
if (valid.length) {
|
|
659
|
-
choices = valid;
|
|
660
|
-
break;
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
const parent = dirname(current);
|
|
664
|
-
if (parent === current) break;
|
|
665
|
-
|
|
666
|
-
current = parent;
|
|
667
|
-
}
|
|
668
|
-
|
|
669
|
-
const resolved = (setting: Setting, formatter: Formatter, config: Config) =>
|
|
670
|
-
setting.columns ??
|
|
671
|
-
(setting.editorconfig
|
|
672
|
-
? editorconfig(
|
|
673
|
-
file,
|
|
674
|
-
[],
|
|
675
|
-
formatter === "biome" ? dirname(config.file) : undefined,
|
|
676
|
-
formatter === "oxfmt",
|
|
677
|
-
).columns
|
|
678
|
-
: undefined) ??
|
|
679
|
-
formats[formatter].defaultWidth;
|
|
680
|
-
|
|
681
|
-
const listed = choices.filter((choice) => dependency.includes(choice.config.formatter));
|
|
682
|
-
const chosen = (listed.length === 1 ? listed : choices).toSorted(
|
|
683
|
-
(left, right) =>
|
|
684
|
-
resolved(left.setting, left.config.formatter, left.config) -
|
|
685
|
-
resolved(right.setting, right.config.formatter, right.config),
|
|
686
|
-
)[0];
|
|
687
|
-
|
|
688
|
-
const formatter =
|
|
689
|
-
chosen?.config.formatter ??
|
|
690
|
-
dependency.toSorted(
|
|
691
|
-
(left, right) => formats[left].defaultWidth - formats[right].defaultWidth,
|
|
692
|
-
)[0];
|
|
693
|
-
|
|
694
|
-
const own = chosen?.setting;
|
|
695
|
-
const reads = formatter !== undefined && (own?.editorconfig ?? formatter !== "biome");
|
|
696
|
-
const chosenEditor = reads
|
|
697
|
-
? editorconfig(
|
|
698
|
-
file,
|
|
699
|
-
unread,
|
|
700
|
-
chosen?.config.formatter === "biome" ? dirname(chosen.config.file) : undefined,
|
|
701
|
-
formatter === "oxfmt",
|
|
702
|
-
)
|
|
703
|
-
: {};
|
|
704
|
-
|
|
705
|
-
const columns =
|
|
706
|
-
own?.columns ??
|
|
707
|
-
(reads ? chosenEditor.columns : undefined) ??
|
|
708
|
-
(formatter ? formats[formatter].defaultWidth : 80);
|
|
709
|
-
|
|
710
|
-
const tab = own?.tab ?? (reads ? chosenEditor.tab : undefined) ?? 2;
|
|
711
|
-
const source =
|
|
712
|
-
own?.columns !== undefined
|
|
713
|
-
? { kind: "config" as const, file: chosen!.config.file }
|
|
714
|
-
: reads && chosenEditor.columns !== undefined
|
|
715
|
-
? { kind: "config" as const, file: chosenEditor.file! }
|
|
716
|
-
: formatter
|
|
717
|
-
? { kind: "default" as const, formatter }
|
|
718
|
-
: { kind: "fallback" as const };
|
|
719
|
-
|
|
720
|
-
const result = { columns, tab, source, unread };
|
|
721
|
-
widths.set(file, result);
|
|
722
|
-
return result;
|
|
723
|
-
}
|