@pandacss/parser 0.22.1 → 0.24.0

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,8 +1,12 @@
1
1
  // src/project.ts
2
- import { Project as TsProject, ScriptKind } from "ts-morph";
2
+ import {
3
+ ScriptKind,
4
+ Project as TsProject
5
+ } from "ts-morph";
3
6
 
4
7
  // src/parser.ts
5
- import { extract, unbox, box } from "@pandacss/extractor";
8
+ import { resolveTsPathPattern } from "@pandacss/config/ts-path";
9
+ import { box, extract, unbox } from "@pandacss/extractor";
6
10
  import { logger } from "@pandacss/logger";
7
11
  import { astish, memo as memo2 } from "@pandacss/shared";
8
12
  import { Node } from "ts-morph";
@@ -63,7 +67,14 @@ function getImportDeclarations(file, options) {
63
67
  }
64
68
 
65
69
  // src/parser-result.ts
66
- var ParserResult = class _ParserResult {
70
+ import { getOrCreateSet } from "@pandacss/shared";
71
+ var ParserResult = class {
72
+ constructor(context, encoder) {
73
+ this.context = context;
74
+ this.encoder = encoder ?? context.encoder;
75
+ }
76
+ /** Ordered list of all ResultItem */
77
+ all = [];
67
78
  jsx = /* @__PURE__ */ new Set();
68
79
  css = /* @__PURE__ */ new Set();
69
80
  cva = /* @__PURE__ */ new Set();
@@ -71,42 +82,95 @@ var ParserResult = class _ParserResult {
71
82
  recipe = /* @__PURE__ */ new Map();
72
83
  pattern = /* @__PURE__ */ new Map();
73
84
  filePath;
85
+ encoder;
86
+ append(result) {
87
+ this.all.push(result);
88
+ return result;
89
+ }
74
90
  set(name, result) {
75
- this[name].add({ type: "object", ...result });
91
+ this[name].add(this.append(Object.assign({ type: "object" }, result)));
92
+ const encoder = this.encoder;
93
+ if (name == "css") {
94
+ result.data.forEach((obj) => encoder.processStyleProps(obj));
95
+ return;
96
+ }
97
+ if (name === "cva") {
98
+ result.data.forEach((data) => encoder.processAtomicRecipe(data));
99
+ return;
100
+ }
101
+ if (name === "sva") {
102
+ result.data.forEach((data) => encoder.processAtomicSlotRecipe(data));
103
+ return;
104
+ }
76
105
  }
77
106
  setCva(result) {
78
- this.cva.add({ type: "cva", ...result });
107
+ this.cva.add(this.append(Object.assign({ type: "cva" }, result)));
108
+ const encoder = this.encoder;
109
+ result.data.forEach((data) => encoder.processAtomicRecipe(data));
79
110
  }
80
111
  setSva(result) {
81
- this.sva.add({ type: "sva", ...result });
112
+ this.sva.add(this.append(Object.assign({ type: "sva" }, result)));
113
+ const encoder = this.encoder;
114
+ result.data.forEach((data) => encoder.processAtomicSlotRecipe(data));
82
115
  }
83
116
  setJsx(result) {
84
- this.jsx.add({ type: "jsx", ...result });
117
+ this.jsx.add(this.append(Object.assign({ type: "jsx" }, result)));
118
+ const encoder = this.encoder;
119
+ result.data.forEach((obj) => encoder.processStyleProps(obj));
85
120
  }
86
121
  setPattern(name, result) {
87
- this.pattern.get(name) ?? this.pattern.set(name, /* @__PURE__ */ new Set());
88
- this.pattern.get(name)?.add({ type: "pattern", name, ...result });
122
+ const set = getOrCreateSet(this.pattern, name);
123
+ set.add(this.append(Object.assign({ type: "pattern", name }, result)));
124
+ const encoder = this.encoder;
125
+ result.data.forEach(
126
+ (obj) => encoder.processPattern(name, obj, result.type ?? "pattern", result.name)
127
+ );
89
128
  }
90
- setRecipe(name, result) {
91
- this.recipe.get(name) ?? this.recipe.set(name, /* @__PURE__ */ new Set());
92
- this.recipe.get(name)?.add({ type: "recipe", ...result });
129
+ setRecipe(recipeName, result) {
130
+ const set = getOrCreateSet(this.recipe, recipeName);
131
+ set.add(this.append(Object.assign({ type: "recipe" }, result)));
132
+ const encoder = this.encoder;
133
+ const recipes = this.context.recipes;
134
+ const recipeConfig = recipes.getConfig(recipeName);
135
+ if (!recipeConfig)
136
+ return;
137
+ const recipe = result;
138
+ if (result.type) {
139
+ recipe.data.forEach((data) => {
140
+ const [recipeProps, styleProps] = recipes.splitProps(recipeName, data);
141
+ encoder.processStyleProps(styleProps);
142
+ encoder.processRecipe(recipeName, recipeProps);
143
+ });
144
+ } else {
145
+ recipe.data.forEach((data) => {
146
+ encoder.processRecipe(recipeName, data);
147
+ });
148
+ }
93
149
  }
94
150
  isEmpty() {
95
- return this.css.size === 0 && this.cva.size === 0 && this.sva.size === 0 && this.recipe.size === 0 && this.pattern.size === 0 && this.jsx.size === 0;
151
+ return this.all.length === 0;
96
152
  }
97
153
  setFilePath(filePath) {
98
154
  this.filePath = filePath;
99
155
  return this;
100
156
  }
157
+ merge(result) {
158
+ result.css.forEach((item) => this.css.add(this.append(item)));
159
+ result.cva.forEach((item) => this.cva.add(this.append(item)));
160
+ result.sva.forEach((item) => this.sva.add(this.append(item)));
161
+ result.jsx.forEach((item) => this.jsx.add(this.append(item)));
162
+ result.recipe.forEach((items, name) => {
163
+ const set = getOrCreateSet(this.recipe, name);
164
+ items.forEach((item) => set.add(this.append(item)));
165
+ });
166
+ result.pattern.forEach((items, name) => {
167
+ const set = getOrCreateSet(this.pattern, name);
168
+ items.forEach((item) => set.add(this.append(item)));
169
+ });
170
+ return this;
171
+ }
101
172
  toArray() {
102
- const result = [];
103
- this.css.forEach((item) => result.push(item));
104
- this.cva.forEach((item) => result.push(item));
105
- this.sva.forEach((item) => result.push(item));
106
- this.jsx.forEach((item) => result.push(item));
107
- this.recipe.forEach((items) => items.forEach((item) => result.push(item)));
108
- this.pattern.forEach((items) => items.forEach((item) => result.push(item)));
109
- return result;
173
+ return this.all;
110
174
  }
111
175
  toJSON() {
112
176
  return {
@@ -118,41 +182,14 @@ var ParserResult = class _ParserResult {
118
182
  pattern: Object.fromEntries(Array.from(this.pattern.entries()).map(([key, value]) => [key, Array.from(value)]))
119
183
  };
120
184
  }
121
- merge(result) {
122
- result.css.forEach((item) => this.css.add(item));
123
- result.cva.forEach((item) => this.cva.add(item));
124
- result.sva.forEach((item) => this.sva.add(item));
125
- result.jsx.forEach((item) => this.jsx.add(item));
126
- result.recipe.forEach((items, name) => {
127
- this.recipe.get(name) ?? this.recipe.set(name, /* @__PURE__ */ new Set());
128
- items.forEach((item) => this.recipe.get(name)?.add(item));
129
- });
130
- result.pattern.forEach((items, name) => {
131
- this.pattern.get(name) ?? this.pattern.set(name, /* @__PURE__ */ new Set());
132
- items.forEach((item) => this.pattern.get(name)?.add(item));
133
- });
134
- return this;
135
- }
136
- static fromJSON(json) {
137
- const data = JSON.parse(json);
138
- const result = new _ParserResult();
139
- result.css = new Set(data.css);
140
- result.cva = new Set(data.cva);
141
- result.sva = new Set(data.sva);
142
- result.jsx = new Set(data.jsx);
143
- result.recipe = new Map(Object.entries(data.recipe));
144
- result.pattern = new Map(Object.entries(data.pattern));
145
- return result;
146
- }
147
185
  };
148
- var createParserResult = () => new ParserResult();
149
186
 
150
187
  // src/parser.ts
151
- import { resolveTsPathPattern } from "@pandacss/config/ts-path";
152
188
  var isNodeRecipe = (node) => node.type === "recipe";
153
189
  var isNodePattern = (node) => node.type === "pattern";
154
190
  var cvaProps = ["compoundVariants", "defaultVariants", "variants", "base"];
155
191
  var isCva = (map) => cvaProps.some((prop) => map.has(prop));
192
+ var noop = (..._args) => void 0;
156
193
  function createImportMatcher(mod, values) {
157
194
  const regex = values ? new RegExp(`^(${values.join("|")})$`) : /.*/;
158
195
  return {
@@ -174,18 +211,22 @@ var fallback = (box2) => ({
174
211
  var defaultEnv = { preset: "ECMA" };
175
212
  var identityFn = (styles) => styles;
176
213
  var evaluateOptions = { environment: defaultEnv };
177
- function createParser(options) {
178
- const { jsx, getRecipesByJsxName, getPatternsByJsxName, tsOptions, join } = options;
179
- const importMap = Object.fromEntries(Object.entries(options.importMap).map(([key, value]) => [key, join(...value)]));
214
+ function createParser(context) {
215
+ const { jsx, isValidProperty, tsOptions, join, syntax } = context;
216
+ const getRecipesByJsxName = context.recipes.filter;
217
+ const getPatternsByJsxName = context.patterns.filter;
218
+ const [recipeKeys, patternKeys] = [context.recipes.keys, context.patterns.keys];
219
+ const importMap = Object.fromEntries(Object.entries(context.importMap).map(([key, value]) => [key, join(...value)]));
220
+ const isJsxEnabled = jsx.framework;
180
221
  const importRegex = [
181
222
  createImportMatcher(importMap.css, ["css", "cva", "sva"]),
182
223
  createImportMatcher(importMap.recipe),
183
224
  createImportMatcher(importMap.pattern)
184
225
  ];
185
- if (jsx.framework) {
226
+ if (isJsxEnabled) {
186
227
  importRegex.push(createImportMatcher(importMap.jsx, [jsx.factory, ...jsx.nodes.map((node) => node.jsxName)]));
187
228
  }
188
- return function parse2(sourceFile) {
229
+ return function parse2(sourceFile, encoder) {
189
230
  if (!sourceFile)
190
231
  return;
191
232
  const filePath = sourceFile.getFilePath();
@@ -210,36 +251,37 @@ function createParser(options) {
210
251
  return found;
211
252
  }
212
253
  });
213
- const collector = createParserResult();
254
+ const parserResult = new ParserResult(context, encoder);
214
255
  logger.debug(
215
256
  "ast:import",
216
257
  imports.value.length ? `Found import { ${imports} } in ${filePath}` : `No import found in ${filePath}`
217
258
  );
259
+ if (!imports.value.length && !isJsxEnabled) {
260
+ return parserResult;
261
+ }
218
262
  const [css] = importRegex;
219
- const jsxFactoryAlias = jsx ? imports.getAlias(jsx.factory) : "styled";
220
- const isValidPattern = imports.createMatch(importMap.pattern, options.patternKeys);
221
- const isValidRecipe = imports.createMatch(importMap.recipe, options.recipeKeys);
222
- const isValidStyleFn = (name) => name === jsx?.factory;
223
- const isFactory = (name) => Boolean(jsx && name.startsWith(jsxFactoryAlias));
263
+ const jsxFactoryAlias = isJsxEnabled ? imports.getAlias(jsx.factory) : "styled";
264
+ const isValidPattern = imports.createMatch(importMap.pattern, patternKeys);
265
+ const isValidRecipe = imports.createMatch(importMap.recipe, recipeKeys);
266
+ const isValidStyleFn = (name) => name === jsx.factory;
267
+ const isFactory = (name) => Boolean(isJsxEnabled && name.startsWith(jsxFactoryAlias));
224
268
  const isRawFn = (fullName) => {
225
269
  const name = fullName.split(".raw")[0] ?? "";
226
270
  return name === "css" || isValidPattern(name) || isValidRecipe(name);
227
271
  };
228
- const patternPropertiesByName = /* @__PURE__ */ new Map();
229
- const patternJsxLists = (jsx?.nodes ?? []).filter(isNodePattern).reduce(
230
- (acc, pattern) => {
231
- patternPropertiesByName.set(pattern.jsxName, new Set(pattern.props ?? []));
232
- pattern.jsx?.forEach((jsx2) => {
233
- if (typeof jsx2 === "string") {
234
- acc.string.add(jsx2);
235
- } else if (jsx2) {
236
- acc.regex.push(jsx2);
237
- }
238
- });
239
- return acc;
240
- },
241
- { string: /* @__PURE__ */ new Set(), regex: [] }
242
- );
272
+ const patternPropertiesByJsxName = /* @__PURE__ */ new Map();
273
+ const initialPatterns = { string: /* @__PURE__ */ new Set(), regex: [] };
274
+ const patternJsxLists = isJsxEnabled ? (jsx.nodes ?? []).filter(isNodePattern).reduce((acc, pattern) => {
275
+ patternPropertiesByJsxName.set(pattern.jsxName, new Set(pattern.props ?? []));
276
+ pattern.jsx.forEach((jsx2) => {
277
+ if (typeof jsx2 === "string") {
278
+ acc.string.add(jsx2);
279
+ } else if (jsx2) {
280
+ acc.regex.push(jsx2);
281
+ }
282
+ });
283
+ return acc;
284
+ }, initialPatterns) : initialPatterns;
243
285
  const recipes = /* @__PURE__ */ new Set();
244
286
  const patterns = /* @__PURE__ */ new Set();
245
287
  imports.value.forEach((importDeclaration) => {
@@ -255,25 +297,23 @@ function createParser(options) {
255
297
  const components = /* @__PURE__ */ new Map();
256
298
  const propertiesMap = /* @__PURE__ */ new Map();
257
299
  const recipePropertiesByJsxName = /* @__PURE__ */ new Map();
258
- const recipeJsxLists = (jsx?.nodes ?? []).filter(isNodeRecipe).reduce(
259
- (acc, recipe) => {
260
- recipePropertiesByJsxName.set(recipe.jsxName, new Set(recipe.props ?? []));
261
- recipe.jsx?.forEach((jsx2) => {
262
- if (typeof jsx2 === "string") {
263
- acc.string.add(jsx2);
264
- } else {
265
- acc.regex.push(jsx2);
266
- }
267
- });
268
- return acc;
269
- },
270
- { string: /* @__PURE__ */ new Set(), regex: [] }
271
- );
300
+ const initialRecipes = { string: /* @__PURE__ */ new Set(), regex: [] };
301
+ const recipeJsxLists = isJsxEnabled ? (jsx.nodes ?? []).filter(isNodeRecipe).reduce((acc, recipe) => {
302
+ recipePropertiesByJsxName.set(recipe.jsxName, new Set(recipe.props ?? []));
303
+ recipe.jsx.forEach((jsx2) => {
304
+ if (typeof jsx2 === "string") {
305
+ acc.string.add(jsx2);
306
+ } else {
307
+ acc.regex.push(jsx2);
308
+ }
309
+ });
310
+ return acc;
311
+ }, initialRecipes) : initialRecipes;
272
312
  const cvaAlias = imports.getAlias("cva");
273
313
  const cssAlias = imports.getAlias("css");
274
314
  const svaAlias = imports.getAlias("sva");
275
- if (options.jsx) {
276
- options.jsx.nodes.forEach((node) => {
315
+ if (context.jsx) {
316
+ context.jsx.nodes.forEach((node) => {
277
317
  const alias = imports.getAlias(node.jsxName);
278
318
  node.props?.forEach((prop) => propertiesMap.set(prop, true));
279
319
  functions.set(node.baseName, propertiesMap);
@@ -281,36 +321,39 @@ function createParser(options) {
281
321
  components.set(alias, propertiesMap);
282
322
  });
283
323
  }
284
- const isJsxTagRecipe = memo2(
324
+ const isJsxTagRecipe = isJsxEnabled ? memo2(
285
325
  (tagName) => recipeJsxLists.string.has(tagName) || recipeJsxLists.regex.some((regex) => regex.test(tagName))
286
- );
287
- const isJsxTagPattern = memo2(
326
+ ) : noop;
327
+ const isJsxTagPattern = isJsxEnabled ? memo2(
288
328
  (tagName) => patternJsxLists.string.has(tagName) || patternJsxLists.regex.some((regex) => regex.test(tagName))
289
- );
290
- const matchTag = memo2((tagName) => {
329
+ ) : noop;
330
+ const matchTag = isJsxEnabled ? memo2((tagName) => {
291
331
  if (!tagName)
292
332
  return false;
293
333
  return components.has(tagName) || isUpperCase(tagName) || isFactory(tagName) || isJsxTagRecipe(tagName) || isJsxTagPattern(tagName);
294
- });
334
+ }) : noop;
295
335
  const isRecipeOrPatternProp = memo2((tagName, propName) => {
296
- if (isJsxTagRecipe(tagName)) {
336
+ if (isJsxEnabled && isJsxTagRecipe(tagName)) {
297
337
  const recipeList = getRecipesByJsxName(tagName);
298
338
  return recipeList.some((recipe) => recipePropertiesByJsxName.get(recipe.jsxName)?.has(propName));
299
339
  }
300
- if (isJsxTagPattern(tagName)) {
340
+ if (isJsxEnabled && isJsxTagPattern(tagName)) {
301
341
  const patternList = getPatternsByJsxName(tagName);
302
- return patternList.some((pattern) => patternPropertiesByName.get(pattern.baseName)?.has(propName));
342
+ return patternList.some((pattern) => patternPropertiesByJsxName.get(pattern.jsxName)?.has(propName));
303
343
  }
304
344
  return false;
305
345
  });
306
- const matchTagProp = match(jsx?.styleProps).with(
346
+ const matchTagProp = isJsxEnabled ? match(jsx.styleProps).with(
307
347
  "all",
308
348
  () => memo2((tagName, propName) => {
309
- return Boolean(components.get(tagName)?.has(propName)) || options.jsx?.isStyleProp(propName) || propertiesMap.has(propName) || isRecipeOrPatternProp(tagName, propName);
349
+ return Boolean(components.get(tagName)?.has(propName)) || isValidProperty(propName) || propertiesMap.has(propName) || isRecipeOrPatternProp(tagName, propName);
350
+ })
351
+ ).with(
352
+ "minimal",
353
+ () => memo2((tagName, propName) => {
354
+ return propName === "css" || isRecipeOrPatternProp(tagName, propName);
310
355
  })
311
- ).with("minimal", () => (tagName, propName) => {
312
- return propName === "css" || isRecipeOrPatternProp(tagName, propName);
313
- }).otherwise(() => (tagName, propName) => isRecipeOrPatternProp(tagName, propName));
356
+ ).with("none", () => memo2((tagName, propName) => isRecipeOrPatternProp(tagName, propName))).exhaustive() : noop;
314
357
  const matchFn = memo2((fnName) => {
315
358
  if (recipes.has(fnName) || patterns.has(fnName))
316
359
  return true;
@@ -321,10 +364,10 @@ function createParser(options) {
321
364
  const measure = logger.time.debug(`Tokens extracted from ${filePath}`);
322
365
  const extractResultByName = extract({
323
366
  ast: sourceFile,
324
- components: {
325
- matchTag: (prop) => matchTag(prop.tagName),
326
- matchProp: (prop) => matchTagProp(prop.tagName, prop.propName)
327
- },
367
+ components: isJsxEnabled ? {
368
+ matchTag: (prop) => !!matchTag(prop.tagName),
369
+ matchProp: (prop) => !!matchTagProp(prop.tagName, prop.propName)
370
+ } : void 0,
328
371
  functions: {
329
372
  matchFn: (prop) => matchFn(prop.fnName),
330
373
  matchProp: () => true,
@@ -334,9 +377,7 @@ function createParser(options) {
334
377
  return true;
335
378
  }
336
379
  },
337
- taggedTemplates: {
338
- matchTaggedTemplate: (tag) => matchFn(tag.fnName)
339
- },
380
+ taggedTemplates: syntax === "template-literal" ? { matchTaggedTemplate: (tag) => matchFn(tag.fnName) } : void 0,
340
381
  getEvaluateOptions: (node) => {
341
382
  if (!Node.isCallExpression(node))
342
383
  return evaluateOptions;
@@ -356,16 +397,17 @@ function createParser(options) {
356
397
  });
357
398
  measure();
358
399
  extractResultByName.forEach((result, alias) => {
359
- let name = imports.getName(alias);
400
+ let name = alias;
360
401
  if (isRawFn(name))
361
402
  name = name.replace(".raw", "");
403
+ name = imports.getName(name);
362
404
  logger.debug(`ast:${name}`, name !== alias ? { kind: result.kind, alias } : { kind: result.kind });
363
405
  if (result.kind === "function") {
364
406
  match(name).when(css.match, (name2) => {
365
407
  result.queryList.forEach((query) => {
366
408
  if (query.kind === "call-expression") {
367
409
  if (query.box.value.length > 1) {
368
- collector.set(name2, {
410
+ parserResult.set(name2, {
369
411
  name: name2,
370
412
  box: query.box,
371
413
  data: query.box.value.reduce(
@@ -374,7 +416,7 @@ function createParser(options) {
374
416
  )
375
417
  });
376
418
  } else {
377
- collector.set(name2, {
419
+ parserResult.set(name2, {
378
420
  name: name2,
379
421
  box: query.box.value[0] ?? fallback(query.box),
380
422
  data: combineResult(unbox(query.box.value[0]))
@@ -382,7 +424,7 @@ function createParser(options) {
382
424
  }
383
425
  } else if (query.kind === "tagged-template") {
384
426
  const obj = astish(query.box.value);
385
- collector.set(name2, {
427
+ parserResult.set(name2, {
386
428
  name: name2,
387
429
  box: query.box ?? fallback(query.box),
388
430
  data: [obj]
@@ -392,7 +434,7 @@ function createParser(options) {
392
434
  }).when(isValidPattern, (name2) => {
393
435
  result.queryList.forEach((query) => {
394
436
  if (query.kind === "call-expression") {
395
- collector.setPattern(name2, {
437
+ parserResult.setPattern(name2, {
396
438
  name: name2,
397
439
  box: query.box.value[0] ?? fallback(query.box),
398
440
  data: combineResult(unbox(query.box.value[0]))
@@ -402,7 +444,7 @@ function createParser(options) {
402
444
  }).when(isValidRecipe, (name2) => {
403
445
  result.queryList.forEach((query) => {
404
446
  if (query.kind === "call-expression") {
405
- collector.setRecipe(name2, {
447
+ parserResult.setRecipe(name2, {
406
448
  name: name2,
407
449
  box: query.box.value[0] ?? fallback(query.box),
408
450
  data: combineResult(unbox(query.box.value[0]))
@@ -416,27 +458,27 @@ function createParser(options) {
416
458
  const boxNode = box.isMap(map) ? map : fallback(query.box);
417
459
  const result2 = { name, box: boxNode, data: combineResult(unbox(boxNode)) };
418
460
  if (box.isMap(map) && isCva(map.value)) {
419
- collector.setCva(result2);
461
+ parserResult.setCva(result2);
420
462
  } else {
421
- collector.set("css", result2);
463
+ parserResult.set("css", result2);
422
464
  }
423
- const options2 = query.box.value[2];
424
- if (box.isUnresolvable(map) && options2 && box.isMap(options2) && options2.value.has("defaultProps")) {
465
+ const options = query.box.value[2];
466
+ if (box.isUnresolvable(map) && options && box.isMap(options) && options.value.has("defaultProps")) {
425
467
  const maybeIdentifier = map.getNode();
426
468
  if (Node.isIdentifier(maybeIdentifier)) {
427
469
  const name2 = maybeIdentifier.getText();
428
470
  const recipeName = imports.getName(name2);
429
- collector.setRecipe(recipeName, {
471
+ parserResult.setRecipe(recipeName, {
430
472
  type: "jsx-recipe",
431
473
  name: recipeName,
432
- box: options2,
433
- data: combineResult(unbox(options2.value.get("defaultProps")))
474
+ box: options,
475
+ data: combineResult(unbox(options.value.get("defaultProps")))
434
476
  });
435
477
  }
436
478
  }
437
479
  } else if (query.kind === "tagged-template") {
438
480
  const obj = astish(query.box.value);
439
- collector.set("css", {
481
+ parserResult.set("css", {
440
482
  name,
441
483
  box: query.box ?? fallback(query.box),
442
484
  data: [obj]
@@ -450,13 +492,13 @@ function createParser(options) {
450
492
  const boxNode = box.isMap(map) ? map : fallback(query.box);
451
493
  const result2 = { name: name2, box: boxNode, data: combineResult(unbox(boxNode)) };
452
494
  if (box.isMap(map) && isCva(map.value)) {
453
- collector.setCva(result2);
495
+ parserResult.setCva(result2);
454
496
  } else {
455
- collector.set("css", result2);
497
+ parserResult.set("css", result2);
456
498
  }
457
499
  } else if (query.kind === "tagged-template") {
458
500
  const obj = astish(query.box.value);
459
- collector.set("css", {
501
+ parserResult.set("css", {
460
502
  name: name2,
461
503
  box: query.box ?? fallback(query.box),
462
504
  data: [obj]
@@ -465,32 +507,57 @@ function createParser(options) {
465
507
  });
466
508
  }).otherwise(() => {
467
509
  });
468
- } else if (result.kind === "component") {
510
+ } else if (isJsxEnabled && result.kind === "component") {
469
511
  result.queryList.forEach((query) => {
470
512
  const data = combineResult(unbox(query.box));
471
513
  match(name).when(isFactory, (jsxName) => {
472
- collector.setJsx({ name: jsxName, box: query.box, type: "jsx-factory", data });
514
+ parserResult.setJsx({ name: jsxName, box: query.box, type: "jsx-factory", data });
473
515
  }).when(isJsxTagPattern, (jsxName) => {
474
- collector.setPattern(jsxName, { type: "jsx-pattern", name: jsxName, box: query.box, data });
516
+ parserResult.setPattern(jsxName, { type: "jsx-pattern", name: jsxName, box: query.box, data });
475
517
  }).when(isJsxTagRecipe, (jsxName) => {
476
518
  const recipeList = getRecipesByJsxName(jsxName);
477
519
  recipeList.map((recipe) => {
478
- collector.setRecipe(recipe.baseName, { type: "jsx-recipe", name: jsxName, box: query.box, data });
520
+ parserResult.setRecipe(recipe.baseName, { type: "jsx-recipe", name: jsxName, box: query.box, data });
479
521
  });
480
522
  }).otherwise(() => {
481
- collector.setJsx({ name, box: query.box, type: "jsx", data });
523
+ parserResult.setJsx({ name, box: query.box, type: "jsx", data });
482
524
  });
483
525
  });
484
526
  }
485
527
  });
486
- return collector;
528
+ return parserResult;
487
529
  };
488
530
  }
489
531
  var isUpperCase = (value) => value[0] === value[0]?.toUpperCase();
490
532
 
533
+ // src/svelte-to-tsx.ts
534
+ import MagicString from "magic-string";
535
+ var regex_style_tags = /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi;
536
+ var regex_script_tags = /<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi;
537
+ var svelteToTsx = (code) => {
538
+ try {
539
+ const scripts = [];
540
+ const original = new MagicString(code);
541
+ let match2;
542
+ while ((match2 = regex_script_tags.exec(code)) != null) {
543
+ const [fullMatch, _attributesStr, scriptContent] = match2;
544
+ if (scriptContent) {
545
+ scripts.push(scriptContent);
546
+ original.remove(match2.index, match2.index + fullMatch.length);
547
+ }
548
+ }
549
+ const templateContent = original.toString().trimStart().replace(regex_style_tags, "").replace(regex_style_tags, "");
550
+ const transformed = `${scripts.join("")}
551
+ const render = <div>${templateContent}</div>`;
552
+ return transformed.toString().trim();
553
+ } catch (err) {
554
+ return "";
555
+ }
556
+ };
557
+
491
558
  // src/vue-to-tsx.ts
492
559
  import { parse } from "@vue/compiler-sfc";
493
- import MagicString from "magic-string";
560
+ import MagicString2 from "magic-string";
494
561
  var NodeTypes = {
495
562
  ROOT: 0,
496
563
  ELEMENT: 1,
@@ -523,7 +590,7 @@ var NodeTypes = {
523
590
  var vueToTsx = (code) => {
524
591
  try {
525
592
  const parsed = parse(code);
526
- const fileStr = new MagicString(`<template>${parsed.descriptor.template?.content}</template>` ?? "");
593
+ const fileStr = new MagicString2(`<template>${parsed.descriptor.template?.content}</template>` ?? "");
527
594
  const rewriteProp = (prop) => {
528
595
  if (prop.type === NodeTypes.DIRECTIVE && prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION && prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION) {
529
596
  fileStr.replace(prop.loc.source, `${prop.arg.content}={${prop.exp.content}}`);
@@ -540,7 +607,7 @@ var vueToTsx = (code) => {
540
607
  }
541
608
  }
542
609
  const scriptContent = (parsed.descriptor.scriptSetup ?? parsed.descriptor.script)?.content + "\n";
543
- const transformed = new MagicString(`${scriptContent}
610
+ const transformed = new MagicString2(`${scriptContent}
544
611
  const render = ${fileStr.toString()}`);
545
612
  return transformed.toString();
546
613
  } catch (err) {
@@ -548,31 +615,6 @@ const render = ${fileStr.toString()}`);
548
615
  }
549
616
  };
550
617
 
551
- // src/svelte-to-tsx.ts
552
- import MagicString2 from "magic-string";
553
- var regex_style_tags = /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi;
554
- var regex_script_tags = /<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi;
555
- var svelteToTsx = (code) => {
556
- try {
557
- const scripts = [];
558
- const original = new MagicString2(code);
559
- let match2;
560
- while ((match2 = regex_script_tags.exec(code)) != null) {
561
- const [fullMatch, _attributesStr, scriptContent] = match2;
562
- if (scriptContent) {
563
- scripts.push(scriptContent);
564
- original.remove(match2.index, match2.index + fullMatch.length);
565
- }
566
- }
567
- const templateContent = original.toString().trimStart().replace(regex_style_tags, "").replace(regex_style_tags, "");
568
- const transformed = `${scripts.join("")}
569
- const render = <div>${templateContent}</div>`;
570
- return transformed.toString().trim();
571
- } catch (err) {
572
- return "";
573
- }
574
- };
575
-
576
618
  // src/project.ts
577
619
  var createTsProject = (options) => new TsProject({
578
620
  skipAddingFilesFromTsConfig: true,
@@ -586,86 +628,100 @@ var createTsProject = (options) => new TsProject({
586
628
  ...options.compilerOptions
587
629
  }
588
630
  });
589
- var createProject = ({
590
- getFiles,
591
- readFile,
592
- parserOptions,
593
- hooks,
594
- ...projectOptions
595
- }) => {
596
- const project = createTsProject(projectOptions);
597
- const parser = createParser(parserOptions);
598
- const getSourceFile = (filePath) => project.getSourceFile(filePath);
599
- const removeSourceFile = (filePath) => {
600
- const sourceFile = project.getSourceFile(filePath);
601
- if (sourceFile)
602
- project.removeSourceFile(sourceFile);
631
+ var Project = class {
632
+ constructor(options) {
633
+ this.options = options;
634
+ const { parserOptions } = options;
635
+ this.project = createTsProject(options);
636
+ this.parser = createParser(parserOptions);
637
+ this.createSourceFiles();
638
+ }
639
+ project;
640
+ parser;
641
+ get files() {
642
+ return this.options.getFiles();
643
+ }
644
+ getSourceFile = (filePath) => {
645
+ return this.project.getSourceFile(filePath);
603
646
  };
604
- const createSourceFile = (filePath) => project.createSourceFile(filePath, readFile(filePath), {
605
- overwrite: true,
606
- scriptKind: ScriptKind.TSX
607
- });
608
- const addSourceFile = (filePath, content) => project.createSourceFile(filePath, content, {
609
- overwrite: true,
610
- scriptKind: ScriptKind.TSX
611
- });
612
- const parseSourceFile = (filePath) => {
647
+ createSourceFile = (filePath) => {
648
+ const { readFile } = this.options;
649
+ return this.project.createSourceFile(filePath, readFile(filePath), {
650
+ overwrite: true,
651
+ scriptKind: ScriptKind.TSX
652
+ });
653
+ };
654
+ createSourceFiles = () => {
655
+ const files = this.getFiles();
656
+ for (const file of files) {
657
+ this.createSourceFile(file);
658
+ }
659
+ };
660
+ addSourceFile = (filePath, content) => {
661
+ return this.project.createSourceFile(filePath, content, {
662
+ overwrite: true,
663
+ scriptKind: ScriptKind.TSX
664
+ });
665
+ };
666
+ removeSourceFile = (filePath) => {
667
+ const sourceFile = this.project.getSourceFile(filePath);
668
+ if (sourceFile) {
669
+ return this.project.removeSourceFile(sourceFile);
670
+ }
671
+ return false;
672
+ };
673
+ reloadSourceFile = (filePath) => {
674
+ return this.getSourceFile(filePath)?.refreshFromFileSystemSync();
675
+ };
676
+ reloadSourceFiles = () => {
677
+ const files = this.getFiles();
678
+ for (const file of files) {
679
+ const source = this.getSourceFile(file);
680
+ source?.refreshFromFileSystemSync() ?? this.project.addSourceFileAtPath(file);
681
+ }
682
+ };
683
+ get readFile() {
684
+ return this.options.readFile;
685
+ }
686
+ get getFiles() {
687
+ return this.options.getFiles;
688
+ }
689
+ parseJson = (filePath) => {
690
+ const { readFile, parserOptions } = this.options;
691
+ const content = readFile(filePath);
692
+ parserOptions.encoder.fromJSON(content);
693
+ const result = new ParserResult(parserOptions);
694
+ return result.setFilePath(filePath);
695
+ };
696
+ parseSourceFile = (filePath, encoder) => {
697
+ const { hooks } = this.options;
613
698
  if (filePath.endsWith(".json")) {
614
- const content2 = readFile(filePath);
615
- hooks.callHook("parser:before", filePath, content2);
616
- const result2 = ParserResult.fromJSON(content2).setFilePath(filePath);
617
- hooks.callHook("parser:after", filePath, result2);
618
- return result2;
699
+ return this.parseJson(filePath);
619
700
  }
620
- const sourceFile = project.getSourceFile(filePath);
701
+ const sourceFile = this.project.getSourceFile(filePath);
621
702
  if (!sourceFile)
622
703
  return;
623
704
  const content = sourceFile.getText();
624
- const transformed = transformFile(filePath, content);
705
+ const transformed = this.transformFile(filePath, content);
625
706
  if (content !== transformed) {
626
707
  sourceFile.replaceWithText(transformed);
627
708
  }
628
709
  hooks.callHook("parser:before", filePath, content);
629
- const result = parser(sourceFile)?.setFilePath(filePath);
710
+ const result = this.parser(sourceFile, encoder)?.setFilePath(filePath);
630
711
  hooks.callHook("parser:after", filePath, result);
631
712
  return result;
632
713
  };
633
- const files = getFiles();
634
- for (const file of files) {
635
- createSourceFile(file);
636
- }
637
- const reloadSourceFile = (filePath) => getSourceFile(filePath)?.refreshFromFileSystemSync();
638
- const reloadSourceFiles = () => {
639
- const files2 = getFiles();
640
- for (const file of files2) {
641
- const source = getSourceFile(file);
642
- source?.refreshFromFileSystemSync() ?? project.addSourceFileAtPath(file);
714
+ transformFile = (filePath, content) => {
715
+ if (filePath.endsWith(".vue")) {
716
+ return vueToTsx(content);
643
717
  }
718
+ if (filePath.endsWith(".svelte")) {
719
+ return svelteToTsx(content);
720
+ }
721
+ return content;
644
722
  };
645
- return {
646
- getSourceFile,
647
- removeSourceFile,
648
- createSourceFile,
649
- addSourceFile,
650
- parseSourceFile,
651
- reloadSourceFile,
652
- reloadSourceFiles,
653
- files,
654
- getFiles,
655
- readFile
656
- };
657
- };
658
- var transformFile = (filePath, content) => {
659
- if (filePath.endsWith(".vue")) {
660
- return vueToTsx(content);
661
- }
662
- if (filePath.endsWith(".svelte")) {
663
- return svelteToTsx(content);
664
- }
665
- return content;
666
723
  };
667
724
  export {
668
725
  ParserResult,
669
- createParserResult,
670
- createProject
726
+ Project
671
727
  };