@litsx/babel-preset-litsx 0.8.2 → 0.9.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.
@@ -1,4 +1,9 @@
1
1
  import { createRuntimeHooksTransform } from "@litsx/babel-plugin-shared-hooks";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import traverse from "@babel/traverse";
5
+ import parser from "@litsx/babel-parser";
6
+ import { ensureTypescriptModule } from "./transform-litsx-properties.js";
2
7
 
3
8
  const RUNTIME_MODULE = "@litsx/core";
4
9
  const IMPORT_SOURCES = [RUNTIME_MODULE];
@@ -31,6 +36,836 @@ const RUNTIME_HELPERS = [
31
36
  "useStableId",
32
37
  ];
33
38
 
39
+ const SOURCE_EXTENSIONS = [
40
+ "",
41
+ ".litsx",
42
+ ".tsx",
43
+ ".ts",
44
+ ".jsx",
45
+ ".js",
46
+ ".mjs",
47
+ ".cjs",
48
+ ];
49
+ const DEFAULT_MODULE_RESOLUTION_OPTIONS = {
50
+ moduleResolution: 100,
51
+ allowJs: true,
52
+ checkJs: false,
53
+ jsx: 1,
54
+ target: 99,
55
+ module: 99,
56
+ esModuleInterop: true,
57
+ allowSyntheticDefaultImports: true,
58
+ };
59
+
60
+ function normalizeFilePath(value) {
61
+ return normalizePath(value);
62
+ }
63
+
64
+ function getTraverse() {
65
+ return traverse.default || traverse;
66
+ }
67
+
68
+ function normalizeInMemoryFiles(files) {
69
+ const normalized = new Map();
70
+ if (!files || typeof files !== "object") {
71
+ return normalized;
72
+ }
73
+ for (const [filename, source] of Object.entries(files)) {
74
+ if (typeof source !== "string") continue;
75
+ normalized.set(normalizeFilePath(filename), source);
76
+ }
77
+ return normalized;
78
+ }
79
+
80
+ function createStructuralHookResolver(options = {}) {
81
+ const inMemoryFiles = normalizeInMemoryFiles(options.inMemoryFiles);
82
+ const compilationSession = options.__litsxCompilationSession || null;
83
+ const moduleCache = compilationSession?.importedModuleAnalysisCache || new Map();
84
+ const resolvedImportCache = compilationSession?.resolvedImportCache || new Map();
85
+ const providedTypescriptSession =
86
+ options?.typescriptSession?.projectSession || options?.typescriptSession || null;
87
+ const compilerOptionsCache = new Map();
88
+ const moduleResolutionHostCache = new Map();
89
+
90
+ function getProgramForFile(filename) {
91
+ if (!providedTypescriptSession?.getProgram || !filename) {
92
+ return null;
93
+ }
94
+
95
+ try {
96
+ if (providedTypescriptSession.kind === "project") {
97
+ return providedTypescriptSession.getProgram();
98
+ }
99
+ if (providedTypescriptSession.kind === "standalone") {
100
+ return providedTypescriptSession.getProgram(normalizeFilePath(filename));
101
+ }
102
+ } catch {
103
+ return null;
104
+ }
105
+
106
+ return null;
107
+ }
108
+
109
+ function getCompilerOptions(filename) {
110
+ const cacheKey = normalizeFilePath(filename);
111
+ if (compilerOptionsCache.has(cacheKey)) {
112
+ return compilerOptionsCache.get(cacheKey);
113
+ }
114
+ const program = getProgramForFile(filename);
115
+ const compilerOptions =
116
+ program?.getCompilerOptions?.() ||
117
+ options.compilerOptions ||
118
+ DEFAULT_MODULE_RESOLUTION_OPTIONS;
119
+ compilerOptionsCache.set(cacheKey, compilerOptions);
120
+ return compilerOptions;
121
+ }
122
+
123
+ function getModuleResolutionHost(filename) {
124
+ const cacheKey = normalizeFilePath(filename);
125
+ if (moduleResolutionHostCache.has(cacheKey)) {
126
+ return moduleResolutionHostCache.get(cacheKey);
127
+ }
128
+ const ts = ensureTypescriptModule();
129
+ const host = providedTypescriptSession?.host || ts.sys;
130
+ moduleResolutionHostCache.set(cacheKey, host);
131
+ return host;
132
+ }
133
+
134
+ function fileExists(filename) {
135
+ const normalized = normalizeFilePath(filename);
136
+ return inMemoryFiles.has(normalized) || fs.existsSync(normalized);
137
+ }
138
+
139
+ function readFile(filename) {
140
+ const normalized = normalizeFilePath(filename);
141
+ if (inMemoryFiles.has(normalized)) {
142
+ return inMemoryFiles.get(normalized);
143
+ }
144
+ try {
145
+ return fs.readFileSync(normalized, "utf8");
146
+ } catch {
147
+ return null;
148
+ }
149
+ }
150
+
151
+ function resolveWithExtensions(base) {
152
+ const normalizedBase = normalizeFilePath(base);
153
+ for (const ext of SOURCE_EXTENSIONS) {
154
+ const candidate = `${normalizedBase}${ext}`;
155
+ if (fileExists(candidate)) {
156
+ return normalizeFilePath(candidate);
157
+ }
158
+ }
159
+ for (const ext of SOURCE_EXTENSIONS.filter(Boolean)) {
160
+ const candidate = normalizeFilePath(path.join(base, `index${ext}`));
161
+ if (fileExists(candidate)) {
162
+ return candidate;
163
+ }
164
+ }
165
+ return null;
166
+ }
167
+
168
+ function resolvePathAlias(containingFile, source) {
169
+ const compilerOptions = getCompilerOptions(containingFile) || {};
170
+ const baseUrl = compilerOptions.baseUrl
171
+ ? normalizeFilePath(
172
+ path.isAbsolute(compilerOptions.baseUrl)
173
+ ? compilerOptions.baseUrl
174
+ : path.resolve(path.dirname(containingFile), compilerOptions.baseUrl)
175
+ )
176
+ : normalizeFilePath(path.dirname(containingFile));
177
+ const pathMappings = compilerOptions.paths || {};
178
+
179
+ for (const [pattern, substitutions] of Object.entries(pathMappings)) {
180
+ const starIndex = pattern.indexOf("*");
181
+ const isStarPattern = starIndex !== -1;
182
+ const prefix = isStarPattern ? pattern.slice(0, starIndex) : pattern;
183
+ const suffix = isStarPattern ? pattern.slice(starIndex + 1) : "";
184
+
185
+ if (isStarPattern) {
186
+ if (!source.startsWith(prefix) || !source.endsWith(suffix)) {
187
+ continue;
188
+ }
189
+ } else if (source !== pattern) {
190
+ continue;
191
+ }
192
+
193
+ const wildcardValue = isStarPattern
194
+ ? source.slice(prefix.length, source.length - suffix.length)
195
+ : "";
196
+
197
+ for (const substitution of substitutions || []) {
198
+ const substituted = isStarPattern
199
+ ? substitution.replace("*", wildcardValue)
200
+ : substitution;
201
+ const candidateBase = path.isAbsolute(substituted)
202
+ ? substituted
203
+ : path.join(baseUrl, substituted);
204
+ const resolved = resolveWithExtensions(candidateBase);
205
+ if (resolved) {
206
+ return resolved;
207
+ }
208
+ }
209
+ }
210
+
211
+ return null;
212
+ }
213
+
214
+ function resolveImport(containingFile, source) {
215
+ if (typeof source !== "string" || !containingFile) {
216
+ return null;
217
+ }
218
+ const cacheKey = `${normalizeFilePath(containingFile)}::${source}`;
219
+ if (resolvedImportCache.has(cacheKey)) {
220
+ return resolvedImportCache.get(cacheKey);
221
+ }
222
+
223
+ const baseDir = path.dirname(containingFile);
224
+ let resolved = source.startsWith(".") || source.startsWith("/")
225
+ ? resolveWithExtensions(path.resolve(baseDir, source))
226
+ : resolvePathAlias(containingFile, source);
227
+
228
+ if (!resolved) {
229
+ const ts = ensureTypescriptModule();
230
+ try {
231
+ const resolution = ts.resolveModuleName(
232
+ source,
233
+ normalizeFilePath(containingFile),
234
+ getCompilerOptions(containingFile),
235
+ getModuleResolutionHost(containingFile)
236
+ );
237
+ const resolvedFileName = resolution?.resolvedModule?.resolvedFileName;
238
+ if (resolvedFileName) {
239
+ resolved = resolveWithExtensions(resolvedFileName) || normalizeFilePath(resolvedFileName);
240
+ }
241
+ } catch {
242
+ resolved = null;
243
+ }
244
+ }
245
+
246
+ resolvedImportCache.set(cacheKey, resolved);
247
+ return resolved;
248
+ }
249
+
250
+ function resolveModuleReference(analysis, reference) {
251
+ if (!analysis || !reference?.source || reference.source === RUNTIME_MODULE) {
252
+ return reference?.resolvedSource || null;
253
+ }
254
+ if (Object.prototype.hasOwnProperty.call(reference, "resolvedSource")) {
255
+ return reference.resolvedSource;
256
+ }
257
+ reference.resolvedSource = resolveImport(analysis.filename, reference.source);
258
+ return reference.resolvedSource;
259
+ }
260
+
261
+ function getParserPluginsForModule(filename, source) {
262
+ if (/\.(?:[cm]?ts|tsx|litsx)$/i.test(filename)) {
263
+ return ["typescript"];
264
+ }
265
+ if (/\b(?:as|satisfies)\s+[^;,)]+/.test(source)) {
266
+ return ["typescript"];
267
+ }
268
+ return [];
269
+ }
270
+
271
+ function getImportedName(specifier) {
272
+ if (specifier.type === "ImportDefaultSpecifier") return "default";
273
+ if (specifier.type === "ImportNamespaceSpecifier") return "*";
274
+ return specifier.imported?.name ?? specifier.imported?.value ?? null;
275
+ }
276
+
277
+ function isDefineHookCallee(node, analysis) {
278
+ if (!node) return false;
279
+ if (node.type === "Identifier") {
280
+ return analysis.defineHookLocals.has(node.name);
281
+ }
282
+ if (
283
+ node.type === "MemberExpression" &&
284
+ !node.computed &&
285
+ node.property?.type === "Identifier" &&
286
+ node.property.name === "defineHook" &&
287
+ node.object?.type === "Identifier"
288
+ ) {
289
+ return analysis.runtimeNamespaceLocals.has(node.object.name);
290
+ }
291
+ return false;
292
+ }
293
+
294
+ function addExportBinding(analysis, exportedName, info) {
295
+ if (exportedName) {
296
+ analysis.exportBindings.set(exportedName, info);
297
+ }
298
+ }
299
+
300
+ function getDefineHookPhaseInfo(init) {
301
+ const definition = init?.arguments?.[0];
302
+ if (!definition || definition.type !== "ObjectExpression") {
303
+ return {
304
+ hasStaticPhase: false,
305
+ hasInstancePhase: true,
306
+ };
307
+ }
308
+ const hasProperty = (name) => definition.properties.some((property) => {
309
+ if (property.type !== "ObjectProperty" && property.type !== "ObjectMethod") {
310
+ return false;
311
+ }
312
+ const key = property.key;
313
+ return (
314
+ (key.type === "Identifier" && key.name === name) ||
315
+ (key.type === "StringLiteral" && key.value === name)
316
+ );
317
+ });
318
+ return {
319
+ hasStaticPhase: hasProperty("static"),
320
+ hasInstancePhase: hasProperty("setup") || hasProperty("createState") || hasProperty("middlewares"),
321
+ };
322
+ }
323
+
324
+ function analyzeModule(filename) {
325
+ const normalizedFilename = normalizeFilePath(filename);
326
+ if (!normalizedFilename) return null;
327
+ if (moduleCache.has(normalizedFilename)) {
328
+ return moduleCache.get(normalizedFilename);
329
+ }
330
+
331
+ const source = readFile(normalizedFilename);
332
+ if (typeof source !== "string") {
333
+ moduleCache.set(normalizedFilename, null);
334
+ return null;
335
+ }
336
+
337
+ const analysis = {
338
+ filename: normalizedFilename,
339
+ importBindings: new Map(),
340
+ exportBindings: new Map(),
341
+ exportAllSources: [],
342
+ defineHookLocals: new Set(),
343
+ runtimeNamespaceLocals: new Set(),
344
+ structuralLocals: new Set(),
345
+ structuralLocalInfo: new Map(),
346
+ customHookPaths: new Map(),
347
+ customHookUsageCache: new Map(),
348
+ customHookRuntimeUsageCache: new Map(),
349
+ };
350
+ moduleCache.set(normalizedFilename, analysis);
351
+
352
+ let ast;
353
+ try {
354
+ ast = parser.parse(source, {
355
+ sourceType: "module",
356
+ plugins: getParserPluginsForModule(normalizedFilename, source),
357
+ });
358
+ } catch {
359
+ moduleCache.set(normalizedFilename, null);
360
+ return null;
361
+ }
362
+
363
+ getTraverse()(ast, {
364
+ Program(programPath) {
365
+ for (const statementPath of programPath.get("body")) {
366
+ const node = statementPath.node;
367
+
368
+ if (statementPath.isImportDeclaration()) {
369
+ const sourceValue = node.source.value;
370
+ for (const specifier of node.specifiers) {
371
+ const localName = specifier.local?.name;
372
+ if (!localName) continue;
373
+ const importedName = getImportedName(specifier);
374
+ analysis.importBindings.set(localName, {
375
+ importedName,
376
+ source: sourceValue,
377
+ });
378
+ if (sourceValue === RUNTIME_MODULE && importedName === "defineHook") {
379
+ analysis.defineHookLocals.add(localName);
380
+ }
381
+ if (
382
+ sourceValue === RUNTIME_MODULE &&
383
+ (specifier.type === "ImportNamespaceSpecifier" ||
384
+ specifier.type === "ImportDefaultSpecifier")
385
+ ) {
386
+ analysis.runtimeNamespaceLocals.add(localName);
387
+ }
388
+ }
389
+ continue;
390
+ }
391
+
392
+ const declarationPath = statementPath.isExportNamedDeclaration()
393
+ ? statementPath.get("declaration")
394
+ : statementPath;
395
+
396
+ if (declarationPath?.isFunctionDeclaration?.()) {
397
+ const localName = declarationPath.node.id?.name;
398
+ if (localName && /^use[A-Z0-9]/.test(localName)) {
399
+ analysis.customHookPaths.set(localName, declarationPath);
400
+ }
401
+ }
402
+
403
+ if (declarationPath?.isVariableDeclaration?.()) {
404
+ for (const declaratorPath of declarationPath.get("declarations")) {
405
+ const id = declaratorPath.node.id;
406
+ if (id?.type !== "Identifier") continue;
407
+ const init = declaratorPath.node.init;
408
+ if (init?.type === "CallExpression" && isDefineHookCallee(init.callee, analysis)) {
409
+ analysis.structuralLocals.add(id.name);
410
+ analysis.structuralLocalInfo.set(id.name, getDefineHookPhaseInfo(init));
411
+ } else if (
412
+ /^use[A-Z0-9]/.test(id.name) &&
413
+ (
414
+ init?.type === "FunctionExpression" ||
415
+ init?.type === "ArrowFunctionExpression"
416
+ )
417
+ ) {
418
+ analysis.customHookPaths.set(id.name, declaratorPath.get("init"));
419
+ }
420
+ }
421
+ }
422
+
423
+ if (statementPath.isExportAllDeclaration()) {
424
+ analysis.exportAllSources.push({
425
+ source: node.source.value,
426
+ });
427
+ continue;
428
+ }
429
+
430
+ if (statementPath.isExportNamedDeclaration()) {
431
+ const exportNode = statementPath.node;
432
+ const declaration = exportNode.declaration;
433
+ if (declaration?.type === "VariableDeclaration") {
434
+ for (const declarator of declaration.declarations) {
435
+ const localName = declarator.id?.name;
436
+ if (localName) {
437
+ addExportBinding(analysis, localName, { localName });
438
+ }
439
+ }
440
+ } else if (
441
+ declaration?.type === "FunctionDeclaration" ||
442
+ declaration?.type === "ClassDeclaration"
443
+ ) {
444
+ addExportBinding(analysis, declaration.id?.name, {
445
+ localName: declaration.id?.name,
446
+ });
447
+ }
448
+
449
+ for (const specifier of exportNode.specifiers) {
450
+ const exportedName = specifier.exported?.name ?? specifier.exported?.value ?? null;
451
+ if (!exportedName) continue;
452
+ const localName = specifier.local?.name ?? specifier.local?.value ?? exportedName;
453
+ if (exportNode.source?.value) {
454
+ addExportBinding(analysis, exportedName, {
455
+ importedName: localName,
456
+ source: exportNode.source.value,
457
+ });
458
+ } else {
459
+ addExportBinding(analysis, exportedName, { localName });
460
+ }
461
+ }
462
+ }
463
+ }
464
+ },
465
+ });
466
+
467
+ return analysis;
468
+ }
469
+
470
+ function isNamespaceStructuralUse(analysis, objectName, propertyName, seen) {
471
+ const importInfo = analysis.importBindings.get(objectName);
472
+ const resolvedSource = resolveModuleReference(analysis, importInfo);
473
+ if (!resolvedSource || importInfo.importedName !== "*") {
474
+ return false;
475
+ }
476
+ const importedModule = analyzeModule(resolvedSource);
477
+ return (
478
+ isStructuralExport(importedModule, propertyName, seen) ||
479
+ isStructuralCustomExport(importedModule, propertyName, seen)
480
+ );
481
+ }
482
+
483
+ function isNamespaceRuntimeHelperUse(analysis, objectName, propertyName) {
484
+ const importInfo = analysis.importBindings.get(objectName);
485
+ return (
486
+ importInfo?.source === RUNTIME_MODULE &&
487
+ importInfo.importedName === "*" &&
488
+ RUNTIME_HELPERS.includes(propertyName)
489
+ );
490
+ }
491
+
492
+ function isRuntimeHelperImport(analysis, localName) {
493
+ const importInfo = analysis.importBindings.get(localName);
494
+ return (
495
+ importInfo?.source === RUNTIME_MODULE &&
496
+ RUNTIME_HELPERS.includes(importInfo.importedName)
497
+ );
498
+ }
499
+
500
+ function localCustomHookUsesStructural(analysis, localName, seen = new Set()) {
501
+ if (!analysis || !localName) return false;
502
+ const key = `${analysis.filename}:local:${localName}`;
503
+ if (analysis.customHookUsageCache.has(localName)) {
504
+ return analysis.customHookUsageCache.get(localName);
505
+ }
506
+ if (seen.has(key)) return false;
507
+ const nextSeen = new Set(seen);
508
+ nextSeen.add(key);
509
+
510
+ const fnPath = analysis.customHookPaths.get(localName);
511
+ if (!fnPath?.traverse) {
512
+ analysis.customHookUsageCache.set(localName, false);
513
+ return false;
514
+ }
515
+
516
+ let usesStructural = false;
517
+ fnPath.traverse({
518
+ CallExpression(callPath) {
519
+ if (usesStructural) {
520
+ callPath.stop();
521
+ return;
522
+ }
523
+ const callee = callPath.get("callee");
524
+ if (callee.isIdentifier()) {
525
+ const name = callee.node.name;
526
+ if (analysis.structuralLocals.has(name)) {
527
+ usesStructural = true;
528
+ callPath.stop();
529
+ return;
530
+ }
531
+ if (
532
+ analysis.customHookPaths.has(name) &&
533
+ localCustomHookUsesStructural(analysis, name, nextSeen)
534
+ ) {
535
+ usesStructural = true;
536
+ callPath.stop();
537
+ return;
538
+ }
539
+ const importInfo = analysis.importBindings.get(name);
540
+ const resolvedSource = resolveModuleReference(analysis, importInfo);
541
+ if (resolvedSource && importInfo.importedName !== "*") {
542
+ const importedModule = analyzeModule(resolvedSource);
543
+ if (
544
+ isStructuralExport(importedModule, importInfo.importedName, nextSeen) ||
545
+ isStructuralCustomExport(importedModule, importInfo.importedName, nextSeen)
546
+ ) {
547
+ usesStructural = true;
548
+ callPath.stop();
549
+ }
550
+ }
551
+ return;
552
+ }
553
+
554
+ if (callee.isMemberExpression({ computed: false })) {
555
+ const object = callee.get("object");
556
+ const property = callee.get("property");
557
+ if (
558
+ object.isIdentifier() &&
559
+ property.isIdentifier() &&
560
+ isNamespaceStructuralUse(analysis, object.node.name, property.node.name, nextSeen)
561
+ ) {
562
+ usesStructural = true;
563
+ callPath.stop();
564
+ }
565
+ }
566
+ },
567
+ });
568
+
569
+ analysis.customHookUsageCache.set(localName, usesStructural);
570
+ return usesStructural;
571
+ }
572
+
573
+ function localCustomHookUsesRuntimeHook(analysis, localName, seen = new Set()) {
574
+ if (!analysis || !localName) return false;
575
+ const key = `${analysis.filename}:runtime-local:${localName}`;
576
+ if (analysis.customHookRuntimeUsageCache.has(localName)) {
577
+ return analysis.customHookRuntimeUsageCache.get(localName);
578
+ }
579
+ if (seen.has(key)) return false;
580
+ const nextSeen = new Set(seen);
581
+ nextSeen.add(key);
582
+
583
+ const fnPath = analysis.customHookPaths.get(localName);
584
+ if (!fnPath?.traverse) {
585
+ analysis.customHookRuntimeUsageCache.set(localName, false);
586
+ return false;
587
+ }
588
+
589
+ let usesRuntimeHook = false;
590
+ fnPath.traverse({
591
+ CallExpression(callPath) {
592
+ if (usesRuntimeHook) {
593
+ callPath.stop();
594
+ return;
595
+ }
596
+ const callee = callPath.get("callee");
597
+ if (callee.isIdentifier()) {
598
+ const name = callee.node.name;
599
+ if (isRuntimeHelperImport(analysis, name)) {
600
+ usesRuntimeHook = true;
601
+ callPath.stop();
602
+ return;
603
+ }
604
+ if (
605
+ analysis.customHookPaths.has(name) &&
606
+ localCustomHookUsesRuntimeHook(analysis, name, nextSeen)
607
+ ) {
608
+ usesRuntimeHook = true;
609
+ callPath.stop();
610
+ return;
611
+ }
612
+ const importInfo = analysis.importBindings.get(name);
613
+ const resolvedSource = resolveModuleReference(analysis, importInfo);
614
+ if (resolvedSource && importInfo.importedName !== "*") {
615
+ const importedModule = analyzeModule(resolvedSource);
616
+ if (isRuntimeCustomExport(importedModule, importInfo.importedName, nextSeen)) {
617
+ usesRuntimeHook = true;
618
+ callPath.stop();
619
+ }
620
+ }
621
+ return;
622
+ }
623
+
624
+ if (callee.isMemberExpression({ computed: false })) {
625
+ const object = callee.get("object");
626
+ const property = callee.get("property");
627
+ if (!object.isIdentifier() || !property.isIdentifier()) {
628
+ return;
629
+ }
630
+ if (isNamespaceRuntimeHelperUse(analysis, object.node.name, property.node.name)) {
631
+ usesRuntimeHook = true;
632
+ callPath.stop();
633
+ return;
634
+ }
635
+ const importInfo = analysis.importBindings.get(object.node.name);
636
+ const resolvedSource = resolveModuleReference(analysis, importInfo);
637
+ if (resolvedSource && importInfo.importedName === "*") {
638
+ const importedModule = analyzeModule(resolvedSource);
639
+ if (isRuntimeCustomExport(importedModule, property.node.name, nextSeen)) {
640
+ usesRuntimeHook = true;
641
+ callPath.stop();
642
+ }
643
+ }
644
+ }
645
+ },
646
+ });
647
+
648
+ analysis.customHookRuntimeUsageCache.set(localName, usesRuntimeHook);
649
+ return usesRuntimeHook;
650
+ }
651
+
652
+ function getStructuralExportInfo(analysis, exportedName, seen = new Set()) {
653
+ if (!analysis || !exportedName) return false;
654
+ const key = `${analysis.filename}:${exportedName}`;
655
+ if (seen.has(key)) return false;
656
+ const nextSeen = new Set(seen);
657
+ nextSeen.add(key);
658
+
659
+ const exportInfo = analysis.exportBindings.get(exportedName);
660
+ if (!exportInfo) {
661
+ for (const exportAll of analysis.exportAllSources || []) {
662
+ const resolvedSource = resolveModuleReference(analysis, exportAll);
663
+ if (!resolvedSource) continue;
664
+ const info = getStructuralExportInfo(
665
+ analyzeModule(resolvedSource),
666
+ exportedName,
667
+ nextSeen
668
+ );
669
+ if (info) return info;
670
+ }
671
+ return false;
672
+ }
673
+
674
+ const exportSource = resolveModuleReference(analysis, exportInfo);
675
+ if (exportSource) {
676
+ return getStructuralExportInfo(
677
+ analyzeModule(exportSource),
678
+ exportInfo.importedName,
679
+ nextSeen
680
+ );
681
+ }
682
+
683
+ if (analysis.structuralLocals.has(exportInfo.localName)) {
684
+ return {
685
+ kind: "structural-hook",
686
+ ...(analysis.structuralLocalInfo.get(exportInfo.localName) || {
687
+ hasStaticPhase: false,
688
+ hasInstancePhase: true,
689
+ }),
690
+ };
691
+ }
692
+
693
+ const importInfo = analysis.importBindings.get(exportInfo.localName);
694
+ const importSource = resolveModuleReference(analysis, importInfo);
695
+ if (importSource && importInfo.importedName !== "*") {
696
+ return getStructuralExportInfo(
697
+ analyzeModule(importSource),
698
+ importInfo.importedName,
699
+ nextSeen
700
+ );
701
+ }
702
+
703
+ return false;
704
+ }
705
+
706
+ function isStructuralExport(analysis, exportedName, seen = new Set()) {
707
+ return Boolean(getStructuralExportInfo(analysis, exportedName, seen));
708
+ }
709
+
710
+ function isStructuralCustomExport(analysis, exportedName, seen = new Set()) {
711
+ if (!analysis || !exportedName) return false;
712
+ const key = `${analysis.filename}:custom:${exportedName}`;
713
+ if (seen.has(key)) return false;
714
+ const nextSeen = new Set(seen);
715
+ nextSeen.add(key);
716
+
717
+ const exportInfo = analysis.exportBindings.get(exportedName);
718
+ if (!exportInfo) {
719
+ for (const exportAll of analysis.exportAllSources || []) {
720
+ const resolvedSource = resolveModuleReference(analysis, exportAll);
721
+ if (!resolvedSource) continue;
722
+ if (
723
+ isStructuralCustomExport(
724
+ analyzeModule(resolvedSource),
725
+ exportedName,
726
+ nextSeen
727
+ )
728
+ ) {
729
+ return true;
730
+ }
731
+ }
732
+ return false;
733
+ }
734
+
735
+ const exportSource = resolveModuleReference(analysis, exportInfo);
736
+ if (exportSource) {
737
+ return isStructuralCustomExport(
738
+ analyzeModule(exportSource),
739
+ exportInfo.importedName,
740
+ nextSeen
741
+ );
742
+ }
743
+
744
+ if (localCustomHookUsesStructural(analysis, exportInfo.localName, nextSeen)) {
745
+ return true;
746
+ }
747
+
748
+ const importInfo = analysis.importBindings.get(exportInfo.localName);
749
+ const importSource = resolveModuleReference(analysis, importInfo);
750
+ if (importSource && importInfo.importedName !== "*") {
751
+ return isStructuralCustomExport(
752
+ analyzeModule(importSource),
753
+ importInfo.importedName,
754
+ nextSeen
755
+ );
756
+ }
757
+
758
+ return false;
759
+ }
760
+
761
+ function hasExportBinding(analysis, exportedName, seen = new Set()) {
762
+ if (!analysis || !exportedName) return "unresolved";
763
+ const key = `${analysis.filename}:has-export:${exportedName}`;
764
+ if (seen.has(key)) return false;
765
+ const nextSeen = new Set(seen);
766
+ nextSeen.add(key);
767
+
768
+ if (analysis.exportBindings.has(exportedName)) {
769
+ return true;
770
+ }
771
+
772
+ let sawUnresolvedExportAll = false;
773
+ for (const exportAll of analysis.exportAllSources || []) {
774
+ const resolvedSource = resolveModuleReference(analysis, exportAll);
775
+ if (!resolvedSource) {
776
+ sawUnresolvedExportAll = true;
777
+ continue;
778
+ }
779
+ const result = hasExportBinding(
780
+ analyzeModule(resolvedSource),
781
+ exportedName,
782
+ nextSeen
783
+ );
784
+ if (result === true) return true;
785
+ if (result === "unresolved") sawUnresolvedExportAll = true;
786
+ }
787
+
788
+ return sawUnresolvedExportAll ? "unresolved" : false;
789
+ }
790
+
791
+ function isRuntimeCustomExport(analysis, exportedName, seen = new Set()) {
792
+ if (!analysis || !exportedName) return false;
793
+ const key = `${analysis.filename}:runtime-custom:${exportedName}`;
794
+ if (seen.has(key)) return false;
795
+ const nextSeen = new Set(seen);
796
+ nextSeen.add(key);
797
+
798
+ const exportInfo = analysis.exportBindings.get(exportedName);
799
+ if (!exportInfo) {
800
+ for (const exportAll of analysis.exportAllSources || []) {
801
+ const resolvedSource = resolveModuleReference(analysis, exportAll);
802
+ if (!resolvedSource) continue;
803
+ if (
804
+ isRuntimeCustomExport(
805
+ analyzeModule(resolvedSource),
806
+ exportedName,
807
+ nextSeen
808
+ )
809
+ ) {
810
+ return true;
811
+ }
812
+ }
813
+ return false;
814
+ }
815
+
816
+ const exportSource = resolveModuleReference(analysis, exportInfo);
817
+ if (exportSource) {
818
+ return isRuntimeCustomExport(
819
+ analyzeModule(exportSource),
820
+ exportInfo.importedName,
821
+ nextSeen
822
+ );
823
+ }
824
+
825
+ if (localCustomHookUsesRuntimeHook(analysis, exportInfo.localName, nextSeen)) {
826
+ return true;
827
+ }
828
+
829
+ const importInfo = analysis.importBindings.get(exportInfo.localName);
830
+ const importSource = resolveModuleReference(analysis, importInfo);
831
+ if (importSource && importInfo.importedName !== "*") {
832
+ return isRuntimeCustomExport(
833
+ analyzeModule(importSource),
834
+ importInfo.importedName,
835
+ nextSeen
836
+ );
837
+ }
838
+
839
+ return false;
840
+ }
841
+
842
+ return function structuralHookResolver({ filename, source, importedName, runtimeCustomOnly = false }) {
843
+ const resolved = resolveImport(filename, source);
844
+ if (!resolved) return runtimeCustomOnly ? "unresolved-custom-hook" : false;
845
+ const analysis = analyzeModule(resolved);
846
+ if (!analysis && runtimeCustomOnly) {
847
+ return "unresolved-custom-hook";
848
+ }
849
+ if (runtimeCustomOnly) {
850
+ const exportStatus = hasExportBinding(analysis, importedName);
851
+ if (exportStatus !== true) {
852
+ return "unresolved-custom-hook";
853
+ }
854
+ return isRuntimeCustomExport(analysis, importedName)
855
+ ? "runtime-custom-hook"
856
+ : false;
857
+ }
858
+ const structuralInfo = getStructuralExportInfo(analysis, importedName);
859
+ if (structuralInfo) {
860
+ return structuralInfo;
861
+ }
862
+ if (isStructuralCustomExport(analysis, importedName)) {
863
+ return "structural-custom-hook";
864
+ }
865
+ return false;
866
+ };
867
+ }
868
+
34
869
  function normalizePath(value) {
35
870
  return String(value || "").replace(/\\/g, "/");
36
871
  }
