@nudojs/cli 0.3.0 → 0.3.1

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/src/narrowing.ts DELETED
@@ -1,544 +0,0 @@
1
- import type { Node } from "@babel/types";
2
- import {
3
- type TypeValue,
4
- type Environment,
5
- T,
6
- narrowType,
7
- subtractType,
8
- getPrimitiveTypeOf,
9
- typeValueEquals,
10
- isSubtypeOf,
11
- createRange,
12
- v as termVar,
13
- } from "@nudojs/core";
14
- import { attachTerm } from "./term-registry.ts";
15
-
16
- /**
17
- * Given a test expression and the current environment, produce two
18
- * environments: one where the test is truthy, one where it is falsy.
19
- */
20
- export function narrow(
21
- test: Node,
22
- env: Environment,
23
- ): [trueEnv: Environment, falseEnv: Environment] {
24
- // a || b:fallthrough(both-false)是链式交集 —— narrow(a) 的 false
25
- // env 再 narrow(b) 的 false。这是 `if (typeof x !== 'object' || x === null)
26
- // return x;` 守卫模式的关键:后续代码里 x 只剩 object 成员。true 侧
27
- // 环境无法并集合并,保守退回未 narrow 的 env。
28
- // a && b 对称:true 侧链式(both-true),false 侧保守。
29
- if (test.type === "LogicalExpression" && (test.operator === "||" || test.operator === "&&")) {
30
- const [trueOfLeft, falseOfLeft] = narrow(test.left, env);
31
- const [, falseOfBoth] = narrow(test.right, falseOfLeft);
32
- const [trueOfBoth] = narrow(test.right, trueOfLeft);
33
- return test.operator === "||"
34
- ? [env, falseOfBoth]
35
- : [trueOfBoth, env];
36
- }
37
-
38
- // typeof x === "string"
39
- if (
40
- test.type === "BinaryExpression" &&
41
- test.operator === "===" &&
42
- isTypeofExpr(test.left) &&
43
- test.right.type === "StringLiteral"
44
- ) {
45
- const target = getTypeofTarget(test.left);
46
- if (target) {
47
- return target ? narrowByTypeof(target, test.right.value, env) : [env, env];
48
- }
49
- }
50
-
51
- // "string" === typeof x
52
- if (
53
- test.type === "BinaryExpression" &&
54
- test.operator === "===" &&
55
- test.left.type === "StringLiteral" &&
56
- isTypeofExpr(test.right)
57
- ) {
58
- const target = getTypeofTarget(test.right);
59
- if (target) {
60
- return target ? narrowByTypeof(target, test.left.value, env) : [env, env];
61
- }
62
- }
63
-
64
- // typeof x !== "string"
65
- if (
66
- test.type === "BinaryExpression" &&
67
- test.operator === "!==" &&
68
- isTypeofExpr(test.left) &&
69
- test.right.type === "StringLiteral"
70
- ) {
71
- const target = getTypeofTarget(test.left);
72
- if (target) {
73
- const [trueEnv, falseEnv] = target ? narrowByTypeof(target, test.right.value, env) : [env, env];
74
- return [falseEnv, trueEnv];
75
- }
76
- }
77
-
78
- // "string" !== typeof x
79
- if (
80
- test.type === "BinaryExpression" &&
81
- test.operator === "!==" &&
82
- test.left.type === "StringLiteral" &&
83
- isTypeofExpr(test.right)
84
- ) {
85
- const target = getTypeofTarget(test.right);
86
- if (target) {
87
- const [trueEnv, falseEnv] = narrowByTypeof(target, test.left.value, env);
88
- return [falseEnv, trueEnv];
89
- }
90
- }
91
-
92
- // obj.prop === literal (discriminated union)
93
- if (
94
- test.type === "BinaryExpression" &&
95
- test.operator === "===" &&
96
- test.left.type === "MemberExpression" &&
97
- test.left.object.type === "Identifier" &&
98
- test.left.property.type === "Identifier" &&
99
- isLiteralNode(test.right)
100
- ) {
101
- return narrowByDiscriminant(test.left.object.name, test.left.property.name, getLiteralValue(test.right), env);
102
- }
103
-
104
- // literal === obj.prop
105
- if (
106
- test.type === "BinaryExpression" &&
107
- test.operator === "===" &&
108
- isLiteralNode(test.left) &&
109
- test.right.type === "MemberExpression" &&
110
- test.right.object.type === "Identifier" &&
111
- test.right.property.type === "Identifier"
112
- ) {
113
- return narrowByDiscriminant(test.right.object.name, test.right.property.name, getLiteralValue(test.left), env);
114
- }
115
-
116
- // obj.prop !== literal
117
- if (
118
- test.type === "BinaryExpression" &&
119
- test.operator === "!==" &&
120
- test.left.type === "MemberExpression" &&
121
- test.left.object.type === "Identifier" &&
122
- test.left.property.type === "Identifier" &&
123
- isLiteralNode(test.right)
124
- ) {
125
- const [trueEnv, falseEnv] = narrowByDiscriminant(test.left.object.name, test.left.property.name, getLiteralValue(test.right), env);
126
- return [falseEnv, trueEnv];
127
- }
128
-
129
- // x === literal
130
- if (
131
- test.type === "BinaryExpression" &&
132
- test.operator === "===" &&
133
- test.left.type === "Identifier" &&
134
- isLiteralNode(test.right)
135
- ) {
136
- return narrowByStrictEqual(test.left.name, getLiteralValue(test.right), env);
137
- }
138
-
139
- // literal === x
140
- if (
141
- test.type === "BinaryExpression" &&
142
- test.operator === "===" &&
143
- isLiteralNode(test.left) &&
144
- test.right.type === "Identifier"
145
- ) {
146
- return narrowByStrictEqual(test.right.name, getLiteralValue(test.left), env);
147
- }
148
-
149
- // x !== literal
150
- if (
151
- test.type === "BinaryExpression" &&
152
- test.operator === "!==" &&
153
- test.left.type === "Identifier" &&
154
- isLiteralNode(test.right)
155
- ) {
156
- const [trueEnv, falseEnv] = narrowByStrictEqual(test.left.name, getLiteralValue(test.right), env);
157
- return [falseEnv, trueEnv];
158
- }
159
-
160
- // literal !== x
161
- if (
162
- test.type === "BinaryExpression" &&
163
- test.operator === "!==" &&
164
- isLiteralNode(test.left) &&
165
- test.right.type === "Identifier"
166
- ) {
167
- const [trueEnv, falseEnv] = narrowByStrictEqual(test.right.name, getLiteralValue(test.left), env);
168
- return [falseEnv, trueEnv];
169
- }
170
-
171
- // x instanceof C
172
- if (
173
- test.type === "BinaryExpression" &&
174
- test.operator === "instanceof" &&
175
- test.left.type === "Identifier" &&
176
- test.right.type === "Identifier"
177
- ) {
178
- return narrowByInstanceof(test.left.name, test.right.name, env);
179
- }
180
-
181
- // x >= literal / x > literal / x <= literal / x < literal
182
- if (
183
- test.type === "BinaryExpression" &&
184
- (test.operator === ">=" || test.operator === ">" || test.operator === "<=" || test.operator === "<") &&
185
- test.left.type === "Identifier" &&
186
- isLiteralNode(test.right)
187
- ) {
188
- const litVal = getLiteralValue(test.right);
189
- if (litVal.kind === "literal" && typeof litVal.value === "number") {
190
- return narrowByComparison(test.left.name, test.operator, litVal.value, env);
191
- }
192
- }
193
-
194
- // literal >= x / literal > x / literal <= x / literal < x
195
- if (
196
- test.type === "BinaryExpression" &&
197
- (test.operator === ">=" || test.operator === ">" || test.operator === "<=" || test.operator === "<") &&
198
- isLiteralNode(test.left) &&
199
- test.right.type === "Identifier"
200
- ) {
201
- const litVal = getLiteralValue(test.left);
202
- if (litVal.kind === "literal" && typeof litVal.value === "number") {
203
- const flipped = { ">=": "<=", ">": "<", "<=": ">=", "<": ">" } as const;
204
- return narrowByComparison(test.right.name, flipped[test.operator as keyof typeof flipped], litVal.value, env);
205
- }
206
- }
207
-
208
- // "key" in obj
209
- if (
210
- test.type === "BinaryExpression" &&
211
- test.operator === "in" &&
212
- test.left.type === "StringLiteral" &&
213
- test.right.type === "Identifier"
214
- ) {
215
- return narrowByPropertyIn(test.left.value, test.right.name, env);
216
- }
217
-
218
- // Array.isArray(x)
219
- if (
220
- test.type === "CallExpression" &&
221
- test.callee.type === "MemberExpression" &&
222
- test.callee.object.type === "Identifier" &&
223
- test.callee.object.name === "Array" &&
224
- test.callee.property.type === "Identifier" &&
225
- test.callee.property.name === "isArray" &&
226
- test.arguments.length === 1 &&
227
- test.arguments[0].type === "Identifier"
228
- ) {
229
- return narrowByIsArray(test.arguments[0].name, env);
230
- }
231
-
232
- // !expr (negate)
233
- if (test.type === "UnaryExpression" && test.operator === "!") {
234
- const [trueEnv, falseEnv] = narrow(test.argument, env);
235
- return [falseEnv, trueEnv];
236
- }
237
-
238
- // Truthiness narrowing: if (x)
239
- if (test.type === "Identifier") {
240
- return narrowByTruthy(test.name, env);
241
- }
242
-
243
- return [env, env];
244
- }
245
-
246
- function isTypeofExpr(node: Node): boolean {
247
- return node.type === "UnaryExpression" && node.operator === "typeof";
248
- }
249
-
250
- /** typeof 目标:标识符(x)或成员访问链(ref.has / ns.fn) */
251
- function getTypeofTarget(node: Node): { kind: "ident"; name: string } | { kind: "member"; objName: string; propName: string } | null {
252
- if (
253
- node.type === "UnaryExpression" &&
254
- node.operator === "typeof" &&
255
- node.argument.type === "Identifier"
256
- ) {
257
- return { kind: "ident", name: node.argument.name };
258
- }
259
- if (
260
- node.type === "UnaryExpression" &&
261
- node.operator === "typeof" &&
262
- node.argument.type === "MemberExpression" &&
263
- node.argument.object.type === "Identifier" &&
264
- !node.argument.computed &&
265
- node.argument.property.type === "Identifier"
266
- ) {
267
- return { kind: "member", objName: node.argument.object.name, propName: node.argument.property.name };
268
- }
269
- return null;
270
- }
271
-
272
- function isLiteralNode(node: Node): boolean {
273
- return (
274
- node.type === "NumericLiteral" ||
275
- node.type === "StringLiteral" ||
276
- node.type === "BooleanLiteral" ||
277
- node.type === "NullLiteral" ||
278
- (node.type === "Identifier" && node.name === "undefined")
279
- );
280
- }
281
-
282
- function getLiteralValue(node: Node): TypeValue {
283
- if (node.type === "NumericLiteral") return T.literal(node.value);
284
- if (node.type === "StringLiteral") return T.literal(node.value);
285
- if (node.type === "BooleanLiteral") return T.literal(node.value);
286
- if (node.type === "NullLiteral") return T.null;
287
- if (node.type === "Identifier" && node.name === "undefined") return T.undefined;
288
- return T.unknown;
289
- }
290
-
291
- const typeofToPrimitive: Record<string, TypeValue> = {
292
- number: T.number,
293
- string: T.string,
294
- boolean: T.boolean,
295
- bigint: T.bigint,
296
- symbol: T.symbol,
297
- };
298
-
299
- /** 内置类实例的原型方法归属(与 evaluator classMethods 表对齐的子集) */
300
- const INSTANCE_METHOD_OWNERS: Record<string, Set<string>> = {
301
- has: new Set(["Set", "Map", "WeakSet", "WeakMap"]),
302
- get: new Set(["Map", "WeakMap"]),
303
- set: new Set(["Map", "WeakMap", "Set"]),
304
- add: new Set(["Set", "WeakSet"]),
305
- delete: new Set(["Set", "Map", "WeakSet", "WeakMap"]),
306
- clear: new Set(["Set", "Map"]),
307
- forEach: new Set(["Set", "Map", "Array"]),
308
- size: new Set(["Set", "Map"]),
309
- keys: new Set(["Set", "Map"]),
310
- values: new Set(["Set", "Map"]),
311
- entries: new Set(["Set", "Map"]),
312
- test: new Set(["RegExp"]),
313
- exec: new Set(["RegExp"]),
314
- };
315
-
316
- function narrowByTypeof(
317
- target: { kind: "ident"; name: string } | { kind: "member"; objName: string; propName: string },
318
- typeStr: string,
319
- env: Environment,
320
- ): [Environment, Environment] {
321
- // 成员目标(typeof ref.has === 'function'):按对象成员中 prop 的类型
322
- // 过滤 union——留下 has 为函数的成员(如 Set),剔除 tuple/{}
323
- if (target.kind === "member") {
324
- const current = env.lookup(target.objName);
325
- if (!current || current.kind !== "union") return [env, env];
326
- const propMatches = (m: TypeValue): boolean => {
327
- let prop: TypeValue | undefined;
328
- if (m.kind === "object") prop = m.properties[target.propName];
329
- else if (m.kind === "instance") {
330
- // instance 的 properties 不含原型方法:按内置类方法表判断
331
- // (Set/Map 的 has 等——与 evaluator 的 classMethods 表保持一致)
332
- if (INSTANCE_METHOD_OWNERS[target.propName]?.has(m.className)) return true;
333
- prop = m.properties[target.propName];
334
- }
335
- if (typeStr === "function") return prop?.kind === "function" || prop?.kind === "unknown";
336
- return true;
337
- };
338
- const narrowed = narrowType(current, propMatches);
339
- const excluded = subtractType(current, propMatches);
340
- if (narrowed.kind === "never") return [env, env];
341
- const trueEnv = env.extend({});
342
- trueEnv.bind(target.objName, narrowed);
343
- const falseEnv = env.extend({});
344
- falseEnv.bind(target.objName, excluded.kind === "never" ? current : excluded);
345
- return [trueEnv, falseEnv];
346
- }
347
-
348
- const varName = target.name;
349
- const current = env.lookup(varName);
350
- const targetPrimitive = typeofToPrimitive[typeStr];
351
-
352
- const matchesPrimitive = (m: TypeValue): boolean => {
353
- const pt = getPrimitiveTypeOf(m);
354
- return pt === typeStr;
355
- };
356
-
357
- if (targetPrimitive) {
358
- const narrowed = narrowType(current, matchesPrimitive);
359
- const excluded = subtractType(current, matchesPrimitive);
360
-
361
- const trueEnv = env.extend({});
362
- trueEnv.bind(varName, narrowed.kind === "never" ? targetPrimitive : narrowed);
363
- const falseEnv = env.extend({});
364
- falseEnv.bind(varName, excluded.kind === "never" ? current : excluded);
365
- return [trueEnv, falseEnv];
366
- }
367
-
368
- if (typeStr === "object") {
369
- const narrowed = narrowType(current, (m) => {
370
- const pt = getPrimitiveTypeOf(m);
371
- return pt === "object";
372
- });
373
- const excluded = subtractType(current, (m) => {
374
- const pt = getPrimitiveTypeOf(m);
375
- return pt === "object";
376
- });
377
- const trueEnv = env.extend({});
378
- trueEnv.bind(varName, narrowed.kind === "never" ? current : narrowed);
379
- const falseEnv = env.extend({});
380
- falseEnv.bind(varName, excluded.kind === "never" ? current : excluded);
381
- return [trueEnv, falseEnv];
382
- }
383
-
384
- if (typeStr === "function") {
385
- const narrowed = narrowType(current, (m) => m.kind === "function");
386
- const excluded = subtractType(current, (m) => m.kind === "function");
387
- const trueEnv = env.extend({});
388
- trueEnv.bind(varName, narrowed.kind === "never" ? current : narrowed);
389
- const falseEnv = env.extend({});
390
- falseEnv.bind(varName, excluded.kind === "never" ? current : excluded);
391
- return [trueEnv, falseEnv];
392
- }
393
-
394
- return [env, env];
395
- }
396
-
397
- function narrowByInstanceof(
398
- varName: string,
399
- className: string,
400
- env: Environment,
401
- ): [Environment, Environment] {
402
- const current = env.lookup(varName);
403
-
404
- const narrowed = narrowType(current, (m) =>
405
- m.kind === "instance" && m.className === className,
406
- );
407
- const excluded = subtractType(current, (m) =>
408
- m.kind === "instance" && m.className === className,
409
- );
410
-
411
- const trueEnv = env.extend({});
412
- trueEnv.bind(varName, narrowed.kind === "never" ? T.instanceOf(className) : narrowed);
413
- const falseEnv = env.extend({});
414
- falseEnv.bind(varName, excluded.kind === "never" ? current : excluded);
415
- return [trueEnv, falseEnv];
416
- }
417
-
418
- function narrowByStrictEqual(
419
- varName: string,
420
- literalTV: TypeValue,
421
- env: Environment,
422
- ): [Environment, Environment] {
423
- const current = env.lookup(varName);
424
-
425
- const narrowed = narrowType(current, (m) => typeValueEquals(m, literalTV));
426
- const excluded = subtractType(current, (m) => typeValueEquals(m, literalTV));
427
-
428
- const trueEnv = env.extend({});
429
- trueEnv.bind(varName, narrowed.kind === "never" ? literalTV : narrowed);
430
- const falseEnv = env.extend({});
431
- falseEnv.bind(varName, excluded.kind === "never" ? current : excluded);
432
- return [trueEnv, falseEnv];
433
- }
434
-
435
- function narrowByComparison(
436
- varName: string,
437
- op: ">=" | ">" | "<=" | "<",
438
- value: number,
439
- env: Environment,
440
- ): [Environment, Environment] {
441
- const current = env.lookup(varName);
442
- if (!isSubtypeOf(current, T.number) && current.kind !== "union") return [env, env];
443
-
444
- const trueEnv = env.extend({});
445
- const falseEnv = env.extend({});
446
-
447
- const trueRange = op === ">=" ? createRange({ min: value })
448
- : op === ">" ? createRange({ min: value + 1 })
449
- : op === "<=" ? createRange({ max: value })
450
- : createRange({ max: value - 1 });
451
-
452
- const falseRange = op === ">=" ? createRange({ max: value - 1 })
453
- : op === ">" ? createRange({ max: value })
454
- : op === "<=" ? createRange({ min: value + 1 })
455
- : createRange({ min: value });
456
-
457
- // 保留项身份:range 界经 toAbsWithTerms 编码为 Pred,代数继续传播
458
- const term = termVar(varName);
459
- attachTerm(trueRange, term);
460
- attachTerm(falseRange, term);
461
-
462
- trueEnv.bind(varName, trueRange);
463
- falseEnv.bind(varName, falseRange);
464
- return [trueEnv, falseEnv];
465
- }
466
-
467
- function narrowByPropertyIn(propName: string, varName: string, env: Environment): [Environment, Environment] {
468
- const current = env.lookup(varName);
469
-
470
- const narrowed = narrowType(current, (m) =>
471
- m.kind === "object" && propName in m.properties
472
- );
473
- const excluded = subtractType(current, (m) =>
474
- m.kind === "object" && propName in m.properties
475
- );
476
-
477
- const trueEnv = env.extend({});
478
- trueEnv.bind(varName, narrowed.kind === "never" ? current : narrowed);
479
- const falseEnv = env.extend({});
480
- falseEnv.bind(varName, excluded.kind === "never" ? current : excluded);
481
- return [trueEnv, falseEnv];
482
- }
483
-
484
- function narrowByIsArray(varName: string, env: Environment): [Environment, Environment] {
485
- const current = env.lookup(varName);
486
-
487
- const narrowed = narrowType(current, (m) => m.kind === "array" || m.kind === "tuple");
488
- const excluded = subtractType(current, (m) => m.kind === "array" || m.kind === "tuple");
489
-
490
- const trueEnv = env.extend({});
491
- trueEnv.bind(varName, narrowed.kind === "never" ? T.array(T.unknown) : narrowed);
492
- const falseEnv = env.extend({});
493
- falseEnv.bind(varName, excluded.kind === "never" ? current : excluded);
494
- return [trueEnv, falseEnv];
495
- }
496
-
497
- function narrowByDiscriminant(
498
- objName: string,
499
- propName: string,
500
- literalValue: TypeValue,
501
- env: Environment,
502
- ): [Environment, Environment] {
503
- const current = env.lookup(objName);
504
-
505
- const narrowed = narrowType(current, (m) => {
506
- if (m.kind !== "object") return false;
507
- const propType = m.properties[propName];
508
- if (!propType) return false;
509
- return typeValueEquals(propType, literalValue);
510
- });
511
-
512
- const excluded = subtractType(current, (m) => {
513
- if (m.kind !== "object") return false;
514
- const propType = m.properties[propName];
515
- if (!propType) return false;
516
- return typeValueEquals(propType, literalValue);
517
- });
518
-
519
- const trueEnv = env.extend({});
520
- trueEnv.bind(objName, narrowed.kind === "never" ? current : narrowed);
521
- const falseEnv = env.extend({});
522
- falseEnv.bind(objName, excluded.kind === "never" ? current : excluded);
523
- return [trueEnv, falseEnv];
524
- }
525
-
526
- function isFalsyType(tv: TypeValue): boolean {
527
- if (tv.kind === "literal") {
528
- return tv.value === null || tv.value === undefined || tv.value === false || tv.value === 0 || tv.value === "";
529
- }
530
- return false;
531
- }
532
-
533
- function narrowByTruthy(varName: string, env: Environment): [Environment, Environment] {
534
- const current = env.lookup(varName);
535
-
536
- const narrowed = narrowType(current, (m) => !isFalsyType(m));
537
- const excluded = subtractType(current, (m) => !isFalsyType(m));
538
-
539
- const trueEnv = env.extend({});
540
- trueEnv.bind(varName, narrowed.kind === "never" ? current : narrowed);
541
- const falseEnv = env.extend({});
542
- falseEnv.bind(varName, excluded.kind === "never" ? current : excluded);
543
- return [trueEnv, falseEnv];
544
- }
@@ -1,42 +0,0 @@
1
- /**
2
- * 从 Babel 测试表达式提取代数 Φ 约束。
3
- * 支持 `x > n` / `x >= n` / `x < n` / `x <= n`(Identifier × NumericLiteral)。
4
- * false 分支用对偶比较(`>` ↔ `≤`),使 else 路径也能携带约束。
5
- */
6
-
7
- import type { Node } from "@babel/types";
8
- import type { Phi } from "@nudojs/core";
9
- import { pTrue, and as phiAnd, gt, ge, lt, le, v as termVar, lit } from "@nudojs/core";
10
-
11
- export function phiFromTest(test: Node): { whenTrue: Phi; whenFalse: Phi } {
12
- if (test.type !== "BinaryExpression") {
13
- return { whenTrue: pTrue, whenFalse: pTrue };
14
- }
15
- const op = test.operator;
16
- const left = test.left;
17
- const right = test.right;
18
-
19
- // x ≷ n
20
- if (
21
- left.type === "Identifier" &&
22
- right.type === "NumericLiteral" &&
23
- (op === ">" || op === ">=" || op === "<" || op === "<=")
24
- ) {
25
- const t = termVar(left.name);
26
- const n = lit(right.value);
27
- const pred =
28
- op === ">" ? gt(t, n) : op === ">=" ? ge(t, n) : op === "<" ? lt(t, n) : le(t, n);
29
- // 对偶:¬(a≷b) = a≲b(数值全序)
30
- const dual =
31
- op === ">" ? le(t, n) : op === ">=" ? lt(t, n) : op === "<" ? ge(t, n) : gt(t, n);
32
- return { whenTrue: pred, whenFalse: dual };
33
- }
34
-
35
- return { whenTrue: pTrue, whenFalse: pTrue };
36
- }
37
-
38
- export function combinePhi(base: Phi, extra: Phi): Phi {
39
- if (extra.op === "true") return base;
40
- if (base.op === "true") return extra;
41
- return phiAnd(base, extra);
42
- }
@@ -1,63 +0,0 @@
1
- import { readFileSync, existsSync } from "node:fs";
2
- import { resolve, join, dirname } from "node:path";
3
-
4
- function findNodeModules(startDir: string): string | null {
5
- let dir = resolve(startDir);
6
- const root = resolve("/");
7
-
8
- while (dir !== root) {
9
- const nmPath = join(dir, "node_modules");
10
- if (existsSync(nmPath)) return nmPath;
11
- const parent = dirname(dir);
12
- if (parent === dir) break;
13
- dir = parent;
14
- }
15
- return null;
16
- }
17
-
18
- function resolveExportsNudo(exports: unknown, subpath: string): string | null {
19
- if (!exports || typeof exports !== "object") return null;
20
-
21
- const entry = (exports as Record<string, unknown>)[subpath];
22
- if (!entry) return null;
23
-
24
- if (typeof entry === "object" && entry !== null && "nudo" in entry) {
25
- const nudoEntry = (entry as Record<string, unknown>)["nudo"];
26
- if (typeof nudoEntry === "string") return nudoEntry;
27
- }
28
-
29
- return null;
30
- }
31
-
32
- export function resolveNpmNudo(
33
- source: string,
34
- fromDir: string,
35
- ): string | null {
36
- const isRelative = source.startsWith(".") || source.startsWith("/");
37
- if (isRelative) return null;
38
-
39
- const parts = source.startsWith("@")
40
- ? source.split("/").slice(0, 2)
41
- : source.split("/").slice(0, 1);
42
- const pkgName = parts.join("/");
43
- const subpath = source.slice(pkgName.length) || ".";
44
-
45
- const nodeModules = findNodeModules(fromDir);
46
- if (!nodeModules) return null;
47
-
48
- const pkgJsonPath = join(nodeModules, pkgName, "package.json");
49
- if (!existsSync(pkgJsonPath)) return null;
50
-
51
- try {
52
- const pkg = JSON.parse(readFileSync(pkgJsonPath, "utf-8"));
53
- const nudoEntry = resolveExportsNudo(pkg.exports, subpath);
54
- if (nudoEntry) {
55
- const resolved = resolve(dirname(pkgJsonPath), nudoEntry);
56
- if (existsSync(resolved)) return resolved;
57
- }
58
- } catch {
59
- // ignore parse errors
60
- }
61
-
62
- return null;
63
- }