@bamboocss/extractor 1.11.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/dist/index.mjs ADDED
@@ -0,0 +1,1947 @@
1
+ // src/get-typeof-literal.ts
2
+ import { BambooError } from "@bamboocss/shared";
3
+ var getTypeOfLiteral = (value) => {
4
+ if (Array.isArray(value)) return "array";
5
+ if (typeof value === "string") return "string";
6
+ if (typeof value === "number") return "number";
7
+ if (typeof value === "boolean") return "boolean";
8
+ if (value === null) return "null";
9
+ if (value === void 0) return "undefined";
10
+ throw new BambooError(
11
+ "UNKNOWN_LITERAL_TYPE",
12
+ `Unexpected literal type: ${value}. Expected: string, number, boolean, null, undefined, or array`
13
+ );
14
+ };
15
+
16
+ // src/utils.ts
17
+ import { Node } from "ts-morph";
18
+ var isNotNullish = (element) => element != null;
19
+ var isNullish = (element) => element == null;
20
+ var isTruthyOrZero = (element) => !!element || element === 0;
21
+ var isObject = (value) => value != null && typeof value === "object";
22
+ var isArray = (value) => Array.isArray(value);
23
+ var isPrimitiveType = (value) => {
24
+ const type = typeof value;
25
+ return type === "string" || type === "number" || type === "boolean" || value === null || value === void 0;
26
+ };
27
+ var unwrapExpression = (node) => {
28
+ if (Node.isAsExpression(node)) {
29
+ return unwrapExpression(node.getExpression());
30
+ }
31
+ if (Node.isParenthesizedExpression(node)) {
32
+ return unwrapExpression(node.getExpression());
33
+ }
34
+ if (Node.isNonNullExpression(node)) {
35
+ return unwrapExpression(node.getExpression());
36
+ }
37
+ if (Node.isTypeAssertion(node)) {
38
+ return unwrapExpression(node.getExpression());
39
+ }
40
+ if (Node.isSatisfiesExpression(node)) {
41
+ return unwrapExpression(node.getExpression());
42
+ }
43
+ return node;
44
+ };
45
+ var getComponentName = (node) => {
46
+ return node.getTagNameNode().getText();
47
+ };
48
+ var whitespaceRegex = /\s+/g;
49
+ var trimWhitespace = (str) => {
50
+ return str.replaceAll(whitespaceRegex, " ");
51
+ };
52
+
53
+ // src/get-node-range.ts
54
+ var getNodeRange = (node) => {
55
+ const src = node.getSourceFile();
56
+ const [startPosition, endPosition] = [node.getStart(), node.getEnd()];
57
+ const startInfo = src.getLineAndColumnAtPos(startPosition);
58
+ const endInfo = src.getLineAndColumnAtPos(endPosition);
59
+ return {
60
+ startPosition,
61
+ startLineNumber: startInfo.line,
62
+ startColumn: startInfo.column,
63
+ endPosition,
64
+ endLineNumber: endInfo.line,
65
+ endColumn: endInfo.column
66
+ };
67
+ };
68
+
69
+ // src/box-factory.ts
70
+ var BoxNodeType = class {
71
+ type;
72
+ stack = [];
73
+ node;
74
+ constructor(definition) {
75
+ this.type = definition.type;
76
+ this.node = definition.node;
77
+ this.stack = [...definition.stack ?? []];
78
+ }
79
+ getNode() {
80
+ return this.node;
81
+ }
82
+ getStack() {
83
+ return this.stack;
84
+ }
85
+ getRange = () => getNodeRange(this.node);
86
+ toJSON() {
87
+ const range = this.getRange();
88
+ return {
89
+ type: this.type,
90
+ // @ts-expect-error
91
+ value: this.value,
92
+ node: this.node.getKindName(),
93
+ line: range.startLineNumber,
94
+ column: range.startColumn,
95
+ endLineNumber: range.endLineNumber,
96
+ endColumn: range.endColumn
97
+ };
98
+ }
99
+ toString() {
100
+ return JSON.stringify(this.toJSON(), null, 2);
101
+ }
102
+ };
103
+ var BoxNodeObject = class extends BoxNodeType {
104
+ value;
105
+ isEmpty;
106
+ constructor(definition) {
107
+ super(definition);
108
+ this.value = definition.value;
109
+ this.isEmpty = definition.isEmpty;
110
+ }
111
+ };
112
+ var BoxNodeLiteral = class extends BoxNodeType {
113
+ value;
114
+ kind;
115
+ constructor(definition) {
116
+ super(definition);
117
+ this.value = definition.value;
118
+ this.kind = definition.kind;
119
+ }
120
+ };
121
+ var recipeProps = ["compoundVariants", "defaultVariants", "variants", "base"];
122
+ var BoxNodeMap = class extends BoxNodeType {
123
+ value;
124
+ spreadConditions;
125
+ constructor(definition) {
126
+ super(definition);
127
+ this.value = definition.value;
128
+ }
129
+ isRecipe = () => {
130
+ return recipeProps.some((prop) => this.value.has(prop));
131
+ };
132
+ };
133
+ var BoxNodeArray = class extends BoxNodeType {
134
+ value;
135
+ constructor(definition) {
136
+ super(definition);
137
+ this.value = definition.value;
138
+ }
139
+ };
140
+ var BoxNodeUnresolvable = class extends BoxNodeType {
141
+ };
142
+ var BoxNodeConditional = class extends BoxNodeType {
143
+ whenTrue;
144
+ whenFalse;
145
+ constructor(definition) {
146
+ super(definition);
147
+ this.whenTrue = definition.whenTrue;
148
+ this.whenFalse = definition.whenFalse;
149
+ }
150
+ };
151
+ var BoxNodeEmptyInitializer = class extends BoxNodeType {
152
+ };
153
+ var isBoxNode = (value) => value instanceof BoxNodeType;
154
+
155
+ // src/to-box-node.ts
156
+ function toBoxNode(value, node, stack) {
157
+ if (isNullish(value)) return;
158
+ if (isBoxNode(value)) return value;
159
+ if (isObject(value)) return box.object(value, node, stack);
160
+ if (isArray(value)) {
161
+ if (value.length === 1) return toBoxNode(value[0], node, stack);
162
+ return value.map((item) => toBoxNode(item, node, stack));
163
+ }
164
+ if (isPrimitiveType(value)) return box.literal(value, node, stack);
165
+ }
166
+
167
+ // src/box.ts
168
+ var box = {
169
+ object(value, node, stack) {
170
+ return new BoxNodeObject({ type: "object", value, node, stack });
171
+ },
172
+ literal(value, node, stack) {
173
+ return new BoxNodeLiteral({ type: "literal", value, kind: getTypeOfLiteral(value), node, stack });
174
+ },
175
+ map(value, node, stack) {
176
+ return new BoxNodeMap({ type: "map", value, node, stack });
177
+ },
178
+ array(value, node, stack) {
179
+ return new BoxNodeArray({ type: "array", value, node, stack });
180
+ },
181
+ conditional(whenTrue, whenFalse, node, stack) {
182
+ return new BoxNodeConditional({ type: "conditional", whenTrue, whenFalse, node, stack });
183
+ },
184
+ from: toBoxNode,
185
+ objectToMap: (value, node, stack) => {
186
+ const map = new Map(
187
+ Object.entries(value).map(([k, v]) => {
188
+ const boxed = box.from(v, node, stack);
189
+ return [k, boxed || null];
190
+ })
191
+ );
192
+ return new BoxNodeMap({ type: "map", value: map, node, stack });
193
+ },
194
+ //
195
+ emptyObject: (node, stack) => {
196
+ return new BoxNodeObject({ type: "object", value: {}, isEmpty: true, node, stack });
197
+ },
198
+ emptyInitializer: (node, stack) => {
199
+ return new BoxNodeEmptyInitializer({ type: "empty-initializer", node, stack });
200
+ },
201
+ unresolvable: (node, stack) => {
202
+ return new BoxNodeUnresolvable({ type: "unresolvable", node, stack });
203
+ },
204
+ fallback(node) {
205
+ return {
206
+ value: void 0,
207
+ getNode: () => node.getNode(),
208
+ getStack: () => node.getStack(),
209
+ getRange: () => node.getRange()
210
+ };
211
+ },
212
+ /**
213
+ * box.type === “object” -> object that was resolved using ts-evaluator, most likely from a
214
+ * complex condition OR a simple CallExpression eval result, we don’t have access to individual
215
+ * AST nodes here so we need the distinction
216
+ */
217
+ isObject(value) {
218
+ return value?.type === "object";
219
+ },
220
+ isLiteral(value) {
221
+ return value?.type === "literal";
222
+ },
223
+ /**
224
+ * box.type === “map” -> basically any object that was statically analyzable, we store each
225
+ * prop + value in a Map
226
+ */
227
+ isMap(value) {
228
+ return value?.type === "map";
229
+ },
230
+ isRecipe(value) {
231
+ return box.isMap(value) && value.isRecipe();
232
+ },
233
+ isArray(value) {
234
+ return value?.type === "array";
235
+ },
236
+ isUnresolvable(value) {
237
+ return value?.type === "unresolvable";
238
+ },
239
+ isConditional(value) {
240
+ return value?.type === "conditional";
241
+ },
242
+ isEmptyInitializer(value) {
243
+ return value?.type === "empty-initializer";
244
+ },
245
+ isNumberLiteral(node) {
246
+ return box.isLiteral(node) && node.kind === "number";
247
+ },
248
+ hasValue: (node) => {
249
+ return box.isObject(node) || box.isLiteral(node) || box.isMap(node) || box.isArray(node);
250
+ }
251
+ };
252
+
253
+ // src/maybe-box-node.ts
254
+ import { Node as Node5, ts as ts2 } from "ts-morph";
255
+
256
+ // src/evaluate-node.ts
257
+ import { evaluate } from "ts-evaluator";
258
+ import { ts } from "ts-morph";
259
+ var TsEvalError = Symbol("EvalError");
260
+ var cacheMap = /* @__PURE__ */ new WeakMap();
261
+ var evaluateNode = (node, stack, ctx) => {
262
+ if (ctx.flags?.skipEvaluate) return;
263
+ if (ctx.canEval && !ctx.canEval?.(node, stack)) return;
264
+ if (cacheMap.has(node)) {
265
+ return cacheMap.get(node);
266
+ }
267
+ const result = evaluate({
268
+ policy: {
269
+ deterministic: true,
270
+ network: false,
271
+ console: false,
272
+ maxOps: Number.POSITIVE_INFINITY,
273
+ maxOpDuration: 1e3,
274
+ io: { read: true, write: false },
275
+ process: { exit: false, spawnChild: false }
276
+ },
277
+ ...ctx.getEvaluateOptions?.(node, stack),
278
+ node: node.compilerNode,
279
+ typescript: ts
280
+ });
281
+ const expr = result.success ? result.value : TsEvalError;
282
+ cacheMap.set(node, expr);
283
+ return expr;
284
+ };
285
+ var safeEvaluateNode = (node, stack, ctx) => {
286
+ const result = evaluateNode(node, stack, ctx);
287
+ if (result === TsEvalError) return;
288
+ return result;
289
+ };
290
+
291
+ // src/find-identifier-value-declaration.ts
292
+ import {
293
+ Node as Node2
294
+ } from "ts-morph";
295
+ function isScope(node) {
296
+ return Node2.isFunctionDeclaration(node) || Node2.isFunctionExpression(node) || Node2.isArrowFunction(node) || Node2.isSourceFile(node);
297
+ }
298
+ function getDeclarationFor(node, stack, ctx) {
299
+ const parent = node.getParent();
300
+ if (!parent) return;
301
+ const declarationStack = [];
302
+ let declaration;
303
+ if ((Node2.isVariableDeclaration(parent) || Node2.isParameterDeclaration(parent) || Node2.isFunctionDeclaration(parent) || Node2.isEnumDeclaration(parent) || Node2.isBindingElement(parent)) && parent.getNameNode() == node) {
304
+ declarationStack.push(parent);
305
+ declaration = parent;
306
+ } else if (Node2.isImportSpecifier(parent) && parent.getNameNode() == node) {
307
+ if (ctx.flags?.skipTraverseFiles) return;
308
+ const sourceFile = getModuleSpecifierSourceFile(parent.getImportDeclaration());
309
+ if (sourceFile) {
310
+ const exportStack = [parent, sourceFile];
311
+ const maybeVar = getExportedVarDeclarationWithName(node.getText(), sourceFile, exportStack, ctx);
312
+ if (maybeVar) {
313
+ declarationStack.push(...exportStack.concat(maybeVar));
314
+ declaration = maybeVar;
315
+ }
316
+ }
317
+ }
318
+ if (declaration) {
319
+ stack.push(...declarationStack);
320
+ }
321
+ return declaration;
322
+ }
323
+ var getInnermostScope = (from) => {
324
+ let scope = from.getParent();
325
+ while (scope && !isScope(scope)) {
326
+ scope = scope.getParent();
327
+ }
328
+ return scope;
329
+ };
330
+ function findIdentifierValueDeclaration(identifier, stack, ctx, visitedsWithStack = /* @__PURE__ */ new Map()) {
331
+ let scope = identifier;
332
+ let foundNode;
333
+ let isUnresolvable = false;
334
+ let count = 0;
335
+ const innerStack = [];
336
+ do {
337
+ scope = getInnermostScope(scope);
338
+ count++;
339
+ if (!scope) return;
340
+ const refName = identifier.getText();
341
+ scope.forEachDescendant((node, traversal) => {
342
+ if (visitedsWithStack.has(node)) {
343
+ traversal.skip();
344
+ innerStack.push(...visitedsWithStack.get(node));
345
+ return;
346
+ }
347
+ if (node == identifier) return;
348
+ visitedsWithStack.set(node, innerStack);
349
+ if (Node2.isIdentifier(node) && node.getText() == refName) {
350
+ const declarationStack = [node];
351
+ const maybeDeclaration = getDeclarationFor(node, declarationStack, ctx);
352
+ if (maybeDeclaration) {
353
+ if (Node2.isParameterDeclaration(maybeDeclaration)) {
354
+ const initializer = maybeDeclaration.getInitializer();
355
+ const typeNode = maybeDeclaration.getTypeNode();
356
+ if (initializer) {
357
+ innerStack.push(...declarationStack.concat(initializer));
358
+ foundNode = maybeDeclaration;
359
+ } else if (typeNode && Node2.isTypeLiteral(typeNode)) {
360
+ innerStack.push(...declarationStack.concat(typeNode));
361
+ foundNode = maybeDeclaration;
362
+ } else {
363
+ isUnresolvable = true;
364
+ }
365
+ traversal.stop();
366
+ return;
367
+ }
368
+ innerStack.push(...declarationStack);
369
+ foundNode = maybeDeclaration;
370
+ traversal.stop();
371
+ }
372
+ }
373
+ });
374
+ if (foundNode || isUnresolvable) {
375
+ if (foundNode) {
376
+ stack.push(...innerStack);
377
+ }
378
+ return foundNode;
379
+ }
380
+ } while (scope && !Node2.isSourceFile(scope) && !foundNode && !isUnresolvable && count < 100);
381
+ }
382
+
383
+ // src/get-object-literal-expression-prop-pairs.ts
384
+ import { Node as Node4 } from "ts-morph";
385
+
386
+ // src/get-property-name.ts
387
+ import { Node as Node3 } from "ts-morph";
388
+ var getPropertyName = (property, stack, ctx) => {
389
+ if (!property) return;
390
+ if (Node3.isPropertyAssignment(property)) {
391
+ const node = unwrapExpression(property.getNameNode());
392
+ if (Node3.isIdentifier(node)) return box.from(node.getText(), node, stack);
393
+ if (Node3.isComputedPropertyName(node)) {
394
+ const expression = node.getExpression();
395
+ stack.push(expression);
396
+ return maybePropName(expression, stack, ctx);
397
+ }
398
+ if (Node3.isStringLiteral(node) || Node3.isNumericLiteral(node)) return box.from(node.getLiteralText(), node, stack);
399
+ }
400
+ if (Node3.isShorthandPropertyAssignment(property)) {
401
+ const name = property.getName();
402
+ if (name != null) return box.from(name, property, stack);
403
+ }
404
+ if (Node3.isGetAccessorDeclaration(property)) {
405
+ const node = unwrapExpression(property.getNameNode());
406
+ if (Node3.isIdentifier(node)) return box.from(node.getText(), node, stack);
407
+ if (Node3.isComputedPropertyName(node)) {
408
+ const expression = node.getExpression();
409
+ stack.push(expression);
410
+ return maybePropName(expression, stack, ctx);
411
+ }
412
+ if (Node3.isStringLiteral(node) || Node3.isNumericLiteral(node)) return box.from(node.getLiteralText(), node, stack);
413
+ }
414
+ };
415
+
416
+ // src/get-object-literal-expression-prop-pairs.ts
417
+ var getObjectLiteralExpressionPropPairs = (expression, expressionStack, ctx, matchProp) => {
418
+ const properties = expression.getProperties();
419
+ if (properties.length === 0) {
420
+ return box.emptyObject(expression, expressionStack);
421
+ }
422
+ const extractedPropValues = [];
423
+ const spreadConditions = [];
424
+ properties.forEach((property) => {
425
+ const stack = [...expressionStack];
426
+ stack.push(property);
427
+ if (Node4.isPropertyAssignment(property) || Node4.isShorthandPropertyAssignment(property) || Node4.isGetAccessorDeclaration(property)) {
428
+ const propNameBox = getPropertyName(property, stack, ctx);
429
+ if (!propNameBox) return;
430
+ const propName = propNameBox.value;
431
+ if (isNullish(propName)) return;
432
+ if (matchProp && !matchProp?.({ propName, propNode: property })) {
433
+ return;
434
+ }
435
+ if (Node4.isShorthandPropertyAssignment(property)) {
436
+ const initializer2 = property.getNameNode();
437
+ stack.push(initializer2);
438
+ const maybeValue2 = maybeBoxNode(initializer2, stack, ctx);
439
+ if (maybeValue2) {
440
+ extractedPropValues.push([propName.toString(), maybeValue2]);
441
+ return;
442
+ }
443
+ }
444
+ let init;
445
+ if (Node4.isGetAccessorDeclaration(property)) {
446
+ const body = property.getBody();
447
+ init = Node4.isBlock(body) ? body.getStatements().at(-1) : void 0;
448
+ } else {
449
+ init = property.getInitializer();
450
+ }
451
+ if (!init) return;
452
+ const returnExpression = Node4.isReturnStatement(init) ? init.getExpression() : void 0;
453
+ const initializer = unwrapExpression(returnExpression ?? init);
454
+ stack.push(initializer);
455
+ const maybeValue = maybeBoxNode(initializer, stack, ctx);
456
+ if (maybeValue) {
457
+ extractedPropValues.push([propName.toString(), maybeValue]);
458
+ return;
459
+ }
460
+ }
461
+ if (Node4.isSpreadAssignment(property)) {
462
+ const initializer = unwrapExpression(property.getExpression());
463
+ stack.push(initializer);
464
+ const maybeObject = maybeBoxNode(initializer, stack, ctx, matchProp);
465
+ if (!maybeObject) return;
466
+ if (box.isObject(maybeObject)) {
467
+ Object.entries(maybeObject.value).forEach(([propName, value]) => {
468
+ const boxNode = box.from(value, initializer, stack);
469
+ if (!boxNode) return;
470
+ extractedPropValues.push([propName, boxNode]);
471
+ });
472
+ return;
473
+ }
474
+ if (box.isMap(maybeObject)) {
475
+ maybeObject.value.forEach((nested, propName) => {
476
+ extractedPropValues.push([propName, nested]);
477
+ });
478
+ return;
479
+ }
480
+ if (box.isConditional(maybeObject)) {
481
+ spreadConditions.push(maybeObject);
482
+ }
483
+ }
484
+ });
485
+ const orderedMapValue = /* @__PURE__ */ new Map();
486
+ extractedPropValues.forEach(([propName, value]) => {
487
+ if (orderedMapValue.has(propName)) {
488
+ orderedMapValue.delete(propName);
489
+ }
490
+ orderedMapValue.set(propName, value);
491
+ });
492
+ const map = box.map(orderedMapValue, expression, expressionStack);
493
+ if (spreadConditions.length > 0) {
494
+ map.spreadConditions = spreadConditions;
495
+ }
496
+ return map;
497
+ };
498
+
499
+ // src/maybe-box-node.ts
500
+ var cacheMap2 = /* @__PURE__ */ new WeakMap();
501
+ var isCached = (node) => cacheMap2.has(node);
502
+ var getCached = (node) => cacheMap2.get(node);
503
+ var isPlusSyntax = (op) => op === ts2.SyntaxKind.PlusToken;
504
+ var isLogicalSyntax = (op) => op === ts2.SyntaxKind.BarBarToken || op === ts2.SyntaxKind.QuestionQuestionToken || op === ts2.SyntaxKind.AmpersandAmpersandToken || op === ts2.SyntaxKind.EqualsEqualsEqualsToken || op === ts2.SyntaxKind.EqualsEqualsToken || op === ts2.SyntaxKind.ExclamationEqualsEqualsToken || op === ts2.SyntaxKind.ExclamationEqualsToken || op === ts2.SyntaxKind.GreaterThanEqualsToken || op === ts2.SyntaxKind.GreaterThanToken || op === ts2.SyntaxKind.LessThanEqualsToken || op === ts2.SyntaxKind.LessThanToken || op === ts2.SyntaxKind.InstanceOfKeyword || op === ts2.SyntaxKind.InKeyword;
505
+ var isOperationSyntax = (op) => op === ts2.SyntaxKind.AsteriskToken || op === ts2.SyntaxKind.SlashToken || op === ts2.SyntaxKind.PercentToken || op === ts2.SyntaxKind.AsteriskAsteriskToken || op === ts2.SyntaxKind.MinusToken;
506
+ var canReturnWhenTrueInLogicalExpression = (op) => {
507
+ return op === ts2.SyntaxKind.BarBarToken || op === ts2.SyntaxKind.QuestionQuestionToken;
508
+ };
509
+ function maybeBoxNode(node, stack, ctx, matchProp) {
510
+ const cache = (value) => {
511
+ cacheMap2.set(node, value);
512
+ return value;
513
+ };
514
+ if (isCached(node)) {
515
+ return getCached(node);
516
+ }
517
+ if (Node5.isStringLiteral(node) || Node5.isNoSubstitutionTemplateLiteral(node)) {
518
+ const value = trimWhitespace(node.getLiteralValue());
519
+ return cache(box.literal(value, node, stack));
520
+ }
521
+ if (Node5.isObjectLiteralExpression(node)) {
522
+ return cache(getObjectLiteralExpressionPropPairs(node, stack, ctx, matchProp));
523
+ }
524
+ if (Node5.isTrueLiteral(node) || Node5.isFalseLiteral(node)) {
525
+ const value = node.getLiteralValue();
526
+ return cache(box.literal(value, node, stack));
527
+ }
528
+ if (Node5.isNumericLiteral(node)) {
529
+ const value = node.getLiteralValue();
530
+ return cache(box.literal(value, node, stack));
531
+ }
532
+ if (Node5.isNullLiteral(node)) {
533
+ return cache(box.literal(null, node, stack));
534
+ }
535
+ if (Node5.isPrefixUnaryExpression(node)) {
536
+ const operand = node.getOperand();
537
+ const operator = node.getOperatorToken();
538
+ const boxNode = maybeBoxNode(operand, stack, ctx);
539
+ if (!box.isNumberLiteral(boxNode)) return;
540
+ return cache(operator === ts2.SyntaxKind.MinusToken ? box.literal(-Number(boxNode.value), node, stack) : boxNode);
541
+ }
542
+ if (Node5.isArrayLiteralExpression(node)) {
543
+ const boxNodes = node.getElements().map((element) => {
544
+ return maybeBoxNode(element, stack, ctx) ?? cache(box.unresolvable(element, stack));
545
+ });
546
+ return cache(box.array(boxNodes, node, stack));
547
+ }
548
+ if (Node5.isIdentifier(node)) {
549
+ const value = node.getText();
550
+ if (value === "undefined") return cache(box.literal(void 0, node, stack));
551
+ return cache(maybeIdentifierValue(node, stack, ctx));
552
+ }
553
+ if (Node5.isTemplateHead(node)) {
554
+ return cache(box.literal(node.getLiteralText(), node, stack));
555
+ }
556
+ if (Node5.isTemplateExpression(node)) {
557
+ const value = maybeTemplateStringValue(node, stack, ctx);
558
+ return cache(box.literal(value, node, stack));
559
+ }
560
+ if (Node5.isTaggedTemplateExpression(node)) {
561
+ return cache(maybeBoxNode(node.getTemplate(), stack, ctx));
562
+ }
563
+ if (Node5.isElementAccessExpression(node)) {
564
+ return cache(getElementAccessedExpressionValue(node, stack, ctx));
565
+ }
566
+ if (Node5.isPropertyAccessExpression(node)) {
567
+ return cache(getPropertyAccessedExpressionValue(node, [], stack, ctx));
568
+ }
569
+ if (Node5.isConditionalExpression(node)) {
570
+ if (ctx.flags?.skipConditions) {
571
+ return cache(box.unresolvable(node, stack));
572
+ }
573
+ const condExpr = unwrapExpression(node.getCondition());
574
+ const condBoxNode = (Node5.isIdentifier(condExpr) ? maybeBoxNode(condExpr, [], ctx) : safeEvaluateNode(condExpr, stack, ctx)) ?? box.unresolvable(condExpr, stack);
575
+ const condValue = isBoxNode(condBoxNode) ? condBoxNode : box.from(condBoxNode, node, stack);
576
+ if (box.isEmptyInitializer(condValue)) return;
577
+ const isFromDefaultBinding = condValue.getStack().some((node2) => Node5.isBindingElement(node2));
578
+ if (box.isUnresolvable(condValue) || box.isConditional(condValue) || isFromDefaultBinding) {
579
+ const whenTrueExpr = unwrapExpression(node.getWhenTrue());
580
+ const whenFalseExpr2 = unwrapExpression(node.getWhenFalse());
581
+ return cache(maybeResolveConditionalExpression({ whenTrueExpr, whenFalseExpr: whenFalseExpr2, node, stack }, ctx));
582
+ }
583
+ if (condValue.value) {
584
+ const whenTrueExpr = unwrapExpression(node.getWhenTrue());
585
+ const innerStack2 = [...stack];
586
+ const maybeValue2 = maybeBoxNode(whenTrueExpr, innerStack2, ctx);
587
+ return cache(maybeValue2 ?? box.unresolvable(whenTrueExpr, stack));
588
+ }
589
+ const whenFalseExpr = unwrapExpression(node.getWhenFalse());
590
+ const innerStack = [...stack];
591
+ const maybeValue = maybeBoxNode(whenFalseExpr, innerStack, ctx);
592
+ return cache(maybeValue ?? box.unresolvable(node, stack));
593
+ }
594
+ if (Node5.isCallExpression(node)) {
595
+ if (ctx.tokens) {
596
+ const expr = node.getExpression();
597
+ let fnName = "";
598
+ let isVarMethod = false;
599
+ if (Node5.isIdentifier(expr)) {
600
+ fnName = expr.getText();
601
+ } else if (Node5.isPropertyAccessExpression(expr)) {
602
+ const exprName = expr.getExpression();
603
+ const propertyName = expr.getName();
604
+ if (Node5.isIdentifier(exprName)) {
605
+ fnName = exprName.getText();
606
+ isVarMethod = propertyName === "var";
607
+ }
608
+ }
609
+ const isTokenFunction = ctx.tokens.isTokenFn ? ctx.tokens.isTokenFn(fnName) : fnName === "token";
610
+ if (isTokenFunction) {
611
+ const args = node.getArguments();
612
+ const tokenPathArg = args[0];
613
+ if (tokenPathArg && Node5.isStringLiteral(tokenPathArg)) {
614
+ const tokenPath = tokenPathArg.getLiteralValue();
615
+ const fallbackArg = args[1];
616
+ const fallbackValue = fallbackArg && Node5.isStringLiteral(fallbackArg) ? fallbackArg.getLiteralValue() : void 0;
617
+ const resolvedValue = isVarMethod ? ctx.tokens.view.getVar(tokenPath, fallbackValue) : ctx.tokens.view.get(tokenPath, fallbackValue);
618
+ if (resolvedValue) {
619
+ return cache(box.literal(resolvedValue, node, stack));
620
+ }
621
+ }
622
+ }
623
+ }
624
+ const value = safeEvaluateNode(node, stack, ctx);
625
+ if (!value) return;
626
+ return cache(box.from(value, node, stack));
627
+ }
628
+ if (Node5.isBinaryExpression(node)) {
629
+ const operatorKind = node.getOperatorToken().getKind();
630
+ if (isPlusSyntax(operatorKind)) {
631
+ const value = tryComputingPlusTokenBinaryExpressionToString(node, stack, ctx) ?? safeEvaluateNode(node, stack, ctx);
632
+ if (!value) return;
633
+ return cache(box.from(value, node, stack));
634
+ }
635
+ if (isLogicalSyntax(operatorKind)) {
636
+ const whenTrueExpr = unwrapExpression(node.getLeft());
637
+ const whenFalseExpr = unwrapExpression(node.getRight());
638
+ const exprObject = {
639
+ whenTrueExpr,
640
+ whenFalseExpr,
641
+ node,
642
+ stack,
643
+ canReturnWhenTrue: canReturnWhenTrueInLogicalExpression(operatorKind)
644
+ };
645
+ return cache(maybeResolveConditionalExpression(exprObject, ctx));
646
+ }
647
+ if (isOperationSyntax(operatorKind)) {
648
+ return cache(box.literal(safeEvaluateNode(node, stack, ctx), node, stack));
649
+ }
650
+ }
651
+ }
652
+ var onlyStringLiteral = (boxNode) => {
653
+ if (!boxNode) return;
654
+ if (isBoxNode(boxNode) && box.isLiteral(boxNode) && typeof boxNode.value === "string") {
655
+ return boxNode;
656
+ }
657
+ };
658
+ var onlyNumberLiteral = (boxNode) => {
659
+ if (!boxNode) return;
660
+ if (isBoxNode(boxNode) && box.isLiteral(boxNode) && typeof boxNode.value === "number") {
661
+ return boxNode;
662
+ }
663
+ };
664
+ var maybeStringLiteral = (node, stack, ctx) => onlyStringLiteral(maybeBoxNode(node, stack, ctx));
665
+ var maybePropName = (node, stack, ctx) => {
666
+ const boxed = maybeBoxNode(node, stack, ctx);
667
+ const strBox = onlyStringLiteral(boxed);
668
+ if (strBox) return strBox;
669
+ const numberBox = onlyNumberLiteral(boxed);
670
+ if (numberBox) return numberBox;
671
+ };
672
+ var maybeResolveConditionalExpression = ({
673
+ whenTrueExpr,
674
+ whenFalseExpr,
675
+ node,
676
+ stack,
677
+ canReturnWhenTrue
678
+ }, ctx) => {
679
+ const whenTrueValue = maybeBoxNode(whenTrueExpr, stack, ctx);
680
+ const whenFalseValue = maybeBoxNode(whenFalseExpr, stack, ctx);
681
+ if (canReturnWhenTrue && whenTrueValue && !box.isUnresolvable(whenTrueValue)) {
682
+ return whenTrueValue;
683
+ }
684
+ if (Node5.isBinaryExpression(node) && node.getOperatorToken().getKind() === ts2.SyntaxKind.AmpersandAmpersandToken && whenTrueValue && whenFalseValue && box.isLiteral(whenTrueValue) && whenTrueValue.value === true) {
685
+ return whenFalseValue;
686
+ }
687
+ if (!whenTrueValue && !whenFalseValue) {
688
+ return;
689
+ }
690
+ if (whenTrueValue && !whenFalseValue) {
691
+ return whenTrueValue;
692
+ }
693
+ if (!whenTrueValue && whenFalseValue) {
694
+ return whenFalseValue;
695
+ }
696
+ const whenTrue = whenTrueValue;
697
+ const whenFalse = whenFalseValue;
698
+ if (box.isLiteral(whenTrue) && box.isLiteral(whenFalse) && whenTrue.value === whenFalse.value) {
699
+ return whenTrue;
700
+ }
701
+ return box.conditional(whenTrue, whenFalse, node, stack);
702
+ };
703
+ var findProperty = (node, propName, _stack, ctx) => {
704
+ const stack = [..._stack];
705
+ if (Node5.isPropertyAssignment(node)) {
706
+ const name = node.getNameNode();
707
+ if (Node5.isIdentifier(name) && name.getText() === propName) {
708
+ stack.push(name);
709
+ return node;
710
+ }
711
+ if (Node5.isStringLiteral(name) && name.getLiteralText() === propName) {
712
+ stack.push(name);
713
+ return name.getLiteralText();
714
+ }
715
+ if (Node5.isComputedPropertyName(name)) {
716
+ const expression = unwrapExpression(name.getExpression());
717
+ const computedPropNameBox = maybePropName(expression, stack, ctx);
718
+ if (!computedPropNameBox) return;
719
+ if (String(computedPropNameBox.value) === propName) {
720
+ stack.push(name, expression);
721
+ return node;
722
+ }
723
+ }
724
+ }
725
+ if (Node5.isShorthandPropertyAssignment(node)) {
726
+ const name = node.getNameNode();
727
+ if (Node5.isIdentifier(name) && name.getText() === propName) {
728
+ stack.push(name);
729
+ return node;
730
+ }
731
+ }
732
+ };
733
+ var getObjectLiteralPropValue = (initializer, accessList, _stack, ctx) => {
734
+ const stack = [..._stack];
735
+ const propName = accessList.pop();
736
+ const property = initializer.getProperty(propName) ?? initializer.getProperties().find((p) => findProperty(p, propName, stack, ctx));
737
+ if (!property) return;
738
+ stack.push(property);
739
+ if (Node5.isPropertyAssignment(property)) {
740
+ const propInit = property.getInitializer();
741
+ if (!propInit) return;
742
+ if (Node5.isObjectLiteralExpression(propInit)) {
743
+ if (accessList.length > 0) {
744
+ return getObjectLiteralPropValue(propInit, accessList, stack, ctx);
745
+ }
746
+ return maybeBoxNode(propInit, stack, ctx);
747
+ }
748
+ const maybePropValue = maybeBoxNode(propInit, stack, ctx);
749
+ if (maybePropValue) {
750
+ return maybePropValue;
751
+ }
752
+ }
753
+ if (Node5.isShorthandPropertyAssignment(property)) {
754
+ const identifier = property.getNameNode();
755
+ if (accessList.length > 0) {
756
+ return maybePropIdentifierValue(identifier, accessList, stack, ctx);
757
+ }
758
+ const maybePropValue = maybeBoxNode(identifier, stack, ctx);
759
+ if (maybePropValue) {
760
+ return maybePropValue;
761
+ }
762
+ }
763
+ };
764
+ var maybeTemplateStringValue = (template, stack, ctx) => {
765
+ const head = template.getHead();
766
+ const tail = template.getTemplateSpans();
767
+ const headValue = maybeStringLiteral(head, stack, ctx);
768
+ if (!headValue) return;
769
+ const tailValues = tail.map((t) => {
770
+ const expression = t.getExpression();
771
+ const propBox = maybePropName(expression, stack, ctx);
772
+ if (!propBox) return;
773
+ const literal = t.getLiteral();
774
+ return propBox.value + literal.getLiteralText();
775
+ });
776
+ if (tailValues.every(isNotNullish)) {
777
+ return headValue.value + tailValues.join("");
778
+ }
779
+ };
780
+ var maybeBindingElementValue = (def, stack, propName, ctx) => {
781
+ const parent = def.getParent();
782
+ if (!parent) return;
783
+ const grandParent = parent.getParent();
784
+ if (!grandParent) return;
785
+ if (Node5.isArrayBindingPattern(parent)) {
786
+ const index = parent.getChildIndex();
787
+ if (Number.isNaN(index)) return;
788
+ if (Node5.isVariableDeclaration(grandParent)) {
789
+ const init = grandParent.getInitializer();
790
+ if (!init) return;
791
+ const initializer = unwrapExpression(init);
792
+ if (!Node5.isArrayLiteralExpression(initializer)) return;
793
+ const element = initializer.getElements()[index + 1];
794
+ if (!element) return;
795
+ const innerStack = [...stack, initializer, element];
796
+ const maybeObject = maybeBoxNode(element, innerStack, ctx);
797
+ if (!maybeObject) return;
798
+ if (box.isObject(maybeObject)) {
799
+ const propValue2 = maybeObject.value[propName];
800
+ return box.from(propValue2, element, innerStack);
801
+ }
802
+ if (!box.isMap(maybeObject)) {
803
+ return maybeObject;
804
+ }
805
+ const propValue = maybeObject.value.get(propName);
806
+ if (!propValue) return;
807
+ return propValue;
808
+ }
809
+ }
810
+ if (Node5.isObjectBindingPattern(parent)) {
811
+ }
812
+ };
813
+ function maybePropDefinitionValue(def, accessList, _stack, ctx) {
814
+ const propName = accessList.at(-1);
815
+ if (Node5.isVariableDeclaration(def)) {
816
+ const init = def.getInitializer();
817
+ if (!init) {
818
+ const type = def.getTypeNode();
819
+ if (!type) return;
820
+ if (Node5.isTypeLiteral(type)) {
821
+ if (accessList.length > 0) {
822
+ const stack = [..._stack];
823
+ stack.push(type);
824
+ let propName2 = accessList.pop();
825
+ let typeProp = type.getProperty(propName2);
826
+ let typeLiteral = typeProp?.getTypeNode();
827
+ while (typeProp && accessList.length > 0 && typeLiteral && Node5.isTypeLiteral(typeLiteral)) {
828
+ stack.push(typeProp, typeLiteral);
829
+ propName2 = accessList.pop();
830
+ typeProp = typeLiteral.getProperty(propName2);
831
+ typeLiteral = typeProp?.getTypeNode();
832
+ }
833
+ if (!typeLiteral) return;
834
+ const typeValue = getTypeNodeValue(typeLiteral, stack, ctx);
835
+ return box.from(typeValue, typeLiteral, stack);
836
+ }
837
+ const propValue = getTypeLiteralNodePropValue(type, propName, _stack, ctx);
838
+ _stack.push(type);
839
+ return box.from(propValue, type, _stack);
840
+ }
841
+ return;
842
+ }
843
+ const initializer = unwrapExpression(init);
844
+ if (Node5.isObjectLiteralExpression(initializer)) {
845
+ const propValue = getObjectLiteralPropValue(initializer, accessList, _stack, ctx);
846
+ if (!propValue) return;
847
+ _stack.push(initializer);
848
+ return propValue;
849
+ }
850
+ if (Node5.isArrayLiteralExpression(initializer)) {
851
+ const index = Number(propName);
852
+ if (Number.isNaN(index)) return;
853
+ const element = initializer.getElements()[index];
854
+ if (!element) return;
855
+ _stack.push(initializer);
856
+ const boxed = maybeBoxNode(element, _stack, ctx);
857
+ if (boxed && isBoxNode(boxed) && box.isLiteral(boxed)) {
858
+ return boxed;
859
+ }
860
+ }
861
+ const innerStack = [..._stack, initializer];
862
+ const maybeValue = maybeBoxNode(initializer, innerStack, ctx);
863
+ if (maybeValue) return maybeValue;
864
+ }
865
+ if (Node5.isBindingElement(def)) {
866
+ const value = maybeBindingElementValue(def, _stack, propName, ctx);
867
+ if (value) return value;
868
+ }
869
+ if (Node5.isEnumDeclaration(def)) {
870
+ const member = def.getMember(propName);
871
+ if (!member) return;
872
+ const initializer = member.getInitializer();
873
+ if (!initializer) return;
874
+ const innerStack = [..._stack, initializer];
875
+ const maybeValue = maybeBoxNode(initializer, innerStack, ctx);
876
+ if (maybeValue) return maybeValue;
877
+ }
878
+ }
879
+ var maybePropIdentifierValue = (identifier, accessList, _stack, ctx) => {
880
+ const maybeValueDeclaration = findIdentifierValueDeclaration(identifier, _stack, ctx);
881
+ if (!maybeValueDeclaration) {
882
+ return box.unresolvable(identifier, _stack);
883
+ }
884
+ const maybeValue = maybePropDefinitionValue(maybeValueDeclaration, accessList, _stack, ctx);
885
+ if (maybeValue) return maybeValue;
886
+ return box.unresolvable(identifier, _stack);
887
+ };
888
+ var typeLiteralCache = /* @__PURE__ */ new WeakMap();
889
+ var getTypeLiteralNodePropValue = (type, propName, stack, ctx) => {
890
+ if (typeLiteralCache.has(type)) {
891
+ const map = typeLiteralCache.get(type);
892
+ if (map === null) return;
893
+ if (map?.has(propName)) {
894
+ return map.get(propName);
895
+ }
896
+ }
897
+ const members = type.getMembers();
898
+ const prop = members.find((member) => Node5.isPropertySignature(member) && member.getName() === propName);
899
+ if (Node5.isPropertySignature(prop) && prop.isReadonly()) {
900
+ const propType = prop.getTypeNode();
901
+ if (!propType) {
902
+ typeLiteralCache.set(type, null);
903
+ return;
904
+ }
905
+ const propValue = getTypeNodeValue(propType, stack, ctx);
906
+ if (isNotNullish(propValue)) {
907
+ if (!typeLiteralCache.has(type)) {
908
+ typeLiteralCache.set(type, /* @__PURE__ */ new Map());
909
+ }
910
+ const map = typeLiteralCache.get(type);
911
+ map.set(propName, propValue);
912
+ return propValue;
913
+ }
914
+ }
915
+ typeLiteralCache.set(type, null);
916
+ };
917
+ function getNameLiteral(wrapper) {
918
+ if (Node5.isStringLiteral(wrapper)) return wrapper.getLiteralText();
919
+ return wrapper.getText();
920
+ }
921
+ var typeNodeCache = /* @__PURE__ */ new WeakMap();
922
+ var getTypeNodeValue = (type, stack, ctx) => {
923
+ if (typeNodeCache.has(type)) {
924
+ return typeNodeCache.get(type);
925
+ }
926
+ if (Node5.isLiteralTypeNode(type)) {
927
+ const literal = type.getLiteral();
928
+ if (Node5.isStringLiteral(literal)) {
929
+ const result = literal.getLiteralText();
930
+ typeNodeCache.set(type, result);
931
+ return result;
932
+ }
933
+ }
934
+ if (Node5.isTypeLiteral(type)) {
935
+ const members = type.getMembers();
936
+ if (!members.some((member) => !Node5.isPropertySignature(member) || !member.isReadonly())) {
937
+ const props = members;
938
+ const entries = props.map((member) => {
939
+ const nameNode = member.getNameNode();
940
+ const nameText = nameNode.getText();
941
+ const name = getNameLiteral(nameNode);
942
+ if (!name) return;
943
+ const value = getTypeLiteralNodePropValue(type, nameText, stack, ctx);
944
+ return [name, value];
945
+ }).filter(isNotNullish);
946
+ const result = Object.fromEntries(entries);
947
+ typeNodeCache.set(type, result);
948
+ return result;
949
+ }
950
+ }
951
+ typeNodeCache.set(type, void 0);
952
+ };
953
+ var maybeDefinitionValue = (def, stack, ctx) => {
954
+ if (Node5.isShorthandPropertyAssignment(def)) {
955
+ const propNameNode = def.getNameNode();
956
+ return maybePropIdentifierValue(propNameNode, [propNameNode.getText()], stack, ctx);
957
+ }
958
+ if (Node5.isVariableDeclaration(def)) {
959
+ const init = def.getInitializer();
960
+ if (!init) {
961
+ const type = def.getTypeNode();
962
+ if (!type) return;
963
+ if (Node5.isTypeLiteral(type)) {
964
+ stack.push(type);
965
+ const maybeTypeValue = getTypeNodeValue(type, stack, ctx);
966
+ if (isNotNullish(maybeTypeValue)) return box.from(maybeTypeValue, def, stack);
967
+ }
968
+ return box.unresolvable(def, stack);
969
+ }
970
+ const initializer = unwrapExpression(init);
971
+ const innerStack = [...stack, initializer];
972
+ const maybeValue = maybeBoxNode(initializer, innerStack, ctx);
973
+ if (maybeValue) return maybeValue;
974
+ }
975
+ if (Node5.isBindingElement(def)) {
976
+ const init = def.getInitializer();
977
+ if (!init) {
978
+ const nameNode = def.getPropertyNameNode() ?? def.getNameNode();
979
+ const propName = nameNode.getText();
980
+ const innerStack2 = [...stack, nameNode];
981
+ const value = maybeBindingElementValue(def, innerStack2, propName, ctx);
982
+ if (value) return value;
983
+ return box.unresolvable(def, stack);
984
+ }
985
+ const initializer = unwrapExpression(init);
986
+ const innerStack = [...stack, initializer];
987
+ const maybeValue = maybeBoxNode(initializer, innerStack, ctx);
988
+ if (maybeValue) return maybeValue;
989
+ }
990
+ };
991
+ var getExportedVarDeclarationWithName = (varName, sourceFile, stack, ctx) => {
992
+ const maybeVar = sourceFile.getVariableDeclaration(varName);
993
+ if (maybeVar) return maybeVar;
994
+ const exportDeclaration = resolveVarDeclarationFromExportWithName(varName, sourceFile, stack, ctx);
995
+ if (!exportDeclaration) return;
996
+ return exportDeclaration;
997
+ };
998
+ var hasNamedExportWithName = (name, exportDeclaration) => {
999
+ const namedExports = exportDeclaration.getNamedExports();
1000
+ if (namedExports.length === 0) return true;
1001
+ for (const namedExport of namedExports) {
1002
+ const exportedName = namedExport.getNameNode().getText();
1003
+ if (exportedName === name) {
1004
+ return true;
1005
+ }
1006
+ }
1007
+ };
1008
+ var getModuleSpecifierSourceFile = (declaration) => {
1009
+ const project = declaration.getProject();
1010
+ const moduleName = declaration.getModuleSpecifierValue();
1011
+ if (!moduleName) return;
1012
+ const containingFile = declaration.getSourceFile().getFilePath();
1013
+ const resolved = ts2.resolveModuleName(
1014
+ moduleName,
1015
+ containingFile,
1016
+ project.getCompilerOptions(),
1017
+ project.getModuleResolutionHost()
1018
+ );
1019
+ if (!resolved.resolvedModule) return;
1020
+ const sourceFile = project.addSourceFileAtPath(resolved.resolvedModule.resolvedFileName);
1021
+ return sourceFile;
1022
+ };
1023
+ function resolveVarDeclarationFromExportWithName(symbolName, sourceFile, stack, ctx) {
1024
+ for (const exportDeclaration of sourceFile.getExportDeclarations()) {
1025
+ const exportStack = [exportDeclaration];
1026
+ if (!hasNamedExportWithName(symbolName, exportDeclaration)) continue;
1027
+ const maybeFile = getModuleSpecifierSourceFile(exportDeclaration);
1028
+ if (!maybeFile) continue;
1029
+ exportStack.push(maybeFile);
1030
+ const maybeVar = getExportedVarDeclarationWithName(symbolName, maybeFile, stack, ctx);
1031
+ if (maybeVar) {
1032
+ stack.push(...exportStack.concat(maybeVar));
1033
+ return maybeVar;
1034
+ }
1035
+ }
1036
+ }
1037
+ var maybeIdentifierValue = (identifier, _stack, ctx) => {
1038
+ const valueDeclaration = findIdentifierValueDeclaration(identifier, _stack, ctx);
1039
+ if (!valueDeclaration) {
1040
+ return box.unresolvable(identifier, _stack);
1041
+ }
1042
+ const declaration = unwrapExpression(valueDeclaration);
1043
+ const stack = [..._stack];
1044
+ const maybeValue = maybeDefinitionValue(declaration, stack, ctx);
1045
+ if (maybeValue) return maybeValue;
1046
+ return box.unresolvable(identifier, stack);
1047
+ };
1048
+ var tryComputingPlusTokenBinaryExpressionToString = (node, stack, ctx) => {
1049
+ const left = unwrapExpression(node.getLeft());
1050
+ const right = unwrapExpression(node.getRight());
1051
+ const leftValue = maybePropName(left, stack, ctx);
1052
+ const rightValue = maybePropName(right, stack, ctx);
1053
+ if (!leftValue || !rightValue) return;
1054
+ if (isNotNullish(leftValue.value) && isNotNullish(rightValue.value)) {
1055
+ return box.literal(String(leftValue.value) + String(rightValue.value), node, stack);
1056
+ }
1057
+ };
1058
+ var getElementAccessedExpressionValue = (expression, _stack, ctx) => {
1059
+ const elementAccessed = unwrapExpression(expression.getExpression());
1060
+ const argExpr = expression.getArgumentExpression();
1061
+ if (!argExpr) return;
1062
+ const arg = unwrapExpression(argExpr);
1063
+ const stack = [..._stack, elementAccessed, arg];
1064
+ const argLiteral = maybePropName(arg, stack, ctx);
1065
+ if (Node5.isIdentifier(elementAccessed) && argLiteral) {
1066
+ if (!isNotNullish(argLiteral.value)) return;
1067
+ return maybePropIdentifierValue(elementAccessed, [argLiteral.value.toString()], stack, ctx);
1068
+ }
1069
+ if (Node5.isBinaryExpression(arg)) {
1070
+ if (arg.getOperatorToken().getKind() !== ts2.SyntaxKind.PlusToken) return;
1071
+ const propName = tryComputingPlusTokenBinaryExpressionToString(arg, stack, ctx) ?? maybePropName(arg, stack, ctx);
1072
+ if (propName && Node5.isIdentifier(elementAccessed)) {
1073
+ if (!isNotNullish(propName.value)) return;
1074
+ return maybePropIdentifierValue(elementAccessed, [propName.value.toString()], stack, ctx);
1075
+ }
1076
+ }
1077
+ if (Node5.isTemplateExpression(arg)) {
1078
+ const propName = maybeTemplateStringValue(arg, stack, ctx);
1079
+ if (propName && Node5.isIdentifier(elementAccessed)) {
1080
+ return maybePropIdentifierValue(elementAccessed, [propName], stack, ctx);
1081
+ }
1082
+ }
1083
+ if (Node5.isObjectLiteralExpression(elementAccessed) && argLiteral) {
1084
+ if (!isNotNullish(argLiteral.value)) return;
1085
+ return getObjectLiteralPropValue(elementAccessed, [argLiteral.value.toString()], stack, ctx);
1086
+ }
1087
+ if (Node5.isPropertyAccessExpression(arg)) {
1088
+ return getPropertyAccessedExpressionValue(arg, [], stack, ctx);
1089
+ }
1090
+ if (Node5.isPropertyAccessExpression(elementAccessed) && argLiteral && isNotNullish(argLiteral.value)) {
1091
+ const propRefValue = getPropertyAccessedExpressionValue(elementAccessed, [], stack, ctx);
1092
+ if (!propRefValue) return box.unresolvable(elementAccessed, stack);
1093
+ const propName = argLiteral.value.toString();
1094
+ if (box.isObject(propRefValue)) {
1095
+ const propValue = propRefValue.value[propName];
1096
+ return box.from(propValue, arg, stack);
1097
+ }
1098
+ if (box.isMap(propRefValue)) {
1099
+ const propValue = propRefValue.value.get(propName);
1100
+ return box.from(propValue, arg, stack);
1101
+ }
1102
+ if (box.isArray(propRefValue)) {
1103
+ const propValue = propRefValue.value[Number(propName)];
1104
+ return box.from(propValue, arg, stack);
1105
+ }
1106
+ return box.unresolvable(elementAccessed, stack);
1107
+ }
1108
+ if (Node5.isIdentifier(elementAccessed) && Node5.isElementAccessExpression(arg)) {
1109
+ const propName = getElementAccessedExpressionValue(arg, stack, ctx);
1110
+ if (typeof propName === "string" && isNotNullish(propName)) {
1111
+ return maybePropIdentifierValue(elementAccessed, [propName], stack, ctx);
1112
+ }
1113
+ }
1114
+ if (Node5.isElementAccessExpression(elementAccessed) && argLiteral && isNotNullish(argLiteral.value)) {
1115
+ const identifier = getElementAccessedExpressionValue(elementAccessed, stack, ctx);
1116
+ if (isObject(identifier)) {
1117
+ const argValue = argLiteral.value.toString();
1118
+ if (box.isMap(identifier)) {
1119
+ const maybeValue = identifier.value.get(argValue);
1120
+ return maybeValue;
1121
+ }
1122
+ if (box.isObject(identifier)) {
1123
+ const maybeLiteralValue = identifier.value[argValue];
1124
+ if (!maybeLiteralValue) return;
1125
+ return box.from(maybeLiteralValue, expression, stack);
1126
+ }
1127
+ }
1128
+ }
1129
+ if (Node5.isArrayLiteralExpression(elementAccessed) && argLiteral) {
1130
+ return getArrayElementValueAtIndex(elementAccessed, Number(argLiteral.value), stack, ctx);
1131
+ }
1132
+ if (Node5.isConditionalExpression(arg)) {
1133
+ if (ctx.flags?.skipConditions) return box.unresolvable(arg, stack);
1134
+ const propName = maybePropName(arg, stack, ctx);
1135
+ if (isNotNullish(propName) && isNotNullish(propName.value)) {
1136
+ if (Node5.isIdentifier(elementAccessed)) {
1137
+ return maybePropIdentifierValue(elementAccessed, [propName.value.toString()], stack, ctx);
1138
+ }
1139
+ }
1140
+ const whenTrueExpr = unwrapExpression(arg.getWhenTrue());
1141
+ const whenFalseExpr = unwrapExpression(arg.getWhenFalse());
1142
+ const whenTrueValue = maybePropName(whenTrueExpr, stack, ctx);
1143
+ const whenFalseValue = maybePropName(whenFalseExpr, stack, ctx);
1144
+ if (Node5.isIdentifier(elementAccessed)) {
1145
+ const whenTrueResolved = whenTrueValue && isNotNullish(whenTrueValue.value) ? maybePropIdentifierValue(elementAccessed, [whenTrueValue.value.toString()], stack, ctx) : void 0;
1146
+ const whenFalseResolved = whenFalseValue && isNotNullish(whenFalseValue.value) ? maybePropIdentifierValue(elementAccessed, [whenFalseValue.value.toString()], stack, ctx) : void 0;
1147
+ if (!whenTrueResolved && !whenFalseResolved) {
1148
+ return;
1149
+ }
1150
+ if (whenTrueResolved && !whenFalseResolved) {
1151
+ return whenTrueResolved;
1152
+ }
1153
+ if (!whenTrueResolved && whenFalseResolved) {
1154
+ return whenFalseResolved;
1155
+ }
1156
+ return box.conditional(whenTrueResolved, whenFalseResolved, arg, stack);
1157
+ }
1158
+ }
1159
+ };
1160
+ var getArrayElementValueAtIndex = (array, index, stack, ctx) => {
1161
+ const element = array.getElements()[index];
1162
+ if (!element) return;
1163
+ const value = maybeBoxNode(element, stack, ctx);
1164
+ if (isNotNullish(value)) {
1165
+ return value;
1166
+ }
1167
+ };
1168
+ var getPropertyAccessedExpressionValue = (expression, _accessList, stack, ctx) => {
1169
+ const propName = expression.getName();
1170
+ const elementAccessed = unwrapExpression(expression.getExpression());
1171
+ const accessList = _accessList.concat(propName);
1172
+ stack.push(elementAccessed);
1173
+ if (Node5.isIdentifier(elementAccessed)) {
1174
+ return maybePropIdentifierValue(elementAccessed, accessList, stack, ctx);
1175
+ }
1176
+ if (Node5.isPropertyAccessExpression(elementAccessed)) {
1177
+ const propValue = getPropertyAccessedExpressionValue(elementAccessed, accessList, stack, ctx);
1178
+ return propValue;
1179
+ }
1180
+ if (Node5.isElementAccessExpression(elementAccessed)) {
1181
+ const leftElementAccessed = getElementAccessedExpressionValue(elementAccessed, stack, ctx);
1182
+ if (!leftElementAccessed) return;
1183
+ if (box.isObject(leftElementAccessed)) {
1184
+ const propValue = leftElementAccessed.value[propName];
1185
+ return box.from(propValue, expression, stack);
1186
+ }
1187
+ if (box.isMap(leftElementAccessed)) {
1188
+ const propValue = leftElementAccessed.value.get(propName);
1189
+ return box.from(propValue, expression, stack);
1190
+ }
1191
+ }
1192
+ };
1193
+
1194
+ // src/call-expression.ts
1195
+ var trueFn = () => true;
1196
+ var extractCallExpressionArguments = (node, ctx, matchProp = trueFn, matchArg = trueFn) => {
1197
+ const fnArguments = node.getArguments();
1198
+ const fnName = node.getExpression().getText();
1199
+ if (fnArguments.length === 0) {
1200
+ return box.array([], node, []);
1201
+ }
1202
+ return box.array(
1203
+ fnArguments.map((argument, index) => {
1204
+ const argNode = unwrapExpression(argument);
1205
+ const stack = [node, argNode];
1206
+ if (matchArg({ fnNode: node, fnName, argNode, index })) {
1207
+ return maybeBoxNode(argNode, stack, ctx, matchProp) ?? box.unresolvable(argNode, stack);
1208
+ }
1209
+ return box.unresolvable(argNode, stack);
1210
+ }),
1211
+ node,
1212
+ []
1213
+ );
1214
+ };
1215
+
1216
+ // src/extract.ts
1217
+ import { Node as Node7 } from "ts-morph";
1218
+
1219
+ // src/compiled-jsx.ts
1220
+ import { Node as MorphNode, SyntaxKind } from "ts-morph";
1221
+ var reactRuntimeMods = /* @__PURE__ */ new Set(["react/jsx-runtime", "react/jsx-dev-runtime"]);
1222
+ var preactRuntimeMods = /* @__PURE__ */ new Set(["preact/jsx-runtime", "preact/jsx-dev-runtime"]);
1223
+ var classicReactMods = /* @__PURE__ */ new Set(["react"]);
1224
+ var classicPreactMods = /* @__PURE__ */ new Set(["preact"]);
1225
+ var solidMods = /* @__PURE__ */ new Set(["solid-js", "solid-js/web"]);
1226
+ var vueMods = /* @__PURE__ */ new Set(["vue"]);
1227
+ var resolveBundledHelperImport = (name, node) => {
1228
+ if (!node) return;
1229
+ const text = node.getText();
1230
+ const isVueBundledCreateVNode = text.includes("type, props = null, children = null") && (text.includes("createBaseVNode") && text.includes("guardReactiveProps") || text.includes("patchFlag = -2") && text.includes("__vccOpts") && text.includes("shapeFlag"));
1231
+ const isVueBundledCreateVNodeTransform = name.includes("createVNodeWithArgsTransform") && text.includes("...args") && text.includes("_createVNode");
1232
+ const isVueBundledMergeProps = text.includes("const ret = {}") && text.includes("toMerge = args[i]") && text.includes("ret[key] = toMerge[key]") && text.includes('key !== ""');
1233
+ const isSolidBundledCreateComponent = name === "createComponent" && text.includes("Comp(props || {})") && text.includes("untrack");
1234
+ const isSolidBundledMergeProps = name === "mergeProps" && text.includes("...sources") && text.includes("resolveSource") && text.includes("sources.length");
1235
+ const isPreactBundledJsxHelper = text.includes("type:") && text.includes("props:") && text.includes("__v:") && text.includes("defaultProps") && text.includes(".vnode");
1236
+ if (isVueBundledCreateVNode || isVueBundledCreateVNodeTransform) {
1237
+ return { importedName: "createVNode", mod: "vue" };
1238
+ }
1239
+ if (isVueBundledMergeProps) {
1240
+ return { importedName: "mergeProps", mod: "vue" };
1241
+ }
1242
+ if (isSolidBundledCreateComponent) {
1243
+ return { importedName: "createComponent", mod: "solid-js/web" };
1244
+ }
1245
+ if (isSolidBundledMergeProps) {
1246
+ return { importedName: "mergeProps", mod: "solid-js" };
1247
+ }
1248
+ if (isPreactBundledJsxHelper) {
1249
+ return { importedName: "jsx", mod: "preact/jsx-runtime" };
1250
+ }
1251
+ return;
1252
+ };
1253
+ var collectImports = (sourceFile) => {
1254
+ const named = /* @__PURE__ */ new Map();
1255
+ const defaultImports = /* @__PURE__ */ new Map();
1256
+ const namespace = /* @__PURE__ */ new Map();
1257
+ const bundledNamed = /* @__PURE__ */ new Map();
1258
+ const bundledNamespace = /* @__PURE__ */ new Map();
1259
+ const getBundledRuntimeModFromName = (name) => {
1260
+ const normalized = name.replace(/[^a-z]/gi, "").toLowerCase();
1261
+ if (normalized.includes("jsxdevruntime")) {
1262
+ return "react/jsx-dev-runtime";
1263
+ }
1264
+ if (normalized.includes("jsxruntime")) {
1265
+ return "react/jsx-runtime";
1266
+ }
1267
+ if (normalized === "react" || normalized.endsWith("react")) {
1268
+ return "react";
1269
+ }
1270
+ };
1271
+ const parcelRuntimeModuleIds = /* @__PURE__ */ new Map();
1272
+ const resolveBundledNamespaceMod = (node) => {
1273
+ if (!node) return;
1274
+ const expression = unwrapExpression(node);
1275
+ if (MorphNode.isCallExpression(expression)) {
1276
+ const callee = unwrapExpression(expression.getExpression());
1277
+ if (!MorphNode.isIdentifier(callee)) return;
1278
+ const calleeName = callee.getText();
1279
+ if (/requirejsxdevruntime/i.test(calleeName)) {
1280
+ return "react/jsx-dev-runtime";
1281
+ }
1282
+ if (/requirejsxruntime/i.test(calleeName)) {
1283
+ return "react/jsx-runtime";
1284
+ }
1285
+ if (/requirereact/i.test(calleeName)) {
1286
+ return "react";
1287
+ }
1288
+ }
1289
+ return;
1290
+ };
1291
+ sourceFile.getDescendantsOfKind(SyntaxKind.CallExpression).forEach((callExpression) => {
1292
+ const callee = unwrapExpression(callExpression.getExpression());
1293
+ if (!MorphNode.isIdentifier(callee) || callee.getText() !== "parcelRegister") return;
1294
+ const [idArg, factoryArg] = callExpression.getArguments();
1295
+ if (!MorphNode.isStringLiteral(idArg)) return;
1296
+ if (!MorphNode.isArrowFunction(factoryArg) && !MorphNode.isFunctionExpression(factoryArg)) return;
1297
+ const factoryText = factoryArg.getBody().getText();
1298
+ if (factoryText.includes('"Fragment"') && factoryText.includes('"jsx"') && factoryText.includes('"jsxs"') && factoryText.includes("module.exports")) {
1299
+ parcelRuntimeModuleIds.set(idArg.getLiteralValue(), "react/jsx-runtime");
1300
+ }
1301
+ });
1302
+ sourceFile.getImportDeclarations().forEach((declaration) => {
1303
+ const mod = declaration.getModuleSpecifierValue();
1304
+ if (!mod) return;
1305
+ const defaultImport = declaration.getDefaultImport();
1306
+ if (defaultImport) {
1307
+ defaultImports.set(defaultImport.getText(), mod);
1308
+ }
1309
+ const namespaceImport = declaration.getNamespaceImport();
1310
+ if (namespaceImport) {
1311
+ namespace.set(namespaceImport.getText(), mod);
1312
+ }
1313
+ declaration.getNamedImports().forEach((specifier) => {
1314
+ const importedName = specifier.getNameNode().getText();
1315
+ const alias = specifier.getAliasNode()?.getText() || importedName;
1316
+ named.set(alias, { importedName, mod });
1317
+ });
1318
+ });
1319
+ sourceFile.getVariableDeclarations().forEach((declaration) => {
1320
+ if (!MorphNode.isIdentifier(declaration.getNameNode())) return;
1321
+ const variableName = declaration.getName();
1322
+ const initializer = declaration.getInitializer();
1323
+ const bundledImport = resolveBundledHelperImport(variableName, initializer);
1324
+ if (bundledImport) {
1325
+ bundledNamed.set(variableName, bundledImport);
1326
+ }
1327
+ let mod = getBundledRuntimeModFromName(variableName) ?? resolveBundledNamespaceMod(initializer);
1328
+ if (!mod && initializer && MorphNode.isCallExpression(initializer)) {
1329
+ const callee = unwrapExpression(initializer.getExpression());
1330
+ const [firstArg] = initializer.getArguments();
1331
+ if (MorphNode.isIdentifier(callee) && callee.getText() === "parcelRequire" && MorphNode.isStringLiteral(firstArg)) {
1332
+ mod = parcelRuntimeModuleIds.get(firstArg.getLiteralValue());
1333
+ }
1334
+ }
1335
+ if (!mod && initializer && MorphNode.isObjectLiteralExpression(initializer)) {
1336
+ const propertyNames = new Set(
1337
+ initializer.getProperties().map((property) => {
1338
+ if (MorphNode.isPropertyAssignment(property)) {
1339
+ return property.getName();
1340
+ }
1341
+ }).filter(Boolean)
1342
+ );
1343
+ if (propertyNames.has("jsx") && propertyNames.has("Fragment")) {
1344
+ mod = "preact/jsx-runtime";
1345
+ }
1346
+ }
1347
+ if (!mod) return;
1348
+ bundledNamespace.set(variableName, mod);
1349
+ });
1350
+ sourceFile.getDescendantsOfKind(SyntaxKind.FunctionDeclaration).forEach((declaration) => {
1351
+ const bundledImport = resolveBundledHelperImport(declaration.getName() ?? "", declaration);
1352
+ if (!bundledImport || !declaration.getName()) return;
1353
+ bundledNamed.set(declaration.getName(), bundledImport);
1354
+ });
1355
+ const resolveBundledAliasImport = (node) => {
1356
+ if (!node) return;
1357
+ const expression = unwrapExpression(node);
1358
+ if (MorphNode.isIdentifier(expression)) {
1359
+ return bundledNamed.get(expression.getText());
1360
+ }
1361
+ if (MorphNode.isConditionalExpression(expression)) {
1362
+ return resolveBundledAliasImport(expression.getWhenTrue()) ?? resolveBundledAliasImport(expression.getWhenFalse());
1363
+ }
1364
+ if (MorphNode.isBinaryExpression(expression) && expression.getOperatorToken().getKind() === SyntaxKind.CommaToken) {
1365
+ return resolveBundledAliasImport(expression.getRight());
1366
+ }
1367
+ };
1368
+ sourceFile.getVariableDeclarations().forEach((declaration) => {
1369
+ if (!MorphNode.isIdentifier(declaration.getNameNode())) return;
1370
+ if (bundledNamed.has(declaration.getName())) return;
1371
+ const bundledAlias = resolveBundledAliasImport(declaration.getInitializer());
1372
+ if (!bundledAlias) return;
1373
+ bundledNamed.set(declaration.getName(), bundledAlias);
1374
+ });
1375
+ return { named, default: defaultImports, namespace, bundledNamed, bundledNamespace };
1376
+ };
1377
+ var normalizeTagName = (text) => {
1378
+ const parcelVarMatch = text.match(/\$[^$]+\$var\$(.+)$/);
1379
+ if (parcelVarMatch?.[1]) {
1380
+ return parcelVarMatch[1];
1381
+ }
1382
+ return text;
1383
+ };
1384
+ var getTagName = (node) => {
1385
+ if (!node) return;
1386
+ const expression = unwrapExpression(node);
1387
+ if (MorphNode.isStringLiteral(expression) || MorphNode.isNoSubstitutionTemplateLiteral(expression)) {
1388
+ return expression.getLiteralValue();
1389
+ }
1390
+ if (MorphNode.isIdentifier(expression) || MorphNode.isPropertyAccessExpression(expression) || MorphNode.isElementAccessExpression(expression)) {
1391
+ return normalizeTagName(expression.getText());
1392
+ }
1393
+ };
1394
+ var isQwikMod = (mod) => mod.includes("qwik");
1395
+ var getArgsAt = (...indices) => (args) => indices.map((index) => args[index]).filter(Boolean);
1396
+ var runtimeProfiles = [
1397
+ {
1398
+ framework: "react",
1399
+ matches: ({ mod, importedName }) => reactRuntimeMods.has(mod) && ["jsx", "jsxs", "jsxDEV"].includes(importedName),
1400
+ getPropNodes: getArgsAt(1)
1401
+ },
1402
+ {
1403
+ framework: "preact",
1404
+ matches: ({ mod, importedName }) => preactRuntimeMods.has(mod) && ["jsx", "jsxs", "jsxDEV"].includes(importedName),
1405
+ getPropNodes: getArgsAt(1)
1406
+ },
1407
+ {
1408
+ framework: "react",
1409
+ matches: ({ mod, importedName }) => classicReactMods.has(mod) && importedName === "createElement",
1410
+ getPropNodes: getArgsAt(1)
1411
+ },
1412
+ {
1413
+ framework: "preact",
1414
+ matches: ({ mod, importedName }) => classicPreactMods.has(mod) && ["createElement", "h"].includes(importedName),
1415
+ getPropNodes: getArgsAt(1)
1416
+ },
1417
+ {
1418
+ framework: "vue",
1419
+ matches: ({ mod, importedName }) => vueMods.has(mod) && ["createVNode", "h"].includes(importedName),
1420
+ getPropNodes: getArgsAt(1)
1421
+ },
1422
+ {
1423
+ framework: "solid",
1424
+ matches: ({ mod, importedName }) => solidMods.has(mod) && importedName === "createComponent",
1425
+ getPropNodes: getArgsAt(1)
1426
+ },
1427
+ {
1428
+ framework: "qwik",
1429
+ matches: ({ mod, importedName }) => isQwikMod(mod) && ["_jsxQ", "_jsxS"].includes(importedName),
1430
+ getPropNodes: getArgsAt(1, 2)
1431
+ },
1432
+ {
1433
+ framework: "qwik",
1434
+ matches: ({ mod, importedName }) => isQwikMod(mod) && ["_jsxC", "jsx", "jsxs", "jsxDEV"].includes(importedName),
1435
+ getPropNodes: getArgsAt(1)
1436
+ }
1437
+ ];
1438
+ var createCompiledJsxContext = (sourceFile) => {
1439
+ const imports = collectImports(sourceFile);
1440
+ const normalizeCallee = (node) => {
1441
+ const expression = unwrapExpression(node);
1442
+ if (MorphNode.isBinaryExpression(expression) && expression.getOperatorToken().getKind() === SyntaxKind.CommaToken) {
1443
+ return normalizeCallee(expression.getRight());
1444
+ }
1445
+ return expression;
1446
+ };
1447
+ const localDefinitionCache = /* @__PURE__ */ new Map();
1448
+ const resolveIdentifierDefinition = (identifier) => {
1449
+ const name = identifier.getText();
1450
+ if (localDefinitionCache.has(name)) {
1451
+ return localDefinitionCache.get(name) ?? void 0;
1452
+ }
1453
+ localDefinitionCache.set(name, null);
1454
+ const resolveLocalAlias = (aliasNode) => {
1455
+ if (!aliasNode) return;
1456
+ const expression = unwrapExpression(aliasNode);
1457
+ if (MorphNode.isIdentifier(expression)) {
1458
+ const imported = imports.bundledNamed.get(expression.getText());
1459
+ if (imported) {
1460
+ return { mod: imported.mod, importedName: imported.importedName };
1461
+ }
1462
+ return resolveIdentifierDefinition(expression);
1463
+ }
1464
+ if (MorphNode.isConditionalExpression(expression)) {
1465
+ return resolveLocalAlias(expression.getWhenTrue()) ?? resolveLocalAlias(expression.getWhenFalse());
1466
+ }
1467
+ if (MorphNode.isBinaryExpression(expression) && expression.getOperatorToken().getKind() === SyntaxKind.CommaToken) {
1468
+ return resolveLocalAlias(expression.getRight());
1469
+ }
1470
+ };
1471
+ for (const definition of identifier.getDefinitions()) {
1472
+ const declaration = definition.getDeclarationNode();
1473
+ if (!declaration) continue;
1474
+ if (MorphNode.isFunctionDeclaration(declaration)) {
1475
+ const imported = resolveBundledHelperImport(declaration.getName() ?? name, declaration);
1476
+ if (!imported) continue;
1477
+ const resolved = { mod: imported.mod, importedName: imported.importedName };
1478
+ localDefinitionCache.set(name, resolved);
1479
+ return resolved;
1480
+ }
1481
+ if (MorphNode.isVariableDeclaration(declaration)) {
1482
+ const directImport = resolveBundledHelperImport(declaration.getName(), declaration.getInitializer());
1483
+ const aliasImport = directImport ? { mod: directImport.mod, importedName: directImport.importedName } : resolveLocalAlias(declaration.getInitializer());
1484
+ if (!aliasImport) continue;
1485
+ localDefinitionCache.set(name, aliasImport);
1486
+ return aliasImport;
1487
+ }
1488
+ }
1489
+ };
1490
+ const resolveCallee = (node) => {
1491
+ const expression = normalizeCallee(node);
1492
+ if (MorphNode.isIdentifier(expression)) {
1493
+ const imported = imports.named.get(expression.getText()) ?? imports.bundledNamed.get(expression.getText());
1494
+ if (imported) {
1495
+ return { mod: imported.mod, importedName: imported.importedName };
1496
+ }
1497
+ return resolveIdentifierDefinition(expression);
1498
+ }
1499
+ if (MorphNode.isPropertyAccessExpression(expression)) {
1500
+ const target = normalizeCallee(expression.getExpression());
1501
+ if (!MorphNode.isIdentifier(target)) return;
1502
+ const targetName = target.getText();
1503
+ const mod = imports.default.get(targetName) ?? imports.namespace.get(targetName) ?? imports.bundledNamespace.get(targetName);
1504
+ if (!mod) return;
1505
+ return { mod, importedName: expression.getName() };
1506
+ }
1507
+ };
1508
+ const isMergePropsCall = (node) => {
1509
+ if (!MorphNode.isCallExpression(node)) return false;
1510
+ const resolved = resolveCallee(node.getExpression());
1511
+ if (!resolved) return false;
1512
+ return resolved.importedName === "mergeProps" && (solidMods.has(resolved.mod) || vueMods.has(resolved.mod));
1513
+ };
1514
+ const getCallInfo = (node) => {
1515
+ const resolved = resolveCallee(node.getExpression());
1516
+ if (!resolved) return;
1517
+ const args = node.getArguments().map((arg) => unwrapExpression(arg));
1518
+ const tagName = getTagName(args[0]);
1519
+ if (!tagName) return;
1520
+ const baseInfo = {
1521
+ tagName,
1522
+ isFactory: tagName.includes(".")
1523
+ };
1524
+ for (const profile of runtimeProfiles) {
1525
+ if (profile.matches(resolved)) {
1526
+ return { framework: profile.framework, ...baseInfo, propNodes: profile.getPropNodes(args) };
1527
+ }
1528
+ }
1529
+ };
1530
+ return {
1531
+ getCallInfo,
1532
+ isMergePropsCall
1533
+ };
1534
+ };
1535
+
1536
+ // src/jsx-attribute.ts
1537
+ import { Node as Node6 } from "ts-morph";
1538
+ var extractJsxAttribute = (jsxAttribute, ctx) => {
1539
+ const initializer = jsxAttribute.getInitializer();
1540
+ const stack = [jsxAttribute, initializer];
1541
+ if (!initializer) {
1542
+ const nameNode = jsxAttribute.getNameNode();
1543
+ return box.emptyInitializer(nameNode, stack);
1544
+ }
1545
+ if (Node6.isStringLiteral(initializer)) {
1546
+ const literalText = initializer.getLiteralText();
1547
+ return box.literal(trimWhitespace(literalText), initializer, stack);
1548
+ }
1549
+ if (Node6.isJsxExpression(initializer)) {
1550
+ const expr = initializer.getExpression();
1551
+ if (!expr) return;
1552
+ const expression = unwrapExpression(expr);
1553
+ if (!expression) return;
1554
+ stack.push(expression);
1555
+ const maybeValue = maybeBoxNode(expression, stack, ctx);
1556
+ if (maybeValue) return maybeValue;
1557
+ }
1558
+ };
1559
+
1560
+ // src/jsx-spread-attribute.ts
1561
+ var extractJsxSpreadAttributeValues = (node, ctx, matchProp) => {
1562
+ const expr = unwrapExpression(node.getExpression());
1563
+ const stack = [];
1564
+ return maybeBoxNode(expr, stack, ctx, matchProp);
1565
+ };
1566
+
1567
+ // src/object-like-to-map.ts
1568
+ var objectLikeToMap = (maybeObject, node) => {
1569
+ if (!maybeObject) {
1570
+ return /* @__PURE__ */ new Map();
1571
+ }
1572
+ if (!isBoxNode(maybeObject)) {
1573
+ return new Map(Object.entries(maybeObject));
1574
+ }
1575
+ if (box.isUnresolvable(maybeObject) || box.isConditional(maybeObject)) {
1576
+ return /* @__PURE__ */ new Map();
1577
+ }
1578
+ if (box.isMap(maybeObject)) {
1579
+ return maybeObject.value;
1580
+ }
1581
+ return new Map(
1582
+ Object.entries(maybeObject.value).map(([key, value]) => {
1583
+ const boxed = box.from(value, maybeObject.getNode() ?? node, maybeObject.getStack() ?? []);
1584
+ return [key, boxed || null];
1585
+ }).filter(([, value]) => value !== null)
1586
+ );
1587
+ };
1588
+
1589
+ // src/extract.ts
1590
+ var isImportOrExport = (node) => Node7.isImportDeclaration(node) || Node7.isExportDeclaration(node);
1591
+ var isJsxElement = (node) => Node7.isJsxOpeningElement(node) || Node7.isJsxSelfClosingElement(node);
1592
+ var extract = ({ ast, ...ctx }) => {
1593
+ const { components, functions, taggedTemplates } = ctx;
1594
+ const compiledJsx = createCompiledJsxContext(ast);
1595
+ const byName = /* @__PURE__ */ new Map();
1596
+ const componentByNode = /* @__PURE__ */ new Map();
1597
+ const ensureComponent = (componentNode, componentName) => {
1598
+ if (!byName.has(componentName)) {
1599
+ byName.set(componentName, { kind: "component", nodesByProp: /* @__PURE__ */ new Map(), queryList: [] });
1600
+ }
1601
+ if (!componentByNode.has(componentNode)) {
1602
+ componentByNode.set(componentNode, { name: componentName, props: /* @__PURE__ */ new Map(), conditionals: [] });
1603
+ }
1604
+ return {
1605
+ component: componentByNode.get(componentNode),
1606
+ boxByProp: byName.get(componentName).nodesByProp
1607
+ };
1608
+ };
1609
+ const addComponentProp = (component, boxByProp, propName, propValue) => {
1610
+ component.props.set(propName, propValue);
1611
+ boxByProp.set(propName, (boxByProp.get(propName) ?? []).concat(propValue));
1612
+ };
1613
+ const processComponentObjectLike = (component, boxByProp, propNode, objLike, matchProp, trackSpreadConditions = false) => {
1614
+ if (trackSpreadConditions && box.isMap(objLike) && objLike.spreadConditions?.length) {
1615
+ component.conditionals.push(...objLike.spreadConditions);
1616
+ }
1617
+ const mapValue = objectLikeToMap(objLike, propNode);
1618
+ mapValue.forEach((propValue, propName) => {
1619
+ if (matchProp({ propName, propNode })) {
1620
+ addComponentProp(component, boxByProp, propName, propValue);
1621
+ }
1622
+ });
1623
+ };
1624
+ const processComponentBoxNode = (component, boxByProp, propNode, boxNode, matchProp, trackSpreadConditions = false) => {
1625
+ if (box.isConditional(boxNode)) {
1626
+ component.conditionals.push(boxNode);
1627
+ return;
1628
+ }
1629
+ if (box.isObject(boxNode) || box.isMap(boxNode)) {
1630
+ processComponentObjectLike(component, boxByProp, propNode, boxNode, matchProp, trackSpreadConditions);
1631
+ }
1632
+ };
1633
+ const processCompiledPropSource = (componentNode, component, boxByProp, propNode, matchProp) => {
1634
+ const expression = unwrapExpression(propNode);
1635
+ const stack = [componentNode, expression];
1636
+ if (Node7.isObjectLiteralExpression(expression)) {
1637
+ const objectMap = getObjectLiteralExpressionPropPairs(expression, stack, ctx, matchProp);
1638
+ processComponentBoxNode(component, boxByProp, expression, objectMap, matchProp, true);
1639
+ return;
1640
+ }
1641
+ if (Node7.isCallExpression(expression) && compiledJsx.isMergePropsCall(expression)) {
1642
+ expression.getArguments().forEach((arg) => {
1643
+ processCompiledPropSource(componentNode, component, boxByProp, arg, matchProp);
1644
+ });
1645
+ return;
1646
+ }
1647
+ const maybeValue = maybeBoxNode(expression, stack, ctx, matchProp);
1648
+ if (!maybeValue) return;
1649
+ processComponentBoxNode(component, boxByProp, expression, maybeValue, matchProp, true);
1650
+ };
1651
+ ast.forEachDescendant((node, traversal) => {
1652
+ if (isImportOrExport(node)) {
1653
+ traversal.skip();
1654
+ return;
1655
+ }
1656
+ if (components) {
1657
+ if (Node7.isJsxOpeningElement(node) || Node7.isJsxSelfClosingElement(node)) {
1658
+ const componentNode = node;
1659
+ const componentName = getComponentName(componentNode);
1660
+ const isFactory = componentName.includes(".");
1661
+ if (!components.matchTag({ tagNode: componentNode, tagName: componentName, isFactory })) {
1662
+ return;
1663
+ }
1664
+ ensureComponent(componentNode, componentName);
1665
+ }
1666
+ if (Node7.isJsxSpreadAttribute(node)) {
1667
+ const componentNode = node.getFirstAncestor(isJsxElement);
1668
+ const component = componentByNode.get(componentNode);
1669
+ if (!componentNode || !component) return;
1670
+ const componentName = getComponentName(componentNode);
1671
+ const boxByProp = byName.get(componentName).nodesByProp;
1672
+ const matchProp = ({ propName, propNode }) => components.matchProp({ tagNode: componentNode, tagName: componentName, propName, propNode });
1673
+ const spreadNode = extractJsxSpreadAttributeValues(node, ctx, matchProp);
1674
+ if (!spreadNode) return;
1675
+ processComponentBoxNode(component, boxByProp, node, spreadNode, matchProp);
1676
+ return;
1677
+ }
1678
+ if (Node7.isJsxAttribute(node)) {
1679
+ const componentNode = node.getFirstAncestor(isJsxElement);
1680
+ const component = componentByNode.get(componentNode);
1681
+ if (!componentNode || !component) return;
1682
+ const componentName = getComponentName(componentNode);
1683
+ const boxByProp = byName.get(componentName).nodesByProp;
1684
+ const propName = node.getNameNode().getText();
1685
+ if (!components.matchProp({ tagNode: componentNode, tagName: componentName, propName, propNode: node })) {
1686
+ return;
1687
+ }
1688
+ const maybeBox = extractJsxAttribute(node, ctx);
1689
+ if (!maybeBox) return;
1690
+ addComponentProp(component, boxByProp, propName, maybeBox);
1691
+ }
1692
+ if (Node7.isCallExpression(node)) {
1693
+ const compiledCall = compiledJsx.getCallInfo(node);
1694
+ if (compiledCall) {
1695
+ const { tagName, isFactory, propNodes } = compiledCall;
1696
+ if (!components.matchTag({ tagNode: node, tagName, isFactory })) {
1697
+ return;
1698
+ }
1699
+ const { component, boxByProp } = ensureComponent(node, tagName);
1700
+ const matchProp = ({ propName, propNode }) => components.matchProp({ tagNode: node, tagName, propName, propNode });
1701
+ propNodes.forEach((propNode) => {
1702
+ processCompiledPropSource(node, component, boxByProp, propNode, matchProp);
1703
+ });
1704
+ }
1705
+ }
1706
+ }
1707
+ if (functions && Node7.isCallExpression(node)) {
1708
+ const expr = node.getExpression();
1709
+ const fnName = Node7.isCallExpression(expr) ? expr.getExpression().getText() : expr.getText();
1710
+ if (!functions.matchFn({ fnNode: node, fnName })) return;
1711
+ const matchProp = ({ propName, propNode }) => functions.matchProp({ fnNode: node, fnName, propName, propNode });
1712
+ if (!byName.has(fnName)) {
1713
+ byName.set(fnName, { kind: "function", nodesByProp: /* @__PURE__ */ new Map(), queryList: [] });
1714
+ }
1715
+ const fnResultMap = byName.get(fnName);
1716
+ const boxByProp = fnResultMap.nodesByProp;
1717
+ const boxNodeArray = extractCallExpressionArguments(node, ctx, matchProp, functions.matchArg);
1718
+ const nodeList = boxNodeArray.value.map((boxNode) => {
1719
+ if (box.isObject(boxNode) || box.isMap(boxNode)) {
1720
+ const mapValue = objectLikeToMap(boxNode, node);
1721
+ const isMap = box.isMap(boxNode);
1722
+ mapValue.forEach((propValue, propName) => {
1723
+ if (isMap ? true : matchProp({ propName, propNode: node })) {
1724
+ boxByProp.set(propName, (boxByProp.get(propName) ?? []).concat(propValue));
1725
+ }
1726
+ });
1727
+ const boxMap = box.map(mapValue, node, boxNode.getStack());
1728
+ if (box.isMap(boxNode) && boxNode.spreadConditions?.length) {
1729
+ boxMap.spreadConditions = boxNode.spreadConditions;
1730
+ }
1731
+ return boxMap;
1732
+ }
1733
+ return boxNode;
1734
+ });
1735
+ const query = {
1736
+ kind: "call-expression",
1737
+ name: fnName,
1738
+ box: box.array(nodeList, node, [])
1739
+ };
1740
+ fnResultMap.queryList.push(query);
1741
+ }
1742
+ if (taggedTemplates && Node7.isTaggedTemplateExpression(node)) {
1743
+ const tag = node.getTag();
1744
+ const fnName = Node7.isCallExpression(tag) ? tag.getExpression().getText() : tag.getText();
1745
+ if (!taggedTemplates.matchTaggedTemplate({ taggedTemplateNode: node, fnName })) return;
1746
+ if (!byName.has(fnName)) {
1747
+ byName.set(fnName, { kind: "function", nodesByProp: /* @__PURE__ */ new Map(), queryList: [] });
1748
+ }
1749
+ const fnResultMap = byName.get(fnName);
1750
+ const query = {
1751
+ kind: "tagged-template",
1752
+ name: fnName,
1753
+ box: maybeBoxNode(node, [], ctx)
1754
+ };
1755
+ fnResultMap.queryList.push(query);
1756
+ }
1757
+ });
1758
+ componentByNode.forEach((parentRef, componentNode) => {
1759
+ const component = componentByNode.get(componentNode);
1760
+ if (!component) return;
1761
+ if (Node7.isCallExpression(componentNode) && component.props.size === 0 && component.conditionals.length === 0)
1762
+ return;
1763
+ const query = {
1764
+ name: parentRef.name,
1765
+ box: box.map(component.props, componentNode, [])
1766
+ };
1767
+ if (component.conditionals?.length) {
1768
+ query.box.spreadConditions = component.conditionals;
1769
+ }
1770
+ const componentName = parentRef.name;
1771
+ const queryList = byName.get(componentName).queryList;
1772
+ queryList.push(query);
1773
+ });
1774
+ byName.forEach((result, name) => {
1775
+ if (result.kind === "component" && result.queryList.length === 0) {
1776
+ byName.delete(name);
1777
+ }
1778
+ });
1779
+ return byName;
1780
+ };
1781
+
1782
+ // src/jsx-element-props.ts
1783
+ import { Node as Node8 } from "ts-morph";
1784
+ var isObjectLike = (node) => box.isObject(node) || box.isMap(node);
1785
+ var extractJsxElementProps = (node, ctx) => {
1786
+ const tagName = node.getTagNameNode().getText();
1787
+ const jsxAttributes = node.getAttributes();
1788
+ const props = /* @__PURE__ */ new Map();
1789
+ jsxAttributes.forEach((attrNode) => {
1790
+ if (Node8.isJsxAttribute(attrNode)) {
1791
+ const nameNode = attrNode.getNameNode();
1792
+ const maybeValue = extractJsxAttribute(attrNode, ctx);
1793
+ if (!maybeValue) return;
1794
+ props.set(nameNode.getText(), maybeValue);
1795
+ return;
1796
+ }
1797
+ if (Node8.isJsxSpreadAttribute(attrNode)) {
1798
+ const maybeValue = extractJsxSpreadAttributeValues(attrNode, ctx, () => true);
1799
+ if (!isObjectLike(maybeValue)) return;
1800
+ if (box.isMap(maybeValue)) {
1801
+ maybeValue.value.forEach((value, propName) => {
1802
+ props.set(propName, value);
1803
+ });
1804
+ }
1805
+ if (box.isObject(maybeValue)) {
1806
+ Object.entries(maybeValue.value).forEach(([propName, value]) => {
1807
+ props.set(propName, box.literal(value, node, []));
1808
+ });
1809
+ }
1810
+ }
1811
+ });
1812
+ return { name: tagName, props };
1813
+ };
1814
+
1815
+ // src/unbox.ts
1816
+ import { Node as Node9 } from "ts-morph";
1817
+ var makeObjAt = (path, value) => {
1818
+ if (!path.length) return value;
1819
+ const obj = {};
1820
+ path.reduce((acc, key, i) => {
1821
+ const isLast = i === path.length - 1;
1822
+ acc[key] = isLast ? value : {};
1823
+ return isLast ? obj : acc[key];
1824
+ }, obj);
1825
+ return obj;
1826
+ };
1827
+ var makeArrayWithValueAt = (index, value) => {
1828
+ if (index < 0) return [];
1829
+ const arr = [];
1830
+ for (let i = 0; i <= index; i++) {
1831
+ arr[i] = i === index ? value : void 0;
1832
+ }
1833
+ return arr;
1834
+ };
1835
+ var getLiteralValue = (node, ctx) => {
1836
+ if (!node) return;
1837
+ if (box.isConditional(node)) {
1838
+ const path = ctx.path;
1839
+ const whenTrue = getLiteralValue(node.whenTrue, Object.assign({}, ctx, { path, parent: node }));
1840
+ const whenFalse = getLiteralValue(node.whenFalse, Object.assign({}, ctx, { path, parent: node }));
1841
+ const last = node.getStack().at(-1);
1842
+ const maybeIndex = Number(path[path.length - 1]);
1843
+ if (last && Node9.isArrayLiteralExpression(last) && !Number.isNaN(maybeIndex)) {
1844
+ const sliced = path.slice(0, -1);
1845
+ if (whenTrue) {
1846
+ ctx.conditions.push(makeObjAt(sliced, makeArrayWithValueAt(maybeIndex, whenTrue)));
1847
+ }
1848
+ if (whenFalse) {
1849
+ ctx.conditions.push(makeObjAt(sliced, makeArrayWithValueAt(maybeIndex, whenFalse)));
1850
+ }
1851
+ return void 0;
1852
+ }
1853
+ if (isTruthyOrZero(whenTrue)) {
1854
+ ctx.conditions.push(makeObjAt(path, whenTrue));
1855
+ }
1856
+ if (isTruthyOrZero(whenFalse)) {
1857
+ ctx.conditions.push(makeObjAt(path, whenFalse));
1858
+ }
1859
+ return void 0;
1860
+ }
1861
+ if (box.isLiteral(node) || box.isObject(node)) {
1862
+ return node.value;
1863
+ }
1864
+ if (box.isEmptyInitializer(node)) {
1865
+ return true;
1866
+ }
1867
+ if (box.isMap(node)) {
1868
+ if (node.spreadConditions) {
1869
+ const path = ctx.path;
1870
+ node.spreadConditions.forEach((spread) => {
1871
+ const whenTrue = getLiteralValue(spread.whenTrue, Object.assign({}, ctx, { path, parent: node }));
1872
+ const whenFalse = getLiteralValue(spread.whenFalse, Object.assign({}, ctx, { path, parent: node }));
1873
+ if (whenTrue) {
1874
+ ctx.spreadConditions.push(makeObjAt(path, whenTrue));
1875
+ }
1876
+ if (whenFalse) {
1877
+ ctx.spreadConditions.push(makeObjAt(path, whenFalse));
1878
+ }
1879
+ });
1880
+ }
1881
+ const obj = {};
1882
+ node.value.forEach((propNode, key) => {
1883
+ const value = getLiteralValue(propNode, Object.assign({}, ctx, { path: ctx.path.concat(key), parent: node }));
1884
+ if (isNotNullish(value)) {
1885
+ obj[key] = value;
1886
+ }
1887
+ });
1888
+ return obj;
1889
+ }
1890
+ if (box.isArray(node)) {
1891
+ return node.value.flatMap(
1892
+ (elementNode, index) => getLiteralValue(elementNode, Object.assign({}, ctx, { path: ctx.path.concat(String(index)), parent: node }))
1893
+ );
1894
+ }
1895
+ };
1896
+ var cacheMap3 = /* @__PURE__ */ new WeakMap();
1897
+ var createCache = (map) => ({
1898
+ value: map,
1899
+ has: (node) => map.has(node),
1900
+ get: (node) => map.get(node),
1901
+ set: (node, value) => map.set(node, value)
1902
+ });
1903
+ var unbox = (node, ctx) => {
1904
+ const _ctx = {
1905
+ cache: ctx?.cache ?? cacheMap3,
1906
+ ...ctx,
1907
+ path: [],
1908
+ parent: void 0,
1909
+ conditions: [],
1910
+ spreadConditions: []
1911
+ };
1912
+ const cache = createCache(_ctx.cache);
1913
+ if (cache.has(node)) {
1914
+ return cache.get(node);
1915
+ }
1916
+ let raw;
1917
+ if (Array.isArray(node)) {
1918
+ raw = node.map((boxNode) => getLiteralValue(boxNode, _ctx)).filter(isNotNullish)[0];
1919
+ } else {
1920
+ raw = getLiteralValue(node, _ctx);
1921
+ }
1922
+ const result = { raw: raw ?? {}, conditions: _ctx.conditions, spreadConditions: _ctx.spreadConditions };
1923
+ if (raw) {
1924
+ cache.set(node, result);
1925
+ }
1926
+ return result;
1927
+ };
1928
+ export {
1929
+ BoxNodeArray,
1930
+ BoxNodeConditional,
1931
+ BoxNodeEmptyInitializer,
1932
+ BoxNodeLiteral,
1933
+ BoxNodeMap,
1934
+ BoxNodeObject,
1935
+ BoxNodeUnresolvable,
1936
+ box,
1937
+ extract,
1938
+ extractCallExpressionArguments,
1939
+ extractJsxAttribute,
1940
+ extractJsxElementProps,
1941
+ extractJsxSpreadAttributeValues,
1942
+ findIdentifierValueDeclaration,
1943
+ isBoxNode,
1944
+ maybeBoxNode,
1945
+ maybeIdentifierValue,
1946
+ unbox
1947
+ };