@danielx/civet 0.6.35 → 0.6.36

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
@@ -526,23 +526,16 @@ ${input.slice(result.pos)}
526
526
  }
527
527
  }
528
528
  function addPostfixStatement(statement, ws, post) {
529
- let children, expressions;
530
- const prefix = post.blockPrefix || post.condition?.expression.blockPrefix;
531
- if (prefix?.length) {
532
- const indent = prefix[0][0];
533
- expressions = [...prefix, [indent, statement]];
534
- children = [" {\n", ...expressions, "\n", indent?.slice?.(0, -2), "}"];
535
- } else {
536
- expressions = [["", statement]];
537
- children = [" { ", ...expressions, " }"];
538
- }
529
+ const expressions = [
530
+ ...post.blockPrefix || [],
531
+ ["", statement]
532
+ ];
539
533
  const block = {
540
534
  type: "BlockStatement",
541
- children,
535
+ children: [" { ", expressions, " }"],
542
536
  expressions
543
537
  };
544
- children = [...post.children];
545
- children.push(block);
538
+ const children = [...post.children, block];
546
539
  if (!isWhitespaceOrEmpty(ws))
547
540
  children.push(ws);
548
541
  post = { ...post, children, block };
@@ -2118,6 +2111,81 @@ ${input.slice(result.pos)}
2118
2111
  children
2119
2112
  };
2120
2113
  }
