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