olum-compiler 1.5.0 → 1.6.0

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.
Files changed (48) hide show
  1. package/.claude/settings.local.json +2 -1
  2. package/bin/build.js +1 -1
  3. package/bin/bundle.js +32 -30
  4. package/bin/compiler.js +20 -10
  5. package/bin/copySrc.js +45 -41
  6. package/bin/dev.js +46 -39
  7. package/bin/index.js +6 -4
  8. package/bin/server.js +58 -33
  9. package/bin/watcher.js +11 -14
  10. package/bin/ws.js +16 -11
  11. package/lib/attr.js +1 -25
  12. package/lib/boolAttr.js +32 -15
  13. package/lib/chokidar.js +52 -101
  14. package/lib/colors.js +1 -7
  15. package/lib/compPlaceholder.js +5 -3
  16. package/lib/compiler.js +3 -6
  17. package/lib/coreLibs.js +1 -10
  18. package/lib/cross-spawn.js +45 -59
  19. package/lib/cssParser.js +61 -37
  20. package/lib/domEvents.js +6 -2
  21. package/lib/events.js +75 -77
  22. package/lib/for.js +56 -28
  23. package/lib/getImports.js +35 -17
  24. package/lib/getTempPath.js +8 -4
  25. package/lib/helpers.js +289 -184
  26. package/lib/html.js +3 -10
  27. package/lib/htmlParser.js +45 -45
  28. package/lib/if.js +40 -20
  29. package/lib/importMap.js +49 -24
  30. package/lib/jsParser.js +8 -13
  31. package/lib/lineCounter.js +18 -5
  32. package/lib/liveProps.js +124 -78
  33. package/lib/log.js +30 -0
  34. package/lib/magic-string.js +45 -52
  35. package/lib/models.js +8 -7
  36. package/lib/parser.js +76 -44
  37. package/lib/placeholder.js +73 -58
  38. package/lib/regex.js +4 -3
  39. package/lib/show.js +16 -8
  40. package/lib/sourcemap.js +23 -24
  41. package/lib/styles.js +7 -13
  42. package/lib/textarea.js +3 -6
  43. package/lib/toJs.js +1 -1
  44. package/lib/tokens.js +10 -22
  45. package/lib/transition.js +86 -0
  46. package/lib/websocket.js +68 -62
  47. package/package.json +1 -1
  48. package/tests/compiler.test.js +423 -188
package/lib/for.js CHANGED
@@ -1,68 +1,96 @@
1
1
  const { getLineInfo } = require("./lineCounter");
2
2
  const { forOf, forIn } = require("./regex");
3
- const { isNum,id } = require("./helpers");
3
+ const { isNum, id } = require("./helpers");
4
4
  const { RAW_OPEN, RAW_CLOSE, shieldOps } = require("./tokens");
5
5
 
