@danielx/civet 0.6.92 → 0.6.93

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
@@ -499,12 +499,32 @@ var Civet = (() => {
499
499
  return;
500
500
  }
501
501
  function isExit(node) {
502
- return [
503
- "ReturnStatement",
504
- "ThrowStatement",
505
- "BreakStatement",
506
- "ContinueStatement"
507
- ].includes(node?.type);
502
+ if (!(node != null)) {
503
+ return false;
504
+ }
505
+ switch (node.type) {
506
+ case "ReturnStatement":
507
+ case "ThrowStatement":
508
+ case "BreakStatement":
509
+ case "ContinueStatement": {
510
+ return true;
511
+ }
512
+ case "IfStatement": {
513
+ return isExit(node.then) && isExit(node.else?.at(-1));
514
+ }
515
+ case "BlockStatement": {
516
+ return isExit(node.expressions.at(-1)?.[1]);
517
+ }
518
+ case "IterationStatement": {
519
+ return node.condition?.type === "ParenthesizedExpression" && node.condition.expression?.type === "Literal" && node.condition.expression?.raw === "true" && gatherRecursiveWithinFunction(
520
+ node.block,
521
+ ({ type }) => type === "BreakStatement"
522
+ ).length === 0;
523
+ }
524
+ default: {
525
+ return false;
526
+ }
527
+ }
508
528
  }
509
529
  function isComma(node) {
510
530
  if (node?.token === ",") {
@@ -634,12 +654,34 @@ var Civet = (() => {
634
654
  if (Array.isArray(node)) {
635
655
  return node.map(deepCopy);
636
656
  }
657
+ if (node?.type === "Ref")
658
+ return node;
637
659
  return Object.fromEntries(
638
660
  Object.entries(node).map(([key, value]) => {
639
661
  return [key, deepCopy(value)];
640
662
  })
641
663
  );
642
664
  }
665
+ function removeHoistDecs(node) {
666
+ if (node == null)
667
+ return;
668
+ if (typeof node !== "object")
669
+ return;
670
+ if ("hoistDec" in node) {
671
+ node.hoistDec = void 0;
672
+ }
673
+ if (Array.isArray(node)) {
674
+ for (const child of node) {
675
+ removeHoistDecs(child);
676
+ }
677
+ return;
678
+ }
679
+ if (node.children) {
680
+ for (const child of node.children) {
681
+ removeHoistDecs(child);
682
+ }
683
+ }
684
+ }
643
685
  function makeAmpersandFunction(bodyAfterRef = []) {
644
686
  const ref = makeRef("$");
645
687
  const body = [ref, ...bodyAfterRef];
@@ -1182,7 +1224,7 @@ var Civet = (() => {
1182
1224
  }
1183
1225
  function processBlocks(statements) {
1184
1226
  insertSemicolon(statements);
1185
- gatherRecursive(statements, ($) => $.type === "BlockStatement").forEach(function({ expressions }) {
1227
+ gatherRecursive(statements, ($) => $.type === "BlockStatement").forEach(({ expressions }) => {
1186
1228
  return processBlocks(expressions);
1187
1229
  });
1188
1230
  }
@@ -1192,7 +1234,7 @@ var Civet = (() => {
1192
1234
  const i = i1;
1193
1235
  const s = statements[i1];
1194
1236
  if (i < l - 1) {
1195
- if (needsPrecedingSemicolon(statements[i + 1])) {
1237
+ if (needsPrecedingSemicolon(statements[i + 1][1])) {
1196
1238
  const delim = s[2];
1197
1239
  if (!delim) {
1198
1240
  s[2] = ";";
@@ -1204,40 +1246,45 @@ var Civet = (() => {
1204
1246
  }
1205
1247
  }
1206
1248
  function needsPrecedingSemicolon(exp) {
1207
- let following;
1208
- if (Array.isArray(exp)) {
1209
- [, following] = exp;
1210
- } else {
1211
- following = exp;
1249
+ if (!exp) {
1250
+ return false;
1212
1251
  }
1213
- if (!following) {
1252
+ if (Array.isArray(exp)) {
1253
+ for (let i2 = 0, len1 = exp.length; i2 < len1; i2++) {
1254
+ const child = exp[i2];
1255
+ if (!(child != null)) {
1256
+ continue;
1257
+ }
1258
+ return needsPrecedingSemicolon(child);
1259
+ }
1214
1260
  return false;
1215
1261
  }
1216
- if (Array.isArray(following)) {
1217
- return needsPrecedingSemicolon(following[0]);
1262
+ if (typeof exp === "string") {
1263
+ return /^\s*[\(\[\`\+\-\/]/.test(exp);
1218
1264
  }
1219
- switch (following.type) {
1265
+ switch (exp.type) {
1220
1266
  case "ParenthesizedExpression":
1221
1267
  case "ArrayExpression":
1222
1268
  case "ArrowFunction":
1223
1269
  case "TemplateLiteral":
1224
1270
  case "RegularExpressionLiteral":
1225
- case "RangeExpression": {
1271
+ case "RangeExpression":
1272
+ case "ComputedPropertyName": {
1226
1273
  return true;
1227
1274
  }
1228
1275
  case "AssignmentExpression": {
1229
- return startsWith(following, /^(\[|\()/);
1276
+ return startsWith(exp, /^(\[|\()/);
1230
1277
  }
1231
1278
  case "Literal": {
1232
- return following.raw?.startsWith("-") || following.raw?.startsWith("+");
1279
+ return exp.raw?.startsWith("-") || exp.raw?.startsWith("+");
1233
1280
  }
1234
1281
  case "PipelineExpression":
1235
1282
  case "UnwrappedExpression": {
1236
- return needsPrecedingSemicolon(following.children[1]);
1283
+ return needsPrecedingSemicolon(exp.children[1]);
1237
1284
  }
1238
1285
  default: {
1239
- if (following.children) {
1240
- return needsPrecedingSemicolon(following.children[0]);
1286
+ if (exp.children) {
1287
+ return needsPrecedingSemicolon(exp.children);
1241
1288
  }
1242
1289
  ;
1243
1290
  return;
@@ -1376,8 +1423,7 @@ var Civet = (() => {
1376
1423
  const { clauses } = caseBlock;
1377
1424
  for (const c of clauses) {
1378
1425
  if (c.type === "WhenClause" && c.break) {
1379
- const last = c.block?.expressions?.at(-1)?.[1];
1380
- if (isExit(last)) {
1426
+ if (isExit(c.block)) {
1381
1427
  c.children.splice(c.children.indexOf(c.break), 1);
1382
1428
  c.break = void 0;
1383
1429
  }
@@ -1936,10 +1982,16 @@ var Civet = (() => {
1936
1982
  if (!Array.isArray(node)) {
1937
1983
  return;
1938
1984
  }
1939
- let [, exp] = node;
1985
+ let [, exp, semi] = node;
1986
+ if (semi?.type === "SemicolonDelimiter") {
1987
+ return;
1988
+ }
1940
1989
  if (!exp) {
1941
1990
  return;
1942
1991
  }
1992
+ if (isExit(exp)) {
1993
+ return;
1994
+ }
1943
1995
  const outer = exp;
1944
1996
  let { type } = exp;
1945
1997
  if (type === "LabelledStatement") {
@@ -2031,6 +2083,9 @@ var Civet = (() => {
2031
2083
  if (!exp) {
2032
2084
  return;
2033
2085
  }
2086
+ if (isExit(exp)) {
2087
+ return;
2088
+ }
2034
2089
  const outer = exp;
2035
2090
  let { type } = exp;
2036
2091
  if (type === "LabelledStatement") {
@@ -2723,6 +2778,7 @@ var Civet = (() => {
2723
2778
  switch (access.type) {
2724
2779
  case "PropertyAccess":
2725
2780
  case "SliceExpression":
2781
+ case "Index":
2726
2782
  break;
2727
2783
  default:
2728
2784
  children.unshift({
@@ -2759,6 +2815,7 @@ var Civet = (() => {
2759
2815
  exp: children
2760
2816
  });
2761
2817
  arg = clone(arg);
2818
+ removeHoistDecs(arg);
2762
2819
  if (arg.children[0].type === "Ref") {
2763
2820
  arg.children[0] = usingRef;
2764
2821
  }
@@ -4398,7 +4455,7 @@ var Civet = (() => {
4398
4455
  const exp = _exp;
4399
4456
  let { ancestor } = findAncestor(exp, ($4) => $4?.type === "Call");
4400
4457
  ancestor = ancestor?.parent;
4401
- while (ancestor?.parent?.type === "UnaryExpression") {
4458
+ while (ancestor?.parent?.type === "UnaryExpression" || ancestor?.parent?.type === "NewExpression") {
4402
4459
  ancestor = ancestor.parent;
4403
4460
  }
4404
4461
  if (ancestor) {
@@ -6404,7 +6461,15 @@ ${input.slice(result.pos)}
6404
6461
  return $EVENT(ctx, state, "ArgumentsWithTrailingMemberExpressions", ArgumentsWithTrailingMemberExpressions$0);
6405
6462
  }
6406
6463
  var TrailingMemberExpressions$0 = $TS($S($Q(MemberExpressionRest), $Q($S(IndentedAtLeast, $Y($S($E($EXPECT($L6, 'TrailingMemberExpressions "?"')), $EXPECT($L7, 'TrailingMemberExpressions "."'), $N($EXPECT($R2, "TrailingMemberExpressions /[0-9]/")))), MemberExpressionRest))), function($skip, $loc, $0, $1, $2) {
6407
- return $1.concat($2);
6464
+ return $1.concat($2.map(([ws, , memberExpressionRest]) => {
6465
+ if (Array.isArray(memberExpressionRest)) {
6466
+ return [ws, ...memberExpressionRest];
6467
+ }
6468
+ return {
6469
+ ...memberExpressionRest,
6470
+ children: [ws, ...memberExpressionRest.children]
6471
+ };
6472
+ }));
6408
6473
  });
6409
6474
  function TrailingMemberExpressions(ctx, state) {
6410
6475
  return $EVENT(ctx, state, "TrailingMemberExpressions", TrailingMemberExpressions$0);
@@ -7123,19 +7188,25 @@ ${input.slice(result.pos)}
7123
7188
  return $EVENT(ctx, state, "ImplementsTarget", ImplementsTarget$0);
7124
7189
  }
7125
7190
  var ClassBody$0 = $TS($S(__, OpenBrace, $E(NestedClassElements), __, CloseBrace), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
7126
- var elements = $3;
7191
+ var expressions = $3;
7192
+ if (!expressions)
7193
+ expressions = $0[2] = [];
7127
7194
  return {
7128
- type: "ClassBody",
7195
+ type: "BlockStatement",
7196
+ subtype: "ClassBody",
7129
7197
  children: $0,
7130
- elements
7198
+ expressions
7131
7199
  };
7132
7200
  });
7133
7201
  var ClassBody$1 = $TS($S(InsertOpenBrace, $E(NestedClassElements), InsertNewline, InsertIndent, InsertCloseBrace), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
7134
- var elements = $2;
7202
+ var expressions = $2;
7203
+ if (!expressions)
7204
+ expressions = $0[1] = [];
7135
7205
  return {
7136
- type: "ClassBody",
7206
+ type: "BlockStatement",
7207
+ subtype: "ClassBody",
7137
7208
  children: $0,
7138
- elements
7209
+ expressions
7139
7210
  };
7140
7211
  });
7141
7212
  var ClassBody$$ = [ClassBody$0, ClassBody$1];
@@ -10746,6 +10817,7 @@ ${input.slice(result.pos)}
10746
10817
  var clause = $1;
10747
10818
  var block = $2;
10748
10819
  return {
10820
+ ...clause,
10749
10821
  type: "IterationStatement",
10750
10822
  children: [...clause.children, block],
10751
10823
  block
@@ -10754,15 +10826,38 @@ ${input.slice(result.pos)}
10754
10826
  function LoopStatement(ctx, state) {
10755
10827
  return $EVENT(ctx, state, "LoopStatement", LoopStatement$0);
10756
10828
  }
10757
- var LoopClause$0 = $T($S(Loop), function(value) {
10758
- return { "type": "IterationStatement", "children": [value[0]] };
10829
+ var LoopClause$0 = $TV(Loop, function($skip, $loc, $0, $1) {
10830
+ var kind = $0;
10831
+ const expression = {
10832
+ type: "Literal",
10833
+ children: ["true"],
10834
+ raw: "true"
10835
+ };
10836
+ const condition = {
10837
+ type: "ParenthesizedExpression",
10838
+ children: ["(", expression, ")"],
10839
+ expression
10840
+ };
10841
+ return {
10842
+ type: "IterationStatement",
10843
+ subtype: kind.token,
10844
+ children: [kind, condition],
10845
+ condition
10846
+ };
10759
10847
  });
10760
10848
  function LoopClause(ctx, state) {
10761
10849
  return $EVENT(ctx, state, "LoopClause", LoopClause$0);
10762
10850
  }
10763
- var DoWhileStatement$0 = $T($S(Do, NoPostfixBracedBlock, __, WhileClause), function(value) {
10764
- var block = value[1];
10765
- return { "type": "IterationStatement", "children": value, "block": block };
10851
+ var DoWhileStatement$0 = $TS($S(Do, NoPostfixBracedBlock, __, WhileClause), function($skip, $loc, $0, $1, $2, $3, $4) {
10852
+ var block = $2;
10853
+ var clause = $4;
10854
+ return {
10855
+ ...clause,
10856
+ type: "IterationStatement",
10857
+ subtype: "do-while",
10858
+ children: $0,
10859
+ block
10860
+ };
10766
10861
  });
10767
10862
  function DoWhileStatement(ctx, state) {
10768
10863
  return $EVENT(ctx, state, "DoWhileStatement", DoWhileStatement$0);
@@ -10801,6 +10896,7 @@ ${input.slice(result.pos)}
10801
10896
  }
10802
10897
  return {
10803
10898
  type: "IterationStatement",
10899
+ subtype: kind.token,
10804
10900
  children: [kind, ws, condition],
10805
10901
  condition
10806
10902
  };
@@ -12925,7 +13021,7 @@ ${input.slice(result.pos)}
12925
13021
  return $EVENT_C(ctx, state, "LetOrConstOrVar", LetOrConstOrVar$$);
12926
13022
  }
12927
13023
  var Loop$0 = $TS($S($EXPECT($L167, 'Loop "loop"'), NonIdContinue), function($skip, $loc, $0, $1, $2) {
12928
- return { $loc, token: "while(true)" };
13024
+ return { $loc, token: "while" };
12929
13025
  });
12930
13026
  function Loop(ctx, state) {
12931
13027
  return $EVENT(ctx, state, "Loop", Loop$0);
@@ -14431,7 +14527,7 @@ ${input.slice(result.pos)}
14431
14527
  "]"
14432
14528
  ];
14433
14529
  });
14434
- var TypeIndexedAccess$2 = $TS($S(CoffeePrototypeEnabled, DoubleColon, $E(IdentifierName)), function($skip, $loc, $0, $1, $2, $3) {
14530
+ var TypeIndexedAccess$2 = $TS($S(CoffeePrototypeEnabled, DoubleColon, $E($C(IdentifierName, LengthShorthand))), function($skip, $loc, $0, $1, $2, $3) {
14435
14531
  var p = $2;
14436
14532
  var id = $3;
14437
14533
  const open = { ...p, token: '["' };
package/dist/main.js CHANGED
@@ -491,12 +491,32 @@ function isWhitespaceOrEmpty(node) {
491
491
  return;
492
492
  }
493
493
  function isExit(node) {
494
- return [
495
- "ReturnStatement",
496
- "ThrowStatement",
497
- "BreakStatement",
498
- "ContinueStatement"
499
- ].includes(node?.type);
494
+ if (!(node != null)) {
495
+ return false;
496
+ }
497
+ switch (node.type) {
498
+ case "ReturnStatement":
499
+ case "ThrowStatement":
500
+ case "BreakStatement":
501
+ case "ContinueStatement": {
502
+ return true;
503
+ }
504
+ case "IfStatement": {
505
+ return isExit(node.then) && isExit(node.else?.at(-1));
506
+ }
507
+ case "BlockStatement": {
508
+ return isExit(node.expressions.at(-1)?.[1]);
509
+ }
510
+ case "IterationStatement": {
511
+ return node.condition?.type === "ParenthesizedExpression" && node.condition.expression?.type === "Literal" && node.condition.expression?.raw === "true" && gatherRecursiveWithinFunction(
512
+ node.block,
513
+ ({ type }) => type === "BreakStatement"
514
+ ).length === 0;
515
+ }
516
+ default: {
517
+ return false;
518
+ }
519
+ }
500
520
  }
501
521
  function isComma(node) {
502
522
  if (node?.token === ",") {
@@ -626,12 +646,34 @@ function deepCopy(node) {
626
646
  if (Array.isArray(node)) {
627
647
  return node.map(deepCopy);
628
648
  }
649
+ if (node?.type === "Ref")
650
+ return node;
629
651
  return Object.fromEntries(
630
652
  Object.entries(node).map(([key, value]) => {
631
653
  return [key, deepCopy(value)];
632
654
  })
633
655
  );
634
656
  }
657
+ function removeHoistDecs(node) {
658
+ if (node == null)
659
+ return;
660
+ if (typeof node !== "object")
661
+ return;
662
+ if ("hoistDec" in node) {
663
+ node.hoistDec = void 0;
664
+ }
665
+ if (Array.isArray(node)) {
666
+ for (const child of node) {
667
+ removeHoistDecs(child);
668
+ }
669
+ return;
670
+ }
671
+ if (node.children) {
672
+ for (const child of node.children) {
673
+ removeHoistDecs(child);
674
+ }
675
+ }
676
+ }
635
677
  function makeAmpersandFunction(bodyAfterRef = []) {
636
678
  const ref = makeRef("$");
637
679
  const body = [ref, ...bodyAfterRef];
@@ -1174,7 +1216,7 @@ function insertHoistDec(block, node, dec) {
1174
1216
  }
1175
1217
  function processBlocks(statements) {
1176
1218
  insertSemicolon(statements);
1177
- gatherRecursive(statements, ($) => $.type === "BlockStatement").forEach(function({ expressions }) {
1219
+ gatherRecursive(statements, ($) => $.type === "BlockStatement").forEach(({ expressions }) => {
1178
1220
  return processBlocks(expressions);
1179
1221
  });
1180
1222
  }
@@ -1184,7 +1226,7 @@ function insertSemicolon(statements) {
1184
1226
  const i = i1;
1185
1227
  const s = statements[i1];
1186
1228
  if (i < l - 1) {
1187
- if (needsPrecedingSemicolon(statements[i + 1])) {
1229
+ if (needsPrecedingSemicolon(statements[i + 1][1])) {
1188
1230
  const delim = s[2];
1189
1231
  if (!delim) {
1190
1232
  s[2] = ";";
@@ -1196,40 +1238,45 @@ function insertSemicolon(statements) {
1196
1238
  }
1197
1239
  }
1198
1240
  function needsPrecedingSemicolon(exp) {
1199
- let following;
1200
- if (Array.isArray(exp)) {
1201
- [, following] = exp;
1202
- } else {
1203
- following = exp;
1241
+ if (!exp) {
1242
+ return false;
1204
1243
  }
1205
- if (!following) {
1244
+ if (Array.isArray(exp)) {
1245
+ for (let i2 = 0, len1 = exp.length; i2 < len1; i2++) {
1246
+ const child = exp[i2];
1247
+ if (!(child != null)) {
1248
+ continue;
1249
+ }
1250
+ return needsPrecedingSemicolon(child);
1251
+ }
1206
1252
  return false;
1207
1253
  }
1208
- if (Array.isArray(following)) {
1209
- return needsPrecedingSemicolon(following[0]);
1254
+ if (typeof exp === "string") {
1255
+ return /^\s*[\(\[\`\+\-\/]/.test(exp);
1210
1256
  }
1211
- switch (following.type) {
1257
+ switch (exp.type) {
1212
1258
  case "ParenthesizedExpression":
1213
1259
  case "ArrayExpression":
1214
1260
  case "ArrowFunction":
1215
1261
  case "TemplateLiteral":
1216
1262
  case "RegularExpressionLiteral":
1217
- case "RangeExpression": {
1263
+ case "RangeExpression":
1264
+ case "ComputedPropertyName": {
1218
1265
  return true;
1219
1266
  }
1220
1267
  case "AssignmentExpression": {
1221
- return startsWith(following, /^(\[|\()/);
1268
+ return startsWith(exp, /^(\[|\()/);
1222
1269
  }
1223
1270
  case "Literal": {
1224
- return following.raw?.startsWith("-") || following.raw?.startsWith("+");
1271
+ return exp.raw?.startsWith("-") || exp.raw?.startsWith("+");
1225
1272
  }
1226
1273
  case "PipelineExpression":
1227
1274
  case "UnwrappedExpression": {
1228
- return needsPrecedingSemicolon(following.children[1]);
1275
+ return needsPrecedingSemicolon(exp.children[1]);
1229
1276
  }
1230
1277
  default: {
1231
- if (following.children) {
1232
- return needsPrecedingSemicolon(following.children[0]);
1278
+ if (exp.children) {
1279
+ return needsPrecedingSemicolon(exp.children);
1233
1280
  }
1234
1281
  ;
1235
1282
  return;
@@ -1368,8 +1415,7 @@ function processPatternMatching(statements, ReservedWord, getRef) {
1368
1415
  const { clauses } = caseBlock;
1369
1416
  for (const c of clauses) {
1370
1417
  if (c.type === "WhenClause" && c.break) {
1371
- const last = c.block?.expressions?.at(-1)?.[1];
1372
- if (isExit(last)) {
1418
+ if (isExit(c.block)) {
1373
1419
  c.children.splice(c.children.indexOf(c.break), 1);
1374
1420
  c.break = void 0;
1375
1421
  }
@@ -1928,10 +1974,16 @@ function assignResults(node, collect) {
1928
1974
  if (!Array.isArray(node)) {
1929
1975
  return;
1930
1976
  }
1931
- let [, exp] = node;
1977
+ let [, exp, semi] = node;
1978
+ if (semi?.type === "SemicolonDelimiter") {
1979
+ return;
1980
+ }
1932
1981
  if (!exp) {
1933
1982
  return;
1934
1983
  }
1984
+ if (isExit(exp)) {
1985
+ return;
1986
+ }
1935
1987
  const outer = exp;
1936
1988
  let { type } = exp;
1937
1989
  if (type === "LabelledStatement") {
@@ -2023,6 +2075,9 @@ function insertReturn(node, outerNode = node) {
2023
2075
  if (!exp) {
2024
2076
  return;
2025
2077
  }
2078
+ if (isExit(exp)) {
2079
+ return;
2080
+ }
2026
2081
  const outer = exp;
2027
2082
  let { type } = exp;
2028
2083
  if (type === "LabelledStatement") {
@@ -2715,6 +2770,7 @@ function processPipelineExpressions(statements) {
2715
2770
  switch (access.type) {
2716
2771
  case "PropertyAccess":
2717
2772
  case "SliceExpression":
2773
+ case "Index":
2718
2774
  break;
2719
2775
  default:
2720
2776
  children.unshift({
@@ -2751,6 +2807,7 @@ function processPipelineExpressions(statements) {
2751
2807
  exp: children
2752
2808
  });
2753
2809
  arg = clone(arg);
2810
+ removeHoistDecs(arg);
2754
2811
  if (arg.children[0].type === "Ref") {
2755
2812
  arg.children[0] = usingRef;
2756
2813
  }
@@ -4390,7 +4447,7 @@ function processPartialPlaceholders(statements) {
4390
4447
  const exp = _exp;
4391
4448
  let { ancestor } = findAncestor(exp, ($4) => $4?.type === "Call");
4392
4449
  ancestor = ancestor?.parent;
4393
- while (ancestor?.parent?.type === "UnaryExpression") {
4450
+ while (ancestor?.parent?.type === "UnaryExpression" || ancestor?.parent?.type === "NewExpression") {
4394
4451
  ancestor = ancestor.parent;
4395
4452
  }
4396
4453
  if (ancestor) {
@@ -6396,7 +6453,15 @@ var require_parser = __commonJS({
6396
6453
  return $EVENT(ctx, state, "ArgumentsWithTrailingMemberExpressions", ArgumentsWithTrailingMemberExpressions$0);
6397
6454
  }
6398
6455
  var TrailingMemberExpressions$0 = $TS($S($Q(MemberExpressionRest), $Q($S(IndentedAtLeast, $Y($S($E($EXPECT($L6, 'TrailingMemberExpressions "?"')), $EXPECT($L7, 'TrailingMemberExpressions "."'), $N($EXPECT($R2, "TrailingMemberExpressions /[0-9]/")))), MemberExpressionRest))), function($skip, $loc, $0, $1, $2) {
6399
- return $1.concat($2);
6456
+ return $1.concat($2.map(([ws, , memberExpressionRest]) => {
6457
+ if (Array.isArray(memberExpressionRest)) {
6458
+ return [ws, ...memberExpressionRest];
6459
+ }
6460
+ return {
6461
+ ...memberExpressionRest,
6462
+ children: [ws, ...memberExpressionRest.children]
6463
+ };
6464
+ }));
6400
6465
  });
6401
6466
  function TrailingMemberExpressions(ctx, state) {
6402
6467
  return $EVENT(ctx, state, "TrailingMemberExpressions", TrailingMemberExpressions$0);
@@ -7115,19 +7180,25 @@ var require_parser = __commonJS({
7115
7180
  return $EVENT(ctx, state, "ImplementsTarget", ImplementsTarget$0);
7116
7181
  }
7117
7182
  var ClassBody$0 = $TS($S(__, OpenBrace, $E(NestedClassElements), __, CloseBrace), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
7118
- var elements = $3;
7183
+ var expressions = $3;
7184
+ if (!expressions)
7185
+ expressions = $0[2] = [];
7119
7186
  return {
7120
- type: "ClassBody",
7187
+ type: "BlockStatement",
7188
+ subtype: "ClassBody",
7121
7189
  children: $0,
7122
- elements
7190
+ expressions
7123
7191
  };
7124
7192
  });
7125
7193
  var ClassBody$1 = $TS($S(InsertOpenBrace, $E(NestedClassElements), InsertNewline, InsertIndent, InsertCloseBrace), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
7126
- var elements = $2;
7194
+ var expressions = $2;
7195
+ if (!expressions)
7196
+ expressions = $0[1] = [];
7127
7197
  return {
7128
- type: "ClassBody",
7198
+ type: "BlockStatement",
7199
+ subtype: "ClassBody",
7129
7200
  children: $0,
7130
- elements
7201
+ expressions
7131
7202
  };
7132
7203
  });
7133
7204
  var ClassBody$$ = [ClassBody$0, ClassBody$1];
@@ -10738,6 +10809,7 @@ var require_parser = __commonJS({
10738
10809
  var clause = $1;
10739
10810
  var block = $2;
10740
10811
  return {
10812
+ ...clause,
10741
10813
  type: "IterationStatement",
10742
10814
  children: [...clause.children, block],
10743
10815
  block
@@ -10746,15 +10818,38 @@ var require_parser = __commonJS({
10746
10818
  function LoopStatement(ctx, state) {
10747
10819
  return $EVENT(ctx, state, "LoopStatement", LoopStatement$0);
10748
10820
  }
10749
- var LoopClause$0 = $T($S(Loop), function(value) {
10750
- return { "type": "IterationStatement", "children": [value[0]] };
10821
+ var LoopClause$0 = $TV(Loop, function($skip, $loc, $0, $1) {
10822
+ var kind = $0;
10823
+ const expression = {
10824
+ type: "Literal",
10825
+ children: ["true"],
10826
+ raw: "true"
10827
+ };
10828
+ const condition = {
10829
+ type: "ParenthesizedExpression",
10830
+ children: ["(", expression, ")"],
10831
+ expression
10832
+ };
10833
+ return {
10834
+ type: "IterationStatement",
10835
+ subtype: kind.token,
10836
+ children: [kind, condition],
10837
+ condition
10838
+ };
10751
10839
  });
10752
10840
  function LoopClause(ctx, state) {
10753
10841
  return $EVENT(ctx, state, "LoopClause", LoopClause$0);
10754
10842
  }
10755
- var DoWhileStatement$0 = $T($S(Do, NoPostfixBracedBlock, __, WhileClause), function(value) {
10756
- var block = value[1];
10757
- return { "type": "IterationStatement", "children": value, "block": block };
10843
+ var DoWhileStatement$0 = $TS($S(Do, NoPostfixBracedBlock, __, WhileClause), function($skip, $loc, $0, $1, $2, $3, $4) {
10844
+ var block = $2;
10845
+ var clause = $4;
10846
+ return {
10847
+ ...clause,
10848
+ type: "IterationStatement",
10849
+ subtype: "do-while",
10850
+ children: $0,
10851
+ block
10852
+ };
10758
10853
  });
10759
10854
  function DoWhileStatement(ctx, state) {
10760
10855
  return $EVENT(ctx, state, "DoWhileStatement", DoWhileStatement$0);
@@ -10793,6 +10888,7 @@ var require_parser = __commonJS({
10793
10888
  }
10794
10889
  return {
10795
10890
  type: "IterationStatement",
10891
+ subtype: kind.token,
10796
10892
  children: [kind, ws, condition],
10797
10893
  condition
10798
10894
  };
@@ -12917,7 +13013,7 @@ var require_parser = __commonJS({
12917
13013
  return $EVENT_C(ctx, state, "LetOrConstOrVar", LetOrConstOrVar$$);
12918
13014
  }
12919
13015
  var Loop$0 = $TS($S($EXPECT($L167, 'Loop "loop"'), NonIdContinue), function($skip, $loc, $0, $1, $2) {
12920
- return { $loc, token: "while(true)" };
13016
+ return { $loc, token: "while" };
12921
13017
  });
12922
13018
  function Loop(ctx, state) {
12923
13019
  return $EVENT(ctx, state, "Loop", Loop$0);
@@ -14423,7 +14519,7 @@ var require_parser = __commonJS({
14423
14519
  "]"
14424
14520
  ];
14425
14521
  });
14426
- var TypeIndexedAccess$2 = $TS($S(CoffeePrototypeEnabled, DoubleColon, $E(IdentifierName)), function($skip, $loc, $0, $1, $2, $3) {
14522
+ var TypeIndexedAccess$2 = $TS($S(CoffeePrototypeEnabled, DoubleColon, $E($C(IdentifierName, LengthShorthand))), function($skip, $loc, $0, $1, $2, $3) {
14427
14523
  var p = $2;
14428
14524
  var id = $3;
14429
14525
  const open = { ...p, token: '["' };
package/dist/main.mjs CHANGED
@@ -489,12 +489,32 @@ function isWhitespaceOrEmpty(node) {
489
489
  return;
490
490
  }
491
491
  function isExit(node) {
492
- return [
493
- "ReturnStatement",
494
- "ThrowStatement",
495
- "BreakStatement",
496
- "ContinueStatement"
497
- ].includes(node?.type);
492
+ if (!(node != null)) {
493
+ return false;
494
+ }
495
+ switch (node.type) {
496
+ case "ReturnStatement":
497
+ case "ThrowStatement":
498
+ case "BreakStatement":
499
+ case "ContinueStatement": {
500
+ return true;
501
+ }
502
+ case "IfStatement": {
503
+ return isExit(node.then) && isExit(node.else?.at(-1));
504
+ }
505
+ case "BlockStatement": {
506
+ return isExit(node.expressions.at(-1)?.[1]);
507
+ }
508
+ case "IterationStatement": {
509
+ return node.condition?.type === "ParenthesizedExpression" && node.condition.expression?.type === "Literal" && node.condition.expression?.raw === "true" && gatherRecursiveWithinFunction(
510
+ node.block,
511
+ ({ type }) => type === "BreakStatement"
512
+ ).length === 0;
513
+ }
514
+ default: {
515
+ return false;
516
+ }
517
+ }
498
518
  }
499
519
  function isComma(node) {
500
520
  if (node?.token === ",") {
@@ -624,12 +644,34 @@ function deepCopy(node) {
624
644
  if (Array.isArray(node)) {
625
645
  return node.map(deepCopy);
626
646
  }
647
+ if (node?.type === "Ref")
648
+ return node;
627
649
  return Object.fromEntries(
628
650
  Object.entries(node).map(([key, value]) => {
629
651
  return [key, deepCopy(value)];
630
652
  })
631
653
  );
632
654
  }
655
+ function removeHoistDecs(node) {
656
+ if (node == null)
657
+ return;
658
+ if (typeof node !== "object")
659
+ return;
660
+ if ("hoistDec" in node) {
661
+ node.hoistDec = void 0;
662
+ }
663
+ if (Array.isArray(node)) {
664
+ for (const child of node) {
665
+ removeHoistDecs(child);
666
+ }
667
+ return;
668
+ }
669
+ if (node.children) {
670
+ for (const child of node.children) {
671
+ removeHoistDecs(child);
672
+ }
673
+ }
674
+ }
633
675
  function makeAmpersandFunction(bodyAfterRef = []) {
634
676
  const ref = makeRef("$");
635
677
  const body = [ref, ...bodyAfterRef];
@@ -1172,7 +1214,7 @@ function insertHoistDec(block, node, dec) {
1172
1214
  }
1173
1215
  function processBlocks(statements) {
1174
1216
  insertSemicolon(statements);
1175
- gatherRecursive(statements, ($) => $.type === "BlockStatement").forEach(function({ expressions }) {
1217
+ gatherRecursive(statements, ($) => $.type === "BlockStatement").forEach(({ expressions }) => {
1176
1218
  return processBlocks(expressions);
1177
1219
  });
1178
1220
  }
@@ -1182,7 +1224,7 @@ function insertSemicolon(statements) {
1182
1224
  const i = i1;
1183
1225
  const s = statements[i1];
1184
1226
  if (i < l - 1) {
1185
- if (needsPrecedingSemicolon(statements[i + 1])) {
1227
+ if (needsPrecedingSemicolon(statements[i + 1][1])) {
1186
1228
  const delim = s[2];
1187
1229
  if (!delim) {
1188
1230
  s[2] = ";";
@@ -1194,40 +1236,45 @@ function insertSemicolon(statements) {
1194
1236
  }
1195
1237
  }
1196
1238
  function needsPrecedingSemicolon(exp) {
1197
- let following;
1198
- if (Array.isArray(exp)) {
1199
- [, following] = exp;
1200
- } else {
1201
- following = exp;
1239
+ if (!exp) {
1240
+ return false;
1202
1241
  }
1203
- if (!following) {
1242
+ if (Array.isArray(exp)) {
1243
+ for (let i2 = 0, len1 = exp.length; i2 < len1; i2++) {
1244
+ const child = exp[i2];
1245
+ if (!(child != null)) {
1246
+ continue;
1247
+ }
1248
+ return needsPrecedingSemicolon(child);
1249
+ }
1204
1250
  return false;
1205
1251
  }
1206
- if (Array.isArray(following)) {
1207
- return needsPrecedingSemicolon(following[0]);
1252
+ if (typeof exp === "string") {
1253
+ return /^\s*[\(\[\`\+\-\/]/.test(exp);
1208
1254
  }
1209
- switch (following.type) {
1255
+ switch (exp.type) {
1210
1256
  case "ParenthesizedExpression":
1211
1257
  case "ArrayExpression":
1212
1258
  case "ArrowFunction":
1213
1259
  case "TemplateLiteral":
1214
1260
  case "RegularExpressionLiteral":
1215
- case "RangeExpression": {
1261
+ case "RangeExpression":
1262
+ case "ComputedPropertyName": {
1216
1263
  return true;
1217
1264
  }
1218
1265
  case "AssignmentExpression": {
1219
- return startsWith(following, /^(\[|\()/);
1266
+ return startsWith(exp, /^(\[|\()/);
1220
1267
  }
1221
1268
  case "Literal": {
1222
- return following.raw?.startsWith("-") || following.raw?.startsWith("+");
1269
+ return exp.raw?.startsWith("-") || exp.raw?.startsWith("+");
1223
1270
  }
1224
1271
  case "PipelineExpression":
1225
1272
  case "UnwrappedExpression": {
1226
- return needsPrecedingSemicolon(following.children[1]);
1273
+ return needsPrecedingSemicolon(exp.children[1]);
1227
1274
  }
1228
1275
  default: {
1229
- if (following.children) {
1230
- return needsPrecedingSemicolon(following.children[0]);
1276
+ if (exp.children) {
1277
+ return needsPrecedingSemicolon(exp.children);
1231
1278
  }
1232
1279
  ;
1233
1280
  return;
@@ -1366,8 +1413,7 @@ function processPatternMatching(statements, ReservedWord, getRef) {
1366
1413
  const { clauses } = caseBlock;
1367
1414
  for (const c of clauses) {
1368
1415
  if (c.type === "WhenClause" && c.break) {
1369
- const last = c.block?.expressions?.at(-1)?.[1];
1370
- if (isExit(last)) {
1416
+ if (isExit(c.block)) {
1371
1417
  c.children.splice(c.children.indexOf(c.break), 1);
1372
1418
  c.break = void 0;
1373
1419
  }
@@ -1926,10 +1972,16 @@ function assignResults(node, collect) {
1926
1972
  if (!Array.isArray(node)) {
1927
1973
  return;
1928
1974
  }
1929
- let [, exp] = node;
1975
+ let [, exp, semi] = node;
1976
+ if (semi?.type === "SemicolonDelimiter") {
1977
+ return;
1978
+ }
1930
1979
  if (!exp) {
1931
1980
  return;
1932
1981
  }
1982
+ if (isExit(exp)) {
1983
+ return;
1984
+ }
1933
1985
  const outer = exp;
1934
1986
  let { type } = exp;
1935
1987
  if (type === "LabelledStatement") {
@@ -2021,6 +2073,9 @@ function insertReturn(node, outerNode = node) {
2021
2073
  if (!exp) {
2022
2074
  return;
2023
2075
  }
2076
+ if (isExit(exp)) {
2077
+ return;
2078
+ }
2024
2079
  const outer = exp;
2025
2080
  let { type } = exp;
2026
2081
  if (type === "LabelledStatement") {
@@ -2713,6 +2768,7 @@ function processPipelineExpressions(statements) {
2713
2768
  switch (access.type) {
2714
2769
  case "PropertyAccess":
2715
2770
  case "SliceExpression":
2771
+ case "Index":
2716
2772
  break;
2717
2773
  default:
2718
2774
  children.unshift({
@@ -2749,6 +2805,7 @@ function processPipelineExpressions(statements) {
2749
2805
  exp: children
2750
2806
  });
2751
2807
  arg = clone(arg);
2808
+ removeHoistDecs(arg);
2752
2809
  if (arg.children[0].type === "Ref") {
2753
2810
  arg.children[0] = usingRef;
2754
2811
  }
@@ -4388,7 +4445,7 @@ function processPartialPlaceholders(statements) {
4388
4445
  const exp = _exp;
4389
4446
  let { ancestor } = findAncestor(exp, ($4) => $4?.type === "Call");
4390
4447
  ancestor = ancestor?.parent;
4391
- while (ancestor?.parent?.type === "UnaryExpression") {
4448
+ while (ancestor?.parent?.type === "UnaryExpression" || ancestor?.parent?.type === "NewExpression") {
4392
4449
  ancestor = ancestor.parent;
4393
4450
  }
4394
4451
  if (ancestor) {
@@ -6394,7 +6451,15 @@ var require_parser = __commonJS({
6394
6451
  return $EVENT(ctx, state, "ArgumentsWithTrailingMemberExpressions", ArgumentsWithTrailingMemberExpressions$0);
6395
6452
  }
6396
6453
  var TrailingMemberExpressions$0 = $TS($S($Q(MemberExpressionRest), $Q($S(IndentedAtLeast, $Y($S($E($EXPECT($L6, 'TrailingMemberExpressions "?"')), $EXPECT($L7, 'TrailingMemberExpressions "."'), $N($EXPECT($R2, "TrailingMemberExpressions /[0-9]/")))), MemberExpressionRest))), function($skip, $loc, $0, $1, $2) {
6397
- return $1.concat($2);
6454
+ return $1.concat($2.map(([ws, , memberExpressionRest]) => {
6455
+ if (Array.isArray(memberExpressionRest)) {
6456
+ return [ws, ...memberExpressionRest];
6457
+ }
6458
+ return {
6459
+ ...memberExpressionRest,
6460
+ children: [ws, ...memberExpressionRest.children]
6461
+ };
6462
+ }));
6398
6463
  });
6399
6464
  function TrailingMemberExpressions(ctx, state) {
6400
6465
  return $EVENT(ctx, state, "TrailingMemberExpressions", TrailingMemberExpressions$0);
@@ -7113,19 +7178,25 @@ var require_parser = __commonJS({
7113
7178
  return $EVENT(ctx, state, "ImplementsTarget", ImplementsTarget$0);
7114
7179
  }
7115
7180
  var ClassBody$0 = $TS($S(__, OpenBrace, $E(NestedClassElements), __, CloseBrace), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
7116
- var elements = $3;
7181
+ var expressions = $3;
7182
+ if (!expressions)
7183
+ expressions = $0[2] = [];
7117
7184
  return {
7118
- type: "ClassBody",
7185
+ type: "BlockStatement",
7186
+ subtype: "ClassBody",
7119
7187
  children: $0,
7120
- elements
7188
+ expressions
7121
7189
  };
7122
7190
  });
7123
7191
  var ClassBody$1 = $TS($S(InsertOpenBrace, $E(NestedClassElements), InsertNewline, InsertIndent, InsertCloseBrace), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
7124
- var elements = $2;
7192
+ var expressions = $2;
7193
+ if (!expressions)
7194
+ expressions = $0[1] = [];
7125
7195
  return {
7126
- type: "ClassBody",
7196
+ type: "BlockStatement",
7197
+ subtype: "ClassBody",
7127
7198
  children: $0,
7128
- elements
7199
+ expressions
7129
7200
  };
7130
7201
  });
7131
7202
  var ClassBody$$ = [ClassBody$0, ClassBody$1];
@@ -10736,6 +10807,7 @@ var require_parser = __commonJS({
10736
10807
  var clause = $1;
10737
10808
  var block = $2;
10738
10809
  return {
10810
+ ...clause,
10739
10811
  type: "IterationStatement",
10740
10812
  children: [...clause.children, block],
10741
10813
  block
@@ -10744,15 +10816,38 @@ var require_parser = __commonJS({
10744
10816
  function LoopStatement(ctx, state) {
10745
10817
  return $EVENT(ctx, state, "LoopStatement", LoopStatement$0);
10746
10818
  }
10747
- var LoopClause$0 = $T($S(Loop), function(value) {
10748
- return { "type": "IterationStatement", "children": [value[0]] };
10819
+ var LoopClause$0 = $TV(Loop, function($skip, $loc, $0, $1) {
10820
+ var kind = $0;
10821
+ const expression = {
10822
+ type: "Literal",
10823
+ children: ["true"],
10824
+ raw: "true"
10825
+ };
10826
+ const condition = {
10827
+ type: "ParenthesizedExpression",
10828
+ children: ["(", expression, ")"],
10829
+ expression
10830
+ };
10831
+ return {
10832
+ type: "IterationStatement",
10833
+ subtype: kind.token,
10834
+ children: [kind, condition],
10835
+ condition
10836
+ };
10749
10837
  });
10750
10838
  function LoopClause(ctx, state) {
10751
10839
  return $EVENT(ctx, state, "LoopClause", LoopClause$0);
10752
10840
  }
10753
- var DoWhileStatement$0 = $T($S(Do, NoPostfixBracedBlock, __, WhileClause), function(value) {
10754
- var block = value[1];
10755
- return { "type": "IterationStatement", "children": value, "block": block };
10841
+ var DoWhileStatement$0 = $TS($S(Do, NoPostfixBracedBlock, __, WhileClause), function($skip, $loc, $0, $1, $2, $3, $4) {
10842
+ var block = $2;
10843
+ var clause = $4;
10844
+ return {
10845
+ ...clause,
10846
+ type: "IterationStatement",
10847
+ subtype: "do-while",
10848
+ children: $0,
10849
+ block
10850
+ };
10756
10851
  });
10757
10852
  function DoWhileStatement(ctx, state) {
10758
10853
  return $EVENT(ctx, state, "DoWhileStatement", DoWhileStatement$0);
@@ -10791,6 +10886,7 @@ var require_parser = __commonJS({
10791
10886
  }
10792
10887
  return {
10793
10888
  type: "IterationStatement",
10889
+ subtype: kind.token,
10794
10890
  children: [kind, ws, condition],
10795
10891
  condition
10796
10892
  };
@@ -12915,7 +13011,7 @@ var require_parser = __commonJS({
12915
13011
  return $EVENT_C(ctx, state, "LetOrConstOrVar", LetOrConstOrVar$$);
12916
13012
  }
12917
13013
  var Loop$0 = $TS($S($EXPECT($L167, 'Loop "loop"'), NonIdContinue), function($skip, $loc, $0, $1, $2) {
12918
- return { $loc, token: "while(true)" };
13014
+ return { $loc, token: "while" };
12919
13015
  });
12920
13016
  function Loop(ctx, state) {
12921
13017
  return $EVENT(ctx, state, "Loop", Loop$0);
@@ -14421,7 +14517,7 @@ var require_parser = __commonJS({
14421
14517
  "]"
14422
14518
  ];
14423
14519
  });
14424
- var TypeIndexedAccess$2 = $TS($S(CoffeePrototypeEnabled, DoubleColon, $E(IdentifierName)), function($skip, $loc, $0, $1, $2, $3) {
14520
+ var TypeIndexedAccess$2 = $TS($S(CoffeePrototypeEnabled, DoubleColon, $E($C(IdentifierName, LengthShorthand))), function($skip, $loc, $0, $1, $2, $3) {
14425
14521
  var p = $2;
14426
14522
  var id = $3;
14427
14523
  const open = { ...p, token: '["' };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
3
  "type": "commonjs",
4
- "version": "0.6.92",
4
+ "version": "0.6.93",
5
5
  "description": "CoffeeScript style syntax for TypeScript",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/main.mjs",
@@ -118,7 +118,8 @@
118
118
  "source"
119
119
  ],
120
120
  "exclude": [
121
- "source/parser/types.civet"
121
+ "source/parser/types.civet",
122
+ "source/bun-civet.civet"
122
123
  ]
123
124
  },
124
125
  "mocha": {