@danielx/civet 0.6.4 → 0.6.5

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.
package/dist/browser.js CHANGED
@@ -193,13 +193,6 @@ var Civet = (() => {
193
193
  }
194
194
  return block;
195
195
  }
196
- function closest(node, types) {
197
- do {
198
- if (types.includes(node.type)) {
199
- return node;
200
- }
201
- } while (node = node.parent);
202
- }
203
196
  function clone(node) {
204
197
  removeParentPointers(node);
205
198
  return deepCopy(node);
@@ -436,8 +429,26 @@ var Civet = (() => {
436
429
  for (let i = 0; i < children.length; i++) {
437
430
  const glob = children[i];
438
431
  if (glob?.type === "PropertyGlob") {
439
- const prefix = children.slice(0, i).concat(glob.dot);
432
+ let prefix = children.slice(0, i);
440
433
  const parts = [];
434
+ let hoistDec, refAssignment = [];
435
+ if (prefix.length > 1) {
436
+ const ref = {
437
+ type: "Ref",
438
+ base: "ref"
439
+ };
440
+ hoistDec = {
441
+ type: "Declaration",
442
+ children: ["let ", ref],
443
+ names: []
444
+ };
445
+ refAssignment = [{
446
+ type: "AssignmentExpression",
447
+ children: [ref, " = ", prefix]
448
+ }, ","];
449
+ prefix = [ref];
450
+ }
451
+ prefix = prefix.concat(glob.dot);
441
452
  for (const part of glob.object.properties) {
442
453
  if (part.type === "MethodDefinition") {
443
454
  throw new Error("Glob pattern cannot have method definition");
@@ -480,11 +491,13 @@ var Civet = (() => {
480
491
  const object = {
481
492
  type: "ObjectExpression",
482
493
  children: [
494
+ ...refAssignment,
483
495
  glob.object.children[0],
484
496
  ...parts,
485
497
  glob.object.children.at(-1)
486
498
  ],
487
- properties: parts
499
+ properties: parts,
500
+ hoistDec
488
501
  };
489
502
  if (i === children.length - 1)
490
503
  return object;
@@ -786,29 +799,26 @@ var Civet = (() => {
786
799
  function hoistRefDecs(statements) {
787
800
  gatherRecursiveAll(statements, (s) => s.hoistDec).forEach((node) => {
788
801
  const { hoistDec } = node;
789
- let outer = closest(node, ["IfStatement", "IterationStatement"]);
790
- if (!outer) {
791
- node.children.push({
792
- type: "Error",
793
- message: "Can't hoist declarations inside expressions yet."
794
- });
795
- return;
796
- }
797
- let block = outer.parent;
798
- if (block.type === "PatternMatchingStatement") {
799
- outer = block;
800
- block = block.parent;
801
- }
802
- const { expressions } = block;
803
- const index = expressions.findIndex(([, s]) => outer === s);
804
- if (index < 0)
805
- throw new Error("Couldn't find expression in block for hoistable declaration.");
806
- const indent = expressions[index][0];
807
- hoistDec[0][0] = indent;
808
- expressions.splice(index, 0, hoistDec);
809
802
  node.hoistDec = null;
803
+ while (node.parent?.type !== "BlockStatement") {
804
+ node = node.parent;
805
+ }
806
+ if (node.parent) {
807
+ insertHoistDec(node.parent, node, hoistDec);
808
+ } else {
809
+ throw new Error("Couldn't find block to hoist declaration into.");
810
+ }
811
+ return;
810
812
  });
811
813
  }
814
+ function insertHoistDec(block, node, dec) {
815
+ const { expressions } = block;
816
+ const index = expressions.findIndex(([, s]) => node === s);
817
+ if (index < 0)
818
+ throw new Error("Couldn't find expression in block for hoistable declaration.");
819
+ const indent = expressions[index][0];
820
+ expressions.splice(index, 0, [indent, dec, ";"]);
821
+ }
812
822
  function insertReturn(node) {
813
823
  if (!node)
814
824
  return;
@@ -1726,8 +1736,18 @@ var Civet = (() => {
1726
1736
  if (expression.type === "ParenthesizedExpression") {
1727
1737
  expression = expression.expression;
1728
1738
  }
1729
- let ref = needsRef(expression, "m") || expression;
1730
- let hoistDec = ref !== expression ? [["", ["const ", ref, " = ", expression], ";"]] : void 0;
1739
+ let hoistDec, refAssignment = [], ref = needsRef(expression, "m") || expression;
1740
+ if (ref !== expression) {
1741
+ hoistDec = {
1742
+ type: "Declaration",
1743
+ children: ["let ", ref],
1744
+ names: []
1745
+ };
1746
+ refAssignment = [{
1747
+ type: "AssignmentExpression",
1748
+ children: [ref, " = ", expression]
1749
+ }, ","];
1750
+ }
1731
1751
  let prev = [], root = prev;
1732
1752
  const l = clauses.length;
1733
1753
  clauses.forEach((c, i) => {
@@ -1755,7 +1775,7 @@ var Civet = (() => {
1755
1775
  });
1756
1776
  const condition = {
1757
1777
  type: "ParenthesizedExpression",
1758
- children: ["(", conditionExpression, ")"],
1778
+ children: ["(", ...refAssignment, conditionExpression, ")"],
1759
1779
  expression: conditionExpression
1760
1780
  };
1761
1781
  const prefix = [];
@@ -1794,6 +1814,7 @@ var Civet = (() => {
1794
1814
  hoistDec
1795
1815
  }]);
1796
1816
  hoistDec = void 0;
1817
+ refAssignment = [];
1797
1818
  prev = next;
1798
1819
  });
1799
1820
  if (s.type === "SwitchExpression") {
@@ -12882,7 +12903,11 @@ ${input.slice(result.pos)}
12882
12903
  const initCondition = {
12883
12904
  type: "AssignmentExpression",
12884
12905
  children: [ref, " ", initializer],
12885
- hoistDec: [["", ["let ", ref], ";"]],
12906
+ hoistDec: {
12907
+ type: "Declaration",
12908
+ children: ["let ", ref],
12909
+ names: []
12910
+ },
12886
12911
  blockPrefix: [
12887
12912
  ["", [binding, "= ", ref, ...splices], ";"],
12888
12913
  ...thisAssignments
package/dist/main.js CHANGED
@@ -192,13 +192,6 @@ var require_lib = __commonJS({
192
192
  }
193
193
  return block;
194
194
  }
195
- function closest(node, types) {
196
- do {
197
- if (types.includes(node.type)) {
198
- return node;
199
- }
200
- } while (node = node.parent);
201
- }
202
195
  function clone(node) {
203
196
  removeParentPointers(node);
204
197
  return deepCopy(node);
@@ -435,8 +428,26 @@ var require_lib = __commonJS({
435
428
  for (let i = 0; i < children.length; i++) {
436
429
  const glob = children[i];
437
430
  if (glob?.type === "PropertyGlob") {
438
- const prefix = children.slice(0, i).concat(glob.dot);
431
+ let prefix = children.slice(0, i);
439
432
  const parts = [];
433
+ let hoistDec, refAssignment = [];
434
+ if (prefix.length > 1) {
435
+ const ref = {
436
+ type: "Ref",
437
+ base: "ref"
438
+ };
439
+ hoistDec = {
440
+ type: "Declaration",
441
+ children: ["let ", ref],
442
+ names: []
443
+ };
444
+ refAssignment = [{
445
+ type: "AssignmentExpression",
446
+ children: [ref, " = ", prefix]
447
+ }, ","];
448
+ prefix = [ref];
449
+ }
450
+ prefix = prefix.concat(glob.dot);
440
451
  for (const part of glob.object.properties) {
441
452
  if (part.type === "MethodDefinition") {
442
453
  throw new Error("Glob pattern cannot have method definition");
@@ -479,11 +490,13 @@ var require_lib = __commonJS({
479
490
  const object = {
480
491
  type: "ObjectExpression",
481
492
  children: [
493
+ ...refAssignment,
482
494
  glob.object.children[0],
483
495
  ...parts,
484
496
  glob.object.children.at(-1)
485
497
  ],
486
- properties: parts
498
+ properties: parts,
499
+ hoistDec
487
500
  };
488
501
  if (i === children.length - 1)
489
502
  return object;
@@ -785,29 +798,26 @@ var require_lib = __commonJS({
785
798
  function hoistRefDecs(statements) {
786
799
  gatherRecursiveAll(statements, (s) => s.hoistDec).forEach((node) => {
787
800
  const { hoistDec } = node;
788
- let outer = closest(node, ["IfStatement", "IterationStatement"]);
789
- if (!outer) {
790
- node.children.push({
791
- type: "Error",
792
- message: "Can't hoist declarations inside expressions yet."
793
- });
794
- return;
795
- }
796
- let block = outer.parent;
797
- if (block.type === "PatternMatchingStatement") {
798
- outer = block;
799
- block = block.parent;
800
- }
801
- const { expressions } = block;
802
- const index = expressions.findIndex(([, s]) => outer === s);
803
- if (index < 0)
804
- throw new Error("Couldn't find expression in block for hoistable declaration.");
805
- const indent = expressions[index][0];
806
- hoistDec[0][0] = indent;
807
- expressions.splice(index, 0, hoistDec);
808
801
  node.hoistDec = null;
802
+ while (node.parent?.type !== "BlockStatement") {
803
+ node = node.parent;
804
+ }
805
+ if (node.parent) {
806
+ insertHoistDec(node.parent, node, hoistDec);
807
+ } else {
808
+ throw new Error("Couldn't find block to hoist declaration into.");
809
+ }
810
+ return;
809
811
  });
810
812
  }
813
+ function insertHoistDec(block, node, dec) {
814
+ const { expressions } = block;
815
+ const index = expressions.findIndex(([, s]) => node === s);
816
+ if (index < 0)
817
+ throw new Error("Couldn't find expression in block for hoistable declaration.");
818
+ const indent = expressions[index][0];
819
+ expressions.splice(index, 0, [indent, dec, ";"]);
820
+ }
811
821
  function insertReturn(node) {
812
822
  if (!node)
813
823
  return;
@@ -1725,8 +1735,18 @@ var require_lib = __commonJS({
1725
1735
  if (expression.type === "ParenthesizedExpression") {
1726
1736
  expression = expression.expression;
1727
1737
  }
1728
- let ref = needsRef(expression, "m") || expression;
1729
- let hoistDec = ref !== expression ? [["", ["const ", ref, " = ", expression], ";"]] : void 0;
1738
+ let hoistDec, refAssignment = [], ref = needsRef(expression, "m") || expression;
1739
+ if (ref !== expression) {
1740
+ hoistDec = {
1741
+ type: "Declaration",
1742
+ children: ["let ", ref],
1743
+ names: []
1744
+ };
1745
+ refAssignment = [{
1746
+ type: "AssignmentExpression",
1747
+ children: [ref, " = ", expression]
1748
+ }, ","];
1749
+ }
1730
1750
  let prev = [], root = prev;
1731
1751
  const l = clauses.length;
1732
1752
  clauses.forEach((c, i) => {
@@ -1754,7 +1774,7 @@ var require_lib = __commonJS({
1754
1774
  });
1755
1775
  const condition = {
1756
1776
  type: "ParenthesizedExpression",
1757
- children: ["(", conditionExpression, ")"],
1777
+ children: ["(", ...refAssignment, conditionExpression, ")"],
1758
1778
  expression: conditionExpression
1759
1779
  };
1760
1780
  const prefix = [];
@@ -1793,6 +1813,7 @@ var require_lib = __commonJS({
1793
1813
  hoistDec
1794
1814
  }]);
1795
1815
  hoistDec = void 0;
1816
+ refAssignment = [];
1796
1817
  prev = next;
1797
1818
  });
1798
1819
  if (s.type === "SwitchExpression") {
@@ -12881,7 +12902,11 @@ ${input.slice(result.pos)}
12881
12902
  const initCondition = {
12882
12903
  type: "AssignmentExpression",
12883
12904
  children: [ref, " ", initializer],
12884
- hoistDec: [["", ["let ", ref], ";"]],
12905
+ hoistDec: {
12906
+ type: "Declaration",
12907
+ children: ["let ", ref],
12908
+ names: []
12909
+ },
12885
12910
  blockPrefix: [
12886
12911
  ["", [binding, "= ", ref, ...splices], ";"],
12887
12912
  ...thisAssignments
package/dist/main.mjs CHANGED
@@ -190,13 +190,6 @@ var require_lib = __commonJS({
190
190
  }
191
191
  return block;
192
192
  }
193
- function closest(node, types) {
194
- do {
195
- if (types.includes(node.type)) {
196
- return node;
197
- }
198
- } while (node = node.parent);
199
- }
200
193
  function clone(node) {
201
194
  removeParentPointers(node);
202
195
  return deepCopy(node);
@@ -433,8 +426,26 @@ var require_lib = __commonJS({
433
426
  for (let i = 0; i < children.length; i++) {
434
427
  const glob = children[i];
435
428
  if (glob?.type === "PropertyGlob") {
436
- const prefix = children.slice(0, i).concat(glob.dot);
429
+ let prefix = children.slice(0, i);
437
430
  const parts = [];
431
+ let hoistDec, refAssignment = [];
432
+ if (prefix.length > 1) {
433
+ const ref = {
434
+ type: "Ref",
435
+ base: "ref"
436
+ };
437
+ hoistDec = {
438
+ type: "Declaration",
439
+ children: ["let ", ref],
440
+ names: []
441
+ };
442
+ refAssignment = [{
443
+ type: "AssignmentExpression",
444
+ children: [ref, " = ", prefix]
445
+ }, ","];
446
+ prefix = [ref];
447
+ }
448
+ prefix = prefix.concat(glob.dot);
438
449
  for (const part of glob.object.properties) {
439
450
  if (part.type === "MethodDefinition") {
440
451
  throw new Error("Glob pattern cannot have method definition");
@@ -477,11 +488,13 @@ var require_lib = __commonJS({
477
488
  const object = {
478
489
  type: "ObjectExpression",
479
490
  children: [
491
+ ...refAssignment,
480
492
  glob.object.children[0],
481
493
  ...parts,
482
494
  glob.object.children.at(-1)
483
495
  ],
484
- properties: parts
496
+ properties: parts,
497
+ hoistDec
485
498
  };
486
499
  if (i === children.length - 1)
487
500
  return object;
@@ -783,29 +796,26 @@ var require_lib = __commonJS({
783
796
  function hoistRefDecs(statements) {
784
797
  gatherRecursiveAll(statements, (s) => s.hoistDec).forEach((node) => {
785
798
  const { hoistDec } = node;
786
- let outer = closest(node, ["IfStatement", "IterationStatement"]);
787
- if (!outer) {
788
- node.children.push({
789
- type: "Error",
790
- message: "Can't hoist declarations inside expressions yet."
791
- });
792
- return;
793
- }
794
- let block = outer.parent;
795
- if (block.type === "PatternMatchingStatement") {
796
- outer = block;
797
- block = block.parent;
798
- }
799
- const { expressions } = block;
800
- const index = expressions.findIndex(([, s]) => outer === s);
801
- if (index < 0)
802
- throw new Error("Couldn't find expression in block for hoistable declaration.");
803
- const indent = expressions[index][0];
804
- hoistDec[0][0] = indent;
805
- expressions.splice(index, 0, hoistDec);
806
799
  node.hoistDec = null;
800
+ while (node.parent?.type !== "BlockStatement") {
801
+ node = node.parent;
802
+ }
803
+ if (node.parent) {
804
+ insertHoistDec(node.parent, node, hoistDec);
805
+ } else {
806
+ throw new Error("Couldn't find block to hoist declaration into.");
807
+ }
808
+ return;
807
809
  });
808
810
  }
811
+ function insertHoistDec(block, node, dec) {
812
+ const { expressions } = block;
813
+ const index = expressions.findIndex(([, s]) => node === s);
814
+ if (index < 0)
815
+ throw new Error("Couldn't find expression in block for hoistable declaration.");
816
+ const indent = expressions[index][0];
817
+ expressions.splice(index, 0, [indent, dec, ";"]);
818
+ }
809
819
  function insertReturn(node) {
810
820
  if (!node)
811
821
  return;
@@ -1723,8 +1733,18 @@ var require_lib = __commonJS({
1723
1733
  if (expression.type === "ParenthesizedExpression") {
1724
1734
  expression = expression.expression;
1725
1735
  }
1726
- let ref = needsRef(expression, "m") || expression;
1727
- let hoistDec = ref !== expression ? [["", ["const ", ref, " = ", expression], ";"]] : void 0;
1736
+ let hoistDec, refAssignment = [], ref = needsRef(expression, "m") || expression;
1737
+ if (ref !== expression) {
1738
+ hoistDec = {
1739
+ type: "Declaration",
1740
+ children: ["let ", ref],
1741
+ names: []
1742
+ };
1743
+ refAssignment = [{
1744
+ type: "AssignmentExpression",
1745
+ children: [ref, " = ", expression]
1746
+ }, ","];
1747
+ }
1728
1748
  let prev = [], root = prev;
1729
1749
  const l = clauses.length;
1730
1750
  clauses.forEach((c, i) => {
@@ -1752,7 +1772,7 @@ var require_lib = __commonJS({
1752
1772
  });
1753
1773
  const condition = {
1754
1774
  type: "ParenthesizedExpression",
1755
- children: ["(", conditionExpression, ")"],
1775
+ children: ["(", ...refAssignment, conditionExpression, ")"],
1756
1776
  expression: conditionExpression
1757
1777
  };
1758
1778
  const prefix = [];
@@ -1791,6 +1811,7 @@ var require_lib = __commonJS({
1791
1811
  hoistDec
1792
1812
  }]);
1793
1813
  hoistDec = void 0;
1814
+ refAssignment = [];
1794
1815
  prev = next;
1795
1816
  });
1796
1817
  if (s.type === "SwitchExpression") {
@@ -12879,7 +12900,11 @@ ${input.slice(result.pos)}
12879
12900
  const initCondition = {
12880
12901
  type: "AssignmentExpression",
12881
12902
  children: [ref, " ", initializer],
12882
- hoistDec: [["", ["let ", ref], ";"]],
12903
+ hoistDec: {
12904
+ type: "Declaration",
12905
+ children: ["let ", ref],
12906
+ names: []
12907
+ },
12883
12908
  blockPrefix: [
12884
12909
  ["", [binding, "= ", ref, ...splices], ";"],
12885
12910
  ...thisAssignments
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
- "version": "0.6.4",
3
+ "version": "0.6.5",
4
4
  "description": "CoffeeScript style syntax for TypeScript",
5
5
  "main": "dist/main.js",
6
6
  "module": "dist/main.mjs",