@danielx/civet 0.6.15 → 0.6.17

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
@@ -745,12 +745,15 @@ var Civet = (() => {
745
745
  }
746
746
  }
747
747
  function findAncestor(node, predicate, stopPredicate) {
748
- node = node.parent;
749
- while (node && !stopPredicate?.(node)) {
750
- if (predicate(node))
751
- return node;
752
- node = node.parent;
748
+ let { parent } = node;
749
+ while (parent && !stopPredicate?.(parent, node)) {
750
+ if (predicate(parent, node)) {
751
+ return { ancestor: parent, child: node };
752
+ }
753
+ node = parent;
754
+ parent = node.parent;
753
755
  }
756
+ return { ancestor: void 0, child: node };
754
757
  }
755
758
  function gatherNodes(node, predicate) {
756
759
  if (node == null)
@@ -823,14 +826,11 @@ var Civet = (() => {
823
826
  }
824
827
  function hoistRefDecs(statements) {
825
828
  gatherRecursiveAll(statements, (s) => s.hoistDec).forEach((node) => {
826
- let { hoistDec, parent } = node;
829
+ let { hoistDec } = node;
827
830
  node.hoistDec = null;
828
- while (parent?.type !== "BlockStatement" || parent.bare && !parent.root) {
829
- node = parent;
830
- parent = node.parent;
831
- }
832
- if (parent) {
833
- insertHoistDec(parent, node, hoistDec);
831
+ const { ancestor, child } = findAncestor(node, (ancestor2) => ancestor2.type === "BlockStatement" && (!ancestor2.bare || ancestor2.root));
832
+ if (ancestor) {
833
+ insertHoistDec(ancestor, child, hoistDec);
834
834
  } else {
835
835
  throw new Error("Couldn't find block to hoist declaration into.");
836
836
  }
@@ -946,6 +946,9 @@ var Civet = (() => {
946
946
  function isVoidType(t) {
947
947
  return t?.type === "LiteralType" && t.t.type === "VoidType";
948
948
  }
949
+ function isPromiseVoidType(t) {
950
+ return t?.type === "IdentifierType" && t.raw === "Promise" && t.args?.types?.length === 1 && isVoidType(t.args.types[0]);
951
+ }
949
952
  function isWhitespaceOrEmpty(node) {
950
953
  if (!node)
951
954
  return true;
@@ -1370,6 +1373,8 @@ var Civet = (() => {
1370
1373
  if (f.abstract || f.block || f.signature?.optional)
1371
1374
  return;
1372
1375
  const { name, parent } = f;
1376
+ if (parent?.type === "ExportDeclaration")
1377
+ return;
1373
1378
  const expressions = parent?.expressions ?? parent?.elements;
1374
1379
  const currentIndex = expressions?.findIndex(([, def]) => def === f);
1375
1380
  const following = currentIndex >= 0 && expressions[currentIndex + 1]?.[1];
@@ -1389,8 +1394,8 @@ var Civet = (() => {
1389
1394
  implicitFunctionBlock(f);
1390
1395
  processParams(f);
1391
1396
  if (!processReturnValue(f) && config.implicitReturns) {
1392
- const { block, returnType } = f;
1393
- const isVoid = isVoidType(returnType?.t);
1397
+ const { async, block, returnType } = f;
1398
+ const isVoid = isVoidType(returnType?.t) || async && isPromiseVoidType(returnType?.t);
1394
1399
  const isBlock = block?.type === "BlockStatement";
1395
1400
  if (!isVoid && isBlock) {
1396
1401
  insertReturn(block);
@@ -2112,7 +2117,7 @@ var Civet = (() => {
2112
2117
  if (!hasDec(x))
2113
2118
  return a.indexOf(x) === i;
2114
2119
  }).forEach(pushVar);
2115
- const fnNodes = gatherNodes(statements, (s) => s.type === "FunctionExpression");
2120
+ const fnNodes = gatherNodes(statements, isFunction);
2116
2121
  const forNodes = gatherNodes(statements, (s) => s.type === "ForStatement");
2117
2122
  const blockNodes = new Set(gatherNodes(statements, (s) => s.type === "BlockStatement"));
2118
2123
  fnNodes.forEach(({ block }) => blockNodes.delete(block));
@@ -2164,7 +2169,7 @@ var Civet = (() => {
2164
2169
  }
2165
2170
  let currentScope = /* @__PURE__ */ new Set();
2166
2171
  scopes.push(currentScope);
2167
- const fnNodes = gatherNodes(statements, (s) => s.type === "FunctionExpression");
2172
+ const fnNodes = gatherNodes(statements, isFunction);
2168
2173
  const forNodes = gatherNodes(statements, (s) => s.type === "ForStatement");
2169
2174
  let targetStatements = [];
2170
2175
  for (const statement of statements) {
@@ -2227,7 +2232,7 @@ var Civet = (() => {
2227
2232
  let declared;
2228
2233
  values.forEach((value) => {
2229
2234
  value.children = [ref];
2230
- const ancestor = findAncestor(
2235
+ const { ancestor } = findAncestor(
2231
2236
  value,
2232
2237
  ({ type }) => type === "Declaration",
2233
2238
  isFunction
@@ -2997,6 +3002,7 @@ ${input.slice(result.pos)}
2997
3002
  NonPipelineArgumentPart,
2998
3003
  BinaryOpExpression,
2999
3004
  BinaryOpRHS,
3005
+ WRHS,
3000
3006
  SingleLineBinaryOpRHS,
3001
3007
  RHS,
3002
3008
  ParenthesizedAssignment,
@@ -3106,6 +3112,8 @@ ${input.slice(result.pos)}
3106
3112
  ExplicitBlock,
3107
3113
  ImplicitNestedBlock,
3108
3114
  Block,
3115
+ BareNestedBlock,
3116
+ BareBlock,
3109
3117
  ThenClause,
3110
3118
  BracedOrEmptyBlock,
3111
3119
  NoPostfixBracedOrEmptyBlock,
@@ -3230,7 +3238,9 @@ ${input.slice(result.pos)}
3230
3238
  PatternExpressionList,
3231
3239
  ConditionFragment,
3232
3240
  CaseExpressionList,
3241
+ CaseExpression,
3233
3242
  ImpliedColon,
3243
+ IgnoreColon,
3234
3244
  TryStatement,
3235
3245
  TryExpression,
3236
3246
  CatchClause,
@@ -3573,6 +3583,7 @@ ${input.slice(result.pos)}
3573
3583
  EOS,
3574
3584
  EOL,
3575
3585
  DebugHere,
3586
+ InsertColon,
3576
3587
  InsertSemicolon,
3577
3588
  InsertOpenParen,
3578
3589
  InsertCloseParen,
@@ -4804,9 +4815,10 @@ ${input.slice(result.pos)}
4804
4815
  var rhs = $2;
4805
4816
  return [[], op, [], rhs];
4806
4817
  });
4807
- var BinaryOpRHS$1 = $TS($S(NewlineBinaryOpAllowed, $S(NotDedentedBinaryOp, $C(_, $S(EOS, __)), RHS)), function($skip, $loc, $0, $1, $2) {
4808
- var rhs = $2;
4809
- return [...rhs[0], ...rhs.slice(1)];
4818
+ var BinaryOpRHS$1 = $TS($S(NewlineBinaryOpAllowed, NotDedentedBinaryOp, WRHS), function($skip, $loc, $0, $1, $2, $3) {
4819
+ var op = $2;
4820
+ var rhs = $3;
4821
+ return [...op, ...rhs];
4810
4822
  });
4811
4823
  var BinaryOpRHS$2 = $T($S($N(NewlineBinaryOpAllowed), SingleLineBinaryOpRHS), function(value) {
4812
4824
  return value[1];
@@ -4833,6 +4845,35 @@ ${input.slice(result.pos)}
4833
4845
  return result;
4834
4846
  }
4835
4847
  }
4848
+ var WRHS$0 = $TS($S(PushIndent, $E($S(Nested, RHS)), PopIndent), function($skip, $loc, $0, $1, $2, $3) {
4849
+ var wrhs = $2;
4850
+ if (!wrhs)
4851
+ return $skip;
4852
+ return wrhs;
4853
+ });
4854
+ var WRHS$1 = $S($C(_, $S(EOS, __)), RHS);
4855
+ function WRHS(state) {
4856
+ let eventData;
4857
+ if (state.events) {
4858
+ const result = state.events.enter?.("WRHS", state);
4859
+ if (result) {
4860
+ if (result.cache)
4861
+ return result.cache;
4862
+ eventData = result.data;
4863
+ }
4864
+ }
4865
+ if (state.tokenize) {
4866
+ const result = $TOKEN("WRHS", state, WRHS$0(state) || WRHS$1(state));
4867
+ if (state.events)
4868
+ state.events.exit?.("WRHS", state, result, eventData);
4869
+ return result;
4870
+ } else {
4871
+ const result = WRHS$0(state) || WRHS$1(state);
4872
+ if (state.events)
4873
+ state.events.exit?.("WRHS", state, result, eventData);
4874
+ return result;
4875
+ }
4876
+ }
4836
4877
  var SingleLineBinaryOpRHS$0 = $TS($S($E(_), BinaryOp, $C(_, $S(EOS, __)), RHS), function($skip, $loc, $0, $1, $2, $3, $4) {
4837
4878
  var ws1 = $1;
4838
4879
  var op = $2;
@@ -5539,8 +5580,8 @@ ${input.slice(result.pos)}
5539
5580
  var PrimaryExpression$2 = TemplateLiteral;
5540
5581
  var PrimaryExpression$3 = Literal;
5541
5582
  var PrimaryExpression$4 = ArrayLiteral;
5542
- var PrimaryExpression$5 = IdentifierReference;
5543
- var PrimaryExpression$6 = FunctionExpression;
5583
+ var PrimaryExpression$5 = FunctionExpression;
5584
+ var PrimaryExpression$6 = IdentifierReference;
5544
5585
  var PrimaryExpression$7 = ClassExpression;
5545
5586
  var PrimaryExpression$8 = RegularExpressionLiteral;
5546
5587
  var PrimaryExpression$9 = ParenthesizedExpression;
@@ -8466,6 +8507,70 @@ ${input.slice(result.pos)}
8466
8507
  return result;
8467
8508
  }
8468
8509
  }
8510
+ var BareNestedBlock$0 = $TS($S($Y(EOS), AllowAll, $E(NestedBlockStatements), RestoreAll), function($skip, $loc, $0, $1, $2, $3, $4) {
8511
+ if (!$3)
8512
+ return $skip;
8513
+ return $3;
8514
+ });
8515
+ function BareNestedBlock(state) {
8516
+ let eventData;
8517
+ if (state.events) {
8518
+ const result = state.events.enter?.("BareNestedBlock", state);
8519
+ if (result) {
8520
+ if (result.cache)
8521
+ return result.cache;
8522
+ eventData = result.data;
8523
+ }
8524
+ }
8525
+ if (state.tokenize) {
8526
+ const result = $TOKEN("BareNestedBlock", state, BareNestedBlock$0(state));
8527
+ if (state.events)
8528
+ state.events.exit?.("BareNestedBlock", state, result, eventData);
8529
+ return result;
8530
+ } else {
8531
+ const result = BareNestedBlock$0(state);
8532
+ if (state.events)
8533
+ state.events.exit?.("BareNestedBlock", state, result, eventData);
8534
+ return result;
8535
+ }
8536
+ }
8537
+ var BareBlock$0 = BareNestedBlock;
8538
+ var BareBlock$1 = ExplicitBlock;
8539
+ var BareBlock$2 = ThenClause;
8540
+ var BareBlock$3 = $TS($S($E(_), $N(EOS), Statement), function($skip, $loc, $0, $1, $2, $3) {
8541
+ var ws = $1;
8542
+ var s = $3;
8543
+ const expressions = [[ws, s]];
8544
+ return {
8545
+ type: "BlockStatement",
8546
+ expressions,
8547
+ children: [expressions],
8548
+ bare: true
8549
+ };
8550
+ });
8551
+ var BareBlock$4 = EmptyBareBlock;
8552
+ function BareBlock(state) {
8553
+ let eventData;
8554
+ if (state.events) {
8555
+ const result = state.events.enter?.("BareBlock", state);
8556
+ if (result) {
8557
+ if (result.cache)
8558
+ return result.cache;
8559
+ eventData = result.data;
8560
+ }
8561
+ }
8562
+ if (state.tokenize) {
8563
+ const result = $TOKEN("BareBlock", state, BareBlock$0(state) || BareBlock$1(state) || BareBlock$2(state) || BareBlock$3(state) || BareBlock$4(state));
8564
+ if (state.events)
8565
+ state.events.exit?.("BareBlock", state, result, eventData);
8566
+ return result;
8567
+ } else {
8568
+ const result = BareBlock$0(state) || BareBlock$1(state) || BareBlock$2(state) || BareBlock$3(state) || BareBlock$4(state);
8569
+ if (state.events)
8570
+ state.events.exit?.("BareBlock", state, result, eventData);
8571
+ return result;
8572
+ }
8573
+ }
8469
8574
  var ThenClause$0 = $T($S(Then, SingleLineStatements), function(value) {
8470
8575
  return value[1];
8471
8576
  });
@@ -10216,13 +10321,17 @@ ${input.slice(result.pos)}
10216
10321
  return {
10217
10322
  type: "ComputedPropertyName",
10218
10323
  children: $0,
10219
- expression
10324
+ expression,
10325
+ implicit: true
10220
10326
  };
10221
10327
  });
10222
10328
  var ComputedPropertyName$2 = $TS($S(InsertOpenBracket, $EXPECT($L20, fail, 'ComputedPropertyName "-"'), NumericLiteral, InsertCloseBracket), function($skip, $loc, $0, $1, $2, $3, $4) {
10329
+ const expression = [$2, $3];
10223
10330
  return {
10224
10331
  type: "ComputedPropertyName",
10225
- children: $0
10332
+ expression,
10333
+ children: [$1, expression, $4],
10334
+ implicit: true
10226
10335
  };
10227
10336
  });
10228
10337
  function ComputedPropertyName(state) {
@@ -12850,7 +12959,7 @@ ${input.slice(result.pos)}
12850
12959
  return result;
12851
12960
  }
12852
12961
  }
12853
- var CaseClause$0 = $TS($S(PatternExpressionList, $C(ThenClause, NestedBlockStatements, EmptyBareBlock)), function($skip, $loc, $0, $1, $2) {
12962
+ var CaseClause$0 = $TS($S(PatternExpressionList, $C(ThenClause, BareBlock)), function($skip, $loc, $0, $1, $2) {
12854
12963
  var patterns = $1;
12855
12964
  var block = $2;
12856
12965
  return {
@@ -12860,13 +12969,13 @@ ${input.slice(result.pos)}
12860
12969
  patterns
12861
12970
  };
12862
12971
  });
12863
- var CaseClause$1 = $T($S(Case, CaseExpressionList, $C(NestedBlockStatements, EmptyBareBlock)), function(value) {
12972
+ var CaseClause$1 = $T($S(Case, CaseExpressionList, IgnoreColon, $C(ThenClause, BareBlock)), function(value) {
12864
12973
  return { "type": "CaseClause", "children": value };
12865
12974
  });
12866
- var CaseClause$2 = $TS($S(When, CaseExpressionList, InsertOpenBrace, $C(ThenClause, NestedBlockStatements, EmptyBareBlock), InsertBreak, InsertNewline, InsertIndent, InsertCloseBrace), function($skip, $loc, $0, $1, $2, $3, $4, $5, $6, $7, $8) {
12975
+ var CaseClause$2 = $TS($S(When, CaseExpressionList, IgnoreColon, InsertOpenBrace, $C(ThenClause, BareBlock), InsertBreak, InsertNewline, InsertIndent, InsertCloseBrace), function($skip, $loc, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9) {
12867
12976
  var cases = $2;
12868
- var block = $4;
12869
- var b = $5;
12977
+ var block = $5;
12978
+ var b = $6;
12870
12979
  return {
12871
12980
  type: "WhenClause",
12872
12981
  cases,
@@ -12875,7 +12984,7 @@ ${input.slice(result.pos)}
12875
12984
  children: $0
12876
12985
  };
12877
12986
  });
12878
- var CaseClause$3 = $TS($S(Default, ImpliedColon, $C(NestedBlockStatements, EmptyBareBlock)), function($skip, $loc, $0, $1, $2, $3) {
12987
+ var CaseClause$3 = $TS($S(Default, ImpliedColon, $C(ThenClause, BareBlock)), function($skip, $loc, $0, $1, $2, $3) {
12879
12988
  var block = $3;
12880
12989
  return {
12881
12990
  type: "DefaultClause",
@@ -12971,7 +13080,7 @@ ${input.slice(result.pos)}
12971
13080
  return result;
12972
13081
  }
12973
13082
  }
12974
- var CaseExpressionList$0 = $TS($S(ForbidMultiLineImplicitObjectLiteral, $E($S($Q(_), ExpressionWithObjectApplicationForbidden, ImpliedColon)), $Q($S(__, Comma, ExpressionWithObjectApplicationForbidden, ImpliedColon)), RestoreMultiLineImplicitObjectLiteral), function($skip, $loc, $0, $1, $2, $3, $4) {
13083
+ var CaseExpressionList$0 = $TS($S(ForbidMultiLineImplicitObjectLiteral, $E($S($E(_), CaseExpression, InsertColon)), $Q($S(__, Comma, CaseExpression, InsertColon)), RestoreMultiLineImplicitObjectLiteral), function($skip, $loc, $0, $1, $2, $3, $4) {
12975
13084
  var first = $2;
12976
13085
  var rest = $3;
12977
13086
  if (!first)
@@ -13007,10 +13116,40 @@ ${input.slice(result.pos)}
13007
13116
  return result;
13008
13117
  }
13009
13118
  }
13010
- var ImpliedColon$0 = $S($E(_), Colon);
13011
- var ImpliedColon$1 = $TV($EXPECT($L0, fail, 'ImpliedColon ""'), function($skip, $loc, $0, $1) {
13012
- return { $loc, token: ":" };
13119
+ var CaseExpression$0 = $TS($S(PropertyName, $Y($S($E(_), Colon))), function($skip, $loc, $0, $1, $2) {
13120
+ var value = $1;
13121
+ if (value.type === "ComputedPropertyName") {
13122
+ if (value.implicit)
13123
+ return value.expression;
13124
+ return { ...value, type: "ArrayExpression" };
13125
+ }
13126
+ return value;
13013
13127
  });
13128
+ var CaseExpression$1 = ExpressionWithObjectApplicationForbidden;
13129
+ function CaseExpression(state) {
13130
+ let eventData;
13131
+ if (state.events) {
13132
+ const result = state.events.enter?.("CaseExpression", state);
13133
+ if (result) {
13134
+ if (result.cache)
13135
+ return result.cache;
13136
+ eventData = result.data;
13137
+ }
13138
+ }
13139
+ if (state.tokenize) {
13140
+ const result = $TOKEN("CaseExpression", state, CaseExpression$0(state) || CaseExpression$1(state));
13141
+ if (state.events)
13142
+ state.events.exit?.("CaseExpression", state, result, eventData);
13143
+ return result;
13144
+ } else {
13145
+ const result = CaseExpression$0(state) || CaseExpression$1(state);
13146
+ if (state.events)
13147
+ state.events.exit?.("CaseExpression", state, result, eventData);
13148
+ return result;
13149
+ }
13150
+ }
13151
+ var ImpliedColon$0 = $S($E(_), Colon);
13152
+ var ImpliedColon$1 = InsertColon;
13014
13153
  function ImpliedColon(state) {
13015
13154
  let eventData;
13016
13155
  if (state.events) {
@@ -13033,6 +13172,32 @@ ${input.slice(result.pos)}
13033
13172
  return result;
13034
13173
  }
13035
13174
  }
13175
+ var IgnoreColon$0 = $TV($E($S($E(_), Colon)), function($skip, $loc, $0, $1) {
13176
+ if ($1)
13177
+ return $1[0];
13178
+ });
13179
+ function IgnoreColon(state) {
13180
+ let eventData;
13181
+ if (state.events) {
13182
+ const result = state.events.enter?.("IgnoreColon", state);
13183
+ if (result) {
13184
+ if (result.cache)
13185
+ return result.cache;
13186
+ eventData = result.data;
13187
+ }
13188
+ }
13189
+ if (state.tokenize) {
13190
+ const result = $TOKEN("IgnoreColon", state, IgnoreColon$0(state));
13191
+ if (state.events)
13192
+ state.events.exit?.("IgnoreColon", state, result, eventData);
13193
+ return result;
13194
+ } else {
13195
+ const result = IgnoreColon$0(state);
13196
+ if (state.events)
13197
+ state.events.exit?.("IgnoreColon", state, result, eventData);
13198
+ return result;
13199
+ }
13200
+ }
13036
13201
  var TryStatement$0 = $TS($S(Try, $N($EXPECT($L12, fail, 'TryStatement ":"')), NoPostfixBracedOrEmptyBlock, $E(CatchClause), $E(FinallyClause)), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
13037
13202
  var t = $1;
13038
13203
  var b = $3;
@@ -14732,14 +14897,15 @@ ${input.slice(result.pos)}
14732
14897
  }
14733
14898
  }
14734
14899
  var ExportDeclaration$0 = $TS($S($E(Decorators), Export, __, Default, __, $C(HoistableDeclaration, ClassDeclaration, ExtendedExpression)), function($skip, $loc, $0, $1, $2, $3, $4, $5, $6) {
14735
- return { type: "ExportDeclaration", children: $0 };
14900
+ var declaration = $6;
14901
+ return { type: "ExportDeclaration", declaration, children: $0 };
14736
14902
  });
14737
14903
  var ExportDeclaration$1 = $TS($S(Export, __, ExportFromClause, __, FromClause), function($skip, $loc, $0, $1, $2, $3, $4, $5) {
14738
14904
  return { type: "ExportDeclaration", ts: $3.ts, children: $0 };
14739
14905
  });
14740
14906
  var ExportDeclaration$2 = $TS($S($E(Decorators), Export, __, $C(Declaration, VariableStatement, TypeAndNamedExports, ExportVarDec)), function($skip, $loc, $0, $1, $2, $3, $4) {
14741
- var decl = $4;
14742
- return { type: "ExportDeclaration", ts: decl.ts, children: $0 };
14907
+ var declaration = $4;
14908
+ return { type: "ExportDeclaration", declaration, ts: declaration.ts, children: $0 };
14743
14909
  });
14744
14910
  function ExportDeclaration(state) {
14745
14911
  let eventData;
@@ -22109,7 +22275,8 @@ ${input.slice(result.pos)}
22109
22275
  }
22110
22276
  }
22111
22277
  var TypeArguments$0 = $TS($S($EXPECT($L155, fail, 'TypeArguments "<"'), $P(TypeArgument), __, $EXPECT($L34, fail, 'TypeArguments ">"')), function($skip, $loc, $0, $1, $2, $3, $4) {
22112
- return { ts: true, children: $0 };
22278
+ var args = $2;
22279
+ return { ts: true, types: args.map(([, t]) => t), children: $0 };
22113
22280
  });
22114
22281
  function TypeArguments(state) {
22115
22282
  let eventData;
@@ -22570,6 +22737,31 @@ ${input.slice(result.pos)}
22570
22737
  return result;
22571
22738
  }
22572
22739
  }
22740
+ var InsertColon$0 = $TV($EXPECT($L0, fail, 'InsertColon ""'), function($skip, $loc, $0, $1) {
22741
+ return { $loc, token: ":" };
22742
+ });
22743
+ function InsertColon(state) {
22744
+ let eventData;
22745
+ if (state.events) {
22746
+ const result = state.events.enter?.("InsertColon", state);
22747
+ if (result) {
22748
+ if (result.cache)
22749
+ return result.cache;
22750
+ eventData = result.data;
22751
+ }
22752
+ }
22753
+ if (state.tokenize) {
22754
+ const result = $TOKEN("InsertColon", state, InsertColon$0(state));
22755
+ if (state.events)
22756
+ state.events.exit?.("InsertColon", state, result, eventData);
22757
+ return result;
22758
+ } else {
22759
+ const result = InsertColon$0(state);
22760
+ if (state.events)
22761
+ state.events.exit?.("InsertColon", state, result, eventData);
22762
+ return result;
22763
+ }
22764
+ }
22573
22765
  var InsertSemicolon$0 = $TV($EXPECT($L0, fail, 'InsertSemicolon ""'), function($skip, $loc, $0, $1) {
22574
22766
  return { $loc, token: ";" };
22575
22767
  });