@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.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,9 +211,12 @@ 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, isTemplateLiteralSyntax } = 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)]));
180
220
  const isJsxEnabled = jsx.framework;
181
221
  const importRegex = [
182
222
  createImportMatcher(importMap.css, ["css", "cva", "sva"]),
@@ -186,7 +226,7 @@ function createParser(options) {
186
226
  if (isJsxEnabled) {
187
227
  importRegex.push(createImportMatcher(importMap.jsx, [jsx.factory, ...jsx.nodes.map((node) => node.jsxName)]));
188
228
  }
189
- return function parse2(sourceFile) {
229
+ return function parse2(sourceFile, encoder) {
190
230
  if (!sourceFile)
191
231
  return;
192
232
  const filePath = sourceFile.getFilePath();
@@ -211,19 +251,19 @@ function createParser(options) {
211
251
  return found;
212
252
  }
213
253
  });
214
- const collector = createParserResult();
254
+ const parserResult = new ParserResult(context, encoder);
215
255
  logger.debug(
216
256
  "ast:import",
217
257
  imports.value.length ? `Found import { ${imports} } in ${filePath}` : `No import found in ${filePath}`
218
258
  );
219
259
  if (!imports.value.length && !isJsxEnabled) {
220
- return collector;
260
+ return parserResult;
221
261
  }
222
262
  const [css] = importRegex;
223
263
  const jsxFactoryAlias = isJsxEnabled ? imports.getAlias(jsx.factory) : "styled";
224
- const isValidPattern = imports.createMatch(importMap.pattern, options.patternKeys);
225
- const isValidRecipe = imports.createMatch(importMap.recipe, options.recipeKeys);
226
- const isValidStyleFn = (name) => name === jsx?.factory;
264
+ const isValidPattern = imports.createMatch(importMap.pattern, patternKeys);
265
+ const isValidRecipe = imports.createMatch(importMap.recipe, recipeKeys);
266
+ const isValidStyleFn = (name) => name === jsx.factory;
227
267
  const isFactory = (name) => Boolean(isJsxEnabled && name.startsWith(jsxFactoryAlias));
228
268
  const isRawFn = (fullName) => {
229
269
  const name = fullName.split(".raw")[0] ?? "";
@@ -231,9 +271,9 @@ function createParser(options) {
231
271
  };
232
272
  const patternPropertiesByJsxName = /* @__PURE__ */ new Map();
233
273
  const initialPatterns = { string: /* @__PURE__ */ new Set(), regex: [] };
234
- const patternJsxLists = isJsxEnabled ? (jsx?.nodes ?? []).filter(isNodePattern).reduce((acc, pattern) => {
274
+ const patternJsxLists = isJsxEnabled ? (jsx.nodes ?? []).filter(isNodePattern).reduce((acc, pattern) => {
235
275
  patternPropertiesByJsxName.set(pattern.jsxName, new Set(pattern.props ?? []));
236
- pattern.jsx?.forEach((jsx2) => {
276
+ pattern.jsx.forEach((jsx2) => {
237
277
  if (typeof jsx2 === "string") {
238
278
  acc.string.add(jsx2);
239
279
  } else if (jsx2) {
@@ -258,9 +298,9 @@ function createParser(options) {
258
298
  const propertiesMap = /* @__PURE__ */ new Map();
259
299
  const recipePropertiesByJsxName = /* @__PURE__ */ new Map();
260
300
  const initialRecipes = { string: /* @__PURE__ */ new Set(), regex: [] };
261
- const recipeJsxLists = isJsxEnabled ? (jsx?.nodes ?? []).filter(isNodeRecipe).reduce((acc, recipe) => {
301
+ const recipeJsxLists = isJsxEnabled ? (jsx.nodes ?? []).filter(isNodeRecipe).reduce((acc, recipe) => {
262
302
  recipePropertiesByJsxName.set(recipe.jsxName, new Set(recipe.props ?? []));
263
- recipe.jsx?.forEach((jsx2) => {
303
+ recipe.jsx.forEach((jsx2) => {
264
304
  if (typeof jsx2 === "string") {
265
305
  acc.string.add(jsx2);
266
306
  } else {
@@ -272,8 +312,8 @@ function createParser(options) {
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);
@@ -283,37 +323,37 @@ function createParser(options) {
283
323
  }
284
324
  const isJsxTagRecipe = isJsxEnabled ? memo2(
285
325
  (tagName) => recipeJsxLists.string.has(tagName) || recipeJsxLists.regex.some((regex) => regex.test(tagName))
286
- ) : void 0;
326
+ ) : noop;
287
327
  const isJsxTagPattern = isJsxEnabled ? memo2(
288
328
  (tagName) => patternJsxLists.string.has(tagName) || patternJsxLists.regex.some((regex) => regex.test(tagName))
289
- ) : void 0;
329
+ ) : noop;
290
330
  const matchTag = isJsxEnabled ? memo2((tagName) => {
291
331
  if (!tagName)
292
332
  return false;
293
- return components.has(tagName) || isUpperCase(tagName) || isFactory(tagName) || isJsxTagRecipe?.(tagName) || isJsxTagPattern?.(tagName);
294
- }) : void 0;
333
+ return components.has(tagName) || isUpperCase(tagName) || isFactory(tagName) || isJsxTagRecipe(tagName) || isJsxTagPattern(tagName);
334
+ }) : noop;
295
335
  const isRecipeOrPatternProp = memo2((tagName, propName) => {
296
- if (isJsxEnabled && 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 (isJsxEnabled && isJsxTagPattern?.(tagName)) {
340
+ if (isJsxEnabled && isJsxTagPattern(tagName)) {
301
341
  const patternList = getPatternsByJsxName(tagName);
302
342
  return patternList.some((pattern) => patternPropertiesByJsxName.get(pattern.jsxName)?.has(propName));
303
343
  }
304
344
  return false;
305
345
  });
306
- const matchTagProp = isJsxEnabled ? 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);
310
350
  })
311
351
  ).with(
312
352
  "minimal",
313
353
  () => memo2((tagName, propName) => {
314
354
  return propName === "css" || isRecipeOrPatternProp(tagName, propName);
315
355
  })
316
- ).with("none", () => memo2((tagName, propName) => isRecipeOrPatternProp(tagName, propName))).exhaustive() : void 0;
356
+ ).with("none", () => memo2((tagName, propName) => isRecipeOrPatternProp(tagName, propName))).exhaustive() : noop;
317
357
  const matchFn = memo2((fnName) => {
318
358
  if (recipes.has(fnName) || patterns.has(fnName))
319
359
  return true;
@@ -325,8 +365,8 @@ function createParser(options) {
325
365
  const extractResultByName = extract({
326
366
  ast: sourceFile,
327
367
  components: isJsxEnabled ? {
328
- matchTag: (prop) => !!matchTag?.(prop.tagName),
329
- matchProp: (prop) => !!matchTagProp?.(prop.tagName, prop.propName)
368
+ matchTag: (prop) => !!matchTag(prop.tagName),
369
+ matchProp: (prop) => !!matchTagProp(prop.tagName, prop.propName)
330
370
  } : void 0,
331
371
  functions: {
332
372
  matchFn: (prop) => matchFn(prop.fnName),
@@ -337,7 +377,7 @@ function createParser(options) {
337
377
  return true;
338
378
  }
339
379
  },
340
- taggedTemplates: isTemplateLiteralSyntax ? { matchTaggedTemplate: (tag) => matchFn(tag.fnName) } : void 0,
380
+ taggedTemplates: syntax === "template-literal" ? { matchTaggedTemplate: (tag) => matchFn(tag.fnName) } : void 0,
341
381
  getEvaluateOptions: (node) => {
342
382
  if (!Node.isCallExpression(node))
343
383
  return evaluateOptions;
@@ -367,7 +407,7 @@ function createParser(options) {
367
407
  result.queryList.forEach((query) => {
368
408
  if (query.kind === "call-expression") {
369
409
  if (query.box.value.length > 1) {
370
- collector.set(name2, {
410
+ parserResult.set(name2, {
371
411
  name: name2,
372
412
  box: query.box,
373
413
  data: query.box.value.reduce(
@@ -376,7 +416,7 @@ function createParser(options) {
376
416
  )
377
417
  });
378
418
  } else {
379
- collector.set(name2, {
419
+ parserResult.set(name2, {
380
420
  name: name2,
381
421
  box: query.box.value[0] ?? fallback(query.box),
382
422
  data: combineResult(unbox(query.box.value[0]))
@@ -384,7 +424,7 @@ function createParser(options) {
384
424
  }
385
425
  } else if (query.kind === "tagged-template") {
386
426
  const obj = astish(query.box.value);
387
- collector.set(name2, {
427
+ parserResult.set(name2, {
388
428
  name: name2,
389
429
  box: query.box ?? fallback(query.box),
390
430
  data: [obj]
@@ -394,7 +434,7 @@ function createParser(options) {
394
434
  }).when(isValidPattern, (name2) => {
395
435
  result.queryList.forEach((query) => {
396
436
  if (query.kind === "call-expression") {
397
- collector.setPattern(name2, {
437
+ parserResult.setPattern(name2, {
398
438
  name: name2,
399
439
  box: query.box.value[0] ?? fallback(query.box),
400
440
  data: combineResult(unbox(query.box.value[0]))
@@ -404,7 +444,7 @@ function createParser(options) {
404
444
  }).when(isValidRecipe, (name2) => {
405
445
  result.queryList.forEach((query) => {
406
446
  if (query.kind === "call-expression") {
407
- collector.setRecipe(name2, {
447
+ parserResult.setRecipe(name2, {
408
448
  name: name2,
409
449
  box: query.box.value[0] ?? fallback(query.box),
410
450
  data: combineResult(unbox(query.box.value[0]))
@@ -418,27 +458,27 @@ function createParser(options) {
418
458
  const boxNode = box.isMap(map) ? map : fallback(query.box);
419
459
  const result2 = { name, box: boxNode, data: combineResult(unbox(boxNode)) };
420
460
  if (box.isMap(map) && isCva(map.value)) {
421
- collector.setCva(result2);
461
+ parserResult.setCva(result2);
422
462
  } else {
423
- collector.set("css", result2);
463
+ parserResult.set("css", result2);
424
464
  }
425
- const options2 = query.box.value[2];
426
- 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")) {
427
467
  const maybeIdentifier = map.getNode();
428
468
  if (Node.isIdentifier(maybeIdentifier)) {
429
469
  const name2 = maybeIdentifier.getText();
430
470
  const recipeName = imports.getName(name2);
431
- collector.setRecipe(recipeName, {
471
+ parserResult.setRecipe(recipeName, {
432
472
  type: "jsx-recipe",
433
473
  name: recipeName,
434
- box: options2,
435
- data: combineResult(unbox(options2.value.get("defaultProps")))
474
+ box: options,
475
+ data: combineResult(unbox(options.value.get("defaultProps")))
436
476
  });
437
477
  }
438
478
  }
439
479
  } else if (query.kind === "tagged-template") {
440
480
  const obj = astish(query.box.value);
441
- collector.set("css", {
481
+ parserResult.set("css", {
442
482
  name,
443
483
  box: query.box ?? fallback(query.box),
444
484
  data: [obj]
@@ -452,13 +492,13 @@ function createParser(options) {
452
492
  const boxNode = box.isMap(map) ? map : fallback(query.box);
453
493
  const result2 = { name: name2, box: boxNode, data: combineResult(unbox(boxNode)) };
454
494
  if (box.isMap(map) && isCva(map.value)) {
455
- collector.setCva(result2);
495
+ parserResult.setCva(result2);
456
496
  } else {
457
- collector.set("css", result2);
497
+ parserResult.set("css", result2);
458
498
  }
459
499
  } else if (query.kind === "tagged-template") {
460
500
  const obj = astish(query.box.value);
461
- collector.set("css", {
501
+ parserResult.set("css", {
462
502
  name: name2,
463
503
  box: query.box ?? fallback(query.box),
464
504
  data: [obj]
@@ -471,28 +511,53 @@ function createParser(options) {
471
511
  result.queryList.forEach((query) => {
472
512
  const data = combineResult(unbox(query.box));
473
513
  match(name).when(isFactory, (jsxName) => {
474
- collector.setJsx({ name: jsxName, box: query.box, type: "jsx-factory", data });
514
+ parserResult.setJsx({ name: jsxName, box: query.box, type: "jsx-factory", data });
475
515
  }).when(isJsxTagPattern, (jsxName) => {
476
- 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 });
477
517
  }).when(isJsxTagRecipe, (jsxName) => {
478
518
  const recipeList = getRecipesByJsxName(jsxName);
479
519
  recipeList.map((recipe) => {
480
- 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 });
481
521
  });
482
522
  }).otherwise(() => {
483
- collector.setJsx({ name, box: query.box, type: "jsx", data });
523
+ parserResult.setJsx({ name, box: query.box, type: "jsx", data });
484
524
  });
485
525
  });
486
526
  }
487
527
  });
488
- return collector;
528
+ return parserResult;
489
529
  };
490
530
  }
491
531
  var isUpperCase = (value) => value[0] === value[0]?.toUpperCase();
492
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
+
493
558
  // src/vue-to-tsx.ts
494
559
  import { parse } from "@vue/compiler-sfc";
495
- import MagicString from "magic-string";
560
+ import MagicString2 from "magic-string";
496
561
  var NodeTypes = {
497
562
  ROOT: 0,
498
563
  ELEMENT: 1,
@@ -525,7 +590,7 @@ var NodeTypes = {
525
590
  var vueToTsx = (code) => {
526
591
  try {
527
592
  const parsed = parse(code);
528
- const fileStr = new MagicString(`<template>${parsed.descriptor.template?.content}</template>` ?? "");
593
+ const fileStr = new MagicString2(`<template>${parsed.descriptor.template?.content}</template>` ?? "");
529
594
  const rewriteProp = (prop) => {
530
595
  if (prop.type === NodeTypes.DIRECTIVE && prop.exp?.type === NodeTypes.SIMPLE_EXPRESSION && prop.arg?.type === NodeTypes.SIMPLE_EXPRESSION) {
531
596
  fileStr.replace(prop.loc.source, `${prop.arg.content}={${prop.exp.content}}`);
@@ -542,7 +607,7 @@ var vueToTsx = (code) => {
542
607
  }
543
608
  }
544
609
  const scriptContent = (parsed.descriptor.scriptSetup ?? parsed.descriptor.script)?.content + "\n";
545
- const transformed = new MagicString(`${scriptContent}
610
+ const transformed = new MagicString2(`${scriptContent}
546
611
  const render = ${fileStr.toString()}`);
547
612
  return transformed.toString();
548
613
  } catch (err) {
@@ -550,31 +615,6 @@ const render = ${fileStr.toString()}`);
550
615
  }
551
616
  };
552
617
 
553
- // src/svelte-to-tsx.ts
554
- import MagicString2 from "magic-string";
555
- var regex_style_tags = /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi;
556
- var regex_script_tags = /<!--[^]*?-->|<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi;
557
- var svelteToTsx = (code) => {
558
- try {
559
- const scripts = [];
560
- const original = new MagicString2(code);
561
- let match2;
562
- while ((match2 = regex_script_tags.exec(code)) != null) {
563
- const [fullMatch, _attributesStr, scriptContent] = match2;
564
- if (scriptContent) {
565
- scripts.push(scriptContent);
566
- original.remove(match2.index, match2.index + fullMatch.length);
567
- }
568
- }
569
- const templateContent = original.toString().trimStart().replace(regex_style_tags, "").replace(regex_style_tags, "");
570
- const transformed = `${scripts.join("")}
571
- const render = <div>${templateContent}</div>`;
572
- return transformed.toString().trim();
573
- } catch (err) {
574
- return "";
575
- }
576
- };
577
-
578
618
  // src/project.ts
579
619
  var createTsProject = (options) => new TsProject({
580
620
  skipAddingFilesFromTsConfig: true,
@@ -588,86 +628,100 @@ var createTsProject = (options) => new TsProject({
588
628
  ...options.compilerOptions
589
629
  }
590
630
  });
591
- var createProject = ({
592
- getFiles,
593
- readFile,
594
- parserOptions,
595
- hooks,
596
- ...projectOptions
597
- }) => {
598
- const project = createTsProject(projectOptions);
599
- const parser = createParser(parserOptions);
600
- const getSourceFile = (filePath) => project.getSourceFile(filePath);
601
- const removeSourceFile = (filePath) => {
602
- const sourceFile = project.getSourceFile(filePath);
603
- if (sourceFile)
604
- 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);
605
646
  };
606
- const createSourceFile = (filePath) => project.createSourceFile(filePath, readFile(filePath), {
607
- overwrite: true,
608
- scriptKind: ScriptKind.TSX
609
- });
610
- const addSourceFile = (filePath, content) => project.createSourceFile(filePath, content, {
611
- overwrite: true,
612
- scriptKind: ScriptKind.TSX
613
- });
614
- 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;
615
698
  if (filePath.endsWith(".json")) {
616
- const content2 = readFile(filePath);
617
- hooks.callHook("parser:before", filePath, content2);
618
- const result2 = ParserResult.fromJSON(content2).setFilePath(filePath);
619
- hooks.callHook("parser:after", filePath, result2);
620
- return result2;
699
+ return this.parseJson(filePath);
621
700
  }
622
- const sourceFile = project.getSourceFile(filePath);
701
+ const sourceFile = this.project.getSourceFile(filePath);
623
702
  if (!sourceFile)
624
703
  return;
625
704
  const content = sourceFile.getText();
626
- const transformed = transformFile(filePath, content);
705
+ const transformed = this.transformFile(filePath, content);
627
706
  if (content !== transformed) {
628
707
  sourceFile.replaceWithText(transformed);
629
708
  }
630
709
  hooks.callHook("parser:before", filePath, content);
631
- const result = parser(sourceFile)?.setFilePath(filePath);
710
+ const result = this.parser(sourceFile, encoder)?.setFilePath(filePath);
632
711
  hooks.callHook("parser:after", filePath, result);
633
712
  return result;
634
713
  };
635
- const files = getFiles();
636
- for (const file of files) {
637
- createSourceFile(file);
638
- }
639
- const reloadSourceFile = (filePath) => getSourceFile(filePath)?.refreshFromFileSystemSync();
640
- const reloadSourceFiles = () => {
641
- const files2 = getFiles();
642
- for (const file of files2) {
643
- const source = getSourceFile(file);
644
- source?.refreshFromFileSystemSync() ?? project.addSourceFileAtPath(file);
714
+ transformFile = (filePath, content) => {
715
+ if (filePath.endsWith(".vue")) {
716
+ return vueToTsx(content);
645
717
  }
718
+ if (filePath.endsWith(".svelte")) {
719
+ return svelteToTsx(content);
720
+ }
721
+ return content;
646
722
  };
647
- return {
648
- getSourceFile,
649
- removeSourceFile,
650
- createSourceFile,
651
- addSourceFile,
652
- parseSourceFile,
653
- reloadSourceFile,
654
- reloadSourceFiles,
655
- files,
656
- getFiles,
657
- readFile
658
- };
659
- };
660
- var transformFile = (filePath, content) => {
661
- if (filePath.endsWith(".vue")) {
662
- return vueToTsx(content);
663
- }
664
- if (filePath.endsWith(".svelte")) {
665
- return svelteToTsx(content);
666
- }
667
- return content;
668
723
  };
669
724
  export {
670
725
  ParserResult,
671
- createParserResult,
672
- createProject
726
+ Project
673
727
  };