espolar 0.3.0 → 0.3.1

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/index.js CHANGED
@@ -109,13 +109,13 @@ function operandOfBinaryExprNeedsParens(node, parent, where) {
109
109
  * which not contains a CallExpression
110
110
  */
111
111
  function validUnparenthesizedNewOperand(node) {
112
+ if (node.type === "ChainExpression" || node.type === "ImportExpression") return false;
112
113
  let cur = node;
113
114
  while (true) if (cur.type === "CallExpression") return false;
114
115
  else if (cur.type === "TSNonNullExpression") cur = cur.expression;
115
116
  else if (cur.type === "MemberExpression") cur = cur.object;
116
117
  else if (cur.type === "TaggedTemplateExpression") cur = cur.tag;
117
- else if (cur.type === "MetaProperty" || cur.type === "NewExpression" || cur.type === "ImportExpression") return true;
118
- else return cur !== node;
118
+ else return true;
119
119
  }
120
120
  function operandOfUnaryExprNeedsParens(node) {
121
121
  return EXPRESSIONS_PRECEDENCE[node.type] < EXPRESSIONS_PRECEDENCE.UnaryExpression;
@@ -130,19 +130,6 @@ function commentNeedsNewline(comment) {
130
130
  if (comment.type === "Line") return true;
131
131
  return comment.value.includes("\n");
132
132
  }
133
- function arrowConciseBodyNeedsWrap(body) {
134
- if (body.type === "BlockStatement") return false;
135
- switch (body.type) {
136
- case "ObjectExpression": return true;
137
- case "AssignmentExpression": return body.left.type === "ObjectPattern";
138
- case "LogicalExpression": return body.left.type === "ObjectExpression";
139
- case "ConditionalExpression": return body.test.type === "ObjectExpression";
140
- case "TSAsExpression":
141
- case "TSSatisfiesExpression":
142
- case "TSNonNullExpression": return body.expression ? arrowConciseBodyNeedsWrap(body.expression) : false;
143
- default: return false;
144
- }
145
- }
146
133
  const defaultPrinters = {
147
134
  Program: printProgram,
148
135
  Identifier: printIdentifier,
@@ -312,6 +299,7 @@ function canStartExpressionStatement(node) {
312
299
  break;
313
300
  case "MemberExpression":
314
301
  lhs = node.object;
302
+ if (node.computed && lhs.type === "Identifier" && lhs.name === "let") return false;
315
303
  break;
316
304
  case "TaggedTemplateExpression":
317
305
  lhs = node.tag;
@@ -686,7 +674,7 @@ function printProperty(property, context) {
686
674
  context.writeNode(value);
687
675
  return;
688
676
  }
689
- if (value.type === "FunctionExpression") {
677
+ if (value.type === "FunctionExpression" && (property.method || property.kind !== "init")) {
690
678
  if (property.kind !== "init") context.write(property.kind + " ");
691
679
  if (value.async === true) context.write("async ");
692
680
  if (value.generator === true) context.write("*");
@@ -708,7 +696,6 @@ function printProperty(property, context) {
708
696
  context.writeNode(property.key);
709
697
  context.write("]: ");
710
698
  } else {
711
- if (property.kind === "get" || property.kind === "set") context.write(property.kind + " ");
712
699
  context.writeNode(property.key);
713
700
  context.write(": ");
714
701
  }
@@ -794,6 +781,44 @@ function printFunction(fn, context) {
794
781
  context.writeNode(fn.body);
795
782
  }
796
783
  }
784
+ function canStartConciseBody(body) {
785
+ if (body.type === "BlockStatement" || body.type === "PrivateIdentifier") return true;
786
+ if (expectAssignmentExprNeedsParen(body)) return false;
787
+ let lhs;
788
+ switch (body.type) {
789
+ default: return true;
790
+ case "ObjectExpression":
791
+ case "FunctionExpression":
792
+ case "ClassExpression":
793
+ case "ObjectPattern": return false;
794
+ case "AssignmentExpression":
795
+ case "LogicalExpression":
796
+ case "BinaryExpression":
797
+ lhs = body.left;
798
+ break;
799
+ case "TSAsExpression":
800
+ case "TSSatisfiesExpression":
801
+ case "TSNonNullExpression":
802
+ lhs = body.expression;
803
+ break;
804
+ case "CallExpression":
805
+ lhs = body.callee;
806
+ break;
807
+ case "MemberExpression":
808
+ lhs = body.object;
809
+ if (body.computed && lhs.type === "Identifier" && lhs.name === "let") return false;
810
+ break;
811
+ case "TaggedTemplateExpression":
812
+ lhs = body.tag;
813
+ break;
814
+ case "ConditionalExpression":
815
+ lhs = body.test;
816
+ break;
817
+ case "UpdateExpression": return canStartConciseBody(body.argument);
818
+ case "ChainExpression": return canStartConciseBody(body.expression);
819
+ }
820
+ return operandOfBinaryExprNeedsParens(lhs, body, "left") || canStartConciseBody(lhs);
821
+ }
797
822
  function printArrowFunctionExpression(fn, context) {
798
823
  if (fn.async === true) context.write("async ");
799
824
  if (fn.typeParameters) context.writeNode(fn.typeParameters);
@@ -803,7 +828,7 @@ function printArrowFunctionExpression(fn, context) {
803
828
  writeReturnType(fn, context);
804
829
  context.write(" => ");
805
830
  const body = fn.body;
806
- if (arrowConciseBodyNeedsWrap(body)) {
831
+ if (!canStartConciseBody(body)) {
807
832
  context.write("(");
808
833
  context.writeNode(body);
809
834
  context.write(")");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "espolar",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {
package/src/printers.ts CHANGED
@@ -225,6 +225,9 @@ function operandOfBinaryExprNeedsParens(
225
225
  * which not contains a CallExpression
226
226
  */
227
227
  function validUnparenthesizedNewOperand(node: MemberLikeExpression): boolean {
228
+ if (node.type === "ChainExpression" || node.type === "ImportExpression") {
229
+ return false;
230
+ }
228
231
  let cur: AST.Expression = node;
229
232
  while (true) {
230
233
  if (cur.type === "CallExpression") {
@@ -235,18 +238,8 @@ function validUnparenthesizedNewOperand(node: MemberLikeExpression): boolean {
235
238
  cur = cur.object;
236
239
  } else if (cur.type === "TaggedTemplateExpression") {
237
240
  cur = cur.tag;
238
- } else if (
239
- cur.type === "MetaProperty" ||
240
- cur.type === "NewExpression" ||
241
- cur.type === "ImportExpression"
242
- ) {
243
- return true;
244
241
  } else {
245
- // The operand is either:
246
- // - an expression with higher precedence, no need to check further
247
- // - an expression with lower precedence, will be inner-parenthesized later
248
- // - a ChainExpression, will be inner-parenthesized later
249
- return cur !== node;
242
+ return true;
250
243
  }
251
244
  }
252
245
  }
@@ -279,30 +272,6 @@ function commentNeedsNewline(comment: Comment): boolean {
279
272
  return comment.value.includes("\n");
280
273
  }
281
274
 
282
- function arrowConciseBodyNeedsWrap(
283
- body: AST.BlockStatement | AST.Expression,
284
- ): boolean {
285
- if (body.type === "BlockStatement") return false;
286
- switch (body.type) {
287
- case "ObjectExpression":
288
- return true;
289
- case "AssignmentExpression":
290
- return body.left.type === "ObjectPattern";
291
- case "LogicalExpression":
292
- return body.left.type === "ObjectExpression";
293
- case "ConditionalExpression":
294
- return body.test.type === "ObjectExpression";
295
- case "TSAsExpression":
296
- case "TSSatisfiesExpression":
297
- case "TSNonNullExpression":
298
- return body.expression
299
- ? arrowConciseBodyNeedsWrap(body.expression)
300
- : false;
301
- default:
302
- return false;
303
- }
304
- }
305
-
306
275
  // Printers
307
276
  export const defaultPrinters = {
308
277
  Program: printProgram,
@@ -487,6 +456,9 @@ function canStartExpressionStatement(
487
456
  break;
488
457
  case "MemberExpression":
489
458
  lhs = node.object;
459
+ if (node.computed && lhs.type === "Identifier" && lhs.name === "let") {
460
+ return false;
461
+ }
490
462
  break;
491
463
  case "TaggedTemplateExpression":
492
464
  lhs = node.tag;
@@ -1161,7 +1133,10 @@ function printProperty(
1161
1133
  }
1162
1134
 
1163
1135
  // shorthand method
1164
- if (value.type === "FunctionExpression") {
1136
+ if (
1137
+ value.type === "FunctionExpression" &&
1138
+ (property.method || property.kind !== "init")
1139
+ ) {
1165
1140
  if (property.kind !== "init") {
1166
1141
  context.write(property.kind + " ");
1167
1142
  }
@@ -1192,9 +1167,6 @@ function printProperty(
1192
1167
  context.writeNode(property.key);
1193
1168
  context.write("]: ");
1194
1169
  } else {
1195
- if (property.kind === "get" || property.kind === "set") {
1196
- context.write(property.kind + " ");
1197
- }
1198
1170
  context.writeNode(property.key);
1199
1171
  context.write(": ");
1200
1172
  }
@@ -1342,6 +1314,60 @@ function printFunction(
1342
1314
  }
1343
1315
  }
1344
1316
 
1317
+ function canStartConciseBody(
1318
+ body: AST.BlockStatement | AST.Expression | AST.PrivateIdentifier,
1319
+ ): boolean {
1320
+ if (body.type === "BlockStatement" || body.type === "PrivateIdentifier") {
1321
+ return true;
1322
+ }
1323
+ if (expectAssignmentExprNeedsParen(body)) {
1324
+ return false;
1325
+ }
1326
+ let lhs: AST.Expression | AST.PrivateIdentifier;
1327
+ switch (body.type) {
1328
+ default:
1329
+ return true;
1330
+ case "ObjectExpression":
1331
+ case "FunctionExpression":
1332
+ case "ClassExpression":
1333
+ case "ObjectPattern":
1334
+ return false;
1335
+ case "AssignmentExpression":
1336
+ case "LogicalExpression":
1337
+ case "BinaryExpression":
1338
+ lhs = body.left;
1339
+ break;
1340
+ case "TSAsExpression":
1341
+ case "TSSatisfiesExpression":
1342
+ case "TSNonNullExpression":
1343
+ lhs = body.expression;
1344
+ break;
1345
+ case "CallExpression":
1346
+ lhs = body.callee;
1347
+ break;
1348
+ case "MemberExpression":
1349
+ lhs = body.object;
1350
+ if (body.computed && lhs.type === "Identifier" && lhs.name === "let") {
1351
+ return false;
1352
+ }
1353
+ break;
1354
+ case "TaggedTemplateExpression":
1355
+ lhs = body.tag;
1356
+ break;
1357
+ case "ConditionalExpression":
1358
+ lhs = body.test;
1359
+ break;
1360
+ case "UpdateExpression":
1361
+ return canStartConciseBody(body.argument);
1362
+ case "ChainExpression":
1363
+ return canStartConciseBody(body.expression);
1364
+ }
1365
+ return (
1366
+ operandOfBinaryExprNeedsParens(lhs, body, "left") ||
1367
+ canStartConciseBody(lhs)
1368
+ );
1369
+ }
1370
+
1345
1371
  function printArrowFunctionExpression(
1346
1372
  fn: AST.ArrowFunctionExpression,
1347
1373
  context: PrinterContext,
@@ -1358,7 +1384,7 @@ function printArrowFunctionExpression(
1358
1384
  writeReturnType(fn, context);
1359
1385
  context.write(" => ");
1360
1386
  const body = fn.body;
1361
- if (arrowConciseBodyNeedsWrap(body)) {
1387
+ if (!canStartConciseBody(body)) {
1362
1388
  context.write("(");
1363
1389
  context.writeNode(body);
1364
1390
  context.write(")");