@bamboocss/extractor 1.11.1 → 1.11.2

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