@pandacss/parser 0.0.2 → 0.3.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.d.ts CHANGED
@@ -1,7 +1,37 @@
1
- import * as _pandacss_types from '@pandacss/types';
2
- import { AnyRecipeConfig } from '@pandacss/types';
3
1
  import * as ts_morph from 'ts-morph';
4
2
  import { ProjectOptions as ProjectOptions$1 } from 'ts-morph';
3
+ import { ResultItem, RecipeConfig, PandaHookable } from '@pandacss/types';
4
+
5
+ declare class ParserResult {
6
+ jsx: Set<ResultItem>;
7
+ css: Set<ResultItem>;
8
+ cva: Set<ResultItem>;
9
+ recipe: Map<string, Set<ResultItem>>;
10
+ pattern: Map<string, Set<ResultItem>>;
11
+ filePath: string | undefined;
12
+ set(name: 'cva' | 'css', result: ResultItem): void;
13
+ setCva(result: ResultItem): void;
14
+ setJsx(result: ResultItem): void;
15
+ setPattern(name: string, result: ResultItem): void;
16
+ setRecipe(name: string, result: ResultItem): void;
17
+ isEmpty(): boolean;
18
+ setFilePath(filePath: string): this;
19
+ toArray(): ResultItem[];
20
+ toJSON(): {
21
+ css: ResultItem[];
22
+ cva: ResultItem[];
23
+ recipe: {
24
+ [k: string]: ResultItem[];
25
+ };
26
+ pattern: {
27
+ [k: string]: ResultItem[];
28
+ };
29
+ jsx: ResultItem[];
30
+ };
31
+ merge(result: ParserResult): this;
32
+ static fromJSON(json: string): ParserResult;
33
+ }
34
+ declare const createParserResult: () => ParserResult;
5
35
 
6
36
  type ParserPatternNode = {
7
37
  name: string;
@@ -14,7 +44,7 @@ type ParserRecipeNode = {
14
44
  type: 'recipe';
15
45
  props: string[];
16
46
  baseName: string;
17
- jsx: AnyRecipeConfig['jsx'];
47
+ jsx: RecipeConfig['jsx'];
18
48
  };
19
49
  type ParserNodeOptions = ParserPatternNode | ParserRecipeNode;
20
50
  type ParserOptions = {
@@ -25,22 +55,24 @@ type ParserOptions = {
25
55
  isStyleProp: (prop: string) => boolean;
26
56
  };
27
57
  getRecipeName: (tagName: string) => string;
28
- getRecipeByName: (name: string) => AnyRecipeConfig | undefined;
58
+ getRecipeByName: (name: string) => RecipeConfig | undefined;
29
59
  };
30
60
 
31
61
  type ProjectOptions = Partial<ProjectOptions$1> & {
32
62
  readFile: (filePath: string) => string;
33
63
  getFiles: () => string[];
64
+ hooks: PandaHookable;
34
65
  parserOptions: ParserOptions;
35
66
  };
36
- declare const createProject: ({ getFiles, readFile, parserOptions, ...projectOptions }: ProjectOptions) => {
67
+ declare const createProject: ({ getFiles, readFile, parserOptions, hooks, ...projectOptions }: ProjectOptions) => {
37
68
  getSourceFile: (filePath: string) => ts_morph.SourceFile | undefined;
38
69
  removeSourceFile: (filePath: string) => void;
39
70
  createSourceFile: (filePath: string) => ts_morph.SourceFile;
40
- parseSourceFile: (filePath: string) => _pandacss_types.ParserResult | undefined;
71
+ addSourceFile: (filePath: string, content: string) => ts_morph.SourceFile;
72
+ parseSourceFile: (filePath: string) => ParserResult | undefined;
41
73
  reloadSourceFile: (filePath: string) => ts_morph.FileSystemRefreshResult | undefined;
42
74
  reloadSourceFiles: () => void;
43
75
  };
44
76
  type Project = ReturnType<typeof createProject>;
45
77
 
46
- export { Project, ProjectOptions, createProject };
78
+ export { ParserResult, Project, ProjectOptions, createParserResult, createProject };
package/dist/index.js CHANGED
@@ -20,6 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
+ ParserResult: () => ParserResult,
24
+ createParserResult: () => createParserResult,
23
25
  createProject: () => createProject
24
26
  });