@@ -62,12 +897,36 @@ function createStableIdCallsiteMetadata(callPath, state, t) {
62
897
  return t.stringLiteral(`litsx-stable-${hashStableId(seed)}`);
63
898
  }
64
899
 
65
- export default createRuntimeHooksTransform({
66
- pluginName: "transform-litsx-hooks",
67
- runtimeModule: RUNTIME_MODULE,
68
- importSources: IMPORT_SOURCES,
69
- helperNames: RUNTIME_HELPERS,
70
- callMetadataByHelper: {
71
- useStableId: createStableIdCallsiteMetadata,
72
- },
73
- });
900
+ export default function transformLitsxHooks(api, options = {}) {
901
+ const structuralHookResolver = createStructuralHookResolver(options);
902
+ const ignoredCustomHookSources = new Set(options.ignoredCustomHookSources || []);
903
+ const plugin = createRuntimeHooksTransform({
904
+ pluginName: "transform-litsx-hooks",
905
+ runtimeModule: RUNTIME_MODULE,
906
+ importSources: IMPORT_SOURCES,
907
+ helperNames: RUNTIME_HELPERS,
908
+ callMetadataByHelper: {
909
+ useStableId: createStableIdCallsiteMetadata,
910
+ },
911
+ });
912
+
913
+ return plugin(api, {
914
+ ...options,
915
+ structuralHookResolver,
916
+ customHookResolver({ filename, source, importedName }) {
917
+ if (ignoredCustomHookSources.has(source)) {
918
+ return false;
919
+ }
920
+ const result = structuralHookResolver({
921
+ filename,
922
+ source,
923
+ importedName,
924
+ runtimeCustomOnly: true,
925
+ });
926
+ if (result === "unresolved-custom-hook") {
927
+ return result;
928
+ }
929
+ return result === "runtime-custom-hook";
930
+ },
931
+ });
932
+ }