@pandacss/parser 0.0.0-dev-20221121153631

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Segun Adebayo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,41 @@
1
+ import { CompilerOptions, Project, SourceFile } from 'ts-morph';
2
+
3
+ declare type Result = {
4
+ name?: string;
5
+ data: Record<string, any>;
6
+ type?: string;
7
+ };
8
+ declare class Collector {
9
+ jsx: Set<Result>;
10
+ css: Set<Result>;
11
+ globalCss: Set<Result>;
12
+ cssMap: Set<Result>;
13
+ recipe: Map<string, Set<Result>>;
14
+ pattern: Map<string, Set<Result>>;
15
+ set(name: string, result: {
16
+ data: Record<string, any>;
17
+ }): void;
18
+ setPattern(name: string, result: {
19
+ data: Record<string, any>;
20
+ }): void;
21
+ setRecipe(name: string, result: {
22
+ data: Record<string, any>;
23
+ }): void;
24
+ get isEmpty(): boolean;
25
+ }
26
+
27
+ declare function createProject(compilerOptions?: Partial<CompilerOptions>): Project;
28
+ declare type ParserOptions = {
29
+ importMap: Record<'css' | 'recipe' | 'pattern' | 'jsx', string>;
30
+ jsx?: {
31
+ factory: string;
32
+ nodes: Array<{
33
+ name: string;
34
+ props?: string[];
35
+ }>;
36
+ isStyleProp: (prop: string) => boolean;
37
+ };
38
+ };
39
+ declare function createParser(options: ParserOptions): (sourceFile: SourceFile | undefined) => Collector | undefined;
40
+
41
+ export { Collector, createParser, createProject };
package/dist/index.js ADDED
@@ -0,0 +1,341 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ createParser: () => createParser,
24
+ createProject: () => createProject
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/parser.ts
29
+ var import_logger = require("@pandacss/logger");
30
+ var import_shared2 = require("@pandacss/shared");
31
+ var import_ts_morph4 = require("ts-morph");
32
+ var import_ts_pattern3 = require("ts-pattern");
33
+
34
+ // src/call-expression.ts
35
+ var import_ts_morph2 = require("ts-morph");
36
+
37
+ // src/literal.ts
38
+ var import_ts_morph = require("ts-morph");
39
+ var import_ts_pattern = require("ts-pattern");
40
+
41
+ // src/strip-quotes.ts
42
+ function stripQuotes(q) {
43
+ const quoteStart = `'|"|\u2018|\u201C|\u2039|\xAB`;
44
+ const quoteEnd = `'|"|\u2019|\u201D|\u203A|\xBB`;
45
+ if (typeof q !== "string")
46
+ throw new Error(`input was '${typeof q}' and not of type 'string'`);
47
+ if (!q.length)
48
+ throw new Error(`input was empty`);
49
+ let s = q;
50
+ let t = s.length;
51
+ if (s.charAt(0).match(new RegExp(quoteStart))) {
52
+ s = s.substring(1, t--);
53
+ }
54
+ if (s.charAt(--t).match(new RegExp(quoteEnd))) {
55
+ s = s.substring(0, t);
56
+ }
57
+ return s;
58
+ }
59
+
60
+ // src/literal.ts
61
+ function isPrimitiveLiteral(node) {
62
+ return import_ts_morph.Node.isStringLiteral(node) || import_ts_morph.Node.isNumericLiteral(node) || import_ts_morph.Node.isTrueLiteral(node) || import_ts_morph.Node.isFalseLiteral(node);
63
+ }
64
+ function extractValue(value) {
65
+ return (0, import_ts_pattern.match)(value).when(isPrimitiveLiteral, (value2) => {
66
+ return value2.getLiteralValue();
67
+ }).when(import_ts_morph.Node.isNullLiteral, () => {
68
+ return null;
69
+ }).when(import_ts_morph.Node.isObjectLiteralExpression, (value2) => {
70
+ return extractObjectLiteral(value2);
71
+ }).when(import_ts_morph.Node.isArrayLiteralExpression, (value2) => {
72
+ return extractArrayLiteral(value2);
73
+ }).otherwise(() => {
74
+ return void 0;
75
+ });
76
+ }
77
+ function extractObjectLiteral(node) {
78
+ const data = {};
79
+ const properties = node.getProperties();
80
+ for (const property of properties) {
81
+ if (import_ts_morph.Node.isPropertyAssignment(property)) {
82
+ const key = stripQuotes(property.getName());
83
+ const value = property.getInitializer();
84
+ const returnValue = extractValue(value);
85
+ if (returnValue !== void 0) {
86
+ data[key] = returnValue;
87
+ }
88
+ }
89
+ }
90
+ return data;
91
+ }
92
+ function extractArrayLiteral(node) {
93
+ const result = [];
94
+ node.forEachChild((child) => {
95
+ (0, import_ts_pattern.match)(child).when(isPrimitiveLiteral, (child2) => {
96
+ result.push(child2.getLiteralValue());
97
+ }).when(import_ts_morph.Node.isUndefinedKeyword, () => {
98
+ result.push(void 0);
99
+ }).when(import_ts_morph.Node.isNullLiteral, () => {
100
+ result.push(null);
101
+ }).when(import_ts_morph.Node.isObjectLiteralExpression, (child2) => {
102
+ result.push(extractObjectLiteral(child2));
103
+ }).when(import_ts_morph.Node.isArrayLiteralExpression, (child2) => {
104
+ result.push(extractArrayLiteral(child2));
105
+ }).otherwise(() => {
106
+ });
107
+ });
108
+ return result;
109
+ }
110
+
111
+ // src/call-expression.ts
112
+ function visitCallExpressions(file, options) {
113
+ const { match: match4, fn } = options;
114
+ const callExpressions = file.getDescendantsOfKind(import_ts_morph2.ts.SyntaxKind.CallExpression);
115
+ for (const node of callExpressions) {
116
+ const expr = node.getExpression();
117
+ const name = expr.getText();
118
+ if (!match4(name)) {
119
+ continue;
120
+ }
121
+ const args = node.getArguments();
122
+ if (args.length === 0) {
123
+ fn({ name, data: {} });
124
+ continue;
125
+ }
126
+ args.forEach((arg) => {
127
+ if (import_ts_morph2.Node.isObjectLiteralExpression(arg)) {
128
+ fn({ name, data: extractObjectLiteral(arg) });
129
+ }
130
+ });
131
+ }
132
+ }
133
+
134
+ // src/collector.ts
135
+ var Collector = class {
136
+ jsx = /* @__PURE__ */ new Set();
137
+ css = /* @__PURE__ */ new Set();
138
+ globalCss = /* @__PURE__ */ new Set();
139
+ cssMap = /* @__PURE__ */ new Set();
140
+ recipe = /* @__PURE__ */ new Map();
141
+ pattern = /* @__PURE__ */ new Map();
142
+ set(name, result) {
143
+ this[name].add({ type: "object", ...result });
144
+ }
145
+ setPattern(name, result) {
146
+ this.pattern.get(name) ?? this.pattern.set(name, /* @__PURE__ */ new Set());
147
+ this.pattern.get(name)?.add({ type: "pattern", name, ...result });
148
+ }
149
+ setRecipe(name, result) {
150
+ this.recipe.get(name) ?? this.recipe.set(name, /* @__PURE__ */ new Set());
151
+ this.recipe.get(name)?.add({ type: "recipe", ...result });
152
+ }
153
+ get isEmpty() {
154
+ return this.css.size === 0 && this.globalCss.size === 0 && this.cssMap.size === 0 && this.recipe.size === 0 && this.pattern.size === 0 && this.jsx.size === 0;
155
+ }
156
+ };
157
+
158
+ // src/import.ts
159
+ var import_shared = require("@pandacss/shared");
160
+ function getImportDeclarations(file, options) {
161
+ const { match: match4 } = options;
162
+ const result = [];
163
+ file.getImportDeclarations().forEach((node) => {
164
+ const source = node.getModuleSpecifierValue();
165
+ if (!source)
166
+ return;
167
+ const specifiers = node.getNamedImports();
168
+ specifiers.forEach((specifier) => {
169
+ const name = specifier.getNameNode().getText();
170
+ const alias = specifier.getAliasNode()?.getText() || name;
171
+ if (!match4({ id: name, mod: source }))
172
+ return;
173
+ result.push({ name, alias, mod: source });
174
+ });
175
+ });
176
+ return {
177
+ value: result,
178
+ toString() {
179
+ return result.map((item) => item.alias).join(", ");
180
+ },
181
+ find(id) {
182
+ return result.find((o) => o.alias === id);
183
+ },
184
+ createMatch(mod) {
185
+ const mods = result.filter((o) => o.mod.includes(mod));
186
+ return (0, import_shared.memo)((id) => !!mods.find((mod2) => mod2.alias === id));
187
+ },
188
+ match(id) {
189
+ return !!this.find(id);
190
+ },
191
+ getName(id) {
192
+ return this.find(id)?.name || id;
193
+ },
194
+ getAlias(id) {
195
+ return result.find((o) => o.name === id)?.alias || id;
196
+ }
197
+ };
198
+ }
199
+
200
+ // src/jsx-element.ts
201
+ var import_ts_morph3 = require("ts-morph");
202
+ var import_ts_pattern2 = require("ts-pattern");
203
+ function visitJsxElement(file, options) {
204
+ const { match: matchProp, fn } = options;
205
+ const elements = [
206
+ ...file.getDescendantsOfKind(import_ts_morph3.ts.SyntaxKind.JsxOpeningElement),
207
+ ...file.getDescendantsOfKind(import_ts_morph3.ts.SyntaxKind.JsxSelfClosingElement)
208
+ ];
209
+ for (const node of elements) {
210
+ const tag = node.getTagNameNode().getText();
211
+ if (!matchProp.tag(tag))
212
+ continue;
213
+ const props = node.getAttributes();
214
+ const data = {};
215
+ for (const prop of props) {
216
+ if (!import_ts_morph3.Node.isJsxAttribute(prop))
217
+ continue;
218
+ const name = prop.getName();
219
+ const value = prop.getInitializer();
220
+ if (!matchProp.prop({ tag, name }))
221
+ continue;
222
+ (0, import_ts_pattern2.match)(value).when(import_ts_morph3.Node.isStringLiteral, (value2) => {
223
+ data[name] = value2.getLiteralValue();
224
+ }).when(import_ts_morph3.Node.isJsxExpression, (value2) => {
225
+ const expr = value2.getExpression();
226
+ const returnValue = extractValue(expr);
227
+ if (returnValue !== void 0) {
228
+ data[name] = returnValue;
229
+ }
230
+ }).otherwise(() => {
231
+ if (!value) {
232
+ data[name] = true;
233
+ }
234
+ });
235
+ }
236
+ fn({ name: tag, data });
237
+ }
238
+ }
239
+
240
+ // src/parser.ts
241
+ function createProject(compilerOptions = {}) {
242
+ return new import_ts_morph4.Project({
243
+ skipAddingFilesFromTsConfig: true,
244
+ skipFileDependencyResolution: true,
245
+ skipLoadingLibFiles: true,
246
+ compilerOptions: {
247
+ allowJs: true,
248
+ strictNullChecks: false,
249
+ skipLibCheck: true,
250
+ ...compilerOptions
251
+ }
252
+ });
253
+ }
254
+ function createImportMatcher(mod, values) {
255
+ const regex = values ? new RegExp(`^(${values.join("|")})$`) : /.*/;
256
+ return {
257
+ mod,
258
+ regex,
259
+ match(value) {
260
+ return regex.test(value);
261
+ }
262
+ };
263
+ }
264
+ function createParser(options) {
265
+ return function parse(sourceFile) {
266
+ if (!sourceFile)
267
+ return;
268
+ const fileName = sourceFile.getFilePath();
269
+ const collector = new Collector();
270
+ const { jsx, importMap } = options;
271
+ const importRegex = [
272
+ createImportMatcher(importMap.css, ["css", "cssMap", "globalCss"]),
273
+ createImportMatcher(importMap.recipe),
274
+ createImportMatcher(importMap.pattern)
275
+ ];
276
+ if (jsx) {
277
+ importRegex.push(createImportMatcher(importMap.jsx, [jsx.factory, ...jsx.nodes.map((node) => node.name)]));
278
+ }
279
+ const imports = getImportDeclarations(sourceFile, {
280
+ match(value) {
281
+ return importRegex.some(({ regex, mod }) => regex.test(value.id) && value.mod.includes(mod));
282
+ }
283
+ });
284
+ if (imports.value.length) {
285
+ import_logger.logger.debug({
286
+ type: "ast:import",
287
+ msg: `Found import { ${imports} } in ${fileName}`
288
+ });
289
+ }
290
+ const isValidPattern = imports.createMatch(importMap.pattern);
291
+ const isValidRecipe = imports.createMatch(importMap.recipe);
292
+ visitCallExpressions(sourceFile, {
293
+ match: (0, import_shared2.memo)((name) => imports.match(name)),
294
+ fn({ name: _name, data }) {
295
+ const name = imports.getName(_name);
296
+ const [css] = importRegex;
297
+ const result = { name, data };
298
+ import_logger.logger.debug({ type: `ast:${name}`, fileName, result });
299
+ (0, import_ts_pattern3.match)(name).when(css.match, (name2) => {
300
+ collector.set(name2, result);
301
+ }).when(isValidPattern, (name2) => {
302
+ collector.setPattern(name2, result);
303
+ }).when(isValidRecipe, (name2) => {
304
+ collector.setRecipe(name2, result);
305
+ }).otherwise(() => {
306
+ });
307
+ }
308
+ });
309
+ const jsxFactoryAlias = jsx ? imports.getAlias(jsx.factory) : "panda";
310
+ const jsxPatternNodes = new RegExp(`(${jsx?.nodes.map((node) => node.name).join("|")})$`);
311
+ visitJsxElement(sourceFile, {
312
+ match: {
313
+ tag: (0, import_shared2.memo)((name) => {
314
+ return name.startsWith(jsxFactoryAlias) || isUpperCase(name);
315
+ }),
316
+ prop: ({ tag, name }) => {
317
+ const node = jsx?.nodes.find((n) => n.name === tag);
318
+ return !!jsx?.isStyleProp(name) || !!node?.props?.includes(name);
319
+ }
320
+ },
321
+ fn({ name, data }) {
322
+ let type;
323
+ if (jsx && name.startsWith(jsxFactoryAlias)) {
324
+ type = "jsx-factory";
325
+ } else if (jsxPatternNodes.test(name)) {
326
+ type = "pattern";
327
+ } else {
328
+ type = "jsx";
329
+ }
330
+ collector.jsx.add({ type, name, data });
331
+ }
332
+ });
333
+ return collector;
334
+ };
335
+ }
336
+ var isUpperCase = (value) => value[0] === value[0].toUpperCase();
337
+ // Annotate the CommonJS export names for ESM import in node:
338
+ 0 && (module.exports = {
339
+ createParser,
340
+ createProject
341
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,315 @@
1
+ // src/parser.ts
2
+ import { logger } from "@pandacss/logger";
3
+ import { memo as memo2 } from "@pandacss/shared";
4
+ import { Project } from "ts-morph";
5
+ import { match as match3 } from "ts-pattern";
6
+
7
+ // src/call-expression.ts
8
+ import { Node as Node2, ts } from "ts-morph";
9
+
10
+ // src/literal.ts
11
+ import {
12
+ Node
13
+ } from "ts-morph";
14
+ import { match } from "ts-pattern";
15
+
16
+ // src/strip-quotes.ts
17
+ function stripQuotes(q) {
18
+ const quoteStart = `'|"|\u2018|\u201C|\u2039|\xAB`;
19
+ const quoteEnd = `'|"|\u2019|\u201D|\u203A|\xBB`;
20
+ if (typeof q !== "string")
21
+ throw new Error(`input was '${typeof q}' and not of type 'string'`);
22
+ if (!q.length)
23
+ throw new Error(`input was empty`);
24
+ let s = q;
25
+ let t = s.length;
26
+ if (s.charAt(0).match(new RegExp(quoteStart))) {
27
+ s = s.substring(1, t--);
28
+ }
29
+ if (s.charAt(--t).match(new RegExp(quoteEnd))) {
30
+ s = s.substring(0, t);
31
+ }
32
+ return s;
33
+ }
34
+
35
+ // src/literal.ts
36
+ function isPrimitiveLiteral(node) {
37
+ return Node.isStringLiteral(node) || Node.isNumericLiteral(node) || Node.isTrueLiteral(node) || Node.isFalseLiteral(node);
38
+ }
39
+ function extractValue(value) {
40
+ return match(value).when(isPrimitiveLiteral, (value2) => {
41
+ return value2.getLiteralValue();
42
+ }).when(Node.isNullLiteral, () => {
43
+ return null;
44
+ }).when(Node.isObjectLiteralExpression, (value2) => {
45
+ return extractObjectLiteral(value2);
46
+ }).when(Node.isArrayLiteralExpression, (value2) => {
47
+ return extractArrayLiteral(value2);
48
+ }).otherwise(() => {
49
+ return void 0;
50
+ });
51
+ }
52
+ function extractObjectLiteral(node) {
53
+ const data = {};
54
+ const properties = node.getProperties();
55
+ for (const property of properties) {
56
+ if (Node.isPropertyAssignment(property)) {
57
+ const key = stripQuotes(property.getName());
58
+ const value = property.getInitializer();
59
+ const returnValue = extractValue(value);
60
+ if (returnValue !== void 0) {
61
+ data[key] = returnValue;
62
+ }
63
+ }
64
+ }
65
+ return data;
66
+ }
67
+ function extractArrayLiteral(node) {
68
+ const result = [];
69
+ node.forEachChild((child) => {
70
+ match(child).when(isPrimitiveLiteral, (child2) => {
71
+ result.push(child2.getLiteralValue());
72
+ }).when(Node.isUndefinedKeyword, () => {
73
+ result.push(void 0);
74
+ }).when(Node.isNullLiteral, () => {
75
+ result.push(null);
76
+ }).when(Node.isObjectLiteralExpression, (child2) => {
77
+ result.push(extractObjectLiteral(child2));
78
+ }).when(Node.isArrayLiteralExpression, (child2) => {
79
+ result.push(extractArrayLiteral(child2));
80
+ }).otherwise(() => {
81
+ });
82
+ });
83
+ return result;
84
+ }
85
+
86
+ // src/call-expression.ts
87
+ function visitCallExpressions(file, options) {
88
+ const { match: match4, fn } = options;
89
+ const callExpressions = file.getDescendantsOfKind(ts.SyntaxKind.CallExpression);
90
+ for (const node of callExpressions) {
91
+ const expr = node.getExpression();
92
+ const name = expr.getText();
93
+ if (!match4(name)) {
94
+ continue;
95
+ }
96
+ const args = node.getArguments();
97
+ if (args.length === 0) {
98
+ fn({ name, data: {} });
99
+ continue;
100
+ }
101
+ args.forEach((arg) => {
102
+ if (Node2.isObjectLiteralExpression(arg)) {
103
+ fn({ name, data: extractObjectLiteral(arg) });
104
+ }
105
+ });
106
+ }
107
+ }
108
+
109
+ // src/collector.ts
110
+ var Collector = class {
111
+ jsx = /* @__PURE__ */ new Set();
112
+ css = /* @__PURE__ */ new Set();
113
+ globalCss = /* @__PURE__ */ new Set();
114
+ cssMap = /* @__PURE__ */ new Set();
115
+ recipe = /* @__PURE__ */ new Map();
116
+ pattern = /* @__PURE__ */ new Map();
117
+ set(name, result) {
118
+ this[name].add({ type: "object", ...result });
119
+ }
120
+ setPattern(name, result) {
121
+ this.pattern.get(name) ?? this.pattern.set(name, /* @__PURE__ */ new Set());
122
+ this.pattern.get(name)?.add({ type: "pattern", name, ...result });
123
+ }
124
+ setRecipe(name, result) {
125
+ this.recipe.get(name) ?? this.recipe.set(name, /* @__PURE__ */ new Set());
126
+ this.recipe.get(name)?.add({ type: "recipe", ...result });
127
+ }
128
+ get isEmpty() {
129
+ return this.css.size === 0 && this.globalCss.size === 0 && this.cssMap.size === 0 && this.recipe.size === 0 && this.pattern.size === 0 && this.jsx.size === 0;
130
+ }
131
+ };
132
+
133
+ // src/import.ts
134
+ import { memo } from "@pandacss/shared";
135
+ function getImportDeclarations(file, options) {
136
+ const { match: match4 } = options;
137
+ const result = [];
138
+ file.getImportDeclarations().forEach((node) => {
139
+ const source = node.getModuleSpecifierValue();
140
+ if (!source)
141
+ return;
142
+ const specifiers = node.getNamedImports();
143
+ specifiers.forEach((specifier) => {
144
+ const name = specifier.getNameNode().getText();
145
+ const alias = specifier.getAliasNode()?.getText() || name;
146
+ if (!match4({ id: name, mod: source }))
147
+ return;
148
+ result.push({ name, alias, mod: source });
149
+ });
150
+ });
151
+ return {
152
+ value: result,
153
+ toString() {
154
+ return result.map((item) => item.alias).join(", ");
155
+ },
156
+ find(id) {
157
+ return result.find((o) => o.alias === id);
158
+ },
159
+ createMatch(mod) {
160
+ const mods = result.filter((o) => o.mod.includes(mod));
161
+ return memo((id) => !!mods.find((mod2) => mod2.alias === id));
162
+ },
163
+ match(id) {
164
+ return !!this.find(id);
165
+ },
166
+ getName(id) {
167
+ return this.find(id)?.name || id;
168
+ },
169
+ getAlias(id) {
170
+ return result.find((o) => o.name === id)?.alias || id;
171
+ }
172
+ };
173
+ }
174
+
175
+ // src/jsx-element.ts
176
+ import { Node as Node3, ts as ts2 } from "ts-morph";
177
+ import { match as match2 } from "ts-pattern";
178
+ function visitJsxElement(file, options) {
179
+ const { match: matchProp, fn } = options;
180
+ const elements = [
181
+ ...file.getDescendantsOfKind(ts2.SyntaxKind.JsxOpeningElement),
182
+ ...file.getDescendantsOfKind(ts2.SyntaxKind.JsxSelfClosingElement)
183
+ ];
184
+ for (const node of elements) {
185
+ const tag = node.getTagNameNode().getText();
186
+ if (!matchProp.tag(tag))
187
+ continue;
188
+ const props = node.getAttributes();
189
+ const data = {};
190
+ for (const prop of props) {
191
+ if (!Node3.isJsxAttribute(prop))
192
+ continue;
193
+ const name = prop.getName();
194
+ const value = prop.getInitializer();
195
+ if (!matchProp.prop({ tag, name }))
196
+ continue;
197
+ match2(value).when(Node3.isStringLiteral, (value2) => {
198
+ data[name] = value2.getLiteralValue();
199
+ }).when(Node3.isJsxExpression, (value2) => {
200
+ const expr = value2.getExpression();
201
+ const returnValue = extractValue(expr);
202
+ if (returnValue !== void 0) {
203
+ data[name] = returnValue;
204
+ }
205
+ }).otherwise(() => {
206
+ if (!value) {
207
+ data[name] = true;
208
+ }
209
+ });
210
+ }
211
+ fn({ name: tag, data });
212
+ }
213
+ }
214
+
215
+ // src/parser.ts
216
+ function createProject(compilerOptions = {}) {
217
+ return new Project({
218
+ skipAddingFilesFromTsConfig: true,
219
+ skipFileDependencyResolution: true,
220
+ skipLoadingLibFiles: true,
221
+ compilerOptions: {
222
+ allowJs: true,
223
+ strictNullChecks: false,
224
+ skipLibCheck: true,
225
+ ...compilerOptions
226
+ }
227
+ });
228
+ }
229
+ function createImportMatcher(mod, values) {
230
+ const regex = values ? new RegExp(`^(${values.join("|")})$`) : /.*/;
231
+ return {
232
+ mod,
233
+ regex,
234
+ match(value) {
235
+ return regex.test(value);
236
+ }
237
+ };
238
+ }
239
+ function createParser(options) {
240
+ return function parse(sourceFile) {
241
+ if (!sourceFile)
242
+ return;
243
+ const fileName = sourceFile.getFilePath();
244
+ const collector = new Collector();
245
+ const { jsx, importMap } = options;
246
+ const importRegex = [
247
+ createImportMatcher(importMap.css, ["css", "cssMap", "globalCss"]),
248
+ createImportMatcher(importMap.recipe),
249
+ createImportMatcher(importMap.pattern)
250
+ ];
251
+ if (jsx) {
252
+ importRegex.push(createImportMatcher(importMap.jsx, [jsx.factory, ...jsx.nodes.map((node) => node.name)]));
253
+ }
254
+ const imports = getImportDeclarations(sourceFile, {
255
+ match(value) {
256
+ return importRegex.some(({ regex, mod }) => regex.test(value.id) && value.mod.includes(mod));
257
+ }
258
+ });
259
+ if (imports.value.length) {
260
+ logger.debug({
261
+ type: "ast:import",
262
+ msg: `Found import { ${imports} } in ${fileName}`
263
+ });
264
+ }
265
+ const isValidPattern = imports.createMatch(importMap.pattern);
266
+ const isValidRecipe = imports.createMatch(importMap.recipe);
267
+ visitCallExpressions(sourceFile, {
268
+ match: memo2((name) => imports.match(name)),
269
+ fn({ name: _name, data }) {
270
+ const name = imports.getName(_name);
271
+ const [css] = importRegex;
272
+ const result = { name, data };
273
+ logger.debug({ type: `ast:${name}`, fileName, result });
274
+ match3(name).when(css.match, (name2) => {
275
+ collector.set(name2, result);
276
+ }).when(isValidPattern, (name2) => {
277
+ collector.setPattern(name2, result);
278
+ }).when(isValidRecipe, (name2) => {
279
+ collector.setRecipe(name2, result);
280
+ }).otherwise(() => {
281
+ });
282
+ }
283
+ });
284
+ const jsxFactoryAlias = jsx ? imports.getAlias(jsx.factory) : "panda";
285
+ const jsxPatternNodes = new RegExp(`(${jsx?.nodes.map((node) => node.name).join("|")})$`);
286
+ visitJsxElement(sourceFile, {
287
+ match: {
288
+ tag: memo2((name) => {
289
+ return name.startsWith(jsxFactoryAlias) || isUpperCase(name);
290
+ }),
291
+ prop: ({ tag, name }) => {
292
+ const node = jsx?.nodes.find((n) => n.name === tag);
293
+ return !!jsx?.isStyleProp(name) || !!node?.props?.includes(name);
294
+ }
295
+ },
296
+ fn({ name, data }) {
297
+ let type;
298
+ if (jsx && name.startsWith(jsxFactoryAlias)) {
299
+ type = "jsx-factory";
300
+ } else if (jsxPatternNodes.test(name)) {
301
+ type = "pattern";
302
+ } else {
303
+ type = "jsx";
304
+ }
305
+ collector.jsx.add({ type, name, data });
306
+ }
307
+ });
308
+ return collector;
309
+ };
310
+ }
311
+ var isUpperCase = (value) => value[0] === value[0].toUpperCase();
312
+ export {
313
+ createParser,
314
+ createProject
315
+ };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@pandacss/parser",
3
+ "version": "0.0.0-dev-20221121153631",
4
+ "description": "The static parser for panda css",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "author": "Segun Adebayo <joseshegs@gmail.com>",
9
+ "sideEffects": false,
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "dependencies": {
14
+ "ts-pattern": "4.0.6",
15
+ "ts-morph": "16.0.0",
16
+ "@pandacss/logger": "0.0.0-dev-20221121153631",
17
+ "@pandacss/shared": "0.0.0-dev-20221121153631",
18
+ "@pandacss/is-valid-prop": "0.0.0-dev-20221121153631",
19
+ "@pandacss/types": "0.0.0-dev-20221121153631"
20
+ },
21
+ "devDependencies": {
22
+ "@pandacss/fixture": "0.0.0-dev-20221121153631"
23
+ },
24
+ "files": [
25
+ "dist"
26
+ ],
27
+ "scripts": {
28
+ "build": "tsup src/index.ts --format=esm,cjs --dts",
29
+ "build-fast": "tsup src/index.ts --format=esm,cjs --no-dts",
30
+ "dev": "pnpm build-fast --watch"
31
+ }
32
+ }