@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.
- package/LICENSE.md +21 -0
- package/README.md +163 -0
- package/package.json +63 -0
- package/src/cli.ts +820 -0
- package/src/compile/darwin-arm64.ts +3 -0
- package/src/compile/darwin-x64.ts +3 -0
- package/src/compile/linux-arm64-musl.ts +3 -0
- package/src/compile/linux-arm64.ts +3 -0
- package/src/compile/linux-x64-musl.ts +3 -0
- package/src/compile/linux-x64.ts +3 -0
- package/src/compiled.ts +6 -0
- package/src/engine/attach.ts +62 -0
- package/src/engine/directives.ts +154 -0
- package/src/engine/doc.ts +93 -0
- package/src/engine/edits.ts +79 -0
- package/src/engine/explain.ts +333 -0
- package/src/engine/gaps.ts +329 -0
- package/src/engine/hunks.ts +320 -0
- package/src/engine/index.ts +254 -0
- package/src/engine/model.ts +102 -0
- package/src/engine/rules.ts +103 -0
- package/src/engine/types.ts +37 -0
- package/src/files.ts +822 -0
- package/src/gitignore.ts +279 -0
- package/src/hook.ts +282 -0
- package/src/languages/index.ts +32 -0
- package/src/languages/javascript/ast.ts +199 -0
- package/src/languages/javascript/braces.ts +374 -0
- package/src/languages/javascript/config/biome.ts +112 -0
- package/src/languages/javascript/config/eslint.ts +128 -0
- package/src/languages/javascript/config/evaluate.ts +278 -0
- package/src/languages/javascript/config/find.ts +108 -0
- package/src/languages/javascript/config/glob.ts +150 -0
- package/src/languages/javascript/config/index.ts +48 -0
- package/src/languages/javascript/config/layers.ts +425 -0
- package/src/languages/javascript/config/module.ts +326 -0
- package/src/languages/javascript/config/oxlint.ts +170 -0
- package/src/languages/javascript/config/width.ts +715 -0
- package/src/languages/javascript/entry.ts +39 -0
- package/src/languages/javascript/facts.ts +268 -0
- package/src/languages/javascript/index.ts +20 -0
- package/src/languages/javascript/lists.ts +49 -0
- package/src/languages/javascript/oracle.ts +191 -0
- package/src/languages/javascript/parse.ts +58 -0
- package/src/languages/javascript/scan.ts +68 -0
- package/src/languages/language.ts +109 -0
- package/src/launcher +21 -0
- package/src/native-file.d.ts +4 -0
- package/src/step.ts +108 -0
- package/src/usage.ts +30 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { basename } from "node:path";
|
|
2
|
+
import { NO_SETTING, type Value, property } from "./evaluate.ts";
|
|
3
|
+
import { configDirectories } from "./find.ts";
|
|
4
|
+
import {
|
|
5
|
+
type Context,
|
|
6
|
+
type Layer,
|
|
7
|
+
type Reader,
|
|
8
|
+
type Tree,
|
|
9
|
+
configAt,
|
|
10
|
+
decided,
|
|
11
|
+
enforcing,
|
|
12
|
+
eslintConfig,
|
|
13
|
+
everyConfig,
|
|
14
|
+
excluding,
|
|
15
|
+
levelOf,
|
|
16
|
+
patterns,
|
|
17
|
+
read,
|
|
18
|
+
resolved,
|
|
19
|
+
tree,
|
|
20
|
+
} from "./layers.ts";
|
|
21
|
+
|
|
22
|
+
function ownLayers(value: Value, context: Context): Layer[] {
|
|
23
|
+
const rules = property(value, "rules");
|
|
24
|
+
const curly = read(rules, "curly", context);
|
|
25
|
+
return curly === undefined || curly === NO_SETTING
|
|
26
|
+
? []
|
|
27
|
+
: [{ reach: "all", setting: levelOf(curly) }];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function packageName(name: string): string {
|
|
31
|
+
if (name.startsWith("@")) {
|
|
32
|
+
const [scope, pkg] = name.split("/");
|
|
33
|
+
return `${scope}/${pkg?.startsWith("eslint-config") ? pkg : pkg ? `eslint-config-${pkg}` : "eslint-config"}`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return name.startsWith("eslint-config-") ? name : `eslint-config-${name}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const flatTree: Tree = {
|
|
40
|
+
scope(value, context) {
|
|
41
|
+
if (property(value, "basePath") !== undefined) return "some";
|
|
42
|
+
const reach = patterns(property(value, "files"), context, "anchored");
|
|
43
|
+
return excluding(reach, property(value, "ignores"), context, "anchored");
|
|
44
|
+
},
|
|
45
|
+
own: ownLayers,
|
|
46
|
+
extend(entry, parent, context) {
|
|
47
|
+
if (!entry.startsWith(".") && !entry.startsWith("/")) {
|
|
48
|
+
const slash = entry.lastIndexOf("/");
|
|
49
|
+
const plugin = property(property(parent, "plugins"), entry.slice(0, slash));
|
|
50
|
+
if (slash > 0 && plugin !== undefined)
|
|
51
|
+
return context.reader.layers(
|
|
52
|
+
property(property(plugin, "configs"), entry.slice(slash + 1)),
|
|
53
|
+
{
|
|
54
|
+
...context,
|
|
55
|
+
shared: true,
|
|
56
|
+
},
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return resolved(entry, context, "import");
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const legacyTree: Tree = {
|
|
65
|
+
scope(value, context) {
|
|
66
|
+
const reach = patterns(property(value, "files"), context, "legacy");
|
|
67
|
+
return excluding(reach, property(value, "excludedFiles"), context, "legacy");
|
|
68
|
+
},
|
|
69
|
+
own: ownLayers,
|
|
70
|
+
extend(entry, _parent, context) {
|
|
71
|
+
const specifier = !entry.startsWith(".") && !entry.startsWith("/") ? packageName(entry) : entry;
|
|
72
|
+
return resolved(specifier, context, "require");
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
function legacyCandidate(file: string): boolean {
|
|
77
|
+
if (basename(file) !== "package.json") return true;
|
|
78
|
+
return eslintConfig(file) !== undefined;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const flat: Reader = {
|
|
82
|
+
family: "flat",
|
|
83
|
+
files: ["js", "mjs", "cjs", "ts", "mts", "cts"].map((ext) => `eslint.config.${ext}`),
|
|
84
|
+
backstop: ["curly"],
|
|
85
|
+
backstopImports: false,
|
|
86
|
+
layers(value, context) {
|
|
87
|
+
return tree(value, context, flatTree);
|
|
88
|
+
},
|
|
89
|
+
decide(dir, extension) {
|
|
90
|
+
return decided(flat, enforcing(everyConfig(dir, extension, flat)));
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const legacy: Reader = {
|
|
95
|
+
family: "legacy",
|
|
96
|
+
files: [
|
|
97
|
+
".eslintrc.js",
|
|
98
|
+
".eslintrc.cjs",
|
|
99
|
+
".eslintrc.yaml",
|
|
100
|
+
".eslintrc.yml",
|
|
101
|
+
".eslintrc.json",
|
|
102
|
+
".eslintrc",
|
|
103
|
+
"package.json",
|
|
104
|
+
],
|
|
105
|
+
candidate: legacyCandidate,
|
|
106
|
+
backstop: ["curly"],
|
|
107
|
+
backstopImports: false,
|
|
108
|
+
layers(value, context) {
|
|
109
|
+
return tree(value, context, legacyTree);
|
|
110
|
+
},
|
|
111
|
+
decide(dir, extension) {
|
|
112
|
+
let hasFlatConfig: boolean | undefined;
|
|
113
|
+
for (const files of configDirectories(dir, legacy)) {
|
|
114
|
+
const file = files[0];
|
|
115
|
+
if (file === undefined) continue;
|
|
116
|
+
|
|
117
|
+
if (basename(file) === "package.json") {
|
|
118
|
+
hasFlatConfig ??= [...configDirectories(dir, flat)].some((files) => files.length > 0);
|
|
119
|
+
if (hasFlatConfig) continue;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const found = configAt(file, dir, extension, legacy);
|
|
123
|
+
if (found.setting !== null || found.root === true) return decided(legacy, found);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return null;
|
|
127
|
+
},
|
|
128
|
+
};
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import type { Node } from "oxc-parser";
|
|
2
|
+
import { children } from "../ast.ts";
|
|
3
|
+
import { resolveModule } from "./find.ts";
|
|
4
|
+
import {
|
|
5
|
+
type Binding,
|
|
6
|
+
type ConfigModule,
|
|
7
|
+
helper,
|
|
8
|
+
memberName,
|
|
9
|
+
moduleAt,
|
|
10
|
+
nameOf,
|
|
11
|
+
requiredSource,
|
|
12
|
+
} from "./module.ts";
|
|
13
|
+
|
|
14
|
+
export const UNKNOWN = Symbol("unknown");
|
|
15
|
+
export const NO_SETTING = Symbol("no setting");
|
|
16
|
+
export type Value =
|
|
17
|
+
| string
|
|
18
|
+
| number
|
|
19
|
+
| boolean
|
|
20
|
+
| null
|
|
21
|
+
| undefined
|
|
22
|
+
| typeof UNKNOWN
|
|
23
|
+
| typeof NO_SETTING
|
|
24
|
+
| Value[]
|
|
25
|
+
| { [key: string]: Value };
|
|
26
|
+
|
|
27
|
+
const emptyConfigs: Value = new Proxy({}, { get: () => [] });
|
|
28
|
+
const js: Value = { configs: { recommended: { rules: {} }, all: { rules: { curly: "error" } } } };
|
|
29
|
+
const prettier: Value = { rules: { curly: "off" } };
|
|
30
|
+
const KNOWN: { pattern: RegExp; value: Value }[] = [
|
|
31
|
+
{ pattern: /^eslint:recommended$/, value: {} },
|
|
32
|
+
{ pattern: /^eslint:all$/, value: { rules: { curly: "error" } } },
|
|
33
|
+
{ pattern: /^@eslint\/js$/, value: js },
|
|
34
|
+
{
|
|
35
|
+
pattern: /^typescript-eslint$/,
|
|
36
|
+
value: { configs: emptyConfigs },
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
pattern:
|
|
40
|
+
/^(prettier|eslint-config-prettier(?:\/flat)?|plugin:prettier\/recommended|eslint-plugin-prettier\/recommended)$/,
|
|
41
|
+
value: prettier,
|
|
42
|
+
},
|
|
43
|
+
{ pattern: /^eslint\/config$/, value: {} },
|
|
44
|
+
];
|
|
45
|
+
|
|
46
|
+
const origins = new WeakMap<object, Map<string, Node>>();
|
|
47
|
+
|
|
48
|
+
export function object(value: Value): value is { [key: string]: Value } {
|
|
49
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function property(value: Value, key: string): Value {
|
|
53
|
+
if (value === undefined || value === NO_SETTING) return value;
|
|
54
|
+
return object(value) ? value[key] : UNKNOWN;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function origin(value: Value, key: string): Node | undefined {
|
|
58
|
+
return object(value) ? origins.get(value)?.get(key) : undefined;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function known(name: string): Value {
|
|
62
|
+
const preset = KNOWN.find((entry) => entry.pattern.test(name));
|
|
63
|
+
if (preset) return preset.value;
|
|
64
|
+
|
|
65
|
+
const parts = name.split("/");
|
|
66
|
+
const packageName = parts.slice(0, name.startsWith("@") ? 2 : 1).join("/");
|
|
67
|
+
return name.startsWith("plugin:") ||
|
|
68
|
+
/^eslint-plugin-|^@[^/]+\/eslint-plugin(-.*)?$/.test(packageName)
|
|
69
|
+
? NO_SETTING
|
|
70
|
+
: undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function bindingValue(binding: Binding | undefined, module: ConfigModule, chain: string[]): Value {
|
|
74
|
+
if (!binding || binding.mutable) return UNKNOWN;
|
|
75
|
+
if (binding.resolved) return binding.value;
|
|
76
|
+
|
|
77
|
+
binding.resolved = true;
|
|
78
|
+
binding.value = UNKNOWN;
|
|
79
|
+
|
|
80
|
+
let value: Value;
|
|
81
|
+
if (binding.imported) {
|
|
82
|
+
const preset = known(binding.imported.source);
|
|
83
|
+
const file =
|
|
84
|
+
preset === undefined
|
|
85
|
+
? resolveModule(binding.imported.source, module.file, binding.imported.loader)
|
|
86
|
+
: undefined;
|
|
87
|
+
|
|
88
|
+
value =
|
|
89
|
+
preset !== undefined
|
|
90
|
+
? binding.imported.name === "default" || binding.imported.name === "*"
|
|
91
|
+
? preset
|
|
92
|
+
: property(preset, binding.imported.name)
|
|
93
|
+
: file
|
|
94
|
+
? exported(file, binding.imported.name, chain)
|
|
95
|
+
: UNKNOWN;
|
|
96
|
+
} else if (binding.local) value = bindingValue(module.bindings.get(binding.local), module, chain);
|
|
97
|
+
else value = binding.node ? evaluate(binding.node, module, chain) : UNKNOWN;
|
|
98
|
+
|
|
99
|
+
binding.value = value;
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function exported(file: string, name: string, chain: string[] = []): Value {
|
|
104
|
+
const module = moduleAt(file);
|
|
105
|
+
if (
|
|
106
|
+
!module ||
|
|
107
|
+
!module.program ||
|
|
108
|
+
module.mutated ||
|
|
109
|
+
chain.includes(module.file) ||
|
|
110
|
+
chain.length >= 32
|
|
111
|
+
)
|
|
112
|
+
return UNKNOWN;
|
|
113
|
+
|
|
114
|
+
if (module.values.has(name)) return module.values.get(name);
|
|
115
|
+
|
|
116
|
+
const next = [...chain, module.file];
|
|
117
|
+
|
|
118
|
+
let value: Value;
|
|
119
|
+
if (name === "*") {
|
|
120
|
+
const namespace: { [key: string]: Value } = {};
|
|
121
|
+
for (const key of module.exports.keys())
|
|
122
|
+
Object.defineProperty(namespace, key, {
|
|
123
|
+
enumerable: true,
|
|
124
|
+
configurable: true,
|
|
125
|
+
get: () => exported(file, key, chain),
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
value = namespace;
|
|
129
|
+
} else value = bindingValue(module.exports.get(name), module, next);
|
|
130
|
+
|
|
131
|
+
module.values.set(name, value);
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function objectValue(
|
|
136
|
+
node: Extract<Node, { type: "ObjectExpression" }>,
|
|
137
|
+
module: ConfigModule,
|
|
138
|
+
chain: string[],
|
|
139
|
+
): Value {
|
|
140
|
+
const result: { [key: string]: Value } = Object.create(null);
|
|
141
|
+
const locations = new Map<string, Node>();
|
|
142
|
+
for (const item of node.properties)
|
|
143
|
+
if (item.type === "SpreadElement") {
|
|
144
|
+
const value = evaluate(item.argument, module, chain);
|
|
145
|
+
if (value === NO_SETTING) continue;
|
|
146
|
+
if (!object(value)) return UNKNOWN;
|
|
147
|
+
|
|
148
|
+
for (const key of Object.keys(value))
|
|
149
|
+
Object.defineProperty(result, key, {
|
|
150
|
+
enumerable: true,
|
|
151
|
+
configurable: true,
|
|
152
|
+
get: () => value[key],
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
for (const [key, location] of origins.get(value) ?? []) locations.set(key, location);
|
|
156
|
+
} else {
|
|
157
|
+
const key = !item.computed && nameOf(item.key);
|
|
158
|
+
if (!key) return UNKNOWN;
|
|
159
|
+
|
|
160
|
+
let value: Value;
|
|
161
|
+
let resolved = false;
|
|
162
|
+
Object.defineProperty(result, key, {
|
|
163
|
+
enumerable: true,
|
|
164
|
+
configurable: true,
|
|
165
|
+
get: () => {
|
|
166
|
+
if (!resolved) {
|
|
167
|
+
resolved = true;
|
|
168
|
+
value = UNKNOWN;
|
|
169
|
+
value =
|
|
170
|
+
item.method || item.kind !== "init" ? UNKNOWN : evaluate(item.value, module, chain);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return value;
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
locations.set(key, item);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
origins.set(result, locations);
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function evaluate(node: Node, module: ConfigModule, chain: string[]): Value {
|
|
185
|
+
switch (node.type) {
|
|
186
|
+
case "Literal":
|
|
187
|
+
return typeof node.value === "bigint" ||
|
|
188
|
+
(typeof node.value === "object" && node.value !== null)
|
|
189
|
+
? UNKNOWN
|
|
190
|
+
: node.value;
|
|
191
|
+
|
|
192
|
+
case "TemplateLiteral":
|
|
193
|
+
return node.expressions.length === 0 ? node.quasis[0]?.value.cooked : UNKNOWN;
|
|
194
|
+
|
|
195
|
+
case "ObjectExpression":
|
|
196
|
+
return objectValue(node, module, chain);
|
|
197
|
+
|
|
198
|
+
case "ArrayExpression": {
|
|
199
|
+
if (node.elements.length > 10000) return UNKNOWN;
|
|
200
|
+
|
|
201
|
+
const result: Value[] = [];
|
|
202
|
+
for (const item of node.elements) {
|
|
203
|
+
const value = item
|
|
204
|
+
? evaluate(item.type === "SpreadElement" ? item.argument : item, module, chain)
|
|
205
|
+
: UNKNOWN;
|
|
206
|
+
|
|
207
|
+
if (item?.type === "SpreadElement") {
|
|
208
|
+
if (value === NO_SETTING) continue;
|
|
209
|
+
if (!Array.isArray(value)) return UNKNOWN;
|
|
210
|
+
if (result.length + value.length > 10000) return UNKNOWN;
|
|
211
|
+
|
|
212
|
+
result.push(...value);
|
|
213
|
+
} else result.push(value);
|
|
214
|
+
|
|
215
|
+
if (result.length > 10000) return UNKNOWN;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
case "Identifier":
|
|
222
|
+
return bindingValue(module.bindings.get(node.name), module, chain);
|
|
223
|
+
|
|
224
|
+
case "MemberExpression": {
|
|
225
|
+
const value = evaluate(node.object, module, chain);
|
|
226
|
+
if (value === NO_SETTING) return value;
|
|
227
|
+
|
|
228
|
+
const key = memberName(node);
|
|
229
|
+
return key === undefined ? UNKNOWN : property(value, key);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
case "CallExpression": {
|
|
233
|
+
const name = requiredSource(node);
|
|
234
|
+
if (name !== undefined) {
|
|
235
|
+
const preset = known(name);
|
|
236
|
+
const file = preset === undefined ? resolveModule(name, module.file, "require") : undefined;
|
|
237
|
+
return preset !== undefined ? preset : file ? exported(file, "default", chain) : UNKNOWN;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const kind = helper(node.callee, module);
|
|
241
|
+
if (kind === "ignore") return {};
|
|
242
|
+
if (kind === "identity")
|
|
243
|
+
return node.arguments.length === 1 ? evaluate(node.arguments[0]!, module, chain) : UNKNOWN;
|
|
244
|
+
|
|
245
|
+
if (kind === "flatten") {
|
|
246
|
+
const result = node.arguments.flatMap((item): Value[] => {
|
|
247
|
+
const value = evaluate(
|
|
248
|
+
item.type === "SpreadElement" ? item.argument : item,
|
|
249
|
+
module,
|
|
250
|
+
chain,
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
return Array.isArray(value) ? value : [value];
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
return result.length > 10000 ? UNKNOWN : result;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return UNKNOWN;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
default:
|
|
263
|
+
if (
|
|
264
|
+
[
|
|
265
|
+
"TSAsExpression",
|
|
266
|
+
"TSSatisfiesExpression",
|
|
267
|
+
"TSNonNullExpression",
|
|
268
|
+
"TSTypeAssertion",
|
|
269
|
+
"ParenthesizedExpression",
|
|
270
|
+
].includes(node.type)
|
|
271
|
+
) {
|
|
272
|
+
const expression = children(node).find(([key]) => key === "expression")?.[1];
|
|
273
|
+
return expression ? evaluate(expression, module, chain) : UNKNOWN;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return UNKNOWN;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
|
|
4
|
+
const suffixes = ["", ".json", ".jsonc", ".js", ".cjs", ".mjs", ".ts", ".mts", ".cts", "/index.js"];
|
|
5
|
+
|
|
6
|
+
const directoryFiles = new Map<string, string[]>();
|
|
7
|
+
|
|
8
|
+
function fileAt(path: string): string | undefined {
|
|
9
|
+
for (const suffix of suffixes) {
|
|
10
|
+
try {
|
|
11
|
+
const candidate = path + suffix;
|
|
12
|
+
if (statSync(candidate).isFile()) return candidate;
|
|
13
|
+
} catch {}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (path.endsWith(".js"))
|
|
17
|
+
return fileAt(path.slice(0, -3) + ".ts") ?? fileAt(path.slice(0, -3) + ".mts");
|
|
18
|
+
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type Loader = "import" | "require";
|
|
23
|
+
|
|
24
|
+
function entryOf(value: unknown, loader: Loader): string | undefined {
|
|
25
|
+
if (typeof value === "string") return value;
|
|
26
|
+
|
|
27
|
+
if (value && typeof value === "object" && !Array.isArray(value))
|
|
28
|
+
for (const [key, target] of Object.entries(value))
|
|
29
|
+
if (key === loader || key === "node" || key === "default") {
|
|
30
|
+
const entry = entryOf(target, loader);
|
|
31
|
+
if (entry !== undefined) return entry;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolveModule(specifier: string, from: string, loader: Loader): string | undefined {
|
|
38
|
+
let dir: string;
|
|
39
|
+
try {
|
|
40
|
+
dir = dirname(realpathSync(from));
|
|
41
|
+
} catch {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (specifier.startsWith(".") || isAbsolute(specifier))
|
|
46
|
+
return fileAt(resolve(dirname(from), specifier));
|
|
47
|
+
|
|
48
|
+
const parts = specifier.split("/");
|
|
49
|
+
const name = parts.splice(0, specifier.startsWith("@") ? 2 : 1).join("/");
|
|
50
|
+
const subpath = parts.join("/");
|
|
51
|
+
while (true) {
|
|
52
|
+
const root = join(dir, "node_modules", name);
|
|
53
|
+
try {
|
|
54
|
+
const manifest: unknown = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
55
|
+
if (!manifest || typeof manifest !== "object") return undefined;
|
|
56
|
+
|
|
57
|
+
const exports: unknown = Reflect.get(manifest, "exports");
|
|
58
|
+
const target =
|
|
59
|
+
exports && typeof exports === "object"
|
|
60
|
+
? Reflect.get(exports, subpath ? `./${subpath}` : ".")
|
|
61
|
+
: subpath
|
|
62
|
+
? undefined
|
|
63
|
+
: exports;
|
|
64
|
+
|
|
65
|
+
const entry = entryOf(target, loader) ?? (!subpath ? entryOf(exports, loader) : undefined);
|
|
66
|
+
const main: unknown = Reflect.get(manifest, "main");
|
|
67
|
+
return fileAt(
|
|
68
|
+
join(root, entry ?? (subpath || (typeof main === "string" ? main : "index.js"))),
|
|
69
|
+
);
|
|
70
|
+
} catch {}
|
|
71
|
+
|
|
72
|
+
const parent = dirname(dir);
|
|
73
|
+
if (parent === dir) return undefined;
|
|
74
|
+
|
|
75
|
+
dir = parent;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
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
|
+
export function* configDirectories(
|
|
86
|
+
dir: string,
|
|
87
|
+
reader: { family: string; files: readonly string[]; candidate?(file: string): boolean },
|
|
88
|
+
): Generator<string[]> {
|
|
89
|
+
while (true) {
|
|
90
|
+
const key = `${reader.family}:${dir}`;
|
|
91
|
+
|
|
92
|
+
let files = directoryFiles.get(key);
|
|
93
|
+
if (!files) {
|
|
94
|
+
files = reader.files
|
|
95
|
+
.map((name) => join(dir, name))
|
|
96
|
+
.filter((file) => existsSync(file) && (reader.candidate?.(file) ?? true));
|
|
97
|
+
|
|
98
|
+
directoryFiles.set(key, files);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
yield files;
|
|
102
|
+
|
|
103
|
+
const parent = dirname(dir);
|
|
104
|
+
if (parent === dir) break;
|
|
105
|
+
|
|
106
|
+
dir = parent;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { extname } from "node:path";
|
|
2
|
+
import type { Reach } from "./layers.ts";
|
|
3
|
+
import { entry } from "../entry.ts";
|
|
4
|
+
|
|
5
|
+
interface CompiledGlob {
|
|
6
|
+
directory: RegExp;
|
|
7
|
+
filenames: Reach[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface PatternList {
|
|
11
|
+
positive: CompiledGlob[];
|
|
12
|
+
uncertain: boolean;
|
|
13
|
+
negated: boolean;
|
|
14
|
+
reaches: Map<string, Reach>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const compiled = new Map<string, CompiledGlob>();
|
|
18
|
+
const lists = new Map<string, PatternList>();
|
|
19
|
+
const identities = new WeakMap<string[], Map<boolean, PatternList>>();
|
|
20
|
+
const allExtensions = [...entry.extensions];
|
|
21
|
+
|
|
22
|
+
const expansionLimit = 256;
|
|
23
|
+
|
|
24
|
+
function expand(pattern: string, limit = expansionLimit): string[] | undefined {
|
|
25
|
+
const match = /\{([^{}]+)\}/.exec(pattern);
|
|
26
|
+
if (!match) return [pattern];
|
|
27
|
+
|
|
28
|
+
const result: string[] = [];
|
|
29
|
+
for (const part of match[1]!.split(",")) {
|
|
30
|
+
const expanded = expand(
|
|
31
|
+
pattern.slice(0, match.index) + part + pattern.slice(match.index + match[0].length),
|
|
32
|
+
limit - result.length,
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
if (!expanded || result.length + expanded.length > limit) return undefined;
|
|
36
|
+
|
|
37
|
+
result.push(...expanded);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function filenameReach(pattern: string, extension: string): Reach {
|
|
44
|
+
if (pattern === "*" || pattern === "**" || pattern === `*${extension}`) return "all";
|
|
45
|
+
if (!pattern.includes("*")) return extname(pattern) === extension ? "some" : "none";
|
|
46
|
+
|
|
47
|
+
const ending = pattern.slice(pattern.lastIndexOf("*") + 1);
|
|
48
|
+
if (ending.includes(".") && !ending.endsWith(extension) && !extension.endsWith(ending))
|
|
49
|
+
return "none";
|
|
50
|
+
|
|
51
|
+
return "some";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function compile(pattern: string): CompiledGlob {
|
|
55
|
+
const cached = compiled.get(pattern);
|
|
56
|
+
if (cached) return cached;
|
|
57
|
+
|
|
58
|
+
const segments = pattern.split("/");
|
|
59
|
+
const filename = segments.pop()!;
|
|
60
|
+
if (filename === "**") segments.push("**");
|
|
61
|
+
|
|
62
|
+
const source = segments
|
|
63
|
+
.map((segment) =>
|
|
64
|
+
segment === "**"
|
|
65
|
+
? "(?:[^/]+/)*"
|
|
66
|
+
: `${segment.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, "[^/]*")}/`,
|
|
67
|
+
)
|
|
68
|
+
.join("");
|
|
69
|
+
|
|
70
|
+
const glob = {
|
|
71
|
+
directory: new RegExp(`^${source}$`),
|
|
72
|
+
filenames: allExtensions.map((extension) => filenameReach(filename, extension)),
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
compiled.set(pattern, glob);
|
|
76
|
+
return glob;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function patternList(patterns: string[], legacy: boolean): PatternList {
|
|
80
|
+
const byMode = identities.get(patterns) ?? new Map<boolean, PatternList>();
|
|
81
|
+
const cached = byMode.get(legacy);
|
|
82
|
+
if (cached) return cached;
|
|
83
|
+
|
|
84
|
+
const key = JSON.stringify([patterns, legacy]);
|
|
85
|
+
|
|
86
|
+
let list = lists.get(key);
|
|
87
|
+
if (!list) {
|
|
88
|
+
let expanded: string[] | undefined = [];
|
|
89
|
+
for (const pattern of patterns.filter((item) => !item.startsWith("!"))) {
|
|
90
|
+
const alternatives: string[] | undefined = expand(pattern, expansionLimit - expanded.length);
|
|
91
|
+
expanded = alternatives && [...expanded, ...alternatives];
|
|
92
|
+
if (!expanded) break;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const uncertain =
|
|
96
|
+
!expanded || patterns.some((pattern) => /[?[\]()+@!]/.test(pattern.replace(/^!/, "")));
|
|
97
|
+
|
|
98
|
+
const positive =
|
|
99
|
+
!expanded || uncertain
|
|
100
|
+
? []
|
|
101
|
+
: expanded.map((input) => {
|
|
102
|
+
const pattern = input.replace(/^\.\//, "");
|
|
103
|
+
if (legacy && !pattern.includes("*"))
|
|
104
|
+
return { directory: /.*/, filenames: allExtensions.map((): Reach => "some") };
|
|
105
|
+
|
|
106
|
+
return compile(legacy && !pattern.includes("/") ? `**/${pattern}` : pattern);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
list = {
|
|
110
|
+
positive,
|
|
111
|
+
uncertain,
|
|
112
|
+
negated: patterns.some((pattern) => pattern.startsWith("!")),
|
|
113
|
+
reaches: new Map(),
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
lists.set(key, list);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
byMode.set(legacy, list);
|
|
120
|
+
identities.set(patterns, byMode);
|
|
121
|
+
return list;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function globReach(
|
|
125
|
+
patterns: string[],
|
|
126
|
+
relDir: string,
|
|
127
|
+
legacy: boolean,
|
|
128
|
+
extension?: string,
|
|
129
|
+
): Reach {
|
|
130
|
+
const list = patternList(patterns, legacy);
|
|
131
|
+
const key = `${relDir}\0${extension ?? ""}`;
|
|
132
|
+
const cached = list.reaches.get(key);
|
|
133
|
+
if (cached) return cached;
|
|
134
|
+
|
|
135
|
+
const index = extension === undefined ? -1 : allExtensions.indexOf(extension);
|
|
136
|
+
const considered = index === -1 ? allExtensions.map((_, at) => at) : [index];
|
|
137
|
+
|
|
138
|
+
let reach: Reach;
|
|
139
|
+
if (list.uncertain) reach = "some";
|
|
140
|
+
else {
|
|
141
|
+
const dir = relDir ? `${relDir}/` : "";
|
|
142
|
+
const matching = list.positive.filter((glob) => glob.directory.test(dir));
|
|
143
|
+
const all = considered.every((at) => matching.some((glob) => glob.filenames[at] === "all"));
|
|
144
|
+
const some = matching.some((glob) => considered.some((at) => glob.filenames[at] !== "none"));
|
|
145
|
+
reach = all && !list.negated ? "all" : some ? "some" : "none";
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
list.reaches.set(key, reach);
|
|
149
|
+
return reach;
|
|
150
|
+
}
|