2114
+ function processDeclarationCondition(condition) {
2115
+ if (!(condition.type === "DeclarationCondition")) {
2116
+ return;
2117
+ }
2118
+ const ref = makeRef();
2119
+ const { decl, bindings } = condition.declaration;
2120
+ const binding = bindings[0];
2121
+ const { pattern, suffix, initializer, splices, thisAssignments } = binding;
2122
+ const initCondition = {
2123
+ type: "AssignmentExpression",
2124
+ children: [ref, initializer],
2125
+ hoistDec: {
2126
+ type: "Declaration",
2127
+ children: ["let ", ref, suffix],
2128
+ names: []
2129
+ },
2130
+ blockPrefix: [
2131
+ ["", [decl, pattern, suffix, " = ", ref, ...splices], ";"],
2132
+ ...thisAssignments
2133
+ ],
2134
+ pattern,
2135
+ ref
2136
+ };
2137
+ Object.assign(condition, initCondition);
2138
+ }
2139
+ function processDeclarationConditions(node) {
2140
+ gatherRecursiveAll(node, (n) => {
2141
+ return n.type === "IfStatement" || n.type === "IterationStatement";
2142
+ }).forEach(processDeclarationConditionStatement);
2143
+ }
2144
+ function processDeclarationConditionStatement(s) {
2145
+ const { condition } = s;
2146
+ if (!condition) {
2147
+ return;
2148
+ }
2149
+ processDeclarationCondition(condition.expression);
2150
+ const { ref, pattern } = condition.expression;
2151
+ if (pattern) {
2152
+ let conditions = [];
2153
+ getPatternConditions(pattern, ref, conditions);
2154
+ conditions = conditions.filter((c) => {
2155
+ return !(c.length === 3 && c[0] === "typeof " && c[1] === ref && c[2] === " === 'object'") && !(c.length === 2 && c[0] === ref && c[1] === " != null");
2156
+ });
2157
+ if (conditions.length) {
2158
+ condition.children.unshift("(");
2159
+ conditions.forEach(function(c) {
2160
+ return condition.children.push(" && ", c);
2161
+ });
2162
+ condition.children.push(")");
2163
+ }
2164
+ }
2165
+ switch (s.type) {
2166
+ case "IfStatement": {
2167
+ const { else: e } = s;
2168
+ const block = blockWithPrefix(condition.expression.blockPrefix, s.then);
2169
+ s.then = block;
2170
+ const toAdd = [block];
2171
+ if (block.bare && e) {
2172
+ toAdd.push(";");
2173
+ }
2174
+ s.children.splice(2, 1, ...toAdd);
2175
+ updateParentPointers(block, s);
2176
+ break;
2177
+ }
2178
+ case "IterationStatement": {
2179
+ const { children, block } = s;
2180
+ const newBlock = blockWithPrefix(condition.expression.blockPrefix, block);
2181
+ s.children = children.map(function(c) {
2182
+ return c.type === "BlockStatement" ? newBlock : c;
2183
+ });
2184
+ updateParentPointers(newBlock, s);
2185
+ break;
2186
+ }
2187
+ }
2188
+ }
2121
2189
  function implicitFunctionBlock(f) {
2122
2190
  if (f.abstract || f.block || f.signature?.optional)
2123
2191
  return;
@@ -2791,6 +2859,7 @@ ${input.slice(result.pos)}
2791
2859
  assert.equal(m.JSXTagStack.length, 1, "JSXTagStack");
2792
2860
  addParentPointers(root);
2793
2861
  const { expressions: statements } = root;
2862
+ processDeclarationConditions(statements);
2794
2863
  processPipelineExpressions(statements);
2795
2864
  processAssignments(statements);
2796
2865
  processPatternMatching(statements, ReservedWord);
@@ -8096,16 +8165,10 @@ ${input.slice(result.pos)}
8096
8165
  var clause = $1;
8097
8166
  var block = $2;
8098
8167
  var e = $3;
8099
- const children = [...clause.children];
8100
- block = blockWithPrefix(clause.condition.expression.blockPrefix, block);
8101
- children.push(block);
8102
- if (block.bare && e)
8103
- children.push(";");
8104
- if (e)
8105
- children.push(e);
8106
8168
  return {
8107
- ...clause,
8108
- children,
8169
+ type: "IfStatement",
8170
+ children: [...clause.children, block, e],
8171
+ condition: clause.condition,
8109
8172
  then: block,
8110
8173
  else: e
8111
8174
  };
@@ -8328,7 +8391,6 @@ ${input.slice(result.pos)}
8328
8391
  var WhileStatement$0 = $TS($S(WhileClause, Block), function($skip, $loc, $0, $1, $2) {
8329
8392
  var clause = $1;
8330
8393
  var block = $2;
8331
- block = blockWithPrefix(clause.condition.expression.blockPrefix, block);
8332
8394
  return {
8333
8395
  ...clause,
8334
8396
  children: [...clause.children, block],
@@ -8947,27 +9009,13 @@ ${input.slice(result.pos)}
8947
9009
  return $EVENT_C(ctx, state, "Condition", Condition$$);
8948
9010
  }
8949
9011
  var DeclarationCondition$0 = $TS($S(ForbidIndentedApplication, $E(LexicalDeclaration), RestoreIndentedApplication), function($skip, $loc, $0, $1, $2, $3) {
8950
- var dec = $2;
8951
- if (!dec)
9012
+ var declaration = $2;
9013
+ if (!declaration)
8952
9014
  return $skip;
8953
- const ref = makeRef();
8954
- const { decl, bindings } = dec;
8955
- const binding = bindings[0];
8956
- const { pattern, suffix, initializer, splices, thisAssignments } = binding;
8957
- const initCondition = {
8958
- type: "AssignmentExpression",
8959
- children: [ref, initializer],
8960
- hoistDec: {
8961
- type: "Declaration",
8962
- children: ["let ", ref, suffix],
8963
- names: []
8964
- },
8965
- blockPrefix: [
8966
- ["", [decl, pattern, suffix, " = ", ref, ...splices], ";"],
8967
- ...thisAssignments
8968
- ]
9015
+ return {
9016
+ type: "DeclarationCondition",
9017
+ declaration
8969
9018
  };
8970
- return initCondition;
8971
9019
  });
8972
9020
  function DeclarationCondition(ctx, state) {
8973
9021
  return $EVENT(ctx, state, "DeclarationCondition", DeclarationCondition$0);
package/dist/main.js CHANGED
@@ -525,23 +525,16 @@ var require_lib = __commonJS({
525
525
  }
526
526
  }
527
527
  function addPostfixStatement(statement, ws, post) {
528
- let children, expressions;
529
- const prefix = post.blockPrefix || post.condition?.expression.blockPrefix;
530
- if (prefix?.length) {
531
- const indent = prefix[0][0];
532
- expressions = [...prefix, [indent, statement]];
533
- children = [" {\n", ...expressions, "\n", indent?.slice?.(0, -2), "}"];
534
- } else {
535
- expressions = [["", statement]];
536
- children = [" { ", ...expressions, " }"];
537
- }
528
+ const expressions = [
529
+ ...post.blockPrefix || [],
530
+ ["", statement]
531
+ ];
538
532
  const block = {
539
533
  type: "BlockStatement",
540
- children,
534
+ children: [" { ", expressions, " }"],
541
535
  expressions
542
536
  };
543
- children = [...post.children];
544
- children.push(block);
537
+ const children = [...post.children, block];
545
538
  if (!isWhitespaceOrEmpty(ws))
546
539
  children.push(ws);
547
540
  post = { ...post, children, block };
@@ -2117,6 +2110,81 @@ var require_lib = __commonJS({
2117
2110
  children
2118
2111
  };
2119
2112
  }
2113
+ function processDeclarationCondition(condition) {
2114
+ if (!(condition.type === "DeclarationCondition")) {
2115
+ return;
2116
+ }
2117
+ const ref = makeRef();
2118
+ const { decl, bindings } = condition.declaration;
2119
+ const binding = bindings[0];
2120
+ const { pattern, suffix, initializer, splices, thisAssignments } = binding;
2121
+ const initCondition = {
2122
+ type: "AssignmentExpression",
2123
+ children: [ref, initializer],
2124
+ hoistDec: {
2125
+ type: "Declaration",
2126
+ children: ["let ", ref, suffix],
2127
+ names: []
2128
+ },
2129
+ blockPrefix: [
2130
+ ["", [decl, pattern, suffix, " = ", ref, ...splices], ";"],
2131
+ ...thisAssignments
2132
+ ],
2133
+ pattern,
2134
+ ref
2135
+ };
2136
+ Object.assign(condition, initCondition);
2137
+ }
2138
+ function processDeclarationConditions(node) {
2139
+ gatherRecursiveAll(node, (n) => {
2140
+ return n.type === "IfStatement" || n.type === "IterationStatement";
2141
+ }).forEach(processDeclarationConditionStatement);
2142
+ }
2143
+ function processDeclarationConditionStatement(s) {
2144
+ const { condition } = s;
2145
+ if (!condition) {
2146
+ return;
2147
+ }
2148
+ processDeclarationCondition(condition.expression);
2149
+ const { ref, pattern } = condition.expression;
2150
+ if (pattern) {
2151
+ let conditions = [];
2152
+ getPatternConditions(pattern, ref, conditions);
2153
+ conditions = conditions.filter((c) => {
2154
+ return !(c.length === 3 && c[0] === "typeof " && c[1] === ref && c[2] === " === 'object'") && !(c.length === 2 && c[0] === ref && c[1] === " != null");
2155
+ });
2156
+ if (conditions.length) {
2157
+ condition.children.unshift("(");
2158
+ conditions.forEach(function(c) {
2159
+ return condition.children.push(" && ", c);
2160
+ });
2161
+ condition.children.push(")");
2162
+ }
2163
+ }
2164
+ switch (s.type) {
2165
+ case "IfStatement": {
2166
+ const { else: e } = s;
2167
+ const block = blockWithPrefix(condition.expression.blockPrefix, s.then);
2168
+ s.then = block;
2169
+ const toAdd = [block];
2170
+ if (block.bare && e) {
2171
+ toAdd.push(";");
2172
+ }
2173
+ s.children.splice(2, 1, ...toAdd);
2174
+ updateParentPointers(block, s);
2175
+ break;
2176
+ }
2177
+ case "IterationStatement": {
2178
+ const { children, block } = s;
2179
+ const newBlock = blockWithPrefix(condition.expression.blockPrefix, block);
2180
+ s.children = children.map(function(c) {
2181
+ return c.type === "BlockStatement" ? newBlock : c;
2182
+ });
2183
+ updateParentPointers(newBlock, s);
2184
+ break;
2185
+ }
2186
+ }
2187
+ }
2120
2188
  function implicitFunctionBlock(f) {
2121
2189
  if (f.abstract || f.block || f.signature?.optional)
2122
2190
  return;
@@ -2790,6 +2858,7 @@ var require_lib = __commonJS({
2790
2858
  assert.equal(m.JSXTagStack.length, 1, "JSXTagStack");
2791
2859
  addParentPointers(root);
2792
2860
  const { expressions: statements } = root;
2861
+ processDeclarationConditions(statements);
2793
2862
  processPipelineExpressions(statements);
2794
2863
  processAssignments(statements);
2795
2864
  processPatternMatching(statements, ReservedWord);
@@ -8095,16 +8164,10 @@ var require_parser = __commonJS({
8095
8164
  var clause = $1;
8096
8165
  var block = $2;
8097
8166
  var e = $3;
8098
- const children = [...clause.children];
8099
- block = blockWithPrefix(clause.condition.expression.blockPrefix, block);
8100
- children.push(block);
8101
- if (block.bare && e)
8102
- children.push(";");
8103
- if (e)
8104
- children.push(e);
8105
8167
  return {
8106
- ...clause,
8107
- children,
8168
+ type: "IfStatement",
8169
+ children: [...clause.children, block, e],
8170
+ condition: clause.condition,
8108
8171
  then: block,
8109
8172
  else: e
8110
8173
  };
@@ -8327,7 +8390,6 @@ var require_parser = __commonJS({
8327
8390
  var WhileStatement$0 = $TS($S(WhileClause, Block), function($skip, $loc, $0, $1, $2) {
8328
8391
  var clause = $1;
8329
8392
  var block = $2;
8330
- block = blockWithPrefix(clause.condition.expression.blockPrefix, block);
8331
8393
  return {
8332
8394
  ...clause,
8333
8395
  children: [...clause.children, block],
@@ -8946,27 +9008,13 @@ var require_parser = __commonJS({
8946
9008
  return $EVENT_C(ctx, state, "Condition", Condition$$);
8947
9009
  }
8948
9010
  var DeclarationCondition$0 = $TS($S(ForbidIndentedApplication, $E(LexicalDeclaration), RestoreIndentedApplication), function($skip, $loc, $0, $1, $2, $3) {
8949
- var dec = $2;
8950
- if (!dec)
9011
+ var declaration = $2;
9012
+ if (!declaration)
8951
9013
  return $skip;
8952
- const ref = makeRef();
8953
- const { decl, bindings } = dec;
8954
- const binding = bindings[0];
8955
- const { pattern, suffix, initializer, splices, thisAssignments } = binding;
8956
- const initCondition = {
8957
- type: "AssignmentExpression",
8958
- children: [ref, initializer],
8959
- hoistDec: {
8960
- type: "Declaration",
8961
- children: ["let ", ref, suffix],
8962
- names: []
8963
- },
8964
- blockPrefix: [
8965
- ["", [decl, pattern, suffix, " = ", ref, ...splices], ";"],
8966
- ...thisAssignments
8967
- ]
9014
+ return {
9015
+ type: "DeclarationCondition",
9016
+ declaration
8968
9017
  };
8969
- return initCondition;
8970
9018
  });
8971
9019
  function DeclarationCondition(ctx, state) {
8972
9020
  return $EVENT(ctx, state, "DeclarationCondition", DeclarationCondition$0);
package/dist/main.mjs CHANGED
@@ -523,23 +523,16 @@ var require_lib = __commonJS({
523
523
  }
524
524
  }
525
525
  function addPostfixStatement(statement, ws, post) {
526
- let children, expressions;
527
- const prefix = post.blockPrefix || post.condition?.expression.blockPrefix;
528
- if (prefix?.length) {
529
- const indent = prefix[0][0];
530
- expressions = [...prefix, [indent, statement]];
531
- children = [" {\n", ...expressions, "\n", indent?.slice?.(0, -2), "}"];
532
- } else {
533
- expressions = [["", statement]];
534
- children = [" { ", ...expressions, " }"];
535
- }
526
+ const expressions = [
527
+ ...post.blockPrefix || [],
528
+ ["", statement]
529
+ ];
536
530
  const block = {
537
531
  type: "BlockStatement",
538
- children,
532
+ children: [" { ", expressions, " }"],
539
533
  expressions
540
534
  };
541
- children = [...post.children];
542
- children.push(block);
535
+ const children = [...post.children, block];
543
536
  if (!isWhitespaceOrEmpty(ws))
544
537
  children.push(ws);
545
538
  post = { ...post, children, block };
@@ -2115,6 +2108,81 @@ var require_lib = __commonJS({
2115
2108
  children
2116
2109
  };
2117
2110
  }
2111
+ function processDeclarationCondition(condition) {
2112
+ if (!(condition.type === "DeclarationCondition")) {
2113
+ return;
2114
+ }
2115
+ const ref = makeRef();
2116
+ const { decl, bindings } = condition.declaration;
2117
+ const binding = bindings[0];
2118
+ const { pattern, suffix, initializer, splices, thisAssignments } = binding;
2119
+ const initCondition = {
2120
+ type: "AssignmentExpression",
2121
+ children: [ref, initializer],
2122
+ hoistDec: {
2123
+ type: "Declaration",
2124
+ children: ["let ", ref, suffix],
2125
+ names: []
2126
+ },
2127
+ blockPrefix: [
2128
+ ["", [decl, pattern, suffix, " = ", ref, ...splices], ";"],
2129
+ ...thisAssignments
2130
+ ],
2131
+ pattern,
2132
+ ref
2133
+ };
2134
+ Object.assign(condition, initCondition);
2135
+ }
2136
+ function processDeclarationConditions(node) {
2137
+ gatherRecursiveAll(node, (n) => {
2138
+ return n.type === "IfStatement" || n.type === "IterationStatement";
2139
+ }).forEach(processDeclarationConditionStatement);
2140
+ }
2141
+ function processDeclarationConditionStatement(s) {
2142
+ const { condition } = s;
2143
+ if (!condition) {
2144
+ return;
2145
+ }
2146
+ processDeclarationCondition(condition.expression);
2147
+ const { ref, pattern } = condition.expression;
2148
+ if (pattern) {
2149
+ let conditions = [];
2150
+ getPatternConditions(pattern, ref, conditions);
2151
+ conditions = conditions.filter((c) => {
2152
+ return !(c.length === 3 && c[0] === "typeof " && c[1] === ref && c[2] === " === 'object'") && !(c.length === 2 && c[0] === ref && c[1] === " != null");
2153
+ });
2154
+ if (conditions.length) {
2155
+ condition.children.unshift("(");
2156
+ conditions.forEach(function(c) {
2157
+ return condition.children.push(" && ", c);
2158
+ });
2159
+ condition.children.push(")");
2160
+ }
2161
+ }
2162
+ switch (s.type) {
2163
+ case "IfStatement": {
2164
+ const { else: e } = s;
2165
+ const block = blockWithPrefix(condition.expression.blockPrefix, s.then);
2166
+ s.then = block;
2167
+ const toAdd = [block];
2168
+ if (block.bare && e) {
2169
+ toAdd.push(";");
2170
+ }
2171
+ s.children.splice(2, 1, ...toAdd);
2172
+ updateParentPointers(block, s);
2173
+ break;
2174
+ }
2175
+ case "IterationStatement": {
2176
+ const { children, block } = s;
2177
+ const newBlock = blockWithPrefix(condition.expression.blockPrefix, block);
2178
+ s.children = children.map(function(c) {
2179
+ return c.type === "BlockStatement" ? newBlock : c;
2180
+ });
2181
+ updateParentPointers(newBlock, s);
2182
+ break;
2183
+ }
2184
+ }
2185
+ }
2118
2186
  function implicitFunctionBlock(f) {
2119
2187
  if (f.abstract || f.block || f.signature?.optional)
2120
2188
  return;
@@ -2788,6 +2856,7 @@ var require_lib = __commonJS({
2788
2856
  assert.equal(m.JSXTagStack.length, 1, "JSXTagStack");
2789
2857
  addParentPointers(root);
2790
2858
  const { expressions: statements } = root;
2859
+ processDeclarationConditions(statements);
2791
2860
  processPipelineExpressions(statements);
2792
2861
  processAssignments(statements);
2793
2862
  processPatternMatching(statements, ReservedWord);
@@ -8093,16 +8162,10 @@ var require_parser = __commonJS({
8093
8162
  var clause = $1;
8094
8163
  var block = $2;
8095
8164
  var e = $3;
8096
- const children = [...clause.children];
8097
- block = blockWithPrefix(clause.condition.expression.blockPrefix, block);
8098
- children.push(block);
8099
- if (block.bare && e)
8100
- children.push(";");
8101
- if (e)
8102
- children.push(e);
8103
8165
  return {
8104
- ...clause,
8105
- children,
8166
+ type: "IfStatement",
8167
+ children: [...clause.children, block, e],
8168
+ condition: clause.condition,
8106
8169
  then: block,
8107
8170
  else: e
8108
8171
  };
@@ -8325,7 +8388,6 @@ var require_parser = __commonJS({
8325
8388
  var WhileStatement$0 = $TS($S(WhileClause, Block), function($skip, $loc, $0, $1, $2) {
8326
8389
  var clause = $1;
8327
8390
  var block = $2;
8328
- block = blockWithPrefix(clause.condition.expression.blockPrefix, block);
8329
8391
  return {
8330
8392
  ...clause,
8331
8393
  children: [...clause.children, block],
@@ -8944,27 +9006,13 @@ var require_parser = __commonJS({
8944
9006
  return $EVENT_C(ctx, state, "Condition", Condition$$);
8945
9007
  }
8946
9008
  var DeclarationCondition$0 = $TS($S(ForbidIndentedApplication, $E(LexicalDeclaration), RestoreIndentedApplication), function($skip, $loc, $0, $1, $2, $3) {
8947
- var dec = $2;
8948
- if (!dec)
9009
+ var declaration = $2;
9010
+ if (!declaration)
8949
9011
  return $skip;
8950
- const ref = makeRef();
8951
- const { decl, bindings } = dec;
8952
- const binding = bindings[0];
8953
- const { pattern, suffix, initializer, splices, thisAssignments } = binding;
8954
- const initCondition = {
8955
- type: "AssignmentExpression",
8956
- children: [ref, initializer],
8957
- hoistDec: {
8958
- type: "Declaration",
8959
- children: ["let ", ref, suffix],
8960
- names: []
8961
- },
8962
- blockPrefix: [
8963
- ["", [decl, pattern, suffix, " = ", ref, ...splices], ";"],
8964
- ...thisAssignments
8965
- ]
9012
+ return {
9013
+ type: "DeclarationCondition",
9014
+ declaration
8966
9015
  };
8967
- return initCondition;
8968
9016
  });
8969
9017
  function DeclarationCondition(ctx, state) {
8970
9018
  return $EVENT(ctx, state, "DeclarationCondition", DeclarationCondition$0);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@danielx/civet",
3
3
  "type": "commonjs",
4
- "version": "0.6.35",
4
+ "version": "0.6.36",
5
5
  "description": "CoffeeScript style syntax for TypeScript",
6
6
  "main": "dist/main.js",
7
7
  "module": "dist/main.mjs",