6
6
  function handleForStatement(body, counter, filePath) {
7
- // using <-olum-> (&lt;-olum-&gt;) as placeholder for "{" inside map method
8
- const forElms = new Array().slice.call(body.querySelectorAll("for")).reverse(); // we must reverse to start with the very deep if tag which doesn't have nested if tags inside it
9
- forElms.forEach(item => {
10
- // console.log(item.outerHTML + "\n----------");
7
+ const forElms = new Array().slice
8
+ .call(body.querySelectorAll("for"))
9
+ .reverse();
10
+ forElms.forEach((item) => {
11
11
  if (item.hasAttribute("each")) {
12
- // `each` is a plain JS expression (no braces): <for each="item of state.items">
13
- // shieldOps: protect <,> from the jsdom text round-trip (restored in htmlParser)
14
12
  const condition = shieldOps(item.getAttribute("each").trim());
15
13
 
16
- // #3 (keyed reconciliation): if the <for> declares key={expr}, stamp every component
17
- // placeholder in this loop with data-o-key="{expr}". `expr` references the loop variable, so
18
- // each iteration emits a distinct key, and the runtime (buildTree) reuses the matching
19
- // instance by identity instead of by position. <for>s are processed deepest-first, so a
20
- // nested loop has already stamped its own placeholders — we skip any that are already keyed.
21
14
  const keyAttr = item.getAttribute("key");
22
15
  if (keyAttr) {
23
16
  const keyExpr = keyAttr.trim();
24
- new Array().slice.call(item.querySelectorAll("olum")).forEach(olum => {
25
- if (!olum.hasAttribute("data-o-key")) olum.setAttribute("data-o-key", "{" + keyExpr + "}");
17
+
18
+ new Array().slice
19
+ .call(item.querySelectorAll("olum"))
20
+ .forEach((olum) => {
21
+ if (!olum.hasAttribute("data-o-key"))
22
+ olum.setAttribute("data-o-key", "{" + keyExpr + "}");
23
+ });
24
+
25
+ const skipTags = ["olum", "show", "if", "for", "else", "else-if"];
26
+ new Array().slice.call(item.children).forEach((child) => {
27
+ if (skipTags.includes(child.tagName.toLowerCase())) return;
28
+ if (!child.hasAttribute("key"))
29
+ child.setAttribute("key", "{" + keyExpr + "}");
26
30
  });
27
31
  }
28
32
 
29
33
  const innerContent = item.innerHTML;
30
34
 
31
- // for of loop
32
35
  condition.replace(forOf, (str, $1, $2, $3) => {
33
36
  if (isNum($3)) {
34
37
  const params = $1.replace(/\(|\)/g, "");
35
38
  const [param1] = params.split(",");
36
39
  const mkID = id();
37
- let variable = param1 + "=index"+mkID+"+1";
38
- // RAW_OPEN/RAW_CLOSE wrap the structural interpolation so #2's escape pass keeps the
39
- // .map().join('') raw (it produces HTML). <-olum-> is the map callback's own `{` brace.
40
- const loop = RAW_OPEN + "new Array(" + $3 + ").fill().map(function(" + param1 +", index"+mkID+ ")<-olum->\n"+ variable +"\nreturn`" + innerContent + "`;\n}).join('')" + RAW_CLOSE;
40
+ let variable = param1 + "=index" + mkID + "+1";
41
+
42
+ const loop =
43
+ RAW_OPEN +
44
+ "new Array(" +
45
+ $3 +
46
+ ").fill().map(function(" +
47
+ param1 +
48
+ ", index" +
49
+ mkID +
50
+ ")<-olum->\n" +
51
+ variable +
52
+ "\nreturn`" +
53
+ innerContent +
54
+ "`;\n}).join('')" +
55
+ RAW_CLOSE;
41
56
  item.insertAdjacentHTML("beforebegin", loop);
42
57
  item.remove();
43
58
  } else {
44
- // console.log({ condition, innerContent });
45
- // console.log("for of loop", { str, $1, $2, $3 });
46
59
  const params = $1.replace(/\(|\)/g, "");
47
- const loop = RAW_OPEN + $3 + ".map(function(" + params + ")<-olum->\nreturn`" + innerContent + "`;\n}).join('')" + RAW_CLOSE;
60
+ const loop =
61
+ RAW_OPEN +
62
+ $3 +
63
+ ".map(function(" +
64
+ params +
65
+ ")<-olum->\nreturn`" +
66
+ innerContent +
67
+ "`;\n}).join('')" +
68
+ RAW_CLOSE;
48
69
  item.insertAdjacentHTML("beforebegin", loop);
49
70
  item.remove();
50
71
  }
51
72
  });
52
73
 
53
- // for in loop
54
74
  condition.replace(forIn, (str, $1, $2, $3) => {
55
- // console.log({ condition, innerContent });
56
- // console.log("for in loop", { str, $1, $2, $3 });
57
75
  const params = $1.replace(/\(|\)/g, "");
58
76
  const [param1, param2, param3] = params.split(",");
59
- // console.log({ param1, param2, param3 });
77
+
60
78
  const value = param3 ? param3 + "=" + $3 + "[" + param1 + "];" : "";
61
- const loop = RAW_OPEN + "Object.keys(" + $3 + ").map(function(" + params + ")<-olum->\n" + value + "\nreturn`" + innerContent + "`;\n}).join('')" + RAW_CLOSE;
79
+ const loop =
80
+ RAW_OPEN +
81
+ "Object.keys(" +
82
+ $3 +
83
+ ").map(function(" +
84
+ params +
85
+ ")<-olum->\n" +
86
+ value +
87
+ "\nreturn`" +
88
+ innerContent +
89
+ "`;\n}).join('')" +
90
+ RAW_CLOSE;
62
91
  item.insertAdjacentHTML("beforebegin", loop);
63
92
  item.remove();
64
93
  });
65
-
66
94
  } else {
67
95
  const msg = "Missing 'each' attribute:";
68
96
  getLineInfo(counter, item, filePath, msg);
package/lib/getImports.js CHANGED
@@ -1,43 +1,61 @@
1
1
  const acorn = require("acorn");
2
2
  const { isFullArr } = require("./helpers");
3
- // FYI: it ignores the comments by default beacuse it's based on lexial environment not based on regex
3
+
4
4
  function getImports(str) {
5
5
  let finalImports = [];
6
- let arr = acorn.parse(str, { ecmaVersion: "latest", sourceType: "module" }).body;
6
+ let arr = acorn.parse(str, {
7
+ ecmaVersion: "latest",
8
+ sourceType: "module",
9
+ }).body;
7
10
  arr.forEach((obj) => {
8
11
  if (obj.type === "ImportDeclaration") {
9
12
  if (obj.specifiers) {
10
13
  if (isFullArr(obj.specifiers)) {
11
14
  if (obj.specifiers.length === 1) {
12
15
  if (obj.specifiers[0].type == "ImportDefaultSpecifier") {
13
- // default
14
16
  const name = obj.specifiers[0].local.name;
15
17
  const statement = "import " + name + " from " + obj.source.raw;
16
- finalImports.push({ statement, regex: `(from)(\\s+)?(${obj.source.raw}).*`});
17
- // console.log("default: ", statement);
18
+ finalImports.push({
19
+ statement,
20
+ regex: `(from)(\\s+)?(${obj.source.raw}).*`,
21
+ });
18
22
  } else {
19
- // non-default
20
23
  const name = obj.specifiers[0].local.name;
21
- const statement = "import { " + name + " } from " + obj.source.raw;
22
- finalImports.push({ statement, regex: `(from)(\\s+)?(${obj.source.raw}).*` });
23
- // console.log("Non-default: ", statement);
24
+ const statement =
25
+ "import { " + name + " } from " + obj.source.raw;
26
+ finalImports.push({
27
+ statement,
28
+ regex: `(from)(\\s+)?(${obj.source.raw}).*`,
29
+ });
24
30
  }
25
31
  } else {
26
32
  let defaultItem = null;
27
33
  let nonDefaultArr = [];
28
34
  obj.specifiers.forEach((item) => {
29
- if (item.type === "ImportDefaultSpecifier") defaultItem = item.local.name;
30
- if (item.type === "ImportSpecifier") nonDefaultArr.push(item.local.name);
35
+ if (item.type === "ImportDefaultSpecifier")
36
+ defaultItem = item.local.name;
37
+ if (item.type === "ImportSpecifier")
38
+ nonDefaultArr.push(item.local.name);
39
+ });
40
+ const statement =
41
+ "import " +
42
+ (defaultItem ? defaultItem + ", " : "") +
43
+ "{ " +
44
+ nonDefaultArr.join(", ") +
45
+ " } from " +
46
+ obj.source.raw;
47
+ finalImports.push({
48
+ statement,
49
+ regex: `(from)(\\s+)?(${obj.source.raw}).*`,
31
50
  });
32
- const statement = "import " + (defaultItem ? defaultItem + ", " : "") + "{ " + nonDefaultArr.join(", ") + " } from " + obj.source.raw;
33
- finalImports.push({ statement, regex: `(from)(\\s+)?(${obj.source.raw}).*` });
34
- // console.log("mix: ", statement);
35
51
  }
36
52
  } else {
37
- // import only
38
53
  const statement = "import " + obj.source.raw;
39
- finalImports.push({ statement, type: "noFrom", regex: `(import)(\\s+)?(${obj.source.raw}).*\\n?` });
40
- // console.log("import only: ", statement);
54
+ finalImports.push({
55
+ statement,
56
+ type: "noFrom",
57
+ regex: `(import)(\\s+)?(${obj.source.raw}).*\\n?`,
58
+ });
41
59
  }
42
60
  }
43
61
  }
@@ -1,13 +1,17 @@
1
1
  const path = require("path");
2
2
  const { ls, isFile, isHTML } = require("./helpers");
3
- const isComp = str => !/^_/.test(str); // if doesn't have underscore
3
+ const isComp = (str) => !/^_/.test(str);
4
4
 
5
5
  function getTempPath() {
6
6
  const entryPoint = path.resolve(__dirname, "../src");
7
7
  let paths = ls(entryPoint);
8
- // clean paths
9
- paths = paths.map((item) => (isFile(item) && isHTML(item) && isComp(path.basename(item)) ? item : null)).filter((item) => item);
8
+
9
+ paths = paths
10
+ .map((item) =>
11
+ isFile(item) && isHTML(item) && isComp(path.basename(item)) ? item : null,
12
+ )
13
+ .filter((item) => item);
10
14
  return paths;
11
15
  }
12
16
 
13
- module.exports = getTempPath;
17
+ module.exports = getTempPath;