25
27
  module.exports = __toCommonJS(src_exports);
@@ -37,11 +39,18 @@ var import_ts_pattern = require("ts-pattern");
37
39
 
38
40
  // src/import.ts
39
41
  var import_shared = require("@pandacss/shared");
42
+ var getModuleSpecifierValue = (node) => {
43
+ try {
44
+ return node.getModuleSpecifierValue();
45
+ } catch {
46
+ return;
47
+ }
48
+ };
40
49
  function getImportDeclarations(file, options) {
41
50
  const { match: match2 } = options;
42
51
  const result = [];
43
52
  file.getImportDeclarations().forEach((node) => {
44
- const source = node.getModuleSpecifierValue();
53
+ const source = getModuleSpecifierValue(node);
45
54
  if (!source)
46
55
  return;
47
56
  const specifiers = node.getNamedImports();
@@ -78,33 +87,38 @@ function getImportDeclarations(file, options) {
78
87
  }
79
88
 
80
89
  // src/parser-result.ts
81
- var createParserResult = () => ({
82
- jsx: /* @__PURE__ */ new Set(),
83
- css: /* @__PURE__ */ new Set(),
84
- cva: /* @__PURE__ */ new Set(),
85
- recipe: /* @__PURE__ */ new Map(),
86
- pattern: /* @__PURE__ */ new Map(),
90
+ var ParserResult = class {
91
+ jsx = /* @__PURE__ */ new Set();
92
+ css = /* @__PURE__ */ new Set();
93
+ cva = /* @__PURE__ */ new Set();
94
+ recipe = /* @__PURE__ */ new Map();
95
+ pattern = /* @__PURE__ */ new Map();
96
+ filePath;
87
97
  set(name, result) {
88
98
  this[name].add({ type: "object", ...result });
89
- },
99
+ }
90
100
  setCva(result) {
91
101
  this.cva.add({ type: "cva", ...result });
92
- },
102
+ }
93
103
  setJsx(result) {
94
104
  this.jsx.add({ type: "jsx", ...result });
95
- },
105
+ }
96
106
  setPattern(name, result) {
97
107
  this.pattern.get(name) ?? this.pattern.set(name, /* @__PURE__ */ new Set());
98
108
  this.pattern.get(name)?.add({ type: "pattern", name, ...result });
99
- },
109
+ }
100
110
  setRecipe(name, result) {
101
111
  this.recipe.get(name) ?? this.recipe.set(name, /* @__PURE__ */ new Set());
102
112
  this.recipe.get(name)?.add({ type: "recipe", ...result });
103
- },
113
+ }
104
114
  isEmpty() {
105
115
  return this.css.size === 0 && this.cva.size === 0 && this.recipe.size === 0 && this.pattern.size === 0 && this.jsx.size === 0;
106
- },
107
- getAll() {
116
+ }
117
+ setFilePath(filePath) {
118
+ this.filePath = filePath;
119
+ return this;
120
+ }
121
+ toArray() {
108
122
  const result = [];
109
123
  this.css.forEach((item) => result.push(item));
110
124
  this.cva.forEach((item) => result.push(item));
@@ -113,7 +127,41 @@ var createParserResult = () => ({
113
127
  this.jsx.forEach((item) => result.push(item));
114
128
  return result;
115
129
  }
116
- });
130
+ toJSON() {
131
+ return {
132
+ css: Array.from(this.css),
133
+ cva: Array.from(this.cva),
134
+ recipe: Object.fromEntries(Array.from(this.recipe.entries()).map(([key, value]) => [key, Array.from(value)])),
135
+ pattern: Object.fromEntries(Array.from(this.pattern.entries()).map(([key, value]) => [key, Array.from(value)])),
136
+ jsx: Array.from(this.jsx)
137
+ };
138
+ }
139
+ merge(result) {
140
+ result.css.forEach((item) => this.css.add(item));
141
+ result.cva.forEach((item) => this.cva.add(item));
142
+ result.recipe.forEach((items, name) => {
143
+ this.recipe.get(name) ?? this.recipe.set(name, /* @__PURE__ */ new Set());
144
+ items.forEach((item) => this.recipe.get(name)?.add(item));
145
+ });
146
+ result.pattern.forEach((items, name) => {
147
+ this.pattern.get(name) ?? this.pattern.set(name, /* @__PURE__ */ new Set());
148
+ items.forEach((item) => this.pattern.get(name)?.add(item));
149
+ });
150
+ result.jsx.forEach((item) => this.jsx.add(item));
151
+ return this;
152
+ }
153
+ static fromJSON(json) {
154
+ const data = JSON.parse(json);
155
+ const result = new ParserResult();
156
+ result.css = new Set(data.css);
157
+ result.cva = new Set(data.cva);
158
+ result.recipe = new Map(Object.entries(data.recipe));
159
+ result.pattern = new Map(Object.entries(data.pattern));
160
+ result.jsx = new Set(data.jsx);
161
+ return result;
162
+ }
163
+ };
164
+ var createParserResult = () => new ParserResult();
117
165
 
