@bamboocss/parser 1.11.1 → 1.11.2
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.cjs +977 -0
- package/dist/index.d.cts +137 -0
- package/dist/index.d.mts +137 -0
- package/dist/index.mjs +950 -1000
- package/package.json +17 -17
- package/dist/index.js +0 -1049
package/dist/index.js
DELETED
|
@@ -1,1049 +0,0 @@
|
|
|
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 index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
ParserResult: () => ParserResult,
|
|
24
|
-
Project: () => Project
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(index_exports);
|
|
27
|
-
|
|
28
|
-
// src/project.ts
|
|
29
|
-
var import_ts_morph2 = require("ts-morph");
|
|
30
|
-
|
|
31
|
-
// src/classify.ts
|
|
32
|
-
var import_extractor = require("@bamboocss/extractor");
|
|
33
|
-
var import_shared = require("@bamboocss/shared");
|
|
34
|
-
function addTo(map, key, value) {
|
|
35
|
-
const set = map.get(key) ?? /* @__PURE__ */ new Set();
|
|
36
|
-
set.add(value);
|
|
37
|
-
map.set(key, set);
|
|
38
|
-
}
|
|
39
|
-
function classifyProject(ctx, resultMap) {
|
|
40
|
-
const byId = /* @__PURE__ */ new Map();
|
|
41
|
-
const byComponentIndex = /* @__PURE__ */ new Map();
|
|
42
|
-
const byFilepath = /* @__PURE__ */ new Map();
|
|
43
|
-
const byComponentInFilepath = /* @__PURE__ */ new Map();
|
|
44
|
-
const globalMaps = createReportMaps();
|
|
45
|
-
const byFilePathMaps = /* @__PURE__ */ new Map();
|
|
46
|
-
const conditions = new Map(Object.entries(ctx.conditions.values));
|
|
47
|
-
const { groupByProp } = getPropertyGroupMap(ctx);
|
|
48
|
-
let id = 0;
|
|
49
|
-
let componentIndex = 0;
|
|
50
|
-
const isKnownUtility = (reportItem, componentReportItem) => {
|
|
51
|
-
const { propName, value, tokenType } = reportItem;
|
|
52
|
-
const utility = ctx.utility.config[propName];
|
|
53
|
-
if (utility) {
|
|
54
|
-
if (ctx.tokens.getByName(`${tokenType}.${value}`)) return true;
|
|
55
|
-
if (ctx.tokens.getReferences(String(value)).length > 0) return true;
|
|
56
|
-
if (ctx.utility.resolveColorMix(String(value)).color) return true;
|
|
57
|
-
return false;
|
|
58
|
-
}
|
|
59
|
-
if (componentReportItem.reportItemType === "pattern") {
|
|
60
|
-
const pattern = ctx.patterns.getConfig(componentReportItem.componentName.toLowerCase());
|
|
61
|
-
const patternProp = pattern?.properties?.[propName];
|
|
62
|
-
if (!patternProp) return false;
|
|
63
|
-
if (patternProp.type === "boolean" || patternProp.type === "number") {
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
if (patternProp.type === "property" && patternProp.value) {
|
|
67
|
-
return Boolean(ctx.utility.config[patternProp.value]);
|
|
68
|
-
}
|
|
69
|
-
if (patternProp.type === "enum" && patternProp.value) {
|
|
70
|
-
return Boolean(patternProp.value.includes(String(value)));
|
|
71
|
-
}
|
|
72
|
-
if (patternProp.type === "token") {
|
|
73
|
-
return Boolean(ctx.tokens.getByName(`${patternProp.value}.${value}`));
|
|
74
|
-
}
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
return false;
|
|
78
|
-
};
|
|
79
|
-
const processPattern = (opts) => {
|
|
80
|
-
const { boxNode, data, item, filepath, localMaps } = opts;
|
|
81
|
-
const name = item.componentName;
|
|
82
|
-
const pattern = ctx.patterns.details.find((p) => p.match.test(name) || p.baseName === name);
|
|
83
|
-
if (!pattern) return;
|
|
84
|
-
const cssObj = pattern.config.transform?.(data || {}, import_shared.patternFns) ?? {};
|
|
85
|
-
const newItem = {
|
|
86
|
-
name: "css",
|
|
87
|
-
type: "css",
|
|
88
|
-
box: import_extractor.box.objectToMap((0, import_shared.compact)(cssObj), boxNode.getNode(), boxNode.getStack()),
|
|
89
|
-
data: [cssObj]
|
|
90
|
-
};
|
|
91
|
-
Object.assign(newItem, { debug: true });
|
|
92
|
-
return processResultItem({ item: newItem, kind: "function", localMaps, filepath });
|
|
93
|
-
};
|
|
94
|
-
const processResultItem = (opts) => {
|
|
95
|
-
const { item, kind, filepath, localMaps } = opts;
|
|
96
|
-
if (!item.box || import_extractor.box.isUnresolvable(item.box)) {
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
if (!item.data) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
const componentReportItem = {
|
|
103
|
-
componentIndex: String(componentIndex++),
|
|
104
|
-
componentName: item.name,
|
|
105
|
-
reportItemType: item.type,
|
|
106
|
-
kind,
|
|
107
|
-
filepath,
|
|
108
|
-
value: item.data,
|
|
109
|
-
range: item.box.getRange(),
|
|
110
|
-
contains: [],
|
|
111
|
-
debug: Reflect.has(item, "debug")
|
|
112
|
-
};
|
|
113
|
-
if (item.type === "pattern" || item.type === "jsx-pattern") {
|
|
114
|
-
return processPattern({ boxNode: item.box, data: item.data[0], item: componentReportItem, filepath, localMaps });
|
|
115
|
-
}
|
|
116
|
-
if (import_extractor.box.isArray(item.box)) {
|
|
117
|
-
addTo(byComponentInFilepath, filepath, componentReportItem.componentIndex);
|
|
118
|
-
return componentReportItem;
|
|
119
|
-
}
|
|
120
|
-
if (import_extractor.box.isMap(item.box)) {
|
|
121
|
-
addTo(byComponentInFilepath, filepath, componentReportItem.componentIndex);
|
|
122
|
-
processMap({ map: item.box, current: [], componentReportItem, filepath, localMaps });
|
|
123
|
-
return componentReportItem;
|
|
124
|
-
}
|
|
125
|
-
if (item.type === "recipe") {
|
|
126
|
-
addTo(byComponentInFilepath, filepath, componentReportItem.componentIndex);
|
|
127
|
-
return componentReportItem;
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
const processResultItemFn = (opts) => {
|
|
131
|
-
const { item, filepath, localMaps, type } = opts;
|
|
132
|
-
const componentReportItem = processResultItem({ item, kind: type, filepath, localMaps });
|
|
133
|
-
if (!componentReportItem) return;
|
|
134
|
-
addTo(globalMaps.byComponentOfKind, type, componentReportItem.componentIndex);
|
|
135
|
-
addTo(localMaps.byComponentOfKind, type, componentReportItem.componentIndex);
|
|
136
|
-
byComponentIndex.set(componentReportItem.componentIndex, componentReportItem);
|
|
137
|
-
};
|
|
138
|
-
const processMap = (opts) => {
|
|
139
|
-
const { map, current, componentReportItem, filepath, localMaps, skipRange } = opts;
|
|
140
|
-
const { reportItemType: type, kind, componentName: name } = componentReportItem;
|
|
141
|
-
map.value.forEach((attrNode, attrName) => {
|
|
142
|
-
if (import_extractor.box.isLiteral(attrNode) || import_extractor.box.isEmptyInitializer(attrNode)) {
|
|
143
|
-
const value = import_extractor.box.isLiteral(attrNode) ? attrNode.value : true;
|
|
144
|
-
const propReportItem = {
|
|
145
|
-
index: String(id++),
|
|
146
|
-
componentIndex: String(componentReportItem.componentIndex),
|
|
147
|
-
componentName: name,
|
|
148
|
-
tokenType: void 0,
|
|
149
|
-
propName: attrName,
|
|
150
|
-
reportItemKind: "utility",
|
|
151
|
-
reportItemType: type,
|
|
152
|
-
kind,
|
|
153
|
-
filepath,
|
|
154
|
-
path: current.concat(attrName),
|
|
155
|
-
value,
|
|
156
|
-
isKnownValue: false,
|
|
157
|
-
range: skipRange ? null : map.getRange()
|
|
158
|
-
};
|
|
159
|
-
componentReportItem.contains.push(propReportItem.index);
|
|
160
|
-
if (conditions.has(attrName)) {
|
|
161
|
-
addTo(globalMaps.byConditionName, attrName, propReportItem.index);
|
|
162
|
-
addTo(localMaps.byConditionName, attrName, propReportItem.index);
|
|
163
|
-
propReportItem.propName = current[0] ?? attrName;
|
|
164
|
-
propReportItem.isKnownValue = isKnownUtility(propReportItem, componentReportItem);
|
|
165
|
-
propReportItem.conditionName = attrName;
|
|
166
|
-
} else {
|
|
167
|
-
if (current.length && conditions.has(current[0])) {
|
|
168
|
-
propReportItem.conditionName = current[0];
|
|
169
|
-
addTo(globalMaps.byConditionName, current[0], propReportItem.index);
|
|
170
|
-
addTo(localMaps.byConditionName, current[0], propReportItem.index);
|
|
171
|
-
}
|
|
172
|
-
const propName = ctx.utility.resolveShorthand(attrName);
|
|
173
|
-
const tokenType = ctx.utility.getTokenType(propName);
|
|
174
|
-
if (tokenType) {
|
|
175
|
-
propReportItem.reportItemKind = "token";
|
|
176
|
-
propReportItem.tokenType = tokenType;
|
|
177
|
-
}
|
|
178
|
-
propReportItem.propName = propName;
|
|
179
|
-
propReportItem.isKnownValue = isKnownUtility(propReportItem, componentReportItem);
|
|
180
|
-
addTo(globalMaps.byPropertyName, propName, propReportItem.index);
|
|
181
|
-
addTo(localMaps.byPropertyName, propName, propReportItem.index);
|
|
182
|
-
if (tokenType) {
|
|
183
|
-
addTo(globalMaps.byTokenType, tokenType, propReportItem.index);
|
|
184
|
-
addTo(localMaps.byTokenType, tokenType, propReportItem.index);
|
|
185
|
-
}
|
|
186
|
-
if (propName.toLowerCase().includes("color") || groupByProp.get(propName) === "Color" || tokenType === "colors") {
|
|
187
|
-
addTo(globalMaps.colorsUsed, value, propReportItem.index);
|
|
188
|
-
addTo(localMaps.colorsUsed, value, propReportItem.index);
|
|
189
|
-
}
|
|
190
|
-
if (ctx.utility.shorthands.has(attrName)) {
|
|
191
|
-
addTo(globalMaps.byShorthand, attrName, propReportItem.index);
|
|
192
|
-
addTo(localMaps.byShorthand, attrName, propReportItem.index);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
if (current.length) {
|
|
196
|
-
addTo(globalMaps.byPropertyPath, propReportItem.path.join("."), propReportItem.index);
|
|
197
|
-
addTo(localMaps.byPropertyPath, propReportItem.path.join("."), propReportItem.index);
|
|
198
|
-
}
|
|
199
|
-
addTo(globalMaps.byTokenName, String(value), propReportItem.index);
|
|
200
|
-
addTo(localMaps.byTokenName, String(value), propReportItem.index);
|
|
201
|
-
addTo(globalMaps.byType, type, propReportItem.index);
|
|
202
|
-
addTo(localMaps.byType, type, propReportItem.index);
|
|
203
|
-
addTo(globalMaps.byComponentName, name, propReportItem.index);
|
|
204
|
-
addTo(localMaps.byComponentName, name, propReportItem.index);
|
|
205
|
-
addTo(globalMaps.fromKind, kind, propReportItem.index);
|
|
206
|
-
addTo(localMaps.fromKind, kind, propReportItem.index);
|
|
207
|
-
addTo(byFilepath, filepath, propReportItem.index);
|
|
208
|
-
byId.set(propReportItem.index, propReportItem);
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
if (import_extractor.box.isMap(attrNode) && attrNode.value.size) {
|
|
212
|
-
return processMap({
|
|
213
|
-
map: attrNode,
|
|
214
|
-
current: current.concat(attrName),
|
|
215
|
-
componentReportItem,
|
|
216
|
-
filepath,
|
|
217
|
-
localMaps
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
};
|
|
222
|
-
resultMap.forEach((parserResult, filepath) => {
|
|
223
|
-
if (parserResult.isEmpty()) return;
|
|
224
|
-
const localMaps = createReportMaps();
|
|
225
|
-
const componentFn = (item) => {
|
|
226
|
-
processResultItemFn({ item, filepath, localMaps, type: "component" });
|
|
227
|
-
};
|
|
228
|
-
const functionFn = (item) => {
|
|
229
|
-
processResultItemFn({ item, filepath, localMaps, type: "function" });
|
|
230
|
-
};
|
|
231
|
-
parserResult.jsx.forEach(componentFn);
|
|
232
|
-
parserResult.css.forEach(functionFn);
|
|
233
|
-
parserResult.cva.forEach(functionFn);
|
|
234
|
-
parserResult.pattern.forEach((itemList) => {
|
|
235
|
-
itemList.forEach(functionFn);
|
|
236
|
-
});
|
|
237
|
-
parserResult.recipe.forEach((itemList) => {
|
|
238
|
-
itemList.forEach(functionFn);
|
|
239
|
-
});
|
|
240
|
-
byFilePathMaps.set(filepath, localMaps);
|
|
241
|
-
});
|
|
242
|
-
const pickCount = 10;
|
|
243
|
-
const filesWithMostComponent = Object.fromEntries(
|
|
244
|
-
Array.from(byComponentInFilepath.entries()).map(([filepath, list]) => [filepath, list.size]).sort((a, b) => b[1] - a[1]).slice(0, pickCount)
|
|
245
|
-
);
|
|
246
|
-
Object.entries(ctx.recipes.config).forEach(([key, recipe]) => {
|
|
247
|
-
const localMaps = createReportMaps();
|
|
248
|
-
const functionFn = (styleObject) => {
|
|
249
|
-
if (!styleObject) return;
|
|
250
|
-
const componentReportItem = {
|
|
251
|
-
componentIndex: "0",
|
|
252
|
-
componentName: `recipes.${key}.base`,
|
|
253
|
-
reportItemType: "css",
|
|
254
|
-
kind: "function",
|
|
255
|
-
filepath: `theme/recipes/${key}`,
|
|
256
|
-
value: styleObject,
|
|
257
|
-
range: null,
|
|
258
|
-
contains: []
|
|
259
|
-
};
|
|
260
|
-
processMap({
|
|
261
|
-
// @ts-expect-error
|
|
262
|
-
map: import_extractor.box.objectToMap(styleObject, null, []),
|
|
263
|
-
current: [],
|
|
264
|
-
filepath: `@config/theme/recipes/${key}`,
|
|
265
|
-
skipRange: true,
|
|
266
|
-
localMaps,
|
|
267
|
-
componentReportItem
|
|
268
|
-
});
|
|
269
|
-
};
|
|
270
|
-
const isSlotRecipe = (_v) => ctx.recipes.isSlotRecipe(key);
|
|
271
|
-
if (isSlotRecipe(recipe)) {
|
|
272
|
-
Object.values(recipe.base ?? {}).forEach(functionFn);
|
|
273
|
-
Object.values(recipe.variants ?? {}).forEach((variants) => {
|
|
274
|
-
Object.values(variants).forEach((v) => {
|
|
275
|
-
Object.values(v).forEach(functionFn);
|
|
276
|
-
});
|
|
277
|
-
});
|
|
278
|
-
recipe.compoundVariants?.forEach((v) => {
|
|
279
|
-
Object.values(v.css).forEach(functionFn);
|
|
280
|
-
});
|
|
281
|
-
} else {
|
|
282
|
-
functionFn(recipe.base);
|
|
283
|
-
Object.values(recipe.variants ?? {}).forEach((variants) => {
|
|
284
|
-
Object.values(variants).forEach(functionFn);
|
|
285
|
-
});
|
|
286
|
-
recipe.compoundVariants?.forEach((v) => functionFn(v.css));
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
Object.values(ctx.config.globalCss ?? {}).forEach((styleObject) => {
|
|
290
|
-
if (!styleObject) return;
|
|
291
|
-
processMap({
|
|
292
|
-
// @ts-expect-error
|
|
293
|
-
map: import_extractor.box.objectToMap(styleObject, null, []),
|
|
294
|
-
current: [],
|
|
295
|
-
filepath: "@config/globalCss",
|
|
296
|
-
skipRange: true,
|
|
297
|
-
localMaps: createReportMaps(),
|
|
298
|
-
componentReportItem: {
|
|
299
|
-
componentIndex: "0",
|
|
300
|
-
componentName: "global",
|
|
301
|
-
reportItemType: "css",
|
|
302
|
-
kind: "function",
|
|
303
|
-
filepath: "global",
|
|
304
|
-
value: styleObject,
|
|
305
|
-
range: null,
|
|
306
|
-
contains: []
|
|
307
|
-
}
|
|
308
|
-
});
|
|
309
|
-
});
|
|
310
|
-
return {
|
|
311
|
-
propById: byId,
|
|
312
|
-
componentById: byComponentIndex,
|
|
313
|
-
details: {
|
|
314
|
-
counts: {
|
|
315
|
-
filesWithTokens: byFilepath.size,
|
|
316
|
-
propNameUsed: globalMaps.byPropertyName.size,
|
|
317
|
-
tokenUsed: globalMaps.byTokenName.size,
|
|
318
|
-
shorthandUsed: globalMaps.byShorthand.size,
|
|
319
|
-
propertyPathUsed: globalMaps.byPropertyPath.size,
|
|
320
|
-
typeUsed: globalMaps.byType.size,
|
|
321
|
-
componentNameUsed: globalMaps.byComponentName.size,
|
|
322
|
-
kindUsed: globalMaps.fromKind.size,
|
|
323
|
-
componentOfKindUsed: globalMaps.byComponentOfKind.size,
|
|
324
|
-
colorsUsed: globalMaps.colorsUsed.size
|
|
325
|
-
},
|
|
326
|
-
stats: {
|
|
327
|
-
filesWithMostComponent,
|
|
328
|
-
mostUseds: getXMostUseds(globalMaps, 10)
|
|
329
|
-
}
|
|
330
|
-
},
|
|
331
|
-
derived: {
|
|
332
|
-
byFilepath,
|
|
333
|
-
byComponentInFilepath,
|
|
334
|
-
globalMaps,
|
|
335
|
-
byFilePathMaps
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
var getXMostUseds = (globalMaps, pickCount) => {
|
|
340
|
-
return {
|
|
341
|
-
propNames: getMostUsedInMap(globalMaps.byPropertyName, pickCount),
|
|
342
|
-
tokens: getMostUsedInMap(globalMaps.byTokenName, pickCount),
|
|
343
|
-
shorthands: getMostUsedInMap(globalMaps.byShorthand, pickCount),
|
|
344
|
-
conditions: getMostUsedInMap(globalMaps.byConditionName, pickCount),
|
|
345
|
-
propertyPaths: getMostUsedInMap(globalMaps.byPropertyPath, pickCount),
|
|
346
|
-
categories: getMostUsedInMap(globalMaps.byTokenType, pickCount),
|
|
347
|
-
types: getMostUsedInMap(globalMaps.byType, pickCount),
|
|
348
|
-
componentNames: getMostUsedInMap(globalMaps.byComponentName, pickCount),
|
|
349
|
-
fromKinds: getMostUsedInMap(globalMaps.fromKind, pickCount),
|
|
350
|
-
componentOfKinds: getMostUsedInMap(globalMaps.byComponentOfKind, pickCount),
|
|
351
|
-
colors: getMostUsedInMap(globalMaps.colorsUsed, pickCount)
|
|
352
|
-
};
|
|
353
|
-
};
|
|
354
|
-
var getMostUsedInMap = (map, pickCount) => {
|
|
355
|
-
return Array.from(map.entries()).map(([key, list]) => [key, list.size]).sort((a, b) => b[1] - a[1]).slice(0, pickCount).map(([key, count]) => ({ key, count }));
|
|
356
|
-
};
|
|
357
|
-
var defaultGroupNames = [
|
|
358
|
-
"System",
|
|
359
|
-
"Container",
|
|
360
|
-
"Display",
|
|
361
|
-
"Visibility",
|
|
362
|
-
"Position",
|
|
363
|
-
"Transform",
|
|
364
|
-
"Flex Layout",
|
|
365
|
-
"Grid Layout",
|
|
366
|
-
"Layout",
|
|
367
|
-
"Border",
|
|
368
|
-
"Border Radius",
|
|
369
|
-
"Width",
|
|
370
|
-
"Height",
|
|
371
|
-
"Margin",
|
|
372
|
-
"Padding",
|
|
373
|
-
"Color",
|
|
374
|
-
"Typography",
|
|
375
|
-
"Background",
|
|
376
|
-
"Shadow",
|
|
377
|
-
"Table",
|
|
378
|
-
"List",
|
|
379
|
-
"Scroll",
|
|
380
|
-
"Interactivity",
|
|
381
|
-
"Transition",
|
|
382
|
-
"Effect",
|
|
383
|
-
"Other",
|
|
384
|
-
"Focus Ring"
|
|
385
|
-
];
|
|
386
|
-
function getPropertyGroupMap(ctx) {
|
|
387
|
-
const groups = new Map(defaultGroupNames.map((name) => [name, /* @__PURE__ */ new Set()]));
|
|
388
|
-
const groupByProp = /* @__PURE__ */ new Map();
|
|
389
|
-
const systemGroup = groups.get("System");
|
|
390
|
-
systemGroup.add("base");
|
|
391
|
-
systemGroup.add("colorPalette");
|
|
392
|
-
const otherStyleProps = groups.get("Other");
|
|
393
|
-
Object.entries(ctx.utility.config).map(([key, value]) => {
|
|
394
|
-
const group = value?.group;
|
|
395
|
-
if (!group) {
|
|
396
|
-
otherStyleProps.add(key);
|
|
397
|
-
return;
|
|
398
|
-
}
|
|
399
|
-
if (!groups.has(group)) {
|
|
400
|
-
groups.set(group, /* @__PURE__ */ new Set());
|
|
401
|
-
}
|
|
402
|
-
const set = groups.get(group);
|
|
403
|
-
if (value.shorthand) {
|
|
404
|
-
if (Array.isArray(value.shorthand)) {
|
|
405
|
-
value.shorthand.forEach((shorthand) => {
|
|
406
|
-
set.add(shorthand);
|
|
407
|
-
groupByProp.set(shorthand, group);
|
|
408
|
-
});
|
|
409
|
-
} else {
|
|
410
|
-
set.add(value.shorthand);
|
|
411
|
-
groupByProp.set(value.shorthand, group);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
set.add(key);
|
|
415
|
-
groupByProp.set(key, group);
|
|
416
|
-
});
|
|
417
|
-
return { groups, groupByProp };
|
|
418
|
-
}
|
|
419
|
-
function createReportMaps() {
|
|
420
|
-
return {
|
|
421
|
-
byComponentOfKind: /* @__PURE__ */ new Map(),
|
|
422
|
-
byPropertyName: /* @__PURE__ */ new Map(),
|
|
423
|
-
byTokenType: /* @__PURE__ */ new Map(),
|
|
424
|
-
byConditionName: /* @__PURE__ */ new Map(),
|
|
425
|
-
byShorthand: /* @__PURE__ */ new Map(),
|
|
426
|
-
byTokenName: /* @__PURE__ */ new Map(),
|
|
427
|
-
byPropertyPath: /* @__PURE__ */ new Map(),
|
|
428
|
-
fromKind: /* @__PURE__ */ new Map(),
|
|
429
|
-
byType: /* @__PURE__ */ new Map(),
|
|
430
|
-
byComponentName: /* @__PURE__ */ new Map(),
|
|
431
|
-
colorsUsed: /* @__PURE__ */ new Map()
|
|
432
|
-
};
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
// src/parser.ts
|
|
436
|
-
var import_extractor2 = require("@bamboocss/extractor");
|
|
437
|
-
var import_logger = require("@bamboocss/logger");
|
|
438
|
-
var import_shared3 = require("@bamboocss/shared");
|
|
439
|
-
var import_ts_morph = require("ts-morph");
|
|
440
|
-
var import_ts_pattern = require("ts-pattern");
|
|
441
|
-
|
|
442
|
-
// src/get-import-declarations.ts
|
|
443
|
-
var import_ts_path = require("@bamboocss/config/ts-path");
|
|
444
|
-
|
|
445
|
-
// src/get-module-specifier-value.ts
|
|
446
|
-
var getModuleSpecifierValue = (node) => {
|
|
447
|
-
try {
|
|
448
|
-
return node.getModuleSpecifierValue();
|
|
449
|
-
} catch {
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
// src/get-import-declarations.ts
|
|
455
|
-
function getImportDeclarations(context, sourceFile) {
|
|
456
|
-
const { imports, tsOptions } = context;
|
|
457
|
-
const importDeclarations = [];
|
|
458
|
-
sourceFile.getImportDeclarations().forEach((node) => {
|
|
459
|
-
const mod = getModuleSpecifierValue(node);
|
|
460
|
-
if (!mod) return;
|
|
461
|
-
node.getNamedImports().forEach((specifier) => {
|
|
462
|
-
const name = specifier.getNameNode().getText();
|
|
463
|
-
const alias = specifier.getAliasNode()?.getText() || name;
|
|
464
|
-
const result = { name, alias, mod, kind: "named" };
|
|
465
|
-
const found = imports.match(result, (mod2) => {
|
|
466
|
-
if (!tsOptions?.pathMappings) return;
|
|
467
|
-
return (0, import_ts_path.resolveTsPathPattern)(tsOptions.pathMappings, mod2);
|
|
468
|
-
});
|
|
469
|
-
if (!found) return;
|
|
470
|
-
importDeclarations.push(result);
|
|
471
|
-
});
|
|
472
|
-
const namespace = node.getNamespaceImport();
|
|
473
|
-
if (namespace) {
|
|
474
|
-
const name = namespace.getText();
|
|
475
|
-
const result = { name, alias: name, mod, kind: "namespace" };
|
|
476
|
-
const found = imports.match(result, (mod2) => {
|
|
477
|
-
if (!tsOptions?.pathMappings) return;
|
|
478
|
-
return (0, import_ts_path.resolveTsPathPattern)(tsOptions.pathMappings, mod2);
|
|
479
|
-
});
|
|
480
|
-
if (!found) return;
|
|
481
|
-
importDeclarations.push(result);
|
|
482
|
-
}
|
|
483
|
-
});
|
|
484
|
-
return importDeclarations;
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
// src/parser-result.ts
|
|
488
|
-
var import_shared2 = require("@bamboocss/shared");
|
|
489
|
-
function cartesian(arrays) {
|
|
490
|
-
if (arrays.length === 0) return [[]];
|
|
491
|
-
const [first, ...rest] = arrays;
|
|
492
|
-
const restProduct = cartesian(rest);
|
|
493
|
-
return first.flatMap((item) => restProduct.map((combo) => [item, ...combo]));
|
|
494
|
-
}
|
|
495
|
-
var ParserResult = class {
|
|
496
|
-
constructor(context, encoder) {
|
|
497
|
-
this.context = context;
|
|
498
|
-
this.encoder = encoder ?? context.encoder;
|
|
499
|
-
}
|
|
500
|
-
/** Ordered list of all ResultItem */
|
|
501
|
-
all = [];
|
|
502
|
-
jsx = /* @__PURE__ */ new Set();
|
|
503
|
-
css = /* @__PURE__ */ new Set();
|
|
504
|
-
cva = /* @__PURE__ */ new Set();
|
|
505
|
-
sva = /* @__PURE__ */ new Set();
|
|
506
|
-
token = /* @__PURE__ */ new Set();
|
|
507
|
-
recipe = /* @__PURE__ */ new Map();
|
|
508
|
-
pattern = /* @__PURE__ */ new Map();
|
|
509
|
-
filePath;
|
|
510
|
-
encoder;
|
|
511
|
-
append(result) {
|
|
512
|
-
this.all.push(result);
|
|
513
|
-
return result;
|
|
514
|
-
}
|
|
515
|
-
set(name, result) {
|
|
516
|
-
switch (name) {
|
|
517
|
-
case "css":
|
|
518
|
-
this.setCss(result);
|
|
519
|
-
break;
|
|
520
|
-
case "cva":
|
|
521
|
-
this.setCva(result);
|
|
522
|
-
break;
|
|
523
|
-
case "sva":
|
|
524
|
-
this.setSva(result);
|
|
525
|
-
break;
|
|
526
|
-
case "token":
|
|
527
|
-
this.setToken(result);
|
|
528
|
-
break;
|
|
529
|
-
default:
|
|
530
|
-
throw new import_shared2.BambooError(
|
|
531
|
-
"UNKNOWN_RESULT_TYPE",
|
|
532
|
-
`Unknown parser result type: "${name}". Expected one of: css, cva, sva, token`
|
|
533
|
-
);
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
setCss(result) {
|
|
537
|
-
this.css.add(this.append(Object.assign({ type: "css" }, result)));
|
|
538
|
-
const encoder = this.encoder;
|
|
539
|
-
const grouped = this.context.config.cssMode === "grouped";
|
|
540
|
-
if (!grouped || result.data.length <= 1) {
|
|
541
|
-
result.data.forEach((obj) => grouped ? encoder.processGrouped(obj) : encoder.processAtomic(obj));
|
|
542
|
-
return;
|
|
543
|
-
}
|
|
544
|
-
const keyCounts = /* @__PURE__ */ new Map();
|
|
545
|
-
for (const obj of result.data) {
|
|
546
|
-
for (const key of Object.keys(obj)) {
|
|
547
|
-
keyCounts.set(key, (keyCounts.get(key) || 0) + 1);
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
const hasOverlap = Array.from(keyCounts.values()).some((c) => c > 1);
|
|
551
|
-
if (!hasOverlap) {
|
|
552
|
-
encoder.processGrouped(Object.assign({}, ...result.data));
|
|
553
|
-
return;
|
|
554
|
-
}
|
|
555
|
-
const overlappingKeys = /* @__PURE__ */ new Set();
|
|
556
|
-
keyCounts.forEach((count, key) => {
|
|
557
|
-
if (count > 1) overlappingKeys.add(key);
|
|
558
|
-
});
|
|
559
|
-
const base = {};
|
|
560
|
-
const branchEntries = [];
|
|
561
|
-
for (const obj of result.data) {
|
|
562
|
-
if (Object.keys(obj).some((k) => overlappingKeys.has(k))) {
|
|
563
|
-
branchEntries.push(obj);
|
|
564
|
-
} else {
|
|
565
|
-
Object.assign(base, obj);
|
|
566
|
-
}
|
|
567
|
-
}
|
|
568
|
-
const branchGroups = /* @__PURE__ */ new Map();
|
|
569
|
-
for (const entry of branchEntries) {
|
|
570
|
-
const keySet = Object.keys(entry).sort().join("\0");
|
|
571
|
-
const group = branchGroups.get(keySet) || [];
|
|
572
|
-
group.push(entry);
|
|
573
|
-
branchGroups.set(keySet, group);
|
|
574
|
-
}
|
|
575
|
-
const groupArrays = Array.from(branchGroups.values());
|
|
576
|
-
const totalCombinations = groupArrays.reduce((acc, g) => acc * g.length, 1);
|
|
577
|
-
if (totalCombinations > 32) {
|
|
578
|
-
result.data.forEach((obj) => encoder.processGrouped(obj));
|
|
579
|
-
return;
|
|
580
|
-
}
|
|
581
|
-
for (const combo of cartesian(groupArrays)) {
|
|
582
|
-
encoder.processGrouped(Object.assign({}, base, ...combo));
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
setCva(result) {
|
|
586
|
-
this.cva.add(this.append(Object.assign({ type: "cva" }, result)));
|
|
587
|
-
const encoder = this.encoder;
|
|
588
|
-
result.data.forEach((data) => encoder.processAtomicRecipe(data));
|
|
589
|
-
}
|
|
590
|
-
setSva(result) {
|
|
591
|
-
this.sva.add(this.append(Object.assign({ type: "sva" }, result)));
|
|
592
|
-
const encoder = this.encoder;
|
|
593
|
-
result.data.forEach((data) => encoder.processAtomicSlotRecipe(data));
|
|
594
|
-
}
|
|
595
|
-
setToken(result) {
|
|
596
|
-
this.token.add(this.append(Object.assign({ type: "token" }, result)));
|
|
597
|
-
}
|
|
598
|
-
setJsx(result) {
|
|
599
|
-
this.jsx.add(this.append(Object.assign({ type: "jsx" }, result)));
|
|
600
|
-
const encoder = this.encoder;
|
|
601
|
-
const grouped = this.context.config.cssMode === "grouped";
|
|
602
|
-
result.data.forEach((obj) => encoder.processStyleProps(obj, grouped));
|
|
603
|
-
}
|
|
604
|
-
setPattern(name, result) {
|
|
605
|
-
const set = (0, import_shared2.getOrCreateSet)(this.pattern, name);
|
|
606
|
-
set.add(this.append(Object.assign({ type: "pattern", name }, result)));
|
|
607
|
-
const encoder = this.encoder;
|
|
608
|
-
result.data.forEach(
|
|
609
|
-
(obj) => encoder.processPattern(name, obj, result.type ?? "pattern", result.name)
|
|
610
|
-
);
|
|
611
|
-
}
|
|
612
|
-
setRecipe(recipeName, result) {
|
|
613
|
-
const set = (0, import_shared2.getOrCreateSet)(this.recipe, recipeName);
|
|
614
|
-
set.add(this.append(Object.assign({ type: "recipe" }, result)));
|
|
615
|
-
const encoder = this.encoder;
|
|
616
|
-
const recipes = this.context.recipes;
|
|
617
|
-
const recipeConfig = recipes.getConfig(recipeName);
|
|
618
|
-
if (!recipeConfig) return;
|
|
619
|
-
const recipe = result;
|
|
620
|
-
if (result.type) {
|
|
621
|
-
recipe.data.forEach((data) => {
|
|
622
|
-
const [recipeProps, styleProps] = recipes.splitProps(recipeName, data);
|
|
623
|
-
encoder.processStyleProps(styleProps);
|
|
624
|
-
encoder.processRecipe(recipeName, recipeProps);
|
|
625
|
-
});
|
|
626
|
-
} else {
|
|
627
|
-
recipe.data.forEach((data) => {
|
|
628
|
-
encoder.processRecipe(recipeName, data);
|
|
629
|
-
});
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
isEmpty() {
|
|
633
|
-
return this.all.length === 0;
|
|
634
|
-
}
|
|
635
|
-
setFilePath(filePath) {
|
|
636
|
-
this.filePath = filePath;
|
|
637
|
-
return this;
|
|
638
|
-
}
|
|
639
|
-
merge(result) {
|
|
640
|
-
result.css.forEach((item) => this.css.add(this.append(item)));
|
|
641
|
-
result.cva.forEach((item) => this.cva.add(this.append(item)));
|
|
642
|
-
result.sva.forEach((item) => this.sva.add(this.append(item)));
|
|
643
|
-
result.token.forEach((item) => this.token.add(this.append(item)));
|
|
644
|
-
result.jsx.forEach((item) => this.jsx.add(this.append(item)));
|
|
645
|
-
result.recipe.forEach((items, name) => {
|
|
646
|
-
const set = (0, import_shared2.getOrCreateSet)(this.recipe, name);
|
|
647
|
-
items.forEach((item) => set.add(this.append(item)));
|
|
648
|
-
});
|
|
649
|
-
result.pattern.forEach((items, name) => {
|
|
650
|
-
const set = (0, import_shared2.getOrCreateSet)(this.pattern, name);
|
|
651
|
-
items.forEach((item) => set.add(this.append(item)));
|
|
652
|
-
});
|
|
653
|
-
return this;
|
|
654
|
-
}
|
|
655
|
-
toArray() {
|
|
656
|
-
return this.all;
|
|
657
|
-
}
|
|
658
|
-
toJSON() {
|
|
659
|
-
return {
|
|
660
|
-
css: Array.from(this.css),
|
|
661
|
-
cva: Array.from(this.cva),
|
|
662
|
-
sva: Array.from(this.sva),
|
|
663
|
-
token: Array.from(this.token),
|
|
664
|
-
jsx: Array.from(this.jsx),
|
|
665
|
-
recipe: Object.fromEntries(Array.from(this.recipe.entries()).map(([key, value]) => [key, Array.from(value)])),
|
|
666
|
-
pattern: Object.fromEntries(Array.from(this.pattern.entries()).map(([key, value]) => [key, Array.from(value)]))
|
|
667
|
-
};
|
|
668
|
-
}
|
|
669
|
-
};
|
|
670
|
-
|
|
671
|
-
// src/parser.ts
|
|
672
|
-
var combineResult = (unboxed) => {
|
|
673
|
-
return [...unboxed.conditions, unboxed.raw, ...unboxed.spreadConditions];
|
|
674
|
-
};
|
|
675
|
-
var defaultEnv = {
|
|
676
|
-
preset: "ECMA"
|
|
677
|
-
};
|
|
678
|
-
var evaluateOptions = {
|
|
679
|
-
environment: defaultEnv
|
|
680
|
-
};
|
|
681
|
-
function createParser(context) {
|
|
682
|
-
const { jsx, imports, recipes, config } = context;
|
|
683
|
-
const syntax = config.syntax;
|
|
684
|
-
return function parse(sourceFile, encoder, options) {
|
|
685
|
-
if (!sourceFile) return;
|
|
686
|
-
const importDeclarations = getImportDeclarations(context, sourceFile);
|
|
687
|
-
const file = imports.file(importDeclarations);
|
|
688
|
-
const filePath = sourceFile.getFilePath();
|
|
689
|
-
import_logger.logger.debug(
|
|
690
|
-
"ast:import",
|
|
691
|
-
!file.isEmpty() ? `Found import { ${file.toString()} } in ${filePath}` : `No import found in ${filePath}`
|
|
692
|
-
);
|
|
693
|
-
const parserResult = new ParserResult(context, encoder);
|
|
694
|
-
if (file.isEmpty() && !jsx.isEnabled) {
|
|
695
|
-
return parserResult;
|
|
696
|
-
}
|
|
697
|
-
const extractResultByName = (0, import_extractor2.extract)({
|
|
698
|
-
ast: sourceFile,
|
|
699
|
-
tokens: context.tokens ? {
|
|
700
|
-
view: context.tokens.view,
|
|
701
|
-
isTokenFn: (fnName) => file.isTokenAlias(fnName)
|
|
702
|
-
} : void 0,
|
|
703
|
-
components: jsx.isEnabled ? {
|
|
704
|
-
matchTag: (prop) => {
|
|
705
|
-
if (options?.matchTag) {
|
|
706
|
-
const isBambooComponent = file.isBambooComponent(prop.tagName);
|
|
707
|
-
const matchTagMode = options.matchTagMode ?? "extend";
|
|
708
|
-
const isCustomMatch = options.matchTag(prop.tagName, isBambooComponent);
|
|
709
|
-
if (matchTagMode === "override") {
|
|
710
|
-
return isCustomMatch;
|
|
711
|
-
}
|
|
712
|
-
return isBambooComponent || isCustomMatch;
|
|
713
|
-
}
|
|
714
|
-
return !!file.matchTag(prop.tagName);
|
|
715
|
-
},
|
|
716
|
-
matchProp: (prop) => {
|
|
717
|
-
const isBambooProp = file.matchTagProp(prop.tagName, prop.propName);
|
|
718
|
-
if (options?.matchTagProp) {
|
|
719
|
-
return isBambooProp && options.matchTagProp(prop.tagName, prop.propName);
|
|
720
|
-
}
|
|
721
|
-
return isBambooProp;
|
|
722
|
-
}
|
|
723
|
-
} : void 0,
|
|
724
|
-
functions: {
|
|
725
|
-
matchFn: (prop) => file.matchFn(prop.fnName),
|
|
726
|
-
matchProp: () => true,
|
|
727
|
-
matchArg: (prop) => {
|
|
728
|
-
if (file.isJsxFactory(prop.fnName) && prop.index === 1 && import_ts_morph.Node.isIdentifier(prop.argNode)) return false;
|
|
729
|
-
return true;
|
|
730
|
-
}
|
|
731
|
-
},
|
|
732
|
-
taggedTemplates: syntax === "template-literal" ? {
|
|
733
|
-
matchTaggedTemplate: (tag) => file.matchFn(tag.fnName)
|
|
734
|
-
} : void 0,
|
|
735
|
-
getEvaluateOptions: (node) => {
|
|
736
|
-
if (!import_ts_morph.Node.isCallExpression(node)) return evaluateOptions;
|
|
737
|
-
const propAccessExpr = node.getExpression();
|
|
738
|
-
if (!import_ts_morph.Node.isPropertyAccessExpression(propAccessExpr)) return evaluateOptions;
|
|
739
|
-
let name = propAccessExpr.getText();
|
|
740
|
-
if (!file.isRawFn(name)) {
|
|
741
|
-
return evaluateOptions;
|
|
742
|
-
}
|
|
743
|
-
name = name.replace(".raw", "");
|
|
744
|
-
return {
|
|
745
|
-
environment: Object.assign({}, defaultEnv, {
|
|
746
|
-
extra: {
|
|
747
|
-
[name]: { raw: (v) => v }
|
|
748
|
-
}
|
|
749
|
-
})
|
|
750
|
-
};
|
|
751
|
-
},
|
|
752
|
-
flags: { skipTraverseFiles: true }
|
|
753
|
-
});
|
|
754
|
-
extractResultByName.forEach((result, alias) => {
|
|
755
|
-
const name = file.getName(file.normalizeFnName(alias));
|
|
756
|
-
import_logger.logger.debug(`ast:${name}`, name !== alias ? { kind: result.kind, alias } : { kind: result.kind });
|
|
757
|
-
if (result.kind === "function") {
|
|
758
|
-
(0, import_ts_pattern.match)(name).when(imports.matchers.css.match, (name2) => {
|
|
759
|
-
result.queryList.forEach((query) => {
|
|
760
|
-
if (query.kind === "call-expression") {
|
|
761
|
-
if (query.box.value.length > 1) {
|
|
762
|
-
parserResult.set(name2, {
|
|
763
|
-
name: name2,
|
|
764
|
-
box: query.box,
|
|
765
|
-
data: query.box.value.reduce(
|
|
766
|
-
(acc, value) => [...acc, ...combineResult((0, import_extractor2.unbox)(value))],
|
|
767
|
-
[]
|
|
768
|
-
)
|
|
769
|
-
});
|
|
770
|
-
} else {
|
|
771
|
-
parserResult.set(name2, {
|
|
772
|
-
name: name2,
|
|
773
|
-
box: query.box.value[0] ?? import_extractor2.box.fallback(query.box),
|
|
774
|
-
data: combineResult((0, import_extractor2.unbox)(query.box.value[0]))
|
|
775
|
-
});
|
|
776
|
-
}
|
|
777
|
-
} else if (query.kind === "tagged-template") {
|
|
778
|
-
const obj = (0, import_shared3.astish)(query.box.value);
|
|
779
|
-
parserResult.set(name2, {
|
|
780
|
-
name: name2,
|
|
781
|
-
box: query.box ?? import_extractor2.box.fallback(query.box),
|
|
782
|
-
data: [obj]
|
|
783
|
-
});
|
|
784
|
-
}
|
|
785
|
-
});
|
|
786
|
-
}).when(imports.matchers.tokens.match, (name2) => {
|
|
787
|
-
result.queryList.forEach((query) => {
|
|
788
|
-
if (query.kind === "call-expression") {
|
|
789
|
-
parserResult.setToken({
|
|
790
|
-
name: name2,
|
|
791
|
-
box: query.box.value[0] ?? import_extractor2.box.fallback(query.box),
|
|
792
|
-
data: combineResult((0, import_extractor2.unbox)(query.box.value[0]))
|
|
793
|
-
});
|
|
794
|
-
}
|
|
795
|
-
});
|
|
796
|
-
}).when(file.isValidPattern, (name2) => {
|
|
797
|
-
result.queryList.forEach((query) => {
|
|
798
|
-
if (query.kind === "call-expression") {
|
|
799
|
-
parserResult.setPattern(name2, {
|
|
800
|
-
name: name2,
|
|
801
|
-
box: query.box.value[0] ?? import_extractor2.box.fallback(query.box),
|
|
802
|
-
data: combineResult((0, import_extractor2.unbox)(query.box.value[0]))
|
|
803
|
-
});
|
|
804
|
-
}
|
|
805
|
-
});
|
|
806
|
-
}).when(file.isValidRecipe, (name2) => {
|
|
807
|
-
result.queryList.forEach((query) => {
|
|
808
|
-
if (query.kind === "call-expression") {
|
|
809
|
-
parserResult.setRecipe(name2, {
|
|
810
|
-
name: name2,
|
|
811
|
-
box: query.box.value[0] ?? import_extractor2.box.fallback(query.box),
|
|
812
|
-
data: combineResult((0, import_extractor2.unbox)(query.box.value[0]))
|
|
813
|
-
});
|
|
814
|
-
}
|
|
815
|
-
});
|
|
816
|
-
}).when(jsx.isJsxFactory, () => {
|
|
817
|
-
result.queryList.forEach((query) => {
|
|
818
|
-
if (query.kind === "call-expression" && query.box.value[1]) {
|
|
819
|
-
const map = query.box.value[1];
|
|
820
|
-
const boxNode = import_extractor2.box.isMap(map) ? map : import_extractor2.box.fallback(query.box);
|
|
821
|
-
const combined = combineResult((0, import_extractor2.unbox)(boxNode));
|
|
822
|
-
const transformed = options?.transform?.({ type: "jsx-factory", data: combined });
|
|
823
|
-
const result2 = { name, box: boxNode, data: transformed ?? combined };
|
|
824
|
-
if (import_extractor2.box.isRecipe(map)) {
|
|
825
|
-
parserResult.setCva(result2);
|
|
826
|
-
} else {
|
|
827
|
-
parserResult.set("css", result2);
|
|
828
|
-
}
|
|
829
|
-
const recipeOptions = query.box.value[2];
|
|
830
|
-
if (import_extractor2.box.isUnresolvable(map) && recipeOptions && import_extractor2.box.isMap(recipeOptions) && recipeOptions.value.has("defaultProps")) {
|
|
831
|
-
const maybeIdentifier = map.getNode();
|
|
832
|
-
if (import_ts_morph.Node.isIdentifier(maybeIdentifier)) {
|
|
833
|
-
const name2 = maybeIdentifier.getText();
|
|
834
|
-
const recipeName = file.getName(name2);
|
|
835
|
-
parserResult.setRecipe(recipeName, {
|
|
836
|
-
type: "jsx-recipe",
|
|
837
|
-
name: recipeName,
|
|
838
|
-
box: recipeOptions,
|
|
839
|
-
data: combineResult((0, import_extractor2.unbox)(recipeOptions.value.get("defaultProps")))
|
|
840
|
-
});
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
} else if (query.kind === "tagged-template") {
|
|
844
|
-
const obj = (0, import_shared3.astish)(query.box.value);
|
|
845
|
-
parserResult.set("css", {
|
|
846
|
-
name,
|
|
847
|
-
box: query.box ?? import_extractor2.box.fallback(query.box),
|
|
848
|
-
data: [obj]
|
|
849
|
-
});
|
|
850
|
-
}
|
|
851
|
-
});
|
|
852
|
-
}).when(file.isJsxFactory, (name2) => {
|
|
853
|
-
result.queryList.forEach((query) => {
|
|
854
|
-
if (query.kind === "call-expression") {
|
|
855
|
-
const map = query.box.value[0];
|
|
856
|
-
const boxNode = import_extractor2.box.isMap(map) ? map : import_extractor2.box.fallback(query.box);
|
|
857
|
-
const combined = combineResult((0, import_extractor2.unbox)(boxNode));
|
|
858
|
-
const transformed = options?.transform?.({ type: "jsx-factory", data: combined });
|
|
859
|
-
const result2 = { name: name2, box: boxNode, data: transformed ?? combined };
|
|
860
|
-
if (import_extractor2.box.isRecipe(map)) {
|
|
861
|
-
parserResult.setCva(result2);
|
|
862
|
-
} else {
|
|
863
|
-
parserResult.set("css", result2);
|
|
864
|
-
}
|
|
865
|
-
} else if (query.kind === "tagged-template") {
|
|
866
|
-
const obj = (0, import_shared3.astish)(query.box.value);
|
|
867
|
-
parserResult.set("css", {
|
|
868
|
-
name: name2,
|
|
869
|
-
box: query.box ?? import_extractor2.box.fallback(query.box),
|
|
870
|
-
data: [obj]
|
|
871
|
-
});
|
|
872
|
-
}
|
|
873
|
-
});
|
|
874
|
-
}).otherwise(() => {
|
|
875
|
-
});
|
|
876
|
-
} else if (jsx.isEnabled && result.kind === "component") {
|
|
877
|
-
result.queryList.forEach((query) => {
|
|
878
|
-
const data = combineResult((0, import_extractor2.unbox)(query.box));
|
|
879
|
-
switch (true) {
|
|
880
|
-
case (file.isJsxFactory(name) || file.isJsxFactory(alias)): {
|
|
881
|
-
parserResult.setJsx({ type: "jsx-factory", name, box: query.box, data });
|
|
882
|
-
break;
|
|
883
|
-
}
|
|
884
|
-
case (jsx.isJsxTagPattern(name) || jsx.isJsxTagPattern(alias)): {
|
|
885
|
-
parserResult.setPattern(name, { type: "jsx-pattern", name, box: query.box, data });
|
|
886
|
-
break;
|
|
887
|
-
}
|
|
888
|
-
// name: Trigger
|
|
889
|
-
case jsx.isJsxTagRecipe(name): {
|
|
890
|
-
const matchingRecipes = recipes.filter(name);
|
|
891
|
-
matchingRecipes.map((recipe) => {
|
|
892
|
-
parserResult.setRecipe(recipe.baseName, { type: "jsx-recipe", name, box: query.box, data });
|
|
893
|
-
});
|
|
894
|
-
break;
|
|
895
|
-
}
|
|
896
|
-
// alias: Tabs.Trigger
|
|
897
|
-
case jsx.isJsxTagRecipe(alias): {
|
|
898
|
-
const matchingRecipes = recipes.filter(alias);
|
|
899
|
-
matchingRecipes.map((recipe) => {
|
|
900
|
-
parserResult.setRecipe(recipe.baseName, { type: "jsx-recipe", name: alias, box: query.box, data });
|
|
901
|
-
});
|
|
902
|
-
break;
|
|
903
|
-
}
|
|
904
|
-
default: {
|
|
905
|
-
parserResult.setJsx({ type: "jsx", name, box: query.box, data });
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
});
|
|
909
|
-
}
|
|
910
|
-
});
|
|
911
|
-
return parserResult;
|
|
912
|
-
};
|
|
913
|
-
}
|
|
914
|
-
|
|
915
|
-
// src/project.ts
|
|
916
|
-
var normalizeCompilerOptions = (raw) => {
|
|
917
|
-
if (!raw) return {};
|
|
918
|
-
const { options } = import_ts_morph2.ts.convertCompilerOptionsFromJson(raw, process.cwd());
|
|
919
|
-
return options;
|
|
920
|
-
};
|
|
921
|
-
var createTsProject = (options) => new import_ts_morph2.Project({
|
|
922
|
-
skipAddingFilesFromTsConfig: true,
|
|
923
|
-
skipFileDependencyResolution: true,
|
|
924
|
-
skipLoadingLibFiles: true,
|
|
925
|
-
...options,
|
|
926
|
-
compilerOptions: {
|
|
927
|
-
allowJs: true,
|
|
928
|
-
strictNullChecks: false,
|
|
929
|
-
skipLibCheck: true,
|
|
930
|
-
...normalizeCompilerOptions(options.compilerOptions)
|
|
931
|
-
}
|
|
932
|
-
});
|
|
933
|
-
var Project = class {
|
|
934
|
-
constructor(options) {
|
|
935
|
-
this.options = options;
|
|
936
|
-
const { parserOptions } = options;
|
|
937
|
-
this.project = createTsProject(options);
|
|
938
|
-
this.parser = createParser(parserOptions);
|
|
939
|
-
this.createSourceFiles();
|
|
940
|
-
}
|
|
941
|
-
project;
|
|
942
|
-
parser;
|
|
943
|
-
get parserOptions() {
|
|
944
|
-
return this.options.parserOptions;
|
|
945
|
-
}
|
|
946
|
-
get files() {
|
|
947
|
-
return this.options.getFiles();
|
|
948
|
-
}
|
|
949
|
-
getSourceFile = (filePath) => {
|
|
950
|
-
return this.project.getSourceFile(filePath);
|
|
951
|
-
};
|
|
952
|
-
createSourceFile = (filePath) => {
|
|
953
|
-
const { readFile } = this.options;
|
|
954
|
-
return this.project.createSourceFile(filePath, readFile(filePath), {
|
|
955
|
-
overwrite: true,
|
|
956
|
-
scriptKind: import_ts_morph2.ScriptKind.TSX
|
|
957
|
-
});
|
|
958
|
-
};
|
|
959
|
-
createSourceFiles = () => {
|
|
960
|
-
const files = this.getFiles();
|
|
961
|
-
for (const file of files) {
|
|
962
|
-
this.createSourceFile(file);
|
|
963
|
-
}
|
|
964
|
-
};
|
|
965
|
-
addSourceFile = (filePath, content) => {
|
|
966
|
-
return this.project.createSourceFile(filePath, content, {
|
|
967
|
-
overwrite: true,
|
|
968
|
-
scriptKind: import_ts_morph2.ScriptKind.TSX
|
|
969
|
-
});
|
|
970
|
-
};
|
|
971
|
-
removeSourceFile = (filePath) => {
|
|
972
|
-
const sourceFile = this.project.getSourceFile(filePath);
|
|
973
|
-
if (sourceFile) {
|
|
974
|
-
return this.project.removeSourceFile(sourceFile);
|
|
975
|
-
}
|
|
976
|
-
return false;
|
|
977
|
-
};
|
|
978
|
-
reloadSourceFile = (filePath) => {
|
|
979
|
-
return this.getSourceFile(filePath)?.refreshFromFileSystemSync();
|
|
980
|
-
};
|
|
981
|
-
reloadSourceFiles = () => {
|
|
982
|
-
const files = this.getFiles();
|
|
983
|
-
for (const file of files) {
|
|
984
|
-
const source = this.getSourceFile(file);
|
|
985
|
-
source?.refreshFromFileSystemSync() ?? this.project.addSourceFileAtPath(file);
|
|
986
|
-
}
|
|
987
|
-
};
|
|
988
|
-
get readFile() {
|
|
989
|
-
return this.options.readFile;
|
|
990
|
-
}
|
|
991
|
-
get getFiles() {
|
|
992
|
-
return this.options.getFiles;
|
|
993
|
-
}
|
|
994
|
-
parseJson = (filePath) => {
|
|
995
|
-
const { readFile, parserOptions } = this.options;
|
|
996
|
-
const content = readFile(filePath);
|
|
997
|
-
parserOptions.encoder.fromJSON(JSON.parse(content));
|
|
998
|
-
const result = new ParserResult(parserOptions);
|
|
999
|
-
return result.setFilePath(filePath);
|
|
1000
|
-
};
|
|
1001
|
-
parseSourceFile = (filePath, encoder) => {
|
|
1002
|
-
const { hooks } = this.options;
|
|
1003
|
-
if (filePath.endsWith(".json")) {
|
|
1004
|
-
return this.parseJson(filePath);
|
|
1005
|
-
}
|
|
1006
|
-
const sourceFile = this.project.getSourceFile(filePath);
|
|
1007
|
-
if (!sourceFile) return;
|
|
1008
|
-
const original = sourceFile.getText();
|
|
1009
|
-
const options = {};
|
|
1010
|
-
const custom = hooks["parser:before"]?.({
|
|
1011
|
-
filePath,
|
|
1012
|
-
content: original,
|
|
1013
|
-
configure(opts) {
|
|
1014
|
-
const { matchTag, matchTagMode, matchTagProp } = opts;
|
|
1015
|
-
if (matchTag) {
|
|
1016
|
-
options.matchTag = matchTag;
|
|
1017
|
-
}
|
|
1018
|
-
if (matchTagMode) {
|
|
1019
|
-
options.matchTagMode = matchTagMode;
|
|
1020
|
-
}
|
|
1021
|
-
if (matchTagProp) {
|
|
1022
|
-
options.matchTagProp = matchTagProp;
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
});
|
|
1026
|
-
const transformed = custom ?? this.transformFile(filePath, original);
|
|
1027
|
-
if (original !== transformed) {
|
|
1028
|
-
sourceFile.replaceWithText(transformed);
|
|
1029
|
-
}
|
|
1030
|
-
if (hooks["parser:preprocess"]) {
|
|
1031
|
-
options.transform = hooks["parser:preprocess"];
|
|
1032
|
-
}
|
|
1033
|
-
const result = this.parser(sourceFile, encoder, options)?.setFilePath(filePath);
|
|
1034
|
-
hooks["parser:after"]?.({ filePath, result });
|
|
1035
|
-
return result;
|
|
1036
|
-
};
|
|
1037
|
-
transformFile = (_filePath, content) => {
|
|
1038
|
-
return content;
|
|
1039
|
-
};
|
|
1040
|
-
classify = (fileMap) => {
|
|
1041
|
-
const { parserOptions } = this.options;
|
|
1042
|
-
return classifyProject(parserOptions, fileMap);
|
|
1043
|
-
};
|
|
1044
|
-
};
|
|
1045
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
1046
|
-
0 && (module.exports = {
|
|
1047
|
-
ParserResult,
|
|
1048
|
-
Project
|
|
1049
|
-
});
|