-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.js
371 lines (318 loc) · 10.9 KB
/
formatter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
const prettier = require("prettier");
/**
* Formats an Eleva.js component
* @param {string} text - The text content to format
* @param {Object} config - Formatter configuration
* @returns {string} - Formatted text
*/
function formatElevaComponent(text, config) {
try {
// First, apply general JavaScript formatting with Prettier
const prettierFormatted = formatWithPrettier(text, config);
// Then apply Eleva-specific formatting
return formatElevaSpecific(prettierFormatted, config);
} catch (error) {
console.error("Error in formatElevaComponent:", error);
// Return original text if formatting fails to avoid data loss
return text;
}
}
/**
* Format JavaScript with Prettier
* @param {string} text - The text to format
* @param {Object} config - Formatter configuration
* @returns {string} - Formatted text
*/
function formatWithPrettier(text, config) {
try {
return prettier.format(text, {
parser: "babel",
singleQuote: config.templateStyle === "singleQuotes",
tabWidth: config.indentSize || 2, // Default to 2 if undefined
printWidth: 80,
trailingComma: "es5",
bracketSpacing: true,
semi: true,
});
} catch (error) {
console.error("Prettier formatting error:", error);
return text; // Return original text if prettier fails
}
}
/**
* Apply Eleva-specific formatting rules
* @param {string} text - The text to format
* @param {Object} config - Formatter configuration
* @returns {string} - Formatted text
*/
function formatElevaSpecific(text, config) {
try {
// Initialize with the input text
let formattedText = text;
// Regular expression to find Eleva template functions
const templateRegex =
/(template\s*:\s*)\(\s*(?:ctx|context|\w+)\s*\)\s*=>\s*(['"`])([\s\S]*?)(\2)/g;
formattedText = formattedText.replace(
templateRegex,
(match, templateDecl, quote, content, endQuote) => {
try {
// Ensure content is not undefined
const templateContent = content || "";
// Convert to backticks for template strings
if (quote !== "`") {
// Format the HTML content with proper indentation
const formattedContent = formatTemplateHTML(
templateContent,
config.indentSize || 2
);
return `${templateDecl}(ctx) => \`${formattedContent}\``;
}
// Already backticks, just format the HTML content
const formattedContent = formatTemplateHTML(
templateContent,
config.indentSize || 2
);
return `${templateDecl}(ctx) => \`${formattedContent}\``;
} catch (error) {
console.error("Error formatting template:", error);
return match; // Return unchanged if error occurs
}
}
);
// Format signal usage to ensure .value access
formattedText = formatSignalUsage(formattedText);
// Format style functions to use backticks
const styleRegex =
/(style\s*:\s*)\(\s*(?:ctx|context|\w+)\s*\)\s*=>\s*(['"`])([\s\S]*?)(\2)/g;
formattedText = formattedText.replace(
styleRegex,
(match, styleDecl, quote, content, endQuote) => {
try {
// Ensure content is not undefined
const styleContent = content || "";
if (quote !== "`") {
// Format the CSS content with proper indentation
const formattedContent = formatCSS(
styleContent,
config.indentSize || 2
);
return `${styleDecl}(ctx) => \`${formattedContent}\``;
}
// Already backticks, just format the CSS
const formattedContent = formatCSS(
styleContent,
config.indentSize || 2
);
return `${styleDecl}(ctx) => \`${formattedContent}\``;
} catch (error) {
console.error("Error formatting style:", error);
return match; // Return unchanged if error occurs
}
}
);
// Format setup function spacing and structure
formattedText = formatSetupFunction(formattedText, config.indentSize || 2);
// Format event handlers to ensure proper spacing
formattedText = formatEventHandlers(formattedText);
return formattedText;
} catch (error) {
console.error("Error in formatElevaSpecific:", error);
return text; // Return original text if formatting fails
}
}
/**
* Format template HTML content with proper indentation
* @param {string} html - HTML content to format
* @param {number} indentSize - Size of indentation
* @returns {string} - Formatted HTML
*/
function formatTemplateHTML(html, indentSize) {
try {
// Handle undefined or null HTML
if (!html) return "";
// Clean up excess whitespace
let cleaned = html
.trim()
.replace(/\s+/g, " ")
.replace(/> </g, ">\n<")
.replace(/<\/([a-z0-9]+)><([a-z0-9]+)>/gi, "</$1>\n<$2>");
// Apply indentation
let depth = 0;
let indent = " ".repeat(indentSize);
let lines = cleaned.split("\n");
let formatted = [];
for (let line of lines) {
let trimmed = line.trim();
if (!trimmed) continue;
// Check for closing tags to decrease indent level
if (trimmed.match(/^<\//) && depth > 0) {
depth--;
}
// Add the line with proper indentation
formatted.push(indent.repeat(depth) + trimmed);
// Check for opening tags to increase indent level
if (trimmed.match(/<[^/][^>]*>/) && !trimmed.match(/<[^/][^>]*\/>/)) {
depth++;
}
}
return formatted.join("\n");
} catch (error) {
console.error("Error in formatTemplateHTML:", error);
return html || ""; // Return original HTML if formatting fails
}
}
/**
* Format CSS content with proper indentation
* @param {string} css - CSS content to format
* @param {number} indentSize - Size of indentation
* @returns {string} - Formatted CSS
*/
function formatCSS(css, indentSize) {
try {
// Handle undefined or null CSS
if (!css) return "";
let cleaned = css.trim();
// Format rule blocks
cleaned = cleaned
.replace(/\s*{\s*/g, " {\n")
.replace(/;\s*/g, ";\n")
.replace(/\s*}\s*/g, "\n}\n");
// Apply indentation
let depth = 0;
let indent = " ".repeat(indentSize);
let lines = cleaned.split("\n");
let formatted = [];
for (let line of lines) {
let trimmed = line.trim();
if (!trimmed) continue;
// Check for closing braces to decrease indent
if (trimmed === "}" && depth > 0) {
depth--;
}
// Add the line with proper indentation
formatted.push(indent.repeat(depth) + trimmed);
// Check for opening braces to increase indent
if (trimmed.includes("{")) {
depth++;
}
}
return formatted.join("\n");
} catch (error) {
console.error("Error in formatCSS:", error);
return css || ""; // Return original CSS if formatting fails
}
}
/**
* Format signal usage to ensure .value is properly accessed
* @param {string} text - Text to format
* @returns {string} - Formatted text
*/
function formatSignalUsage(text) {
try {
// This is a simplistic approach - a real implementation would use AST parsing
// Replace {{signal}} with {{signal.value}}
const interpolationRegex = /\{\{\s*([a-zA-Z0-9_]+)\s*\}\}/g;
let formatted = text.replace(interpolationRegex, (match, signalName) => {
// Skip replacement if it's already using .value or it's a method call or not a signal
if (
match.includes(".value") ||
match.includes("(") ||
!text.includes(`const ${signalName} = signal(`)
) {
return match;
}
return `{{ ${signalName}.value }}`;
});
return formatted;
} catch (error) {
console.error("Error in formatSignalUsage:", error);
return text; // Return original text if formatting fails
}
}
/**
* Format Eleva setup function for consistency
* @param {string} text - Text to format
* @param {number} indentSize - Size of indentation
* @returns {string} - Formatted text
*/
function formatSetupFunction(text, indentSize) {
try {
// Find setup function block
const setupRegex =
/(setup\s*:\s*\(\{\s*(?:signal|props|emitter|on\w+|[^}]+)\s*\}\)\s*(?:=>|\{))([\s\S]*?)(\}|(?:=>|,)\s*\{)/g;
return text.replace(
setupRegex,
(match, setupStart, setupBody, setupEnd) => {
try {
// Check if using arrow function or regular function syntax
const isArrowFn = setupStart.includes("=>");
// Format the structure based on function style
if (isArrowFn) {
// For arrow functions, ensure clean destructuring and return
const formattedStart = setupStart.replace(
/\(\{\s*([^}]+)\s*\}\)/,
(_, $1) => {
// Handle undefined params
if (!$1) return "({})";
const params = $1
.split(",")
.filter((p) => p && p.trim()) // Filter out empty entries
.map((p) => p.trim())
.join(", ");
return `({ ${params} })`;
}
);
return `${formattedStart} ${setupBody || ""} ${setupEnd}`;
} else {
// For standard functions, format the setup body
const indent = " ".repeat(indentSize);
// Handle undefined body
const formattedBody = (setupBody || "")
.trim()
.split("\n")
.map((line) => `${indent}${line.trim()}`)
.join("\n");
return `${setupStart}\n${formattedBody}\n${setupEnd}`;
}
} catch (error) {
console.error("Error formatting setup function:", error);
return match; // Return unchanged if error occurs
}
}
);
} catch (error) {
console.error("Error in formatSetupFunction:", error);
return text; // Return original text if formatting fails
}
}
/**
* Format event handlers for consistent spacing and quotes
* @param {string} text - Text to format
* @returns {string} - Formatted text
*/
function formatEventHandlers(text) {
try {
// Format @event="handler" attributes to ensure consistent spacing and quotes
const eventHandlerRegex = /(@[a-zA-Z][a-zA-Z0-9:-]*)\s*=\s*(['"`])(.*?)\2/g;
return text.replace(
eventHandlerRegex,
(match, eventName, quote, handlerCode) => {
try {
// Clean up handler code - remove extra spaces
const cleanHandler = (handlerCode || "").trim();
// Use double quotes for event handler attributes
return `${eventName}="${cleanHandler}"`;
} catch (error) {
console.error("Error formatting event handler:", error);
return match; // Return unchanged if error occurs
}
}
);
} catch (error) {
console.error("Error in formatEventHandlers:", error);
return text; // Return original text if formatting fails
}
}
module.exports = {
formatElevaComponent,
};