@base44/vite-plugin 0.2.22 → 0.2.24

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.
@@ -0,0 +1,257 @@
1
+ import type { NodePath } from "@babel/traverse";
2
+ import type * as t from "@babel/types";
3
+ import { JSXAttributeUtils, StaticValueUtils } from "./shared-utils.js";
4
+
5
+ export const DATA_ARR_INDEX = "data-arr-index";
6
+ export const DATA_ARR_VARIABLE_NAME = "data-arr-variable-name";
7
+ export const DATA_ARR_FIELD = "data-arr-field";
8
+ const GENERATED_INDEX_PARAM = "__arrIdx__";
9
+
10
+ interface ArrayMapInfo {
11
+ arrayExpression: NodePath<t.Expression>;
12
+ callbackParam: string;
13
+ indexParam: string | null;
14
+ arrayVariableName: string | null;
15
+ mapCallPath: NodePath<t.CallExpression>;
16
+ }
17
+
18
+ export class StaticArrayProcessor {
19
+ private attributeUtils: JSXAttributeUtils;
20
+ private staticValueUtils: StaticValueUtils;
21
+
22
+ constructor(private types: typeof t) {
23
+ this.attributeUtils = new JSXAttributeUtils(types);
24
+ this.staticValueUtils = new StaticValueUtils(types);
25
+ }
26
+
27
+ process(path: NodePath<t.JSXOpeningElement>): void {
28
+ const arrayInfo = this.findParentArrayMap(path);
29
+ if (!arrayInfo) return;
30
+
31
+ if (!this.isStaticArray(arrayInfo.arrayExpression)) return;
32
+
33
+ const indexIdentifier = this.ensureIndexParam(arrayInfo);
34
+ this.addDataAttributes(path, arrayInfo, indexIdentifier);
35
+ }
36
+
37
+ private addDataAttributes(
38
+ path: NodePath<t.JSXOpeningElement>,
39
+ arrayInfo: ArrayMapInfo,
40
+ indexIdentifier: string
41
+ ): void {
42
+ this.attributeUtils.addExpressionAttribute(
43
+ path,
44
+ DATA_ARR_INDEX,
45
+ this.types.identifier(indexIdentifier)
46
+ );
47
+
48
+ if (arrayInfo.arrayVariableName) {
49
+ this.attributeUtils.addStringAttribute(
50
+ path,
51
+ DATA_ARR_VARIABLE_NAME,
52
+ arrayInfo.arrayVariableName
53
+ );
54
+ }
55
+
56
+ const fieldPath = this.findTextContentFieldPath(
57
+ path,
58
+ arrayInfo.callbackParam
59
+ );
60
+ if (fieldPath) {
61
+ this.attributeUtils.addStringAttribute(path, DATA_ARR_FIELD, fieldPath);
62
+ }
63
+ }
64
+
65
+ private findTextContentFieldPath(
66
+ path: NodePath<t.JSXOpeningElement>,
67
+ callbackParam: string
68
+ ): string | null {
69
+ const parentElement = path.parentPath;
70
+ if (!parentElement?.isJSXElement()) return null;
71
+
72
+ for (const child of parentElement.get("children")) {
73
+ const fieldPath = this.extractFieldPathFromChild(child, callbackParam);
74
+ if (fieldPath) return fieldPath;
75
+ }
76
+
77
+ return null;
78
+ }
79
+
80
+ private extractFieldPathFromChild(
81
+ child: NodePath<t.JSXElement["children"][number]>,
82
+ callbackParam: string
83
+ ): string | null {
84
+ if (!child.isJSXExpressionContainer()) return null;
85
+
86
+ const expression = child.get("expression");
87
+ if (!expression.isMemberExpression()) return null;
88
+
89
+ return this.extractFieldPath(expression, callbackParam);
90
+ }
91
+
92
+ private extractFieldPath(
93
+ expr: NodePath<t.MemberExpression>,
94
+ callbackParam: string
95
+ ): string | null {
96
+ const parts = this.collectMemberExpressionParts(expr);
97
+ if (!parts) return null;
98
+
99
+ const { rootName, propertyNames } = parts;
100
+ return rootName === callbackParam ? propertyNames.join(".") : null;
101
+ }
102
+
103
+ private collectMemberExpressionParts(
104
+ expr: NodePath<t.MemberExpression>
105
+ ): { rootName: string; propertyNames: string[] } | null {
106
+ const propertyNames: string[] = [];
107
+ let current: NodePath<t.Expression> = expr;
108
+
109
+ while (current.isMemberExpression()) {
110
+ const property = current.get("property");
111
+ if (!property.isIdentifier()) return null;
112
+
113
+ propertyNames.unshift(property.node.name);
114
+ current = current.get("object") as NodePath<t.Expression>;
115
+ }
116
+
117
+ if (!current.isIdentifier()) return null;
118
+
119
+ return { rootName: current.node.name, propertyNames };
120
+ }
121
+
122
+ private ensureIndexParam(arrayInfo: ArrayMapInfo): string {
123
+ if (arrayInfo.indexParam) {
124
+ return arrayInfo.indexParam;
125
+ }
126
+
127
+ this.addIndexParamToCallback(arrayInfo.mapCallPath);
128
+ return GENERATED_INDEX_PARAM;
129
+ }
130
+
131
+ private addIndexParamToCallback(
132
+ mapCallPath: NodePath<t.CallExpression>
133
+ ): void {
134
+ const callback = this.getMapCallback(mapCallPath);
135
+ if (!callback) return;
136
+
137
+ const params = callback.get("params");
138
+ if (params.length === 1) {
139
+ callback.node.params.push(this.types.identifier(GENERATED_INDEX_PARAM));
140
+ }
141
+ }
142
+
143
+ private getMapCallback(
144
+ mapCallPath: NodePath<t.CallExpression>
145
+ ): NodePath<t.Function> | null {
146
+ const args = mapCallPath.get("arguments");
147
+ const firstArg = args[0];
148
+ if (firstArg && firstArg.isFunction()) {
149
+ return firstArg as NodePath<t.Function>;
150
+ }
151
+ return null;
152
+ }
153
+
154
+ private findParentArrayMap(
155
+ path: NodePath<t.JSXOpeningElement>,
156
+ maxDepth: number = 5
157
+ ): ArrayMapInfo | null {
158
+ let currentPath: NodePath = path;
159
+ let depth = 0;
160
+
161
+ while (currentPath.parentPath && depth < maxDepth) {
162
+ const mapInfo = this.tryExtractMapInfo(currentPath.parentPath);
163
+ if (mapInfo) return mapInfo;
164
+
165
+ currentPath = currentPath.parentPath;
166
+ depth++;
167
+ }
168
+
169
+ return null;
170
+ }
171
+
172
+ private tryExtractMapInfo(parent: NodePath): ArrayMapInfo | null {
173
+ if (!parent.isCallExpression()) return null;
174
+ if (!this.isMapCall(parent)) return null;
175
+
176
+ return this.extractArrayMapInfo(parent);
177
+ }
178
+
179
+ private isMapCall(callExpr: NodePath<t.CallExpression>): boolean {
180
+ const callee = callExpr.get("callee");
181
+ if (!callee.isMemberExpression()) return false;
182
+
183
+ const property = callee.get("property");
184
+ return property.isIdentifier() && property.node.name === "map";
185
+ }
186
+
187
+ private extractArrayMapInfo(
188
+ mapCall: NodePath<t.CallExpression>
189
+ ): ArrayMapInfo | null {
190
+ const callback = this.getMapCallback(mapCall);
191
+ if (!callback) return null;
192
+
193
+ const params = callback.get("params");
194
+ const firstParam = params[0];
195
+ if (!firstParam || !firstParam.isIdentifier()) return null;
196
+
197
+ const callee = mapCall.get("callee") as NodePath<t.MemberExpression>;
198
+ const arrayExpression = callee.get("object") as NodePath<t.Expression>;
199
+
200
+ return {
201
+ arrayExpression,
202
+ callbackParam: firstParam.node.name,
203
+ indexParam: this.extractIndexParam(params),
204
+ arrayVariableName: this.extractArrayVariableName(arrayExpression),
205
+ mapCallPath: mapCall,
206
+ };
207
+ }
208
+
209
+ private extractIndexParam(params: NodePath<t.Node>[]): string | null {
210
+ const secondParam = params[1];
211
+ return secondParam && secondParam.isIdentifier()
212
+ ? secondParam.node.name
213
+ : null;
214
+ }
215
+
216
+ private extractArrayVariableName(
217
+ arrayExpression: NodePath<t.Expression>
218
+ ): string | null {
219
+ return arrayExpression.isIdentifier() ? arrayExpression.node.name : null;
220
+ }
221
+
222
+ private isStaticArray(arrayExpression: NodePath<t.Expression>): boolean {
223
+ const arrayExpr = this.resolveArrayExpression(arrayExpression);
224
+ return arrayExpr ? this.isStaticArrayExpression(arrayExpr) : false;
225
+ }
226
+
227
+ private resolveArrayExpression(
228
+ expression: NodePath<t.Expression>
229
+ ): NodePath<t.ArrayExpression> | null {
230
+ if (expression.isArrayExpression()) {
231
+ return expression;
232
+ }
233
+
234
+ if (expression.isIdentifier()) {
235
+ return this.resolveIdentifierToArray(expression);
236
+ }
237
+
238
+ return null;
239
+ }
240
+
241
+ private resolveIdentifierToArray(
242
+ identifier: NodePath<t.Identifier>
243
+ ): NodePath<t.ArrayExpression> | null {
244
+ const binding = identifier.scope.getBinding(identifier.node.name);
245
+
246
+ if (!binding?.path.isVariableDeclarator()) return null;
247
+
248
+ const init = binding.path.get("init");
249
+ return init.isArrayExpression() ? init : null;
250
+ }
251
+
252
+ private isStaticArrayExpression(
253
+ arrayExpression: NodePath<t.ArrayExpression>
254
+ ): boolean {
255
+ return this.staticValueUtils.isStaticArrayExpression(arrayExpression);
256
+ }
257
+ }
@@ -3,6 +3,8 @@ import { default as traverse } from "@babel/traverse";
3
3
  import { default as generate } from "@babel/generator";
4
4
  import * as t from "@babel/types";
5
5
  import type { Plugin } from "vite";
6
+ import { StaticArrayProcessor } from "./processors/static-array-processor.js";
7
+ import { JSXUtils } from "./jsx-utils.js";
6
8
 
7
9
  // Helper function to check if JSX element contains dynamic content
8
10
  export function checkIfElementHasDynamicContent(jsxElement: any) {
@@ -213,6 +215,8 @@ export function visualEditPlugin() {
213
215
  });
214
216
 
215
217
  // Traverse the AST and add source location and dynamic content attributes to JSX elements
218
+ JSXUtils.init(t);
219
+ const staticArrayProcessor = new StaticArrayProcessor(t);
216
220
  let elementsProcessed = 0;
217
221
  traverse.default(ast, {
218
222
  JSXElement(path) {
@@ -258,6 +262,8 @@ export function visualEditPlugin() {
258
262
  sourceLocationAttr,
259
263
  dynamicContentAttr
260
264
  );
265
+
266
+ staticArrayProcessor.process(path.get("openingElement"));
261
267
  elementsProcessed++;
262
268
  },
263
269
  });