olum-compiler 1.8.0 → 1.8.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.
@@ -0,0 +1,11 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(npm test *)",
5
+ "Bash(node *)",
6
+ "Bash(sed -i 's|require\\(\"./helpers\"\\)|require\\(\"/home/eissapk/olum/compiler/lib/helpers\"\\)|' \"/tmp/claude-1000/-home-eissapk-olum-compiler/3f440c76-2772-4edd-891c-d2dfdb356243/scratchpad/placeholder.old.js\")",
7
+ "Bash(sed \"s|lib/placeholder\\\\\"\\)|../../../../../tmp/claude-1000/-home-eissapk-olum-compiler/3f440c76-2772-4edd-891c-d2dfdb356243/scratchpad/placeholder.old.js\\\\\"\\)|\" \"/tmp/claude-1000/-home-eissapk-olum-compiler/3f440c76-2772-4edd-891c-d2dfdb356243/scratchpad/probe.js\")",
8
+ "Bash(echo \"--- exit: $? ---\")"
9
+ ]
10
+ }
11
+ }
package/lib/htmlParser.js CHANGED
@@ -1,5 +1,4 @@
1
1
  const { parseDOM } = require("./dom");
2
- const { selfClosingTag, normalTag } = require("./regex");
3
2
  const placeholder = require("./placeholder");
4
3
  const handleIfStatement = require("./if");
5
4
  const handleForStatement = require("./for");
@@ -23,20 +22,7 @@ const {
23
22
  } = require("./tokens");
24
23
 
