@xaendar/compiler 0.6.6 → 0.6.9

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.
@@ -1756,26 +1756,11 @@ function isValidIdentifier(name) {
1756
1756
  * @returns The parsed `IfNode`.
1757
1757
  */
1758
1758
  function parseIfControlFlow(cursor, parseNode, token) {
1759
- return parseIfRecursively(cursor, parseNode, token);
1759
+ return parseIfOrElseIf(cursor, parseNode, token);
1760
1760
  }
1761
- function parseIfRecursively(cursor, parseNode, token) {
1761
+ function parseElseIfRecursively(cursor, parseNode, token) {
1762
1762
  switch (token.type) {
1763
- case TokenType.IF:
1764
- case TokenType.ELSE_IF:
1765
- cursor.advance();
1766
- const conditionToken = cursor.peek();
1767
- if (conditionToken.type !== TokenType.CONDITION) throw new Error(`[Parser] Expected CONDITION after ${TokenType[token.type]}, got ${TokenType[conditionToken.type]}`);
1768
- cursor.advance(2);
1769
- const condition = conditionToken.parts[0];
1770
- const validationResult = validateExpression(condition);
1771
- const consequent = parseBlockChildren(cursor, parseNode);
1772
- return {
1773
- type: token.type === TokenType.IF ? ASTNodeType.If : ASTNodeType.ElseIf,
1774
- condition,
1775
- conditionNode: validationResult.node,
1776
- consequent,
1777
- alternate: parseIfRecursively(cursor, parseNode, cursor.peek())
1778
- };
1763
+ case TokenType.ELSE_IF: return parseIfOrElseIf(cursor, parseNode, token);
1779
1764
  case TokenType.ELSE:
1780
1765
  cursor.advance(2);
1781
1766
  const elseChildren = parseBlockChildren(cursor, parseNode);
@@ -1785,6 +1770,22 @@ function parseIfRecursively(cursor, parseNode, token) {
1785
1770
  };
1786
1771
  }
1787
1772
  }
1773
+ function parseIfOrElseIf(cursor, parseNode, token) {
1774
+ cursor.advance();
1775
+ const conditionToken = cursor.peek();
1776
+ if (conditionToken.type !== TokenType.CONDITION) throw new Error(`[Parser] Expected CONDITION after ${TokenType[token.type]}, got ${TokenType[conditionToken.type]}`);
1777
+ cursor.advance(2);
1778
+ const condition = conditionToken.parts[0];
1779
+ const validationResult = validateExpression(condition);
1780
+ const consequent = parseBlockChildren(cursor, parseNode);
1781
+ return {
1782
+ type: token.type === TokenType.IF ? ASTNodeType.If : ASTNodeType.ElseIf,
1783
+ condition,
1784
+ conditionNode: validationResult.node,
1785
+ consequent,
1786
+ alternate: parseElseIfRecursively(cursor, parseNode, cursor.peek())
1787
+ };
1788
+ }
1788
1789
  //#endregion
1789
1790
  //#region ../packages/compiler/src/parser/states/parse-switch.state.ts
1790
1791
  /**
@@ -2141,7 +2142,7 @@ var GLOBAL_IDENTIFIERS = new Set([
2141
2142
  "Document",
2142
2143
  "Window"
2143
2144
  ]);
2144
- var ROOT_NODE = "this._root";
2145
+ var ROOT_NODE = "root";
2145
2146
  /**
2146
2147
  * Resolves references to component properties inside a template expression.
2147
2148
  *
@@ -2223,13 +2224,13 @@ function needsResolution(node, parent) {
2223
2224
  * @returns A string representing the unique variable name for the element.
2224
2225
  */
2225
2226
  function getElementIdentifier(node, parentNode, index) {
2226
- return (parentNode !== "this._root" ? `${parentNode}_${node.tagName}${index}` : `${node.tagName}${index}`).replace(/-/g, "_");
2227
+ return (parentNode !== "root" ? `${parentNode}_${node.tagName}${index}` : `${node.tagName}${index}`).replace(/-/g, "_");
2227
2228
  }
2228
2229
  function getTextIdentifier(parentNode, index, prefix = "text") {
2229
- return (parentNode !== "this._root" ? `${parentNode}_${prefix}${index}` : `${prefix}${index}`).replace(/-/g, "_");
2230
+ return (parentNode !== "root" ? `${parentNode}_${prefix}${index}` : `${prefix}${index}`).replace(/-/g, "_");
2230
2231
  }
2231
2232
  function getBlockIdentifier(parentNode, index, prefix) {
2232
- return (parentNode !== "this._root" ? `${parentNode}_${prefix}${index}` : `${prefix}${index}`).replace(/-/g, "_");
2233
+ return (parentNode !== "root" ? `${parentNode}_${prefix}${index}` : `${prefix}${index}`).replace(/-/g, "_");
2233
2234
  }
2234
2235
  //#endregion
2235
2236
  //#region ../packages/compiler/src/render-generator/states/process-const-declaration.state.ts
@@ -2367,9 +2368,13 @@ function processFor(node, nodeName, parentNode, parentContext) {
2367
2368
  const forKey = getBlockIdentifier(parentNode, nodeName, "for");
2368
2369
  functionsToProcess.set(forKey, {
2369
2370
  code: [`const { ${node.itemAlias}, ${indexName}, ${firstName}, ${lastName}, ${evenName}, ${oddName} } = _iterationVariables(${itemsName}, ${counterName});`, ...node.children.flatMap((child, i) => processNode(child, `${nodeName}_${i}`, parentNode, forContext))],
2370
- args: [itemsName, counterName]
2371
+ args: [
2372
+ parentNode,
2373
+ itemsName,
2374
+ counterName
2375
+ ]
2371
2376
  });
2372
- mainBlock.push(`_for(unwatchFns, () => ${iterableExpr}, this.${forKey}.bind(this));`);
2377
+ mainBlock.push(`_for(${parentNode}, unwatchFns, () => ${iterableExpr}, this.${forKey}.bind(this));`);
2373
2378
  return {
2374
2379
  mainBlock,
2375
2380
  fns: functionsToProcess
@@ -2411,7 +2416,10 @@ function processIf(node, nodeName, parentNode, context) {
2411
2416
  condition: resolveExpression(node.conditionNode, context),
2412
2417
  block: `this.${ifKey}.bind(this)`
2413
2418
  });
2414
- functionsToProcess.set(ifKey, processConsequent(node, nodeName, parentNode, ifContext));
2419
+ functionsToProcess.set(ifKey, {
2420
+ code: processConsequent(node, nodeName, parentNode, ifContext),
2421
+ args: [parentNode]
2422
+ });
2415
2423
  let alt = node.alternate;
2416
2424
  let index = 0;
2417
2425
  while (alt?.type === ASTNodeType.ElseIf) {
@@ -2422,22 +2430,28 @@ function processIf(node, nodeName, parentNode, context) {
2422
2430
  condition: resolveExpression(conditionNode, context),
2423
2431
  block: `this.${keyElseIf}.bind(this)`
2424
2432
  });
2425
- functionsToProcess.set(keyElseIf, processConsequent(alt, nodeName, parentNode, elseIfContext));
2433
+ functionsToProcess.set(keyElseIf, {
2434
+ code: processConsequent(alt, nodeName, parentNode, elseIfContext),
2435
+ args: [parentNode]
2436
+ });
2426
2437
  alt = alt.alternate;
2427
2438
  }
2428
2439
  if (alt) {
2429
2440
  const elseContext = new Context([], context);
2430
2441
  const keyElse = getBlockIdentifier(parentNode, nodeName, "else");
2431
2442
  mainBlock.push({ block: `this.${keyElse}.bind(this)` });
2432
- functionsToProcess.set(keyElse, processConsequent(alt, nodeName, parentNode, elseContext));
2443
+ functionsToProcess.set(keyElse, {
2444
+ code: processConsequent(alt, nodeName, parentNode, elseContext),
2445
+ args: [parentNode]
2446
+ });
2433
2447
  }
2434
2448
  return {
2435
2449
  mainBlock: [
2436
- "_if(unwatchFns, [",
2450
+ `_if(${parentNode}, unwatchFns, [`,
2437
2451
  ...indent(mainBlock.map(({ condition, block }) => {
2438
2452
  return [
2439
2453
  "{",
2440
- ...indent(condition ? [`condition: ${condition.toString()},`, `block: ${block.toString()}`] : [`block: ${block.toString()}`]),
2454
+ ...indent(condition ? [`condition: () => ${condition.toString()},`, `block: ${block.toString()}`] : [`block: ${block.toString()}`]),
2441
2455
  "},"
2442
2456
  ];
2443
2457
  }).flat()),
@@ -2468,7 +2482,10 @@ function processSwitch(node, nodeName, parentNode, context) {
2468
2482
  node.cases.forEach((caseNode, i) => {
2469
2483
  const caseContext = new Context([], context);
2470
2484
  const caseName = caseNode.condition ? getBlockIdentifier(parentNode, `${nodeName}_${i}`, "case") : getBlockIdentifier(parentNode, nodeName, "default");
2471
- functionsToProcess.set(caseName, caseNode.children.map((child, i) => processNode(child, `${nodeName}_${i}_${i}`, parentNode, caseContext)).flat());
2485
+ functionsToProcess.set(caseName, {
2486
+ code: caseNode.children.map((child, i) => processNode(child, `${nodeName}_${i}_${i}`, parentNode, caseContext)).flat(),
2487
+ args: [parentNode]
2488
+ });
2472
2489
  blocks.push({
2473
2490
  condition: caseNode.condition,
2474
2491
  block: `this.${caseName}.bind(this)`
@@ -2476,7 +2493,7 @@ function processSwitch(node, nodeName, parentNode, context) {
2476
2493
  });
2477
2494
  return {
2478
2495
  mainBlock: [
2479
- `_switch(unwatchFns, () => ${resolveExpression(node.expression, context)}, [`,
2496
+ `_switch(${parentNode}, unwatchFns, () => ${resolveExpression(node.expression, context)}, [`,
2480
2497
  ...indent(blocks.map(({ condition, block }) => {
2481
2498
  return [
2482
2499
  "{",
@@ -2521,26 +2538,21 @@ var nodeToProcess = /* @__PURE__ */ new Map();
2521
2538
  */
2522
2539
  function generateRenderFunction(ast, cssVariableName) {
2523
2540
  nodeToProcess.clear();
2524
- const context = new Context();
2525
- const renderFunctions = ["_render() {"];
2526
- if (cssVariableName) renderFunctions.push(indent(`this._root.adoptedStyleSheets = [${cssVariableName}];`));
2541
+ const context = new Context([ROOT_NODE]);
2542
+ const renderFunctions = ["_render() {", indent(`const ${ROOT_NODE} = this._root;`)];
2543
+ if (cssVariableName) renderFunctions.push(indent(`${ROOT_NODE}.adoptedStyleSheets = [${cssVariableName}];`));
2527
2544
  renderFunctions.push(...indent([
2528
2545
  "let unwatchFns = [];",
2529
2546
  ...ast.map((node, i) => [...processNode(node, i.toString(), ROOT_NODE, context)]).flat(),
2530
2547
  "return unwatchFns;"
2531
2548
  ]), "}");
2532
- while (nodeToProcess.size > 0) {
2549
+ while (nodeToProcess.size) {
2533
2550
  const [key, fnData] = nodeToProcess.entries().next().value;
2534
- if ("args" in fnData) renderFunctions.push(`${key}(${fnData.args.join(", ")}) {`, ...indent([
2551
+ renderFunctions.push(`${key}(${fnData.args.join(", ")}) {`, ...indent([
2535
2552
  "let unwatchFns = [];",
2536
2553
  ...fnData.fn(...fnData.args),
2537
2554
  "return unwatchFns;"
2538
2555
  ]), "}");
2539
- else renderFunctions.push(`${key}() {`, ...indent([
2540
- "let unwatchFns = [];",
2541
- ...fnData.fn(),
2542
- "return unwatchFns;"
2543
- ]), "}");
2544
2556
  nodeToProcess.delete(key);
2545
2557
  }
2546
2558
  return renderFunctions.join("\n");
@@ -2557,7 +2569,10 @@ function processNode(node, nodeName, parentNode, context) {
2557
2569
  case ASTNodeType.Element: return processElement(node, getElementIdentifier(node, parentNode, nodeName), parentNode, context);
2558
2570
  case ASTNodeType.If:
2559
2571
  const conditionalBlockData = processIf(node, nodeName, parentNode, context);
2560
- conditionalBlockData.fns.forEach((fnBody, key) => nodeToProcess.set(key, { fn: () => fnBody }));
2572
+ conditionalBlockData.fns.forEach((fnBody, key) => nodeToProcess.set(key, {
2573
+ fn: () => fnBody.code,
2574
+ args: fnBody.args
2575
+ }));
2561
2576
  return conditionalBlockData.mainBlock;
2562
2577
  case ASTNodeType.For:
2563
2578
  const forBlockData = processFor(node, nodeName, parentNode, context);
@@ -2568,7 +2583,10 @@ function processNode(node, nodeName, parentNode, context) {
2568
2583
  return forBlockData.mainBlock;
2569
2584
  case ASTNodeType.Switch:
2570
2585
  const switchBlockData = processSwitch(node, nodeName, parentNode, context);
2571
- switchBlockData.fns.forEach((fnBody, key) => nodeToProcess.set(key, { fn: () => fnBody }));
2586
+ switchBlockData.fns.forEach((fnBody, key) => nodeToProcess.set(key, {
2587
+ fn: () => fnBody.code,
2588
+ args: fnBody.args
2589
+ }));
2572
2590
  return switchBlockData.mainBlock;
2573
2591
  case ASTNodeType.ConstDeclaration: return processConstDeclaration(node, nodeName, parentNode, context);
2574
2592
  default: return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xaendar/compiler",
3
- "version": "0.6.6",
3
+ "version": "0.6.9",
4
4
  "description": "A library for transpiling Xaendar Templates into JavaScript code",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -16,8 +16,8 @@
16
16
  }
17
17
  },
18
18
  "dependencies": {
19
- "@xaendar/common": "0.6.6",
20
- "@xaendar/types": "0.6.6",
19
+ "@xaendar/common": "0.6.9",
20
+ "@xaendar/types": "0.6.9",
21
21
  "typescript": "^6.0.3"
22
22
  }
23
23
  }