round-core 0.1.0 → 0.1.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.
@@ -1,298 +1,394 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
1
3
  import fs from "node:fs";
2
4
  import path from "node:path";
3
- function transform(code) {
5
+ function transform(code, initialDepth = 0) {
6
+ let result = "";
7
+ let i = 0;
8
+ let jsxDepth = initialDepth;
4
9
  function parseBlock(str, startIndex) {
5
10
  let open = 0;
6
11
  let startBlockIndex = -1;
7
- let endBlockIndex = -1;
8
- let inSingle = false;
9
- let inDouble = false;
10
- let inTemplate = false;
11
- let inCommentLine = false;
12
- let inCommentMulti = false;
13
- for (let i = startIndex; i < str.length; i++) {
14
- const ch = str[i];
15
- const prev2 = i > 0 ? str[i - 1] : "";
16
- const next = i < str.length - 1 ? str[i + 1] : "";
17
- if (inCommentLine) {
18
- if (ch === "\n" || ch === "\r") inCommentLine = false;
12
+ let inSingle2 = false, inDouble2 = false, inTemplate2 = false;
13
+ let inCommentLine2 = false, inCommentMulti2 = false;
14
+ for (let j = startIndex; j < str.length; j++) {
15
+ const ch = str[j];
16
+ const prev = j > 0 ? str[j - 1] : "";
17
+ const next = j < str.length - 1 ? str[j + 1] : "";
18
+ if (inCommentLine2) {
19
+ if (ch === "\n" || ch === "\r") inCommentLine2 = false;
19
20
  continue;
20
21
  }
21
- if (inCommentMulti) {
22
+ if (inCommentMulti2) {
22
23
  if (ch === "*" && next === "/") {
23
- inCommentMulti = false;
24
- i++;
24
+ inCommentMulti2 = false;
25
+ j++;
25
26
  }
26
27
  continue;
27
28
  }
28
- if (inTemplate) {
29
- if (ch === "`" && prev2 !== "\\") inTemplate = false;
29
+ if (inTemplate2) {
30
+ if (ch === "`" && prev !== "\\") inTemplate2 = false;
30
31
  continue;
31
32
  }
32
- if (inSingle) {
33
- if (ch === "'" && prev2 !== "\\") inSingle = false;
33
+ if (inSingle2) {
34
+ if (ch === "'" && prev !== "\\") inSingle2 = false;
34
35
  continue;
35
36
  }
36
- if (inDouble) {
37
- if (ch === '"' && prev2 !== "\\") inDouble = false;
37
+ if (inDouble2) {
38
+ if (ch === '"' && prev !== "\\") inDouble2 = false;
38
39
  continue;
39
40
  }
40
41
  if (ch === "/" && next === "/") {
41
- inCommentLine = true;
42
- i++;
42
+ inCommentLine2 = true;
43
+ j++;
43
44
  continue;
44
45
  }
45
46
  if (ch === "/" && next === "*") {
46
- inCommentMulti = true;
47
- i++;
47
+ inCommentMulti2 = true;
48
+ j++;
48
49
  continue;
49
50
  }
50
51
  if (ch === "`") {
51
- inTemplate = true;
52
+ inTemplate2 = true;
52
53
  continue;
53
54
  }
54
55
  if (ch === "'") {
55
- inSingle = true;
56
+ inSingle2 = true;
56
57
  continue;
57
58
  }
58
59
  if (ch === '"') {
59
- inDouble = true;
60
+ inDouble2 = true;
60
61
  continue;
61
62
  }
62
63
  if (ch === "{") {
63
- if (open === 0) startBlockIndex = i;
64
+ if (open === 0) startBlockIndex = j;
64
65
  open++;
65
66
  } else if (ch === "}") {
66
67
  open--;
67
68
  if (open === 0) {
68
- endBlockIndex = i;
69
- return { start: startBlockIndex, end: endBlockIndex };
69
+ return { start: startBlockIndex, end: j };
70
70
  }
71
71
  }
72
72
  }
73
73
  return null;
74
74
  }
75
- let result = code;
76
- function consumeWhitespace(str, i) {
77
- while (i < str.length && /\s/.test(str[i])) i++;
78
- return i;
75
+ __name(parseBlock, "parseBlock");
76
+ function consumeWhitespace(str, idx) {
77
+ while (idx < str.length && /\s/.test(str[idx])) idx++;
78
+ return idx;
79
79
  }
80
- function parseIfChain(str, ifIndex) {
81
- const head = str.slice(ifIndex);
82
- const m = head.match(/^if\s*\((.*?)\)\s*\{/);
83
- if (!m) return null;
84
- let i = ifIndex;
80
+ __name(consumeWhitespace, "consumeWhitespace");
81
+ function extractCondition(str, startIndex) {
82
+ if (str[startIndex] !== "(") return null;
83
+ let depth = 1;
84
+ let j = startIndex + 1;
85
+ let inSingle2 = false, inDouble2 = false, inTemplate2 = false;
86
+ while (j < str.length && depth > 0) {
87
+ const ch = str[j], prev = str[j - 1] || "";
88
+ if (!inDouble2 && !inTemplate2 && ch === "'" && prev !== "\\") inSingle2 = !inSingle2;
89
+ else if (!inSingle2 && !inTemplate2 && ch === '"' && prev !== "\\") inDouble2 = !inDouble2;
90
+ else if (!inSingle2 && !inDouble2 && ch === "`" && prev !== "\\") inTemplate2 = !inTemplate2;
91
+ if (!inSingle2 && !inDouble2 && !inTemplate2) {
92
+ if (ch === "(") depth++;
93
+ else if (ch === ")") depth--;
94
+ }
95
+ j++;
96
+ }
97
+ if (depth !== 0) return null;
98
+ return { cond: str.substring(startIndex + 1, j - 1), end: j };
99
+ }
100
+ __name(extractCondition, "extractCondition");
101
+ function handleIf(currI, isBare = false) {
102
+ let startPtr = currI;
103
+ if (!isBare) {
104
+ startPtr = consumeWhitespace(code, currI + 1);
105
+ }
106
+ if (!code.startsWith("if", startPtr)) return null;
107
+ let ptr = startPtr + 2;
108
+ ptr = consumeWhitespace(code, ptr);
109
+ if (code[ptr] !== "(") return null;
85
110
  const cases = [];
86
111
  let elseContent = null;
112
+ let currentPtr = ptr;
113
+ let first = true;
87
114
  while (true) {
88
- const cur = str.slice(i);
89
- const mm = cur.match(/^if\s*\((.*?)\)\s*\{/);
90
- if (!mm) return null;
91
- let cond = mm[1];
92
- const trimmedCond = String(cond).trim();
93
- const isSimplePath = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*)*$/.test(trimmedCond);
94
- if (isSimplePath && !trimmedCond.endsWith(")")) {
95
- cond = `((typeof (${trimmedCond}) === 'function' && typeof (${trimmedCond}).peek === 'function' && ('value' in (${trimmedCond}))) ? (${trimmedCond})() : (${trimmedCond}))`;
96
- }
97
- const blockStart = i + mm[0].length - 1;
98
- const block = parseBlock(str, blockStart);
115
+ if (!first) {
116
+ if (!code.startsWith("if", currentPtr)) break;
117
+ currentPtr += 2;
118
+ currentPtr = consumeWhitespace(code, currentPtr);
119
+ }
120
+ first = false;
121
+ const condRes = extractCondition(code, currentPtr);
122
+ if (!condRes) return null;
123
+ currentPtr = consumeWhitespace(code, condRes.end);
124
+ if (code[currentPtr] !== "{") return null;
125
+ const block = parseBlock(code, currentPtr);
99
126
  if (!block) return null;
100
- const content = str.substring(block.start + 1, block.end);
101
- cases.push({ cond, content });
102
- i = block.end + 1;
103
- i = consumeWhitespace(str, i);
104
- if (!str.startsWith("else", i)) {
127
+ const rawContent = code.substring(block.start + 1, block.end);
128
+ const transformedContent = transform(rawContent, 1);
129
+ cases.push({ cond: condRes.cond, content: transformedContent });
130
+ currentPtr = block.end + 1;
131
+ currentPtr = consumeWhitespace(code, currentPtr);
132
+ if (code.startsWith("else", currentPtr)) {
133
+ currentPtr += 4;
134
+ currentPtr = consumeWhitespace(code, currentPtr);
135
+ if (code.startsWith("if", currentPtr)) {
136
+ continue;
137
+ } else if (code[currentPtr] === "{") {
138
+ const elseBlock = parseBlock(code, currentPtr);
139
+ if (!elseBlock) return null;
140
+ const rawElse = code.substring(elseBlock.start + 1, elseBlock.end);
141
+ elseContent = transform(rawElse, 1);
142
+ currentPtr = elseBlock.end + 1;
143
+ break;
144
+ } else {
145
+ return null;
146
+ }
147
+ } else {
105
148
  break;
106
149
  }
107
- i += 4;
108
- i = consumeWhitespace(str, i);
109
- if (str.startsWith("if", i)) {
110
- continue;
111
- }
112
- if (str[i] !== "{") return null;
113
- const elseBlock = parseBlock(str, i);
114
- if (!elseBlock) return null;
115
- elseContent = str.substring(elseBlock.start + 1, elseBlock.end);
116
- i = elseBlock.end + 1;
117
- break;
118
- }
119
- const end = i;
150
+ }
151
+ let endIdx = currentPtr;
152
+ if (!isBare) {
153
+ endIdx = consumeWhitespace(code, endIdx);
154
+ if (code[endIdx] !== "}") return null;
155
+ endIdx++;
156
+ }
120
157
  let expr = "";
121
158
  for (let idx = 0; idx < cases.length; idx++) {
122
159
  const c = cases[idx];
123
- const body = `<Fragment>${c.content}</Fragment>`;
124
- if (idx === 0) {
125
- expr = `(${c.cond}) ? (${body}) : `;
126
- } else {
127
- expr += `(${c.cond}) ? (${body}) : `;
160
+ let cond = c.cond.trim();
161
+ const isSimplePath = /^[A-Za-z_$][\w$]*(\.[A-Za-z_$][\w$]*)*$/.test(cond);
162
+ if (isSimplePath && !cond.endsWith(")")) {
163
+ cond = `((typeof (${cond}) === 'function' && typeof (${cond}).peek === 'function' && ('value' in (${cond}))) ? (${cond})() : (${cond}))`;
128
164
  }
165
+ const body = `<Fragment>${c.content}</Fragment>`;
166
+ expr += `(${cond}) ? (${body}) : `;
129
167
  }
130
- if (elseContent !== null) {
131
- expr += `(<Fragment>${elseContent}</Fragment>)`;
132
- } else {
133
- expr += "null";
134
- }
135
- const replacement = `(() => ${expr})`;
136
- return { start: ifIndex, end, replacement };
168
+ expr += elseContent ? `(<Fragment>${elseContent}</Fragment>)` : "null";
169
+ return { end: endIdx, replacement: `{(() => ${expr})}` };
137
170
  }
138
- function parseIfStatement(str, ifIndex) {
139
- if (!str.startsWith("if", ifIndex)) return null;
140
- const chain = parseIfChain(str, ifIndex);
141
- if (!chain) return null;
142
- return {
143
- start: chain.start,
144
- end: chain.end,
145
- replacement: `{${chain.replacement}}`
146
- };
171
+ __name(handleIf, "handleIf");
172
+ function handleFor(currI, isBare = false) {
173
+ let ptr = currI;
174
+ if (!isBare) ptr = consumeWhitespace(code, currI + 1);
175
+ if (!code.startsWith("for", ptr)) return null;
176
+ ptr += 3;
177
+ ptr = consumeWhitespace(code, ptr);
178
+ const condRes = extractCondition(code, ptr);
179
+ if (!condRes) return null;
180
+ const forCond = condRes.cond;
181
+ const inMatch = forCond.match(/^\s*(\S+)\s+in\s+(.+)$/);
182
+ if (!inMatch) return null;
183
+ const item = inMatch[1].trim();
184
+ const list = inMatch[2].trim();
185
+ ptr = consumeWhitespace(code, condRes.end);
186
+ if (code[ptr] !== "{") return null;
187
+ const block = parseBlock(code, ptr);
188
+ if (!block) return null;
189
+ const rawContent = code.substring(block.start + 1, block.end);
190
+ const transformedContent = transform(rawContent, 1);
191
+ let endIdx = block.end + 1;
192
+ if (!isBare) {
193
+ endIdx = consumeWhitespace(code, endIdx);
194
+ if (code[endIdx] !== "}") return null;
195
+ endIdx++;
196
+ }
197
+ const replacement = `{(() => ${list}.map(${item} => (<Fragment>${transformedContent}</Fragment>)))}`;
198
+ return { end: endIdx, replacement };
147
199
  }
148
- function parseIfExpression(str, exprStart) {
149
- if (str[exprStart] !== "{") return null;
150
- let i = consumeWhitespace(str, exprStart + 1);
151
- if (!str.startsWith("if", i)) return null;
152
- const outer = parseBlock(str, exprStart);
153
- if (!outer) return null;
154
- const chain = parseIfChain(str, i);
155
- if (!chain) return null;
156
- return {
157
- start: exprStart,
158
- end: outer.end + 1,
159
- replacement: `{${chain.replacement}}`
160
- };
200
+ __name(handleFor, "handleFor");
201
+ function handleSwitch(currI, isBare = false) {
202
+ let ptr = currI;
203
+ if (!isBare) ptr = consumeWhitespace(code, currI + 1);
204
+ if (!code.startsWith("switch", ptr)) return null;
205
+ ptr += 6;
206
+ ptr = consumeWhitespace(code, ptr);
207
+ const condRes = extractCondition(code, ptr);
208
+ if (!condRes) return null;
209
+ const cond = condRes.cond;
210
+ ptr = consumeWhitespace(code, condRes.end);
211
+ if (code[ptr] !== "{") return null;
212
+ const block = parseBlock(code, ptr);
213
+ if (!block) return null;
214
+ const rawContent = code.substring(block.start + 1, block.end);
215
+ const transformedInner = transform(rawContent, 0);
216
+ const finalContent = transformedInner.replace(/(case\s+.*?:|default:)([\s\S]*?)(?=case\s+.*?:|default:|$)/g, (m, label, body) => {
217
+ const trimmed = body.trim();
218
+ if (!trimmed) return m;
219
+ if (trimmed.startsWith("return ")) return m;
220
+ return `${label} return (<Fragment>${body}</Fragment>);`;
221
+ });
222
+ let endIdx = block.end + 1;
223
+ if (!isBare) {
224
+ endIdx = consumeWhitespace(code, endIdx);
225
+ if (code[endIdx] !== "}") return null;
226
+ endIdx++;
227
+ }
228
+ const replacement = `{function() { __ROUND_SWITCH_TOKEN__(${cond}) { ${finalContent} } }}`;
229
+ return { end: endIdx, replacement };
161
230
  }
162
- let prev = null;
163
- while (prev !== result) {
164
- prev = result;
165
- while (true) {
166
- const match = result.match(/\{\s*if\s*\(/);
167
- if (!match) break;
168
- const matchIndex = match.index;
169
- const parsed = parseIfExpression(result, matchIndex);
170
- if (!parsed) {
171
- console.warn("Unbalanced IF expression found, skipping transformation.");
172
- break;
231
+ __name(handleSwitch, "handleSwitch");
232
+ let inSingle = false, inDouble = false, inTemplate = false;
233
+ let inCommentLine = false, inCommentMulti = false;
234
+ while (i < code.length) {
235
+ const ch = code[i];
236
+ const next = i < code.length - 1 ? code[i + 1] : "";
237
+ const prev = i > 0 ? code[i - 1] : "";
238
+ if (inCommentLine) {
239
+ result += ch;
240
+ if (ch === "\n" || ch === "\r") inCommentLine = false;
241
+ i++;
242
+ continue;
243
+ }
244
+ if (inCommentMulti) {
245
+ result += ch;
246
+ if (ch === "*" && next === "/") {
247
+ inCommentMulti = false;
248
+ result += "/";
249
+ i += 2;
250
+ continue;
173
251
  }
174
- const before = result.substring(0, parsed.start);
175
- const after = result.substring(parsed.end);
176
- result = before + parsed.replacement + after;
252
+ i++;
253
+ continue;
177
254
  }
178
- while (true) {
179
- const match = result.match(/(^|[\n\r])\s*if\s*\(/m);
180
- if (!match) break;
181
- const ifIndex = match.index + match[0].lastIndexOf("if");
182
- const parsed = parseIfStatement(result, ifIndex);
183
- if (!parsed) break;
184
- const before = result.substring(0, parsed.start);
185
- const after = result.substring(parsed.end);
186
- result = before + parsed.replacement + after;
255
+ if (inTemplate) {
256
+ result += ch;
257
+ if (ch === "`" && prev !== "\\") inTemplate = false;
258
+ i++;
259
+ continue;
187
260
  }
188
- while (true) {
189
- const match = result.match(/\{\s*for\s*\((.*?)\s+in\s+(.*?)\)\s*\{/);
190
- if (!match) break;
191
- const item = match[1];
192
- const list = match[2];
193
- const exprStart = match.index;
194
- const outer = parseBlock(result, exprStart);
195
- if (!outer) break;
196
- let i = consumeWhitespace(result, exprStart + 1);
197
- const head = result.slice(i);
198
- const mm = head.match(/^for\s*\((.*?)\s+in\s+(.*?)\)\s*\{/);
199
- if (!mm) break;
200
- const forStart = i;
201
- const blockStart = forStart + mm[0].length - 1;
202
- const block = parseBlock(result, blockStart);
203
- if (!block) break;
204
- const content = result.substring(block.start + 1, block.end);
205
- const replacement = `{(() => ${list}.map(${item} => (<Fragment>${content}</Fragment>)))}`;
206
- const before = result.substring(0, exprStart);
207
- const after = result.substring(outer.end + 1);
208
- result = before + replacement + after;
261
+ if (inSingle) {
262
+ result += ch;
263
+ if (ch === "'" && prev !== "\\") inSingle = false;
264
+ i++;
265
+ continue;
209
266
  }
210
- while (true) {
211
- const match = result.match(/(^|[\n\r])\s*for\s*\((.*?)\s+in\s+(.*?)\)\s*\{/m);
212
- if (!match) break;
213
- const exprStart = match.index + match[0].lastIndexOf("for");
214
- const item = match[2];
215
- const list = match[3];
216
- const forHead = result.slice(exprStart);
217
- const mm = forHead.match(/^for\s*\((.*?)\s+in\s+(.*?)\)\s*\{/);
218
- if (!mm) break;
219
- const blockStart = exprStart + mm[0].length - 1;
220
- const block = parseBlock(result, blockStart);
221
- if (!block) break;
222
- const content = result.substring(block.start + 1, block.end);
223
- const replacement = `{(() => ${list}.map(${item} => (<Fragment>${content}</Fragment>)))}`;
224
- const before = result.substring(0, exprStart);
225
- const after = result.substring(block.end + 1);
226
- result = before + replacement + after;
267
+ if (inDouble) {
268
+ result += ch;
269
+ if (ch === '"' && prev !== "\\") inDouble = false;
270
+ i++;
271
+ continue;
227
272
  }
228
- while (true) {
229
- const match = result.match(/\{\s*switch\s*\(/);
230
- if (!match) break;
231
- const exprStart = match.index;
232
- const outer = parseBlock(result, exprStart);
233
- if (!outer) break;
234
- let i = consumeWhitespace(result, exprStart + 1);
235
- const head = result.slice(i);
236
- const mm = head.match(/^switch\s*\((.*?)\)\s*\{/);
237
- if (!mm) break;
238
- const cond = mm[1];
239
- const blockStart = i + mm[0].length - 1;
240
- const block = parseBlock(result, blockStart);
241
- if (!block) break;
242
- const content = result.substring(block.start + 1, block.end);
243
- const transformedContent = content.replace(/(case\s+.*?:|default:)([\s\S]*?)(?=case\s+.*?:|default:|$)/g, (m, label, body) => {
244
- const trimmedBody = body.trim();
245
- if (!trimmedBody) return m;
246
- if (trimmedBody.startsWith("return ")) return m;
247
- return `${label} return (<Fragment>${body}</Fragment>);`;
248
- });
249
- const replacement = `{(() => { __ROUND_SWITCH__(${cond}) { ${transformedContent} } })}`;
250
- const before = result.substring(0, exprStart);
251
- const after = result.substring(outer.end + 1);
252
- result = before + replacement + after;
273
+ if (ch === "/" && next === "/") {
274
+ inCommentLine = true;
275
+ result += "//";
276
+ i += 2;
277
+ continue;
253
278
  }
254
- while (true) {
255
- const match = result.match(/(^|[\n\r])\s*switch\s*\(/m);
256
- if (!match) break;
257
- const switchStart = match.index + match[0].lastIndexOf("switch");
258
- const head = result.slice(switchStart);
259
- const mm = head.match(/^switch\s*\((.*?)\)\s*\{/);
260
- if (!mm) break;
261
- const cond = mm[1];
262
- const blockStart = switchStart + mm[0].length - 1;
263
- const block = parseBlock(result, blockStart);
264
- if (!block) break;
265
- const content = result.substring(block.start + 1, block.end);
266
- const transformedContent = content.replace(/(case\s+.*?:|default:)([\s\S]*?)(?=case\s+.*?:|default:|$)/g, (m, label, body) => {
267
- const trimmedBody = body.trim();
268
- if (!trimmedBody) return m;
269
- if (trimmedBody.startsWith("return ")) return m;
270
- return `${label} return (<Fragment>${body}</Fragment>);`;
271
- });
272
- const replacement = `{(() => { __ROUND_SWITCH__(${cond}) { ${transformedContent} } })}`;
273
- const before = result.substring(0, switchStart);
274
- const after = result.substring(block.end + 1);
275
- result = before + replacement + after;
279
+ if (ch === "/" && next === "*") {
280
+ inCommentMulti = true;
281
+ result += "/*";
282
+ i += 2;
283
+ continue;
284
+ }
285
+ if (ch === "`") {
286
+ inTemplate = true;
287
+ result += ch;
288
+ i++;
289
+ continue;
290
+ }
291
+ if (ch === "'") {
292
+ inSingle = true;
293
+ result += ch;
294
+ i++;
295
+ continue;
276
296
  }
297
+ if (ch === '"') {
298
+ inDouble = true;
299
+ result += ch;
300
+ i++;
301
+ continue;
302
+ }
303
+ if (ch === "<") {
304
+ const isTag = /[a-zA-Z0-9_$]/.test(next) || next === ">";
305
+ if (isTag) jsxDepth++;
306
+ }
307
+ if (ch === "<" && next === "/") {
308
+ if (jsxDepth > 0) jsxDepth--;
309
+ }
310
+ if (ch === "/" && next === ">") {
311
+ if (jsxDepth > 0) jsxDepth--;
312
+ }
313
+ if (jsxDepth > 0) {
314
+ let processed = false;
315
+ if (ch === "{") {
316
+ let ptr = consumeWhitespace(code, i + 1);
317
+ if (code.startsWith("if", ptr)) {
318
+ const res = handleIf(i, false);
319
+ if (res) {
320
+ result += res.replacement;
321
+ i = res.end;
322
+ processed = true;
323
+ }
324
+ } else if (code.startsWith("for", ptr)) {
325
+ const res = handleFor(i, false);
326
+ if (res) {
327
+ result += res.replacement;
328
+ i = res.end;
329
+ processed = true;
330
+ }
331
+ } else if (code.startsWith("switch", ptr)) {
332
+ const res = handleSwitch(i, false);
333
+ if (res) {
334
+ result += res.replacement;
335
+ i = res.end;
336
+ processed = true;
337
+ }
338
+ }
339
+ } else if (ch === "i" && code.startsWith("if", i)) {
340
+ let ptr = consumeWhitespace(code, i + 2);
341
+ if (code[ptr] === "(") {
342
+ const res = handleIf(i, true);
343
+ if (res) {
344
+ result += res.replacement;
345
+ i = res.end;
346
+ processed = true;
347
+ }
348
+ }
349
+ } else if (ch === "f" && code.startsWith("for", i)) {
350
+ let ptr = consumeWhitespace(code, i + 3);
351
+ if (code[ptr] === "(") {
352
+ const res = handleFor(i, true);
353
+ if (res) {
354
+ result += res.replacement;
355
+ i = res.end;
356
+ processed = true;
357
+ }
358
+ }
359
+ } else if (ch === "s" && code.startsWith("switch", i)) {
360
+ let ptr = consumeWhitespace(code, i + 6);
361
+ if (code[ptr] === "(") {
362
+ const res = handleSwitch(i, true);
363
+ if (res) {
364
+ result += res.replacement;
365
+ i = res.end;
366
+ processed = true;
367
+ }
368
+ }
369
+ }
370
+ if (processed) continue;
371
+ }
372
+ result += ch;
373
+ i++;
277
374
  }
278
375
  function findJsxTagEnd(str, startIndex) {
279
- let inSingle = false;
280
- let inDouble = false;
281
- let inTemplate = false;
376
+ let inSingle2 = false, inDouble2 = false, inTemplate2 = false;
282
377
  let braceDepth = 0;
283
- for (let i = startIndex; i < str.length; i++) {
284
- const ch = str[i];
285
- const prevCh = i > 0 ? str[i - 1] : "";
286
- if (!inDouble && !inTemplate && ch === "'" && prevCh !== "\\") inSingle = !inSingle;
287
- else if (!inSingle && !inTemplate && ch === '"' && prevCh !== "\\") inDouble = !inDouble;
288
- else if (!inSingle && !inDouble && ch === "`" && prevCh !== "\\") inTemplate = !inTemplate;
289
- if (inSingle || inDouble || inTemplate) continue;
290
- if (ch === "{") braceDepth++;
291
- else if (ch === "}") braceDepth = Math.max(0, braceDepth - 1);
292
- else if (ch === ">" && braceDepth === 0) return i;
378
+ for (let k = startIndex; k < str.length; k++) {
379
+ const c = str[k];
380
+ const p = k > 0 ? str[k - 1] : "";
381
+ if (!inDouble2 && !inTemplate2 && c === "'" && p !== "\\") inSingle2 = !inSingle2;
382
+ else if (!inSingle2 && !inTemplate2 && c === '"' && p !== "\\") inDouble2 = !inDouble2;
383
+ else if (!inSingle2 && !inDouble2 && c === "`" && p !== "\\") inTemplate2 = !inTemplate2;
384
+ if (inSingle2 || inDouble2 || inTemplate2) continue;
385
+ if (c === "{") braceDepth++;
386
+ else if (c === "}") braceDepth = Math.max(0, braceDepth - 1);
387
+ else if (c === ">" && braceDepth === 0) return k;
293
388
  }
294
389
  return -1;
295
390
  }
391
+ __name(findJsxTagEnd, "findJsxTagEnd");
296
392
  function transformSuspenseBlocks(str) {
297
393
  let out = str;
298
394
  let cursor = 0;
@@ -306,41 +402,29 @@ function transform(code) {
306
402
  cursor = openEnd + 1;
307
403
  continue;
308
404
  }
309
- let depth = 1;
310
- let i = openEnd + 1;
311
- let closeStart = -1;
312
- while (i < out.length) {
313
- const nextOpen = out.indexOf("<Suspense", i);
314
- const nextClose = out.indexOf("</Suspense>", i);
315
- if (nextClose === -1) break;
316
- if (nextOpen !== -1 && nextOpen < nextClose) {
317
- const innerOpenEnd = findJsxTagEnd(out, nextOpen);
318
- if (innerOpenEnd === -1) break;
319
- const innerOpenText = out.slice(nextOpen, innerOpenEnd + 1);
320
- if (!/\/>\s*$/.test(innerOpenText)) depth++;
321
- i = innerOpenEnd + 1;
322
- continue;
323
- }
324
- depth--;
325
- if (depth === 0) {
326
- closeStart = nextClose;
327
- break;
328
- }
329
- i = nextClose + "</Suspense>".length;
405
+ let depth = 1, k = openEnd + 1, closeStart = -1;
406
+ while (k < out.length) {
407
+ if (out.slice(k).startsWith("<Suspense")) {
408
+ depth++;
409
+ k += 9;
410
+ } else if (out.slice(k).startsWith("</Suspense>")) {
411
+ depth--;
412
+ if (depth === 0) {
413
+ closeStart = k;
414
+ break;
415
+ }
416
+ k += 11;
417
+ } else k++;
330
418
  }
331
419
  if (closeStart === -1) break;
332
420
  const inner = out.slice(openEnd + 1, closeStart);
333
- const innerTrim = inner.trim();
334
- if (innerTrim.startsWith("{() =>")) {
335
- cursor = closeStart + "</Suspense>".length;
336
- continue;
337
- }
338
- const wrapped = `{() => (<Fragment>${inner}</Fragment>)}`;
421
+ const wrapped = `{(() => (<Fragment>${inner}</Fragment>))}`;
339
422
  out = out.slice(0, openEnd + 1) + wrapped + out.slice(closeStart);
340
- cursor = closeStart + wrapped.length + "</Suspense>".length;
423
+ cursor = closeStart + wrapped.length + 11;
341
424
  }
342
425
  return out;
343
426
  }
427
+ __name(transformSuspenseBlocks, "transformSuspenseBlocks");
344
428
  function transformProviderBlocks(str) {
345
429
  let out = str;
346
430
  let cursor = 0;
@@ -351,75 +435,73 @@ function transform(code) {
351
435
  if (lt === -1) break;
352
436
  const openEnd = findJsxTagEnd(out, lt);
353
437
  if (openEnd === -1) break;
354
- const openTagText = out.slice(lt, openEnd + 1);
355
- if (/\/>\s*$/.test(openTagText)) {
438
+ const tagText = out.slice(lt, openEnd + 1);
439
+ if (/\/>\s*$/.test(tagText)) {
356
440
  cursor = openEnd + 1;
357
441
  continue;
358
442
  }
359
- const m = openTagText.match(/^<\s*([A-Za-z_$][\w$]*\.Provider)\b/);
443
+ const m = tagText.match(/^<\s*([A-Za-z_$][\w$]*\.Provider)\b/);
360
444
  if (!m) {
361
445
  cursor = openEnd + 1;
362
446
  continue;
363
447
  }
364
448
  const tagName = m[1];
365
449
  const closeTag = `</${tagName}>`;
366
- let depth = 1;
367
- let i = openEnd + 1;
368
- let closeStart = -1;
369
- while (i < out.length) {
370
- const nextOpen = out.indexOf(`<${tagName}`, i);
371
- const nextClose = out.indexOf(closeTag, i);
372
- if (nextClose === -1) break;
373
- if (nextOpen !== -1 && nextOpen < nextClose) {
374
- const innerOpenEnd = findJsxTagEnd(out, nextOpen);
375
- if (innerOpenEnd === -1) break;
376
- const innerOpenText = out.slice(nextOpen, innerOpenEnd + 1);
377
- if (!/\/>\s*$/.test(innerOpenText)) depth++;
378
- i = innerOpenEnd + 1;
450
+ let depth = 1, k = openEnd + 1, closeStart = -1;
451
+ while (k < out.length) {
452
+ const nOpen = out.indexOf(`<${tagName}`, k);
453
+ const nClose = out.indexOf(closeTag, k);
454
+ if (nClose === -1) break;
455
+ if (nOpen !== -1 && nOpen < nClose) {
456
+ const innerEnd = findJsxTagEnd(out, nOpen);
457
+ if (innerEnd !== -1 && !/\/>\s*$/.test(out.slice(nOpen, innerEnd + 1))) depth++;
458
+ k = innerEnd + 1;
379
459
  continue;
380
460
  }
381
461
  depth--;
382
462
  if (depth === 0) {
383
- closeStart = nextClose;
463
+ closeStart = nClose;
384
464
  break;
385
465
  }
386
- i = nextClose + closeTag.length;
466
+ k = nClose + closeTag.length;
387
467
  }
388
468
  if (closeStart === -1) break;
389
469
  const inner = out.slice(openEnd + 1, closeStart);
390
- const innerTrim = inner.trim();
391
- if (innerTrim.startsWith("{() =>")) {
392
- cursor = closeStart + closeTag.length;
393
- continue;
394
- }
395
- const wrapped = `{() => (<Fragment>${inner}</Fragment>)}`;
470
+ const wrapped = `{(() => (<Fragment>${inner}</Fragment>))}`;
396
471
  out = out.slice(0, openEnd + 1) + wrapped + out.slice(closeStart);
397
472
  cursor = closeStart + wrapped.length + closeTag.length;
398
473
  }
399
474
  return out;
400
475
  }
476
+ __name(transformProviderBlocks, "transformProviderBlocks");
401
477
  result = transformSuspenseBlocks(result);
402
478
  result = transformProviderBlocks(result);
403
479
  result = result.replace(/\{\s*([A-Za-z_$][\w$]*)\s*\(\s*\)\s*\}/g, "{() => $1()}").replace(/=\{\s*([A-Za-z_$][\w$]*)\s*\(\s*\)\s*\}/g, "={() => $1()}");
404
- return result.replace(/__ROUND_SWITCH__/g, "switch");
480
+ return result.replace(/__ROUND_SWITCH_TOKEN__/g, "switch");
405
481
  }
482
+ __name(transform, "transform");
406
483
  function normalizePath(p) {
407
484
  return p.replaceAll("\\", "/");
408
485
  }
486
+ __name(normalizePath, "normalizePath");
409
487
  function isMdRawRequest(id) {
410
488
  return typeof id === "string" && id.includes(".md") && id.includes("?raw");
411
489
  }
490
+ __name(isMdRawRequest, "isMdRawRequest");
412
491
  function stripQuery(id) {
413
492
  return id.split("?")[0];
414
493
  }
494
+ __name(stripQuery, "stripQuery");
415
495
  function escapeForJsString(s) {
416
496
  return String(s).replaceAll("\\", "\\\\").replaceAll("`", "\\`").replaceAll("${", "\\${");
417
497
  }
498
+ __name(escapeForJsString, "escapeForJsString");
418
499
  function resolveMaybeRelative(baseDir, p) {
419
500
  if (!p) return null;
420
501
  if (path.isAbsolute(p)) return p;
421
502
  return path.resolve(baseDir, p);
422
503
  }
504
+ __name(resolveMaybeRelative, "resolveMaybeRelative");
423
505
  function inlineMarkdownInRound(code, fileAbs, addWatchFile) {
424
506
  if (typeof code !== "string" || typeof fileAbs !== "string") return code;
425
507
  const dir = path.dirname(fileAbs);
@@ -447,6 +529,7 @@ ${msg}`);
447
529
  }
448
530
  });
449
531
  }
532
+ __name(inlineMarkdownInRound, "inlineMarkdownInRound");
450
533
  function isExcluded(fileAbsPath, excludeAbs) {
451
534
  const file = normalizePath(fileAbsPath);
452
535
  for (const pat of excludeAbs) {
@@ -456,6 +539,7 @@ function isExcluded(fileAbsPath, excludeAbs) {
456
539
  }
457
540
  return false;
458
541
  }
542
+ __name(isExcluded, "isExcluded");
459
543
  function isIncluded(fileAbsPath, includeAbs) {
460
544
  if (!includeAbs.length) return true;
461
545
  const file = normalizePath(fileAbsPath);
@@ -466,6 +550,7 @@ function isIncluded(fileAbsPath, includeAbs) {
466
550
  }
467
551
  return false;
468
552
  }
553
+ __name(isIncluded, "isIncluded");
469
554
  function RoundPlugin(pluginOptions = {}) {
470
555
  const state = {
471
556
  rootDir: process.cwd(),
@@ -516,6 +601,7 @@ function RoundPlugin(pluginOptions = {}) {
516
601
  state.includeAbs = Array.isArray(include) ? include.map((p) => resolveMaybeRelative(includeBase, p)).filter(Boolean) : [];
517
602
  state.excludeAbs = Array.isArray(exclude) ? exclude.map((p) => resolveMaybeRelative(excludeBase, p)).filter(Boolean) : [];
518
603
  }
604
+ __name(loadConfigOnce, "loadConfigOnce");
519
605
  function findBlock(str, startIndex) {
520
606
  let open = 0;
521
607
  let inSingle = false;
@@ -541,6 +627,7 @@ function RoundPlugin(pluginOptions = {}) {
541
627
  }
542
628
  return null;
543
629
  }
630
+ __name(findBlock, "findBlock");
544
631
  function parseStartHeadCallArgument(str, fromIndex) {
545
632
  const idx = str.indexOf("startHead", fromIndex);
546
633
  if (idx === -1) return null;
@@ -569,6 +656,7 @@ function RoundPlugin(pluginOptions = {}) {
569
656
  }
570
657
  return null;
571
658
  }
659
+ __name(parseStartHeadCallArgument, "parseStartHeadCallArgument");
572
660
  function parseStartHeadInDefaultExport(code) {
573
661
  const m = code.match(/export\s+default\s+function\b/);
574
662
  const hasAnyCall = /\bstartHead\s*\(/.test(code);
@@ -582,6 +670,7 @@ function RoundPlugin(pluginOptions = {}) {
582
670
  const call = parseStartHeadCallArgument(body, 0);
583
671
  return { headExpr: call ? call.arg : null, hasAny: hasAnyCall, hasOutside: hasAnyCall && !call };
584
672
  }
673
+ __name(parseStartHeadInDefaultExport, "parseStartHeadInDefaultExport");
585
674
  function headToHtml(head) {
586
675
  if (!head || typeof head !== "object") return "";
587
676
  let out = "";
@@ -591,10 +680,10 @@ function RoundPlugin(pluginOptions = {}) {
591
680
  }
592
681
  const meta = head.meta;
593
682
  const links = head.links;
594
- const renderAttrs = (attrs) => {
683
+ const renderAttrs = /* @__PURE__ */ __name((attrs) => {
595
684
  if (!attrs || typeof attrs !== "object") return "";
596
685
  return Object.entries(attrs).filter(([, v]) => v !== null && v !== void 0).map(([k, v]) => ` ${k}="${String(v).replaceAll('"', "&quot;")}"`).join("");
597
- };
686
+ }, "renderAttrs");
598
687
  if (Array.isArray(meta)) {
599
688
  meta.forEach((m) => {
600
689
  if (!m) return;
@@ -627,6 +716,7 @@ ${head.raw}`;
627
716
  }
628
717
  return out;
629
718
  }
719
+ __name(headToHtml, "headToHtml");
630
720
  return {
631
721
  name: "vite-plugin-round",
632
722
  enforce: "pre",
@@ -787,6 +877,7 @@ Found: ${trimmed.slice(0, 60)}...`));
787
877
  }
788
878
  };
789
879
  }
880
+ __name(RoundPlugin, "RoundPlugin");
790
881
  export {
791
882
  RoundPlugin as default
792
883
  };