@bamboocss/extractor 1.11.1 → 1.11.3

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