118
166
  // src/parser.ts
119
167
  var isNodeRecipe = (node) => node.type === "recipe";
@@ -137,19 +185,19 @@ var fallback = (box) => ({
137
185
  });
138
186
  var defaultEnv = { preset: "NONE" };
139
187
  function createParser(options) {
188
+ const { jsx, importMap, getRecipeByName } = options;
189
+ const importRegex = [
190
+ createImportMatcher(importMap.css, ["css", "cva"]),
191
+ createImportMatcher(importMap.recipe),
192
+ createImportMatcher(importMap.pattern)
193
+ ];
194
+ if (jsx) {
195
+ importRegex.push(createImportMatcher(importMap.jsx, [jsx.factory, ...jsx.nodes.map((node) => node.name)]));
196
+ }
140
197
  return function parse(sourceFile) {
141
198
  if (!sourceFile)
142
199
  return;
143
200
  const filePath = sourceFile.getFilePath();
144
- const { jsx, importMap, getRecipeByName } = options;
145
- const importRegex = [
146
- createImportMatcher(importMap.css, ["css", "cva"]),
147
- createImportMatcher(importMap.recipe),
148
- createImportMatcher(importMap.pattern)
149
- ];
150
- if (jsx) {
151
- importRegex.push(createImportMatcher(importMap.jsx, [jsx.factory, ...jsx.nodes.map((node) => node.name)]));
152
- }
153
201
  const imports = getImportDeclarations(sourceFile, {
154
202
  match(value) {
155
203
  return importRegex.some(({ regex, mod }) => regex.test(value.name) && value.mod.includes(mod));
@@ -336,7 +384,7 @@ var createTsProject = (options) => new import_ts_morph2.Project({
336
384
  ...options.compilerOptions
337
385
  }
338
386
  });
339
- var createProject = ({ getFiles, readFile, parserOptions, ...projectOptions }) => (0, import_lil_fp.pipe)(
387
+ var createProject = ({ getFiles, readFile, parserOptions, hooks, ...projectOptions }) => (0, import_lil_fp.pipe)(
340
388
  {
341
389
  project: createTsProject(projectOptions),
342
390
  parser: createParser(parserOptions)
@@ -352,8 +400,26 @@ var createProject = ({ getFiles, readFile, parserOptions, ...projectOptions }) =
352
400
  overwrite: true,
353
401
  scriptKind: import_ts_morph2.ScriptKind.TSX
354
402
  }),
403
+ addSourceFile: (filePath, content) => project.createSourceFile(filePath, content, {
404
+ overwrite: true,
405
+ scriptKind: import_ts_morph2.ScriptKind.TSX
406
+ }),
355
407
  parseSourceFile: (filePath) => {
356
- return parser(project.getSourceFile(filePath));
408
+ if (filePath.endsWith(".json")) {
409
+ const content2 = readFile(filePath);
410
+ hooks.callHook("parser:before", filePath, content2);
411
+ const result2 = ParserResult.fromJSON(content2).setFilePath(filePath);
412
+ hooks.callHook("parser:after", filePath, result2);
413
+ return result2;
414
+ }
415
+ const sourceFile = project.getSourceFile(filePath);
416
+ if (!sourceFile)
417
+ return;
418
+ const content = sourceFile.getText();
419
+ hooks.callHook("parser:before", filePath, content);
420
+ const result = parser(sourceFile)?.setFilePath(filePath);
421
+ hooks.callHook("parser:after", filePath, result);
422
+ return result;
357
423
  }
358
424
  })),
359
425
  (0, import_lil_fp.tap)(({ createSourceFile }) => {
@@ -376,5 +442,7 @@ var createProject = ({ getFiles, readFile, parserOptions, ...projectOptions }) =
376
442
  );
377
443
  // Annotate the CommonJS export names for ESM import in node:
378
444
  0 && (module.exports = {
445
+ ParserResult,
446
+ createParserResult,
379
447
  createProject
380
448
  });
package/dist/index.mjs CHANGED
@@ -11,11 +11,18 @@ import { match } from "ts-pattern";
11
11
 
12
12
  // src/import.ts
13
13
  import { memo } from "@pandacss/shared";
14
+ var getModuleSpecifierValue = (node) => {
15
+ try {
16
+ return node.getModuleSpecifierValue();
17
+ } catch {
18
+ return;
19
+ }
20
+ };
14
21
  function getImportDeclarations(file, options) {
15
22
  const { match: match2 } = options;
16
23
  const result = [];
17
24
  file.getImportDeclarations().forEach((node) => {
18
- const source = node.getModuleSpecifierValue();
25
+ const source = getModuleSpecifierValue(node);
19
26
  if (!source)
20
27
  return;
21
28
  const specifiers = node.getNamedImports();
@@ -52,33 +59,38 @@ function getImportDeclarations(file, options) {
52
59
  }
53
60
 
54
61
  // src/parser-result.ts
55
- var createParserResult = () => ({
56
- jsx: /* @__PURE__ */ new Set(),
57
- css: /* @__PURE__ */ new Set(),
58
- cva: /* @__PURE__ */ new Set(),
59
- recipe: /* @__PURE__ */ new Map(),
60
- pattern: /* @__PURE__ */ new Map(),
62
+ var ParserResult = class {
63
+ jsx = /* @__PURE__ */ new Set();
64
+ css = /* @__PURE__ */ new Set();
65
+ cva = /* @__PURE__ */ new Set();
66
+ recipe = /* @__PURE__ */ new Map();
67
+ pattern = /* @__PURE__ */ new Map();
68
+ filePath;
61
69
  set(name, result) {
62
70
  this[name].add({ type: "object", ...result });
63
- },
71
+ }
64
72
  setCva(result) {
65
73
  this.cva.add({ type: "cva", ...result });
66
- },
74
+ }
67
75
  setJsx(result) {
68
76
  this.jsx.add({ type: "jsx", ...result });
69
- },
77
+ }
70
78
  setPattern(name, result) {
71
79
  this.pattern.get(name) ?? this.pattern.set(name, /* @__PURE__ */ new Set());
72
80
  this.pattern.get(name)?.add({ type: "pattern", name, ...result });
73
- },
81
+ }
74
82
  setRecipe(name, result) {
75
83
  this.recipe.get(name) ?? this.recipe.set(name, /* @__PURE__ */ new Set());
76
84
  this.recipe.get(name)?.add({ type: "recipe", ...result });
77
- },
85
+ }
78
86
  isEmpty() {
79
87
  return this.css.size === 0 && this.cva.size === 0 && this.recipe.size === 0 && this.pattern.size === 0 && this.jsx.size === 0;
80
- },
81
- getAll() {
88
+ }
89
+ setFilePath(filePath) {
90
+ this.filePath = filePath;
91
+ return this;
92
+ }
93
+ toArray() {
82
94
  const result = [];
83
95
  this.css.forEach((item) => result.push(item));
84
96
  this.cva.forEach((item) => result.push(item));
@@ -87,7 +99,41 @@ var createParserResult = () => ({
87
99
  this.jsx.forEach((item) => result.push(item));
88
100
  return result;
89
101
  }
90
- });
102
+ toJSON() {
103
+ return {
104
+ css: Array.from(this.css),
105
+ cva: Array.from(this.cva),
106
+ recipe: Object.fromEntries(Array.from(this.recipe.entries()).map(([key, value]) => [key, Array.from(value)])),
107
+ pattern: Object.fromEntries(Array.from(this.pattern.entries()).map(([key, value]) => [key, Array.from(value)])),
108
+ jsx: Array.from(this.jsx)
109
+ };
110
+ }
111
+ merge(result) {
112
+ result.css.forEach((item) => this.css.add(item));
113
+ result.cva.forEach((item) => this.cva.add(item));
114
+ result.recipe.forEach((items, name) => {
115
+ this.recipe.get(name) ?? this.recipe.set(name, /* @__PURE__ */ new Set());
116
+ items.forEach((item) => this.recipe.get(name)?.add(item));
117
+ });
118
+ result.pattern.forEach((items, name) => {
119
+ this.pattern.get(name) ?? this.pattern.set(name, /* @__PURE__ */ new Set());
120
+ items.forEach((item) => this.pattern.get(name)?.add(item));
121
+ });
122
+ result.jsx.forEach((item) => this.jsx.add(item));
123
+ return this;
124
+ }
125
+ static fromJSON(json) {
126
+ const data = JSON.parse(json);
127
+ const result = new ParserResult();
128
+ result.css = new Set(data.css);
129
+ result.cva = new Set(data.cva);
130
+ result.recipe = new Map(Object.entries(data.recipe));
131
+ result.pattern = new Map(Object.entries(data.pattern));
132
+ result.jsx = new Set(data.jsx);
133
+ return result;
134
+ }
135
+ };
136
+ var createParserResult = () => new ParserResult();
91
137
 
92
138
  // src/parser.ts
93
139
  var isNodeRecipe = (node) => node.type === "recipe";
@@ -111,19 +157,19 @@ var fallback = (box) => ({
111
157
  });
112
158
  var defaultEnv = { preset: "NONE" };
113
159
  function createParser(options) {
160
+ const { jsx, importMap, getRecipeByName } = options;
161
+ const importRegex = [
162
+ createImportMatcher(importMap.css, ["css", "cva"]),
163
+ createImportMatcher(importMap.recipe),
164
+ createImportMatcher(importMap.pattern)
165
+ ];
166
+ if (jsx) {
167
+ importRegex.push(createImportMatcher(importMap.jsx, [jsx.factory, ...jsx.nodes.map((node) => node.name)]));
168
+ }
114
169
  return function parse(sourceFile) {
115
170
  if (!sourceFile)
116
171
  return;
117
172
  const filePath = sourceFile.getFilePath();
118
- const { jsx, importMap, getRecipeByName } = options;
119
- const importRegex = [
120
- createImportMatcher(importMap.css, ["css", "cva"]),
121
- createImportMatcher(importMap.recipe),
122
- createImportMatcher(importMap.pattern)
123
- ];
124
- if (jsx) {
125
- importRegex.push(createImportMatcher(importMap.jsx, [jsx.factory, ...jsx.nodes.map((node) => node.name)]));
126
- }
127
173
  const imports = getImportDeclarations(sourceFile, {
128
174
  match(value) {
129
175
  return importRegex.some(({ regex, mod }) => regex.test(value.name) && value.mod.includes(mod));
@@ -310,7 +356,7 @@ var createTsProject = (options) => new TsProject({
310
356
  ...options.compilerOptions
311
357
  }
312
358
  });
313
- var createProject = ({ getFiles, readFile, parserOptions, ...projectOptions }) => pipe(
359
+ var createProject = ({ getFiles, readFile, parserOptions, hooks, ...projectOptions }) => pipe(
314
360
  {
315
361
  project: createTsProject(projectOptions),
316
362
  parser: createParser(parserOptions)
@@ -326,8 +372,26 @@ var createProject = ({ getFiles, readFile, parserOptions, ...projectOptions }) =
326
372
  overwrite: true,
327
373
  scriptKind: ScriptKind.TSX
328
374
  }),
375
+ addSourceFile: (filePath, content) => project.createSourceFile(filePath, content, {
376
+ overwrite: true,
377
+ scriptKind: ScriptKind.TSX
378
+ }),
329
379
  parseSourceFile: (filePath) => {
330
- return parser(project.getSourceFile(filePath));
380
+ if (filePath.endsWith(".json")) {
381
+ const content2 = readFile(filePath);
382
+ hooks.callHook("parser:before", filePath, content2);
383
+ const result2 = ParserResult.fromJSON(content2).setFilePath(filePath);
384
+ hooks.callHook("parser:after", filePath, result2);
385
+ return result2;
386
+ }
387
+ const sourceFile = project.getSourceFile(filePath);
388
+ if (!sourceFile)
389
+ return;
390
+ const content = sourceFile.getText();
391
+ hooks.callHook("parser:before", filePath, content);
392
+ const result = parser(sourceFile)?.setFilePath(filePath);
393
+ hooks.callHook("parser:after", filePath, result);
394
+ return result;
331
395
  }
332
396
  })),
333
397
  tap(({ createSourceFile }) => {
@@ -349,5 +413,7 @@ var createProject = ({ getFiles, readFile, parserOptions, ...projectOptions }) =
349
413
  Obj.omit(["project", "parser"])
350
414
  );
351
415
  export {
416
+ ParserResult,
417
+ createParserResult,
352
418
  createProject
353
419
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pandacss/parser",
3
- "version": "0.0.2",
3
+ "version": "0.3.0",
4
4
  "description": "The static parser for panda css",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -14,15 +14,16 @@
14
14
  "lil-fp": "1.4.5",
15
15
  "ts-morph": "18.0.0",
16
16
  "ts-pattern": "4.3.0",
17
- "@pandacss/extractor": "0.0.2",
18
- "@pandacss/is-valid-prop": "0.0.2",
19
- "@pandacss/logger": "0.0.2",
20
- "@pandacss/types": "0.0.2",
21
- "@pandacss/shared": "0.0.2"
17
+ "@pandacss/extractor": "0.3.0",
18
+ "@pandacss/is-valid-prop": "0.3.0",
19
+ "@pandacss/logger": "0.3.0",
20
+ "@pandacss/shared": "0.3.0",
21
+ "@pandacss/types": "0.3.0"
22
22
  },
23
23
  "devDependencies": {
24
- "@pandacss/generator": "0.0.2",
25
- "@pandacss/fixture": "0.0.2"
24
+ "hookable": "5.5.3",
25
+ "@pandacss/fixture": "0.3.0",
26
+ "@pandacss/generator": "0.3.0"
26
27
  },
27
28
  "files": [
28
29
  "dist"