olum-compiler 1.8.0 → 1.8.3

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,7 @@
1
- const { isFullArr, isUpper } = require("./helpers");
2
-
3
1
  const ESC = "<-olum->";
4
2
 
3
+ const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
4
+
5
5
  function literalExpr(raw) {
6
6
  if (/\{[^{}]+\}/.test(raw)) {
7
7
  const body = raw.replace(/`/g, "\\`").replace(/\{([^{}]+)\}/g, "${$1}");
@@ -103,31 +103,111 @@ function buildPropsAttr(attrsStr, parentMethods, propsAliases) {
103
103
  "})).replace(/'/g,'%27')}";
104
104
  attr += ' data-o-props="-[[-' + expr + '-]]-"';
105
105
  }
106
- if (srcEntries.length)
106
+ if (srcEntries.length) {
107
107
  attr += ' data-o-props-src="' + srcEntries.join("|") + '"';
108
+
109
+ attr += ' data-o-props-owner="-[[-$' + ESC + '_storeKey}-]]-"';
110
+ }
108
111
  return attr;
109
112
  }
110
113
 
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
- }
114
+ const ATTRS = "(?:\"[^\"]*\"|'[^']*'|[^\"'>])*?";
115
+
116
+ const nextComponent = /<([A-Z][\w$]*)(?=[\s/>])/g;
117
+
118
+ const tagCache = new Map();
119
+
120
+ function tagsFor(name) {
121
+ let tags = tagCache.get(name);
122
+ if (!tags) {
123
+ const safe = escapeRegExp(name);
124
+ const open = "<" + safe + "(?=[\\s/>])" + ATTRS + "(/)?>";
125
+ tags = {
126
+ head: new RegExp(open, "y"),
127
+ open: new RegExp(open, "g"),
128
+ close: new RegExp("</" + safe + "\\s*>", "g"),
129
+ };
130
+ tagCache.set(name, tags);
131
+ }
132
+ return tags;
133
+ }
134
+
135
+ function findClose(content, name, from) {
136
+ const { open, close } = tagsFor(name);
137
+ let depth = 1;
138
+ let i = from;
139
+
140
+ for (;;) {
141
+ open.lastIndex = close.lastIndex = i;
142
+ const nextOpen = open.exec(content);
143
+ const nextClose = close.exec(content);
144
+ if (!nextClose) return null;
145
+
146
+ if (nextOpen && nextOpen.index < nextClose.index) {
147
+ if (!nextOpen[1]) depth++;
148
+ i = nextOpen.index + nextOpen[0].length;
149
+ continue;
129
150
  }
130
- });
151
+
152
+ i = nextClose.index + nextClose[0].length;
153
+ if (--depth === 0) return { start: nextClose.index, end: i };
154
+ }
155
+ }
156
+
157
+ function olumTag(name, attrs, slot, parentMethods, propsAliases) {
158
+ const props = attrs.trim()
159
+ ? buildPropsAttr(attrs, parentMethods, propsAliases)
160
+ : "";
161
+ return "<olum name='" + name + "'" + props + ">" + slot + "</olum>";
162
+ }
163
+
164
+ function placeholder(content, parentMethods, propsAliases) {
165
+ let out = "";
166
+ let i = 0;
167
+
168
+ while (i < content.length) {
169
+ nextComponent.lastIndex = i;
170
+ const found = nextComponent.exec(content);
171
+ if (!found) break;
172
+
173
+ const start = found.index;
174
+ const name = found[1];
175
+ const { head } = tagsFor(name);
176
+ head.lastIndex = start;
177
+ const open = head.exec(content);
178
+
179
+ if (!open) {
180
+ out += content.slice(i, start + 1);
181
+ i = start + 1;
182
+ continue;
183
+ }
184
+
185
+ out += content.slice(i, start);
186
+ const selfClosing = !!open[1];
187
+ const attrs = open[0].slice(1 + name.length, selfClosing ? -2 : -1);
188
+ i = start + open[0].length;
189
+
190
+ if (selfClosing) {
191
+ out += olumTag(name, attrs, "", parentMethods, propsAliases);
192
+ continue;
193
+ }
194
+
195
+ const close = findClose(content, name, i);
196
+ if (!close) {
197
+ out += open[0];
198
+ continue;
199
+ }
200
+
201
+ const slot = placeholder(
202
+ content.slice(i, close.start),
203
+ parentMethods,
204
+ propsAliases,
205
+ );
206
+ out += olumTag(name, attrs, slot, parentMethods, propsAliases);
207
+ i = close.end;
208
+ }
209
+
210
+ return out + content.slice(i);
131
211
  }
132
212
 
133
213
  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.3",
4
4
  "description": "A module for compiling/bundling olumjs projects",
5
5
  "main": "bin/index.js",
6
6
  "bin": {
@@ -453,6 +453,120 @@ 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(tmpl(out)) &&
541
+ /<olum name="Badge"><\/olum>/.test(tmpl(out)) &&
542
+ /<olum name="Button">Second<\/olum>/.test(tmpl(out)) &&
543
+ parses(out),
544
+ );
545
+
546
+ check(
547
+ "attributes split across lines and indented with tabs keep the component name intact",
548
+ comp(
549
+ `<div>\n\t<Foo\n\t\tbar="1"\n\t\tbaz="2">x</Foo>\n</div>`,
550
+ `import Foo from "./Foo";`,
551
+ ),
552
+ (out) =>
553
+ /<olum name="Foo" data-o-props=/.test(tmpl(out)) &&
554
+ /bar: '1', baz: '2'/.test(tmpl(out)) &&
555
+ parses(out),
556
+ );
557
+
558
+ check(
559
+ "an unbalanced component tag is left alone, but the components inside it still convert",
560
+ comp(
561
+ `<div><Foo>a<Bar>b</Bar></div>`,
562
+ `import Foo from "./Foo";\nimport Bar from "./Bar";`,
563
+ ),
564
+ (out) =>
565
+ !/<olum name="Foo"/.test(tmpl(out)) &&
566
+ /<olum name="Bar">b<\/olum>/.test(tmpl(out)) &&
567
+ parses(out),
568
+ );
569
+
456
570
  section("§11b Function props");
457
571
 
458
572
  check(
@@ -918,7 +1032,7 @@ if (failed) {
918
1032
  console.log("");
919
1033
  }
920
1034
 
921
- const EXPECTED_CHECKS = 81;
1035
+ const EXPECTED_CHECKS = 90;
922
1036
  const total = passed + failed;
923
1037
  if (total !== EXPECTED_CHECKS) {
924
1038
  console.log(