25
24
  function htmlparser(content, counter, filePath, parentMethods, propsAliases) {
26
- content = placeholder(
27
- content,
28
- selfClosingTag,
29
- "self",
30
- parentMethods,
31
- propsAliases,
32
- );
33
- content = placeholder(
34
- content,
35
- normalTag,
36
- "normal",
37
- parentMethods,
38
- propsAliases,
39
- );
25
+ content = placeholder(content, parentMethods, propsAliases);
40
26
 
41
27
  const anonHandlers = [];
42
28
 
@@ -1,7 +1,8 @@
1
- const { isFullArr, isUpper } = require("./helpers");
2
-
3
1
  const ESC = "<-olum->";
4
2
 
3
+ // Component names can contain `$`, which is special inside a regex.
4
+ const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
5
+
5
6
  function literalExpr(raw) {
6
7
  if (/\{[^{}]+\}/.test(raw)) {
7
8
  const body = raw.replace(/`/g, "\\`").replace(/\{([^{}]+)\}/g, "${$1}");
@@ -108,26 +109,131 @@ function buildPropsAttr(attrsStr, parentMethods, propsAliases) {
108
109
  return attr;
109
110
  }
110
111
 
111
- function placeholder(content, regex, type, parentMethods, propsAliases) {
112
- return content.replace(regex, (str, $1, $2) => {
113
- let data = $1.split(" ");
114
- if (isFullArr(data)) {
115
- let name = data[0].trim();
116
- if (isUpper(name[0])) {
117
- name = "name='" + name + "'";
118
- const propsAttr =
119
- data.length > 1
120
- ? buildPropsAttr(
121
- data.slice(1).join(" "),
122
- parentMethods,
123
- propsAliases,
124
- )
125
- : "";
126
- const slot = type === "normal" ? $2 : "";
127
- return "<olum " + name + propsAttr + ">" + slot + "</olum>";
128
- }
112
+ /* ---------------------------------------------------------------------------
113
+ * Component tags
114
+ *
115
+ * A component is a PascalCase tag -- <Card>...</Card> or <Card />. Everything
116
+ * else is plain HTML and is copied through untouched.
117
+ *
118
+ * Tags are scanned left to right instead of being matched with one global
119
+ * regex, because a regex cannot pair <Card> with its OWN </Card>: it stops at
120
+ * the first uppercase close tag it meets. That breaks the moment a component
121
+ * wraps another one --
122
+ *
123
+ * <Spinner><Spinner></Spinner></Spinner> nested under the same name
124
+ * <KbdGroup><Kbd/><Kbd/></KbdGroup> more than one child
125
+ * <Card><CardHeader/></Card> child name starts with the parent's
126
+ * ------------------------------------------------------------------------- */
127
+
128
+ // Attributes between the tag name and its `>`. Quoted values are consumed
129
+ // whole so that a `>` inside one doesn't end the tag early.
130
+ const ATTRS = "(?:\"[^\"]*\"|'[^']*'|[^\"'>])*?";
131
+
132
+ // Names must be valid JS identifiers -- parser.js emits them straight into the
133
+ // component map as `Card: Card`.
134
+ const nextComponent = /<([A-Z][\w$]*)(?=[\s/>])/g;
135
+
136
+ const tagCache = new Map();
137
+
138
+ // `head` matches an open tag at a known offset; `open` and `close` search
139
+ // forward from one. Group 1 of `head`/`open` is set when the tag self-closes.
140
+ function tagsFor(name) {
141
+ let tags = tagCache.get(name);
142
+ if (!tags) {
143
+ const safe = escapeRegExp(name);
144
+ const open = "<" + safe + "(?=[\\s/>])" + ATTRS + "(/)?>";
145
+ tags = {
146
+ head: new RegExp(open, "y"),
147
+ open: new RegExp(open, "g"),
148
+ close: new RegExp("</" + safe + "\\s*>", "g"),
149
+ };
150
+ tagCache.set(name, tags);
151
+ }
152
+ return tags;
153
+ }
154
+
155
+ // Locates the `</name>` closing the tag that ends at `from`, depth-tracking
156
+ // same-name tags on the way so a component pairs with its own close tag.
157
+ // Returns null when there is none, i.e. the markup is unbalanced.
158
+ function findClose(content, name, from) {
159
+ const { open, close } = tagsFor(name);
160
+ let depth = 1;
161
+ let i = from;
162
+
163
+ for (;;) {
164
+ open.lastIndex = close.lastIndex = i;
165
+ const nextOpen = open.exec(content);
166
+ const nextClose = close.exec(content);
167
+ if (!nextClose) return null;
168
+
169
+ if (nextOpen && nextOpen.index < nextClose.index) {
170
+ if (!nextOpen[1]) depth++; // a nested twin, and not self-closing
171
+ i = nextOpen.index + nextOpen[0].length;
172
+ continue;
173
+ }
174
+
175
+ i = nextClose.index + nextClose[0].length;
176
+ if (--depth === 0) return { start: nextClose.index, end: i };
177
+ }
178
+ }
179
+
180
+ function olumTag(name, attrs, slot, parentMethods, propsAliases) {
181
+ const props = attrs.trim()
182
+ ? buildPropsAttr(attrs, parentMethods, propsAliases)
183
+ : "";
184
+ return "<olum name='" + name + "'" + props + ">" + slot + "</olum>";
185
+ }
186
+
187
+ function placeholder(content, parentMethods, propsAliases) {
188
+ let out = "";
189
+ let i = 0;
190
+
191
+ while (i < content.length) {
192
+ nextComponent.lastIndex = i;
193
+ const found = nextComponent.exec(content);
194
+ if (!found) break;
195
+
196
+ const start = found.index;
197
+ const name = found[1];
198
+ const { head } = tagsFor(name);
199
+ head.lastIndex = start;
200
+ const open = head.exec(content);
201
+
202
+ // `<Card` with no `>` after it isn't a tag -- step over the `<` and go on.
203
+ if (!open) {
204
+ out += content.slice(i, start + 1);
205
+ i = start + 1;
206
+ continue;
129
207
  }
130
- });
208
+
209
+ out += content.slice(i, start);
210
+ const selfClosing = !!open[1];
211
+ const attrs = open[0].slice(1 + name.length, selfClosing ? -2 : -1);
212
+ i = start + open[0].length;
213
+
214
+ if (selfClosing) {
215
+ out += olumTag(name, attrs, "", parentMethods, propsAliases);
216
+ continue;
217
+ }
218
+
219
+ const close = findClose(content, name, i);
220
+ if (!close) {
221
+ // Unbalanced: emit the open tag verbatim and keep scanning its content,
222
+ // so the components inside it are still converted.
223
+ out += open[0];
224
+ continue;
225
+ }
226
+
227
+ const slot = placeholder(
228
+ content.slice(i, close.start),
229
+ parentMethods,
230
+ propsAliases,
231
+ );
232
+ out += olumTag(name, attrs, slot, parentMethods, propsAliases);
233
+ i = close.end;
234
+ }
235
+
236
+ return out + content.slice(i);
131
237
  }
132
238
 
133
239
  module.exports = placeholder;
package/lib/regex.js CHANGED
@@ -2,7 +2,5 @@ module.exports = {
2
2
  forOf: /(.*)( of )(.*)/g,
3
3
  forIn: /(.*)( in )(.*)/g,
4
4
 
5
- selfClosingTag: /<([A-Z](?:"[^"]*"|'[^']*'|[^"'>])*?)\/>/g,
6
- normalTag: /<([A-Z](?:"[^"]*"|'[^']*'|[^"'>])*?)>([\s\S]*?)<\/[A-Z][^>]*>/g,
7
5
  compExt: /\.html$/i,
8
6
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "olum-compiler",
3
- "version": "1.8.0",
3
+ "version": "1.8.2",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {
@@ -453,6 +453,122 @@ check(
453
453
  ) && parses(out),
454
454
  );
455
455
 
456
+ section("§11a Nested components");
457
+
458
+ check(
459
+ "a component nested directly inside another of the same name (both normal pairs)",
460
+ comp(
461
+ `<Spinner><Spinner></Spinner></Spinner>`,
462
+ `import Spinner from "./Spinner";`,
463
+ ),
464
+ (out) =>
465
+ /<olum name="Spinner"><olum name="Spinner"><\/olum><\/olum>/.test(
466
+ tmpl(out),
467
+ ) && parses(out),
468
+ );
469
+
470
+ check(
471
+ "a component wrapping multiple same-named children pairs with its OWN close tag",
472
+ comp(
473
+ `<KbdGroup><Kbd>Ctrl</Kbd><Kbd>Alt</Kbd><Kbd>Del</Kbd></KbdGroup>`,
474
+ `import KbdGroup from "./KbdGroup";\nimport Kbd from "./Kbd";`,
475
+ ),
476
+ (out) =>
477
+ /<olum name="KbdGroup">\s*<olum name="Kbd">Ctrl<\/olum>\s*<olum name="Kbd">Alt<\/olum>\s*<olum name="Kbd">Del<\/olum>\s*<\/olum>/.test(
478
+ tmpl(out),
479
+ ) && parses(out),
480
+ );
481
+
482
+ check(
483
+ "a component wrapping differently-named children (each with its own props) nests correctly",
484
+ comp(
485
+ `<Card variant="outline"><CardHeader>Title</CardHeader><CardContent>Body</CardContent></Card>`,
486
+ `import Card from "./Card";\nimport CardHeader from "./CardHeader";\nimport CardContent from "./CardContent";`,
487
+ ),
488
+ (out) =>
489
+ /<olum name="Card" data-o-props/.test(tmpl(out)) &&
490
+ /<olum name="CardHeader">Title<\/olum>/.test(tmpl(out)) &&
491
+ /<olum name="CardContent">Body<\/olum>/.test(tmpl(out)) &&
492
+ parses(out),
493
+ );
494
+
495
+ check(
496
+ "a child component name that is a PREFIX of the parent's name doesn't confuse pairing (Card / CardHeader)",
497
+ comp(
498
+ `<Card><CardHeader>Hi</CardHeader></Card>`,
499
+ `import Card from "./Card";\nimport CardHeader from "./CardHeader";`,
500
+ ),
501
+ (out) =>
502
+ /<olum name="Card">\s*<olum name="CardHeader">Hi<\/olum>\s*<\/olum>/.test(
503
+ tmpl(out),
504
+ ) && parses(out),
505
+ );
506
+
507
+ check(
508
+ "same-name recursive nesting depth-tracks correctly (Tree > Tree > Tree)",
509
+ comp(
510
+ `<Tree label="root"><Tree label="child"><Tree label="grandchild" /></Tree></Tree>`,
511
+ `import Tree from "./Tree";`,
512
+ ),
513
+ (out) =>
514
+ /<olum name="Tree"[\s\S]*<olum name="Tree"[\s\S]*<olum name="Tree"[\s\S]*<\/olum>\s*<\/olum>\s*<\/olum>/.test(
515
+ tmpl(out),
516
+ ) && parses(out),
517
+ );
518
+
519
+ check(
520
+ "three levels of different-named nesting all convert (Accordion > AccordionItem > AccordionTrigger/Content)",
521
+ comp(
522
+ `<Accordion multiple="{true}"><AccordionItem value="a"><AccordionTrigger>Q</AccordionTrigger><AccordionContent>A</AccordionContent></AccordionItem></Accordion>`,
523
+ `import Accordion from "./Accordion";\nimport AccordionItem from "./AccordionItem";\nimport AccordionTrigger from "./AccordionTrigger";\nimport AccordionContent from "./AccordionContent";`,
524
+ ),
525
+ (out) =>
526
+ /<olum name="Accordion"/.test(tmpl(out)) &&
527
+ /<olum name="AccordionItem"/.test(tmpl(out)) &&
528
+ /<olum name="AccordionTrigger">Q<\/olum>/.test(tmpl(out)) &&
529
+ /<olum name="AccordionContent">A<\/olum>/.test(tmpl(out)) &&
530
+ parses(out),
531
+ );
532
+
533
+ check(
534
+ "self-closing and normal component siblings mix correctly inside plain HTML",
535
+ comp(
536
+ `<div class="row"><Button variant="outline">Click</Button><Badge /><Button>Second</Button></div>`,
537
+ `import Button from "./Button";\nimport Badge from "./Badge";`,
538
+ ),
539
+ (out) =>
540
+ /<olum name="Button" data-o-props[\s\S]*>Click<\/olum>/.test(
541
+ tmpl(out),
542
+ ) &&
543
+ /<olum name="Badge"><\/olum>/.test(tmpl(out)) &&
544
+ /<olum name="Button">Second<\/olum>/.test(tmpl(out)) &&
545
+ parses(out),
546
+ );
547
+
548
+ check(
549
+ "attributes split across lines and indented with tabs keep the component name intact",
550
+ comp(
551
+ `<div>\n\t<Foo\n\t\tbar="1"\n\t\tbaz="2">x</Foo>\n</div>`,
552
+ `import Foo from "./Foo";`,
553
+ ),
554
+ (out) =>
555
+ /<olum name="Foo" data-o-props=/.test(tmpl(out)) &&
556
+ /bar: '1', baz: '2'/.test(tmpl(out)) &&
557
+ parses(out),
558
+ );
559
+
560
+ check(
561
+ "an unbalanced component tag is left alone, but the components inside it still convert",
562
+ comp(
563
+ `<div><Foo>a<Bar>b</Bar></div>`,
564
+ `import Foo from "./Foo";\nimport Bar from "./Bar";`,
565
+ ),
566
+ (out) =>
567
+ !/<olum name="Foo"/.test(tmpl(out)) &&
568
+ /<olum name="Bar">b<\/olum>/.test(tmpl(out)) &&
569
+ parses(out),
570
+ );
571
+
456
572
  section("§11b Function props");
457
573
 
458
574
  check(
@@ -918,7 +1034,7 @@ if (failed) {
918
1034
  console.log("");
919
1035
  }
920
1036
 
921
- const EXPECTED_CHECKS = 81;
1037
+ const EXPECTED_CHECKS = 90;
922
1038
  const total = passed + failed;
923
1039
  if (total !== EXPECTED_CHECKS) {
924
1040
  console.log(