@danielx/civet 0.6.83 → 0.6.84

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
@@ -1186,7 +1186,7 @@ var Civet = (() => {
1186
1186
  });
1187
1187
 
1188
1188
  // source/parser/pattern-matching.civet
1189
- function processPatternMatching(statements, ReservedWord) {
1189
+ function processPatternMatching(statements, ReservedWord, getRef) {
1190
1190
  gatherRecursiveAll(statements, ($) => $.type === "SwitchStatement").forEach((s) => {
1191
1191
  const { caseBlock } = s;
1192
1192
  const { clauses } = caseBlock;
@@ -1246,7 +1246,7 @@ var Civet = (() => {
1246
1246
  const indent = block.expressions?.[0]?.[0] || "";
1247
1247
  const alternativeConditions = patterns.map((pattern2, i2) => {
1248
1248
  const conditions = [];
1249
- getPatternConditions(pattern2, ref, conditions);
1249
+ getPatternConditions(pattern2, ref, conditions, getRef);
1250
1250
  return conditions;
1251
1251
  });
1252
1252
  const conditionExpression = alternativeConditions.map((conditions, i2) => {
@@ -1304,26 +1304,26 @@ var Civet = (() => {
1304
1304
  return addParentPointers(s, s.parent);
1305
1305
  });
1306
1306
  }
1307
- function getPatternConditions(pattern, ref, conditions) {
1307
+ function getPatternConditions(pattern, ref, conditions, getRef) {
1308
1308
  if (pattern.rest)
1309
1309
  return;
1310
1310
  switch (pattern.type) {
1311
1311
  case "ArrayBindingPattern": {
1312
- const { elements, length } = pattern, hasRest = elements.some((e) => e.rest), comparator = hasRest ? " >= " : " === ", l = [comparator, (length - hasRest).toString()];
1312
+ const { elements, length } = pattern, hasRest = elements.some((e) => e.rest), l = (length - hasRest).toString(), lengthCheck = hasRest ? [ref, ".length >= ", l] : [getRef("len"), "(", ref, ", ", l, ")"];
1313
1313
  conditions.push(
1314
1314
  ["Array.isArray(", ref, ")"],
1315
- [ref, ".length", l]
1315
+ lengthCheck
1316
1316
  );
1317
1317
  elements.forEach(({ children: [, e] }, i) => {
1318
1318
  const subRef = [ref, "[", i.toString(), "]"];
1319
- return getPatternConditions(e, subRef, conditions);
1319
+ return getPatternConditions(e, subRef, conditions, getRef);
1320
1320
  });
1321
1321
  const { blockPrefix } = pattern;
1322
1322
  if (blockPrefix) {
1323
1323
  const postElements = blockPrefix.children[1], { length: postLength } = postElements;
1324
1324
  postElements.forEach(({ children: [, e] }, i) => {
1325
1325
  const subRef = [ref, "[", ref, ".length - ", (postLength + i).toString(), "]"];
1326
- return getPatternConditions(e, subRef, conditions);
1326
+ return getPatternConditions(e, subRef, conditions, getRef);
1327
1327
  });
1328
1328
  }
1329
1329
  break;
@@ -1355,7 +1355,7 @@ var Civet = (() => {
1355
1355
  subRef = [ref, ".", name];
1356
1356
  }
1357
1357
  if (value) {
1358
- getPatternConditions(value, subRef, conditions);
1358
+ getPatternConditions(value, subRef, conditions, getRef);
1359
1359
  }
1360
1360
  break;
1361
1361
  }
@@ -2218,12 +2218,14 @@ var Civet = (() => {
2218
2218
  ]
2219
2219
  });
2220
2220
  }
2221
- function processDeclarationConditions(node) {
2221
+ function processDeclarationConditions(node, getRef) {
2222
2222
  gatherRecursiveAll(node, (n) => {
2223
2223
  return n.type === "IfStatement" || n.type === "IterationStatement" || n.type === "SwitchStatement";
2224
- }).forEach(processDeclarationConditionStatement);
2224
+ }).forEach((s) => {
2225
+ return processDeclarationConditionStatement(s, getRef);
2226
+ });
2225
2227
  }
2226
- function processDeclarationConditionStatement(s) {
2228
+ function processDeclarationConditionStatement(s, getRef) {
2227
2229
  const { condition } = s;
2228
2230
  if (!condition?.expression) {
2229
2231
  return;
@@ -2238,7 +2240,7 @@ var Civet = (() => {
2238
2240
  const { ref, pattern } = expression;
2239
2241
  if (pattern) {
2240
2242
  let conditions = [];
2241
- getPatternConditions(pattern, ref, conditions);
2243
+ getPatternConditions(pattern, ref, conditions, getRef);
2242
2244
  conditions = conditions.filter((c) => {
2243
2245
  return !(c.length === 3 && c[0] === "typeof " && c[1] === ref && c[2] === " === 'object'") && !(c.length === 2 && c[0] === ref && c[1] === " != null");
2244
2246
  });
@@ -2634,17 +2636,21 @@ var Civet = (() => {
2634
2636
  stepExp = insertTrimmingSpace(stepExp, "");
2635
2637
  stepRef = maybeRef(stepExp, "step");
2636
2638
  }
2637
- const startRef = maybeRef(start, "start");
2638
- const endRef = maybeRef(end, "end");
2639
+ let startRef = maybeRef(start, "start");
2640
+ let endRef = maybeRef(end, "end");
2639
2641
  const startRefDec = startRef !== start ? [startRef, " = ", start, ", "] : [];
2640
2642
  const endRefDec = endRef !== end ? [endRef, " = ", end, ", "] : [];
2641
2643
  let ascDec = [], ascRef, asc;
2642
2644
  if (stepRef) {
2643
- if (stepRef !== stepExp) {
2645
+ if (!(stepRef === stepExp)) {
2644
2646
  ascDec = [", ", stepRef, " = ", stepExp];
2645
2647
  }
2646
- } else if (start.type === "Literal" && end.type === "Literal") {
2648
+ } else if ("Literal" === start.type && start.type === end.type) {
2647
2649
  asc = literalValue(start) <= literalValue(end);
2650
+ if ("StringLiteral" === start.subtype && start.subtype === end.subtype) {
2651
+ startRef = literalValue(start).charCodeAt(0).toString();
2652
+ endRef = literalValue(end).charCodeAt(0).toString();
2653
+ }
2648
2654
  } else {
2649
2655
  ascRef = makeRef("asc");
2650
2656
  ascDec = [", ", ascRef, " = ", startRef, " <= ", endRef];
@@ -2656,8 +2662,9 @@ var Civet = (() => {
2656
2662
  varAssign = [...insertTrimmingSpace(varName, ""), " = "];
2657
2663
  varLet = [",", ...varName, " = ", counterRef];
2658
2664
  } else {
2665
+ const value = "StringLiteral" === start.subtype ? ["String.fromCharCode(", counterRef, ")"] : counterRef;
2659
2666
  blockPrefix = [
2660
- ["", forDeclaration, " = ", counterRef, ";"]
2667
+ ["", forDeclaration, " = ", value, ";"]
2661
2668
  ];
2662
2669
  }
2663
2670
  } else if (forDeclaration) {
@@ -3911,12 +3918,12 @@ var Civet = (() => {
3911
3918
  addParentPointers(root);
3912
3919
  const { expressions: statements } = root;
3913
3920
  processTypes(statements);
3914
- processDeclarationConditions(statements);
3921
+ processDeclarationConditions(statements, m.getRef);
3915
3922
  processPipelineExpressions(statements);
3916
3923
  processDeclarations(statements);
3917
3924
  processAssignments(statements);
3918
3925
  processStatementExpressions(statements);
3919
- processPatternMatching(statements, ReservedWord);
3926
+ processPatternMatching(statements, ReservedWord, m.getRef);
3920
3927
  gatherRecursiveAll(statements, (n) => n.type === "IterationExpression").forEach((e) => expressionizeIteration(e));
3921
3928
  hoistRefDecs(statements);
3922
3929
  processFunctions(statements, config);
@@ -9493,8 +9500,9 @@ ${input.slice(result.pos)}
9493
9500
  return $EVENT_C(ctx, state, "MethodSignature", MethodSignature$$);
9494
9501
  }
9495
9502
  var ClassElementName$0 = PropertyName;
9496
- var ClassElementName$1 = PrivateIdentifier;
9497
- var ClassElementName$$ = [ClassElementName$0, ClassElementName$1];
9503
+ var ClassElementName$1 = LengthShorthand;
9504
+ var ClassElementName$2 = PrivateIdentifier;
9505
+ var ClassElementName$$ = [ClassElementName$0, ClassElementName$1, ClassElementName$2];
9498
9506
  function ClassElementName(ctx, state) {
9499
9507
  return $EVENT_C(ctx, state, "ClassElementName", ClassElementName$$);
9500
9508
  }
@@ -11120,27 +11128,26 @@ ${input.slice(result.pos)}
11120
11128
  children: [imp, $0.slice(1)]
11121
11129
  };
11122
11130
  });
11123
- var ImportDeclaration$1 = $T($S(Import, __, TypeKeyword, __, ImportClause, __, FromClause, $E(ImportAssertion)), function(value) {
11131
+ var ImportDeclaration$1 = $T($S(Import, __, TypeKeyword, __, ImportClause, __, FromClause), function(value) {
11124
11132
  return { "type": "ImportDeclaration", "ts": true, "children": value };
11125
11133
  });
11126
- var ImportDeclaration$2 = $T($S(Import, __, ImportClause, __, FromClause, $E(ImportAssertion)), function(value) {
11134
+ var ImportDeclaration$2 = $T($S(Import, __, ImportClause, __, FromClause), function(value) {
11127
11135
  return { "type": "ImportDeclaration", "children": value };
11128
11136
  });
11129
- var ImportDeclaration$3 = $T($S(Import, __, ModuleSpecifier, $E(ImportAssertion)), function(value) {
11137
+ var ImportDeclaration$3 = $T($S(Import, __, ModuleSpecifier), function(value) {
11130
11138
  return { "type": "ImportDeclaration", "children": value };
11131
11139
  });
11132
- var ImportDeclaration$4 = $TS($S(ImpliedImport, $E($S(TypeKeyword, __)), ImportClause, __, FromClause, $E(ImportAssertion)), function($skip, $loc, $0, $1, $2, $3, $4, $5, $6) {
11140
+ var ImportDeclaration$4 = $TS($S(ImpliedImport, $E($S(TypeKeyword, __)), ImportClause, __, FromClause), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
11133
11141
  var i = $1;
11134
11142
  var t = $2;
11135
11143
  var c = $3;
11136
11144
  var w = $4;
11137
11145
  var f = $5;
11138
- var a = $6;
11139
11146
  i.$loc = {
11140
11147
  pos: f[0].$loc.pos - 1,
11141
11148
  length: f[0].$loc.length + 1
11142
11149
  };
11143
- const children = [i, t, c, w, f, a];
11150
+ const children = [i, t, c, w, f];
11144
11151
  if (!t)
11145
11152
  return children;
11146
11153
  return { type: "ImportDeclaration", ts: true, children };
@@ -11299,7 +11306,8 @@ ${input.slice(result.pos)}
11299
11306
  function ModuleExportName(ctx, state) {
11300
11307
  return $EVENT_C(ctx, state, "ModuleExportName", ModuleExportName$$);
11301
11308
  }
11302
- var ModuleSpecifier$0 = $TS($S(UnprocessedModuleSpecifier), function($skip, $loc, $0, $1) {
11309
+ var ModuleSpecifier$0 = $TS($S(UnprocessedModuleSpecifier, $E(ImportAssertion)), function($skip, $loc, $0, $1, $2) {
11310
+ var a = $2;
11303
11311
  let { token } = $1;
11304
11312
  if (module.config.rewriteTsImports) {
11305
11313
  token = token.replace(/\.([mc])?ts(['"])$/, ".$1js$2");
@@ -11310,6 +11318,8 @@ ${input.slice(result.pos)}
11310
11318
  `${module.config.rewriteCivetImports.replace(/\$/g, "$$")}$1`
11311
11319
  );
11312
11320
  }
11321
+ if (a)
11322
+ return [{ ...$1, token }, a];
11313
11323
  return { ...$1, token };
11314
11324
  });
11315
11325
  function ModuleSpecifier(ctx, state) {
@@ -11674,7 +11684,11 @@ ${input.slice(result.pos)}
11674
11684
  function TripleSingleStringCharacters(ctx, state) {
11675
11685
  return $EVENT(ctx, state, "TripleSingleStringCharacters", TripleSingleStringCharacters$0);
11676
11686
  }
11677
- var CoffeeStringSubstitution$0 = $S(CoffeeSubstitutionStart, PostfixedExpression, __, CloseBrace);
11687
+ var CoffeeStringSubstitution$0 = $TS($S(CoffeeSubstitutionStart, AllowAll, $E($S(PostfixedExpression, __, CloseBrace)), RestoreAll), function($skip, $loc, $0, $1, $2, $3, $4) {
11688
+ if (!$3)
11689
+ return $skip;
11690
+ return [$1, ...$3];
11691
+ });
11678
11692
  function CoffeeStringSubstitution(ctx, state) {
11679
11693
  return $EVENT(ctx, state, "CoffeeStringSubstitution", CoffeeStringSubstitution$0);
11680
11694
  }
@@ -11851,7 +11865,11 @@ ${input.slice(result.pos)}
11851
11865
  function _TemplateLiteral(ctx, state) {
11852
11866
  return $EVENT_C(ctx, state, "_TemplateLiteral", _TemplateLiteral$$);
11853
11867
  }
11854
- var TemplateSubstitution$0 = $S(SubstitutionStart, PostfixedExpression, __, CloseBrace);
11868
+ var TemplateSubstitution$0 = $TS($S(SubstitutionStart, AllowAll, $E($S(PostfixedExpression, __, CloseBrace)), RestoreAll), function($skip, $loc, $0, $1, $2, $3, $4) {
11869
+ if (!$3)
11870
+ return $skip;
11871
+ return [$1, ...$3];
11872
+ });
11855
11873
  function TemplateSubstitution(ctx, state) {
11856
11874
  return $EVENT(ctx, state, "TemplateSubstitution", TemplateSubstitution$0);
11857
11875
  }
@@ -14602,6 +14620,19 @@ ${input.slice(result.pos)}
14602
14620
  };
14603
14621
  module.prelude.push(["", [preludeVar, isRef, typeSuffix, " = Object.is", asAny, ";\n"]]);
14604
14622
  },
14623
+ /**
14624
+ * Array length check with type guard.
14625
+ * From tlgreg https://discord.com/channels/933472021310996512/1012166187196629113/1157386582546976873
14626
+ */
14627
+ len(lenRef) {
14628
+ module.prelude.push(["", [{
14629
+ ts: true,
14630
+ children: ["function ", lenRef, "<T extends readonly unknown[], N extends number>(arr: T, length: N): arr is T & { length: N } { return arr.length === length }"]
14631
+ }, {
14632
+ js: true,
14633
+ children: ["function ", lenRef, "(arr, length) { return arr.length === length }"]
14634
+ }], "\n"]);
14635
+ },
14605
14636
  modulo(moduloRef) {
14606
14637
  const typeSuffix = {
14607
14638
  ts: true,
package/dist/main.js CHANGED
@@ -1178,7 +1178,7 @@ var init_op = __esm({
1178
1178
  });
1179
1179
 
1180
1180
  // source/parser/pattern-matching.civet
1181
- function processPatternMatching(statements, ReservedWord) {
1181
+ function processPatternMatching(statements, ReservedWord, getRef) {
1182
1182
  gatherRecursiveAll(statements, ($) => $.type === "SwitchStatement").forEach((s) => {
1183
1183
  const { caseBlock } = s;
1184
1184
  const { clauses } = caseBlock;
@@ -1238,7 +1238,7 @@ function processPatternMatching(statements, ReservedWord) {
1238
1238
  const indent = block.expressions?.[0]?.[0] || "";
1239
1239
  const alternativeConditions = patterns.map((pattern2, i2) => {
1240
1240
  const conditions = [];
1241
- getPatternConditions(pattern2, ref, conditions);
1241
+ getPatternConditions(pattern2, ref, conditions, getRef);
1242
1242
  return conditions;
1243
1243
  });
1244
1244
  const conditionExpression = alternativeConditions.map((conditions, i2) => {
@@ -1296,26 +1296,26 @@ function processPatternMatching(statements, ReservedWord) {
1296
1296
  return addParentPointers(s, s.parent);
1297
1297
  });
1298
1298
  }
1299
- function getPatternConditions(pattern, ref, conditions) {
1299
+ function getPatternConditions(pattern, ref, conditions, getRef) {
1300
1300
  if (pattern.rest)
1301
1301
  return;
1302
1302
  switch (pattern.type) {
1303
1303
  case "ArrayBindingPattern": {
1304
- const { elements, length } = pattern, hasRest = elements.some((e) => e.rest), comparator = hasRest ? " >= " : " === ", l = [comparator, (length - hasRest).toString()];
1304
+ const { elements, length } = pattern, hasRest = elements.some((e) => e.rest), l = (length - hasRest).toString(), lengthCheck = hasRest ? [ref, ".length >= ", l] : [getRef("len"), "(", ref, ", ", l, ")"];
1305
1305
  conditions.push(
1306
1306
  ["Array.isArray(", ref, ")"],
1307
- [ref, ".length", l]
1307
+ lengthCheck
1308
1308
  );
1309
1309
  elements.forEach(({ children: [, e] }, i) => {
1310
1310
  const subRef = [ref, "[", i.toString(), "]"];
1311
- return getPatternConditions(e, subRef, conditions);
1311
+ return getPatternConditions(e, subRef, conditions, getRef);
1312
1312
  });
1313
1313
  const { blockPrefix } = pattern;
1314
1314
  if (blockPrefix) {
1315
1315
  const postElements = blockPrefix.children[1], { length: postLength } = postElements;
1316
1316
  postElements.forEach(({ children: [, e] }, i) => {
1317
1317
  const subRef = [ref, "[", ref, ".length - ", (postLength + i).toString(), "]"];
1318
- return getPatternConditions(e, subRef, conditions);
1318
+ return getPatternConditions(e, subRef, conditions, getRef);
1319
1319
  });
1320
1320
  }
1321
1321
  break;
@@ -1347,7 +1347,7 @@ function getPatternConditions(pattern, ref, conditions) {
1347
1347
  subRef = [ref, ".", name];
1348
1348
  }
1349
1349
  if (value) {
1350
- getPatternConditions(value, subRef, conditions);
1350
+ getPatternConditions(value, subRef, conditions, getRef);
1351
1351
  }
1352
1352
  break;
1353
1353
  }
@@ -2210,12 +2210,14 @@ function processDeclarationCondition(condition, rootCondition, parent) {
2210
2210
  ]
2211
2211
  });
2212
2212
  }
2213
- function processDeclarationConditions(node) {
2213
+ function processDeclarationConditions(node, getRef) {
2214
2214
  gatherRecursiveAll(node, (n) => {
2215
2215
  return n.type === "IfStatement" || n.type === "IterationStatement" || n.type === "SwitchStatement";
2216
- }).forEach(processDeclarationConditionStatement);
2216
+ }).forEach((s) => {
2217
+ return processDeclarationConditionStatement(s, getRef);
2218
+ });
2217
2219
  }
2218
- function processDeclarationConditionStatement(s) {
2220
+ function processDeclarationConditionStatement(s, getRef) {
2219
2221
  const { condition } = s;
2220
2222
  if (!condition?.expression) {
2221
2223
  return;
@@ -2230,7 +2232,7 @@ function processDeclarationConditionStatement(s) {
2230
2232
  const { ref, pattern } = expression;
2231
2233
  if (pattern) {
2232
2234
  let conditions = [];
2233
- getPatternConditions(pattern, ref, conditions);
2235
+ getPatternConditions(pattern, ref, conditions, getRef);
2234
2236
  conditions = conditions.filter((c) => {
2235
2237
  return !(c.length === 3 && c[0] === "typeof " && c[1] === ref && c[2] === " === 'object'") && !(c.length === 2 && c[0] === ref && c[1] === " != null");
2236
2238
  });
@@ -2626,17 +2628,21 @@ function forRange(open, forDeclaration, range, stepExp, close) {
2626
2628
  stepExp = insertTrimmingSpace(stepExp, "");
2627
2629
  stepRef = maybeRef(stepExp, "step");
2628
2630
  }
2629
- const startRef = maybeRef(start, "start");
2630
- const endRef = maybeRef(end, "end");
2631
+ let startRef = maybeRef(start, "start");
2632
+ let endRef = maybeRef(end, "end");
2631
2633
  const startRefDec = startRef !== start ? [startRef, " = ", start, ", "] : [];
2632
2634
  const endRefDec = endRef !== end ? [endRef, " = ", end, ", "] : [];
2633
2635
  let ascDec = [], ascRef, asc;
2634
2636
  if (stepRef) {
2635
- if (stepRef !== stepExp) {
2637
+ if (!(stepRef === stepExp)) {
2636
2638
  ascDec = [", ", stepRef, " = ", stepExp];
2637
2639
  }
2638
- } else if (start.type === "Literal" && end.type === "Literal") {
2640
+ } else if ("Literal" === start.type && start.type === end.type) {
2639
2641
  asc = literalValue(start) <= literalValue(end);
2642
+ if ("StringLiteral" === start.subtype && start.subtype === end.subtype) {
2643
+ startRef = literalValue(start).charCodeAt(0).toString();
2644
+ endRef = literalValue(end).charCodeAt(0).toString();
2645
+ }
2640
2646
  } else {
2641
2647
  ascRef = makeRef("asc");
2642
2648
  ascDec = [", ", ascRef, " = ", startRef, " <= ", endRef];
@@ -2648,8 +2654,9 @@ function forRange(open, forDeclaration, range, stepExp, close) {
2648
2654
  varAssign = [...insertTrimmingSpace(varName, ""), " = "];
2649
2655
  varLet = [",", ...varName, " = ", counterRef];
2650
2656
  } else {
2657
+ const value = "StringLiteral" === start.subtype ? ["String.fromCharCode(", counterRef, ")"] : counterRef;
2651
2658
  blockPrefix = [
2652
- ["", forDeclaration, " = ", counterRef, ";"]
2659
+ ["", forDeclaration, " = ", value, ";"]
2653
2660
  ];
2654
2661
  }
2655
2662
  } else if (forDeclaration) {
@@ -3903,12 +3910,12 @@ function processProgram(root, config, m, ReservedWord) {
3903
3910
  addParentPointers(root);
3904
3911
  const { expressions: statements } = root;
3905
3912
  processTypes(statements);
3906
- processDeclarationConditions(statements);
3913
+ processDeclarationConditions(statements, m.getRef);
3907
3914
  processPipelineExpressions(statements);
3908
3915
  processDeclarations(statements);
3909
3916
  processAssignments(statements);
3910
3917
  processStatementExpressions(statements);
3911
- processPatternMatching(statements, ReservedWord);
3918
+ processPatternMatching(statements, ReservedWord, m.getRef);
3912
3919
  gatherRecursiveAll(statements, (n) => n.type === "IterationExpression").forEach((e) => expressionizeIteration(e));
3913
3920
  hoistRefDecs(statements);
3914
3921
  processFunctions(statements, config);
@@ -9485,8 +9492,9 @@ var require_parser = __commonJS({
9485
9492
  return $EVENT_C(ctx, state, "MethodSignature", MethodSignature$$);
9486
9493
  }
9487
9494
  var ClassElementName$0 = PropertyName;
9488
- var ClassElementName$1 = PrivateIdentifier;
9489
- var ClassElementName$$ = [ClassElementName$0, ClassElementName$1];
9495
+ var ClassElementName$1 = LengthShorthand;
9496
+ var ClassElementName$2 = PrivateIdentifier;
9497
+ var ClassElementName$$ = [ClassElementName$0, ClassElementName$1, ClassElementName$2];
9490
9498
  function ClassElementName(ctx, state) {
9491
9499
  return $EVENT_C(ctx, state, "ClassElementName", ClassElementName$$);
9492
9500
  }
@@ -11112,27 +11120,26 @@ var require_parser = __commonJS({
11112
11120
  children: [imp, $0.slice(1)]
11113
11121
  };
11114
11122
  });
11115
- var ImportDeclaration$1 = $T($S(Import, __, TypeKeyword, __, ImportClause, __, FromClause, $E(ImportAssertion)), function(value) {
11123
+ var ImportDeclaration$1 = $T($S(Import, __, TypeKeyword, __, ImportClause, __, FromClause), function(value) {
11116
11124
  return { "type": "ImportDeclaration", "ts": true, "children": value };
11117
11125
  });
11118
- var ImportDeclaration$2 = $T($S(Import, __, ImportClause, __, FromClause, $E(ImportAssertion)), function(value) {
11126
+ var ImportDeclaration$2 = $T($S(Import, __, ImportClause, __, FromClause), function(value) {
11119
11127
  return { "type": "ImportDeclaration", "children": value };
11120
11128
  });
11121
- var ImportDeclaration$3 = $T($S(Import, __, ModuleSpecifier, $E(ImportAssertion)), function(value) {
11129
+ var ImportDeclaration$3 = $T($S(Import, __, ModuleSpecifier), function(value) {
11122
11130
  return { "type": "ImportDeclaration", "children": value };
11123
11131
  });
11124
- var ImportDeclaration$4 = $TS($S(ImpliedImport, $E($S(TypeKeyword, __)), ImportClause, __, FromClause, $E(ImportAssertion)), function($skip, $loc, $0, $1, $2, $3, $4, $5, $6) {
11132
+ var ImportDeclaration$4 = $TS($S(ImpliedImport, $E($S(TypeKeyword, __)), ImportClause, __, FromClause), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
11125
11133
  var i = $1;
11126
11134
  var t = $2;
11127
11135
  var c = $3;
11128
11136
  var w = $4;
11129
11137
  var f = $5;
11130
- var a = $6;
11131
11138
  i.$loc = {
11132
11139
  pos: f[0].$loc.pos - 1,
11133
11140
  length: f[0].$loc.length + 1
11134
11141
  };
11135
- const children = [i, t, c, w, f, a];
11142
+ const children = [i, t, c, w, f];
11136
11143
  if (!t)
11137
11144
  return children;
11138
11145
  return { type: "ImportDeclaration", ts: true, children };
@@ -11291,7 +11298,8 @@ var require_parser = __commonJS({
11291
11298
  function ModuleExportName(ctx, state) {
11292
11299
  return $EVENT_C(ctx, state, "ModuleExportName", ModuleExportName$$);
11293
11300
  }
11294
- var ModuleSpecifier$0 = $TS($S(UnprocessedModuleSpecifier), function($skip, $loc, $0, $1) {
11301
+ var ModuleSpecifier$0 = $TS($S(UnprocessedModuleSpecifier, $E(ImportAssertion)), function($skip, $loc, $0, $1, $2) {
11302
+ var a = $2;
11295
11303
  let { token } = $1;
11296
11304
  if (module2.config.rewriteTsImports) {
11297
11305
  token = token.replace(/\.([mc])?ts(['"])$/, ".$1js$2");
@@ -11302,6 +11310,8 @@ var require_parser = __commonJS({
11302
11310
  `${module2.config.rewriteCivetImports.replace(/\$/g, "$$")}$1`
11303
11311
  );
11304
11312
  }
11313
+ if (a)
11314
+ return [{ ...$1, token }, a];
11305
11315
  return { ...$1, token };
11306
11316
  });
11307
11317
  function ModuleSpecifier(ctx, state) {
@@ -11666,7 +11676,11 @@ var require_parser = __commonJS({
11666
11676
  function TripleSingleStringCharacters(ctx, state) {
11667
11677
  return $EVENT(ctx, state, "TripleSingleStringCharacters", TripleSingleStringCharacters$0);
11668
11678
  }
11669
- var CoffeeStringSubstitution$0 = $S(CoffeeSubstitutionStart, PostfixedExpression, __, CloseBrace);
11679
+ var CoffeeStringSubstitution$0 = $TS($S(CoffeeSubstitutionStart, AllowAll, $E($S(PostfixedExpression, __, CloseBrace)), RestoreAll), function($skip, $loc, $0, $1, $2, $3, $4) {
11680
+ if (!$3)
11681
+ return $skip;
11682
+ return [$1, ...$3];
11683
+ });
11670
11684
  function CoffeeStringSubstitution(ctx, state) {
11671
11685
  return $EVENT(ctx, state, "CoffeeStringSubstitution", CoffeeStringSubstitution$0);
11672
11686
  }
@@ -11843,7 +11857,11 @@ var require_parser = __commonJS({
11843
11857
  function _TemplateLiteral(ctx, state) {
11844
11858
  return $EVENT_C(ctx, state, "_TemplateLiteral", _TemplateLiteral$$);
11845
11859
  }
11846
- var TemplateSubstitution$0 = $S(SubstitutionStart, PostfixedExpression, __, CloseBrace);
11860
+ var TemplateSubstitution$0 = $TS($S(SubstitutionStart, AllowAll, $E($S(PostfixedExpression, __, CloseBrace)), RestoreAll), function($skip, $loc, $0, $1, $2, $3, $4) {
11861
+ if (!$3)
11862
+ return $skip;
11863
+ return [$1, ...$3];
11864
+ });
11847
11865
  function TemplateSubstitution(ctx, state) {
11848
11866
  return $EVENT(ctx, state, "TemplateSubstitution", TemplateSubstitution$0);
11849
11867
  }
@@ -14594,6 +14612,19 @@ var require_parser = __commonJS({
14594
14612
  };
14595
14613
  module2.prelude.push(["", [preludeVar, isRef, typeSuffix, " = Object.is", asAny, ";\n"]]);
14596
14614
  },
14615
+ /**
14616
+ * Array length check with type guard.
14617
+ * From tlgreg https://discord.com/channels/933472021310996512/1012166187196629113/1157386582546976873
14618
+ */
14619
+ len(lenRef) {
14620
+ module2.prelude.push(["", [{
14621
+ ts: true,
14622
+ children: ["function ", lenRef, "<T extends readonly unknown[], N extends number>(arr: T, length: N): arr is T & { length: N } { return arr.length === length }"]
14623
+ }, {
14624
+ js: true,
14625
+ children: ["function ", lenRef, "(arr, length) { return arr.length === length }"]
14626
+ }], "\n"]);
14627
+ },
14597
14628
  modulo(moduloRef) {
14598
14629
  const typeSuffix = {
14599
14630
  ts: true,
package/dist/main.mjs CHANGED
@@ -1176,7 +1176,7 @@ var init_op = __esm({
1176
1176
  });
1177
1177
 
1178
1178
  // source/parser/pattern-matching.civet
1179
- function processPatternMatching(statements, ReservedWord) {
1179
+ function processPatternMatching(statements, ReservedWord, getRef) {
1180
1180
  gatherRecursiveAll(statements, ($) => $.type === "SwitchStatement").forEach((s) => {
1181
1181
  const { caseBlock } = s;
1182
1182
  const { clauses } = caseBlock;
@@ -1236,7 +1236,7 @@ function processPatternMatching(statements, ReservedWord) {
1236
1236
  const indent = block.expressions?.[0]?.[0] || "";
1237
1237
  const alternativeConditions = patterns.map((pattern2, i2) => {
1238
1238
  const conditions = [];
1239
- getPatternConditions(pattern2, ref, conditions);
1239
+ getPatternConditions(pattern2, ref, conditions, getRef);
1240
1240
  return conditions;
1241
1241
  });
1242
1242
  const conditionExpression = alternativeConditions.map((conditions, i2) => {
@@ -1294,26 +1294,26 @@ function processPatternMatching(statements, ReservedWord) {
1294
1294
  return addParentPointers(s, s.parent);
1295
1295
  });
1296
1296
  }
1297
- function getPatternConditions(pattern, ref, conditions) {
1297
+ function getPatternConditions(pattern, ref, conditions, getRef) {
1298
1298
  if (pattern.rest)
1299
1299
  return;
1300
1300
  switch (pattern.type) {
1301
1301
  case "ArrayBindingPattern": {
1302
- const { elements, length } = pattern, hasRest = elements.some((e) => e.rest), comparator = hasRest ? " >= " : " === ", l = [comparator, (length - hasRest).toString()];
1302
+ const { elements, length } = pattern, hasRest = elements.some((e) => e.rest), l = (length - hasRest).toString(), lengthCheck = hasRest ? [ref, ".length >= ", l] : [getRef("len"), "(", ref, ", ", l, ")"];
1303
1303
  conditions.push(
1304
1304
  ["Array.isArray(", ref, ")"],
1305
- [ref, ".length", l]
1305
+ lengthCheck
1306
1306
  );
1307
1307
  elements.forEach(({ children: [, e] }, i) => {
1308
1308
  const subRef = [ref, "[", i.toString(), "]"];
1309
- return getPatternConditions(e, subRef, conditions);
1309
+ return getPatternConditions(e, subRef, conditions, getRef);
1310
1310
  });
1311
1311
  const { blockPrefix } = pattern;
1312
1312
  if (blockPrefix) {
1313
1313
  const postElements = blockPrefix.children[1], { length: postLength } = postElements;
1314
1314
  postElements.forEach(({ children: [, e] }, i) => {
1315
1315
  const subRef = [ref, "[", ref, ".length - ", (postLength + i).toString(), "]"];
1316
- return getPatternConditions(e, subRef, conditions);
1316
+ return getPatternConditions(e, subRef, conditions, getRef);
1317
1317
  });
1318
1318
  }
1319
1319
  break;
@@ -1345,7 +1345,7 @@ function getPatternConditions(pattern, ref, conditions) {
1345
1345
  subRef = [ref, ".", name];
1346
1346
  }
1347
1347
  if (value) {
1348
- getPatternConditions(value, subRef, conditions);
1348
+ getPatternConditions(value, subRef, conditions, getRef);
1349
1349
  }
1350
1350
  break;
1351
1351
  }
@@ -2208,12 +2208,14 @@ function processDeclarationCondition(condition, rootCondition, parent) {
2208
2208
  ]
2209
2209
  });
2210
2210
  }
2211
- function processDeclarationConditions(node) {
2211
+ function processDeclarationConditions(node, getRef) {
2212
2212
  gatherRecursiveAll(node, (n) => {
2213
2213
  return n.type === "IfStatement" || n.type === "IterationStatement" || n.type === "SwitchStatement";
2214
- }).forEach(processDeclarationConditionStatement);
2214
+ }).forEach((s) => {
2215
+ return processDeclarationConditionStatement(s, getRef);
2216
+ });
2215
2217
  }
2216
- function processDeclarationConditionStatement(s) {
2218
+ function processDeclarationConditionStatement(s, getRef) {
2217
2219
  const { condition } = s;
2218
2220
  if (!condition?.expression) {
2219
2221
  return;
@@ -2228,7 +2230,7 @@ function processDeclarationConditionStatement(s) {
2228
2230
  const { ref, pattern } = expression;
2229
2231
  if (pattern) {
2230
2232
  let conditions = [];
2231
- getPatternConditions(pattern, ref, conditions);
2233
+ getPatternConditions(pattern, ref, conditions, getRef);
2232
2234
  conditions = conditions.filter((c) => {
2233
2235
  return !(c.length === 3 && c[0] === "typeof " && c[1] === ref && c[2] === " === 'object'") && !(c.length === 2 && c[0] === ref && c[1] === " != null");
2234
2236
  });
@@ -2624,17 +2626,21 @@ function forRange(open, forDeclaration, range, stepExp, close) {
2624
2626
  stepExp = insertTrimmingSpace(stepExp, "");
2625
2627
  stepRef = maybeRef(stepExp, "step");
2626
2628
  }
2627
- const startRef = maybeRef(start, "start");
2628
- const endRef = maybeRef(end, "end");
2629
+ let startRef = maybeRef(start, "start");
2630
+ let endRef = maybeRef(end, "end");
2629
2631
  const startRefDec = startRef !== start ? [startRef, " = ", start, ", "] : [];
2630
2632
  const endRefDec = endRef !== end ? [endRef, " = ", end, ", "] : [];
2631
2633
  let ascDec = [], ascRef, asc;
2632
2634
  if (stepRef) {
2633
- if (stepRef !== stepExp) {
2635
+ if (!(stepRef === stepExp)) {
2634
2636
  ascDec = [", ", stepRef, " = ", stepExp];
2635
2637
  }
2636
- } else if (start.type === "Literal" && end.type === "Literal") {
2638
+ } else if ("Literal" === start.type && start.type === end.type) {
2637
2639
  asc = literalValue(start) <= literalValue(end);
2640
+ if ("StringLiteral" === start.subtype && start.subtype === end.subtype) {
2641
+ startRef = literalValue(start).charCodeAt(0).toString();
2642
+ endRef = literalValue(end).charCodeAt(0).toString();
2643
+ }
2638
2644
  } else {
2639
2645
  ascRef = makeRef("asc");
2640
2646
  ascDec = [", ", ascRef, " = ", startRef, " <= ", endRef];
@@ -2646,8 +2652,9 @@ function forRange(open, forDeclaration, range, stepExp, close) {
2646
2652
  varAssign = [...insertTrimmingSpace(varName, ""), " = "];
2647
2653
  varLet = [",", ...varName, " = ", counterRef];
2648
2654
  } else {
2655
+ const value = "StringLiteral" === start.subtype ? ["String.fromCharCode(", counterRef, ")"] : counterRef;
2649
2656
  blockPrefix = [
2650
- ["", forDeclaration, " = ", counterRef, ";"]
2657
+ ["", forDeclaration, " = ", value, ";"]
2651
2658
  ];
2652
2659
  }
2653
2660
  } else if (forDeclaration) {
@@ -3901,12 +3908,12 @@ function processProgram(root, config, m, ReservedWord) {
3901
3908
  addParentPointers(root);
3902
3909
  const { expressions: statements } = root;
3903
3910
  processTypes(statements);
3904
- processDeclarationConditions(statements);
3911
+ processDeclarationConditions(statements, m.getRef);
3905
3912
  processPipelineExpressions(statements);
3906
3913
  processDeclarations(statements);
3907
3914
  processAssignments(statements);
3908
3915
  processStatementExpressions(statements);
3909
- processPatternMatching(statements, ReservedWord);
3916
+ processPatternMatching(statements, ReservedWord, m.getRef);
3910
3917
  gatherRecursiveAll(statements, (n) => n.type === "IterationExpression").forEach((e) => expressionizeIteration(e));
3911
3918
  hoistRefDecs(statements);
3912
3919
  processFunctions(statements, config);
@@ -9483,8 +9490,9 @@ var require_parser = __commonJS({
9483
9490
  return $EVENT_C(ctx, state, "MethodSignature", MethodSignature$$);
9484
9491
  }
9485
9492
  var ClassElementName$0 = PropertyName;
9486
- var ClassElementName$1 = PrivateIdentifier;
9487
- var ClassElementName$$ = [ClassElementName$0, ClassElementName$1];
9493
+ var ClassElementName$1 = LengthShorthand;
9494
+ var ClassElementName$2 = PrivateIdentifier;
9495
+ var ClassElementName$$ = [ClassElementName$0, ClassElementName$1, ClassElementName$2];
9488
9496
  function ClassElementName(ctx, state) {
9489
9497
  return $EVENT_C(ctx, state, "ClassElementName", ClassElementName$$);
9490
9498
  }
@@ -11110,27 +11118,26 @@ var require_parser = __commonJS({
11110
11118
  children: [imp, $0.slice(1)]
11111
11119
  };
11112
11120
  });
11113
- var ImportDeclaration$1 = $T($S(Import, __, TypeKeyword, __, ImportClause, __, FromClause, $E(ImportAssertion)), function(value) {
11121
+ var ImportDeclaration$1 = $T($S(Import, __, TypeKeyword, __, ImportClause, __, FromClause), function(value) {
11114
11122
  return { "type": "ImportDeclaration", "ts": true, "children": value };
11115
11123
  });
11116
- var ImportDeclaration$2 = $T($S(Import, __, ImportClause, __, FromClause, $E(ImportAssertion)), function(value) {
11124
+ var ImportDeclaration$2 = $T($S(Import, __, ImportClause, __, FromClause), function(value) {
11117
11125
  return { "type": "ImportDeclaration", "children": value };
11118
11126
  });
11119
- var ImportDeclaration$3 = $T($S(Import, __, ModuleSpecifier, $E(ImportAssertion)), function(value) {
11127
+ var ImportDeclaration$3 = $T($S(Import, __, ModuleSpecifier), function(value) {
11120
11128
  return { "type": "ImportDeclaration", "children": value };
11121
11129
  });
11122
- var ImportDeclaration$4 = $TS($S(ImpliedImport, $E($S(TypeKeyword, __)), ImportClause, __, FromClause, $E(ImportAssertion)), function($skip, $loc, $0, $1, $2, $3, $4, $5, $6) {
11130
+ var ImportDeclaration$4 = $TS($S(ImpliedImport, $E($S(TypeKeyword, __)), ImportClause, __, FromClause), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
11123
11131
  var i = $1;
11124
11132
  var t = $2;
11125
11133
  var c = $3;
11126
11134
  var w = $4;
11127
11135
  var f = $5;
11128
- var a = $6;
11129
11136
  i.$loc = {
11130
11137
  pos: f[0].$loc.pos - 1,
11131
11138
  length: f[0].$loc.length + 1
11132
11139
  };
11133
- const children = [i, t, c, w, f, a];
11140
+ const children = [i, t, c, w, f];
11134
11141
  if (!t)
11135
11142
  return children;
11136
11143
  return { type: "ImportDeclaration", ts: true, children };
@@ -11289,7 +11296,8 @@ var require_parser = __commonJS({
11289
11296
  function ModuleExportName(ctx, state) {
11290
11297
  return $EVENT_C(ctx, state, "ModuleExportName", ModuleExportName$$);
11291
11298
  }
11292
- var ModuleSpecifier$0 = $TS($S(UnprocessedModuleSpecifier), function($skip, $loc, $0, $1) {
11299
+ var ModuleSpecifier$0 = $TS($S(UnprocessedModuleSpecifier, $E(ImportAssertion)), function($skip, $loc, $0, $1, $2) {
11300
+ var a = $2;
11293
11301
  let { token } = $1;
11294
11302
  if (module.config.rewriteTsImports) {
11295
11303
  token = token.replace(/\.([mc])?ts(['"])$/, ".$1js$2");
@@ -11300,6 +11308,8 @@ var require_parser = __commonJS({
11300
11308
  `${module.config.rewriteCivetImports.replace(/\$/g, "$$")}$1`
11301
11309
  );
11302
11310
  }
11311
+ if (a)
11312
+ return [{ ...$1, token }, a];
11303
11313
  return { ...$1, token };
11304
11314
  });
11305
11315
  function ModuleSpecifier(ctx, state) {
@@ -11664,7 +11674,11 @@ var require_parser = __commonJS({
11664
11674
  function TripleSingleStringCharacters(ctx, state) {
11665
11675
  return $EVENT(ctx, state, "TripleSingleStringCharacters", TripleSingleStringCharacters$0);
11666
11676
  }
11667
- var CoffeeStringSubstitution$0 = $S(CoffeeSubstitutionStart, PostfixedExpression, __, CloseBrace);
11677
+ var CoffeeStringSubstitution$0 = $TS($S(CoffeeSubstitutionStart, AllowAll, $E($S(PostfixedExpression, __, CloseBrace)), RestoreAll), function($skip, $loc, $0, $1, $2, $3, $4) {
11678
+ if (!$3)
11679
+ return $skip;
11680
+ return [$1, ...$3];
11681
+ });
11668
11682
  function CoffeeStringSubstitution(ctx, state) {
11669
11683
  return $EVENT(ctx, state, "CoffeeStringSubstitution", CoffeeStringSubstitution$0);
11670
11684
  }
@@ -11841,7 +11855,11 @@ var require_parser = __commonJS({
11841
11855
  function _TemplateLiteral(ctx, state) {
11842
11856
  return $EVENT_C(ctx, state, "_TemplateLiteral", _TemplateLiteral$$);
11843
11857
  }
11844
- var TemplateSubstitution$0 = $S(SubstitutionStart, PostfixedExpression, __, CloseBrace);
11858
+ var TemplateSubstitution$0 = $TS($S(SubstitutionStart, AllowAll, $E($S(PostfixedExpression, __, CloseBrace)), RestoreAll), function($skip, $loc, $0, $1, $2, $3, $4) {
11859
+ if (!$3)
11860
+ return $skip;
11861
+ return [$1, ...$3];
11862
+ });
11845
11863
  function TemplateSubstitution(ctx, state) {
11846
11864
  return $EVENT(ctx, state, "TemplateSubstitution", TemplateSubstitution$0);
11847
11865
  }
@@ -14592,6 +14610,19 @@ var require_parser = __commonJS({
14592
14610
  };
14593
14611
  module.prelude.push(["", [preludeVar, isRef, typeSuffix, " = Object.is", asAny, ";\n"]]);
14594
14612
  },
14613
+ /**
14614
+ * Array length check with type guard.
14615
+ * From tlgreg https://discord.com/channels/933472021310996512/1012166187196629113/1157386582546976873
14616
+ */
14617
+ len(lenRef) {
14618
+ module.prelude.push(["", [{
14619
+ ts: true,
14620
+ children: ["function ", lenRef, "<T extends readonly unknown[], N extends number>(arr: T, length: N): arr is T & { length: N } { return arr.length === length }"]
14621
+ }, {
14622
+ js: true,
14623
+ children: ["function ", lenRef, "(arr, length) { return arr.length === length }"]
14624
+ }], "\n"]);
14625
+ },
14595
14626
  modulo(moduloRef) {
14596
14627
  const typeSuffix = {
14597
14628
  ts: true,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
3
  "type": "commonjs",
4
- "version": "0.6.83",
4
+ "version": "0.6.84",
5
5
  "description": "CoffeeScript style syntax for TypeScript",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/main.mjs",