esrap 2.2.7 → 2.2.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esrap",
3
- "version": "2.2.7",
3
+ "version": "2.2.9",
4
4
  "description": "Parse in reverse",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1010,12 +1010,7 @@ export default (options = {}) => {
1010
1010
 
1011
1011
  context.write(' => ');
1012
1012
 
1013
- if (
1014
- node.body.type === 'ObjectExpression' ||
1015
- (node.body.type === 'AssignmentExpression' && node.body.left.type === 'ObjectPattern') ||
1016
- (node.body.type === 'LogicalExpression' && node.body.left.type === 'ObjectExpression') ||
1017
- (node.body.type === 'ConditionalExpression' && node.body.test.type === 'ObjectExpression')
1018
- ) {
1013
+ if (arrow_concise_body_needs_wrap(node.body)) {
1019
1014
  context.write('(');
1020
1015
  context.visit(node.body);
1021
1016
  context.write(')');
@@ -1406,6 +1401,8 @@ export default (options = {}) => {
1406
1401
  },
1407
1402
 
1408
1403
  ImportSpecifier(node, context) {
1404
+ if (node.importKind == 'type') context.write('type ');
1405
+
1409
1406
  if (
1410
1407
  node.local.type === 'Identifier' &&
1411
1408
  node.imported.type === 'Identifier' &&
@@ -1415,7 +1412,6 @@ export default (options = {}) => {
1415
1412
  context.write(' as ');
1416
1413
  }
1417
1414
 
1418
- if (node.importKind == 'type') context.write('type ');
1419
1415
  context.visit(node.local);
1420
1416
  },
1421
1417
 
@@ -2321,6 +2317,34 @@ export default (options = {}) => {
2321
2317
 
2322
2318
  /** @satisfies {Visitors} */
2323
2319
 
2320
+ /**
2321
+ * Arrow functions with a concise body must wrap certain expressions in parentheses,
2322
+ * otherwise `{` can start a block statement instead of an object literal (`as` /
2323
+ * `satisfies` / `!` do not change that (e.g. `() => { x } as const`).
2324
+ * @param {TSESTree.BlockStatement | TSESTree.Expression} body
2325
+ * @returns {boolean}
2326
+ */
2327
+ function arrow_concise_body_needs_wrap(body) {
2328
+ if (body.type === 'BlockStatement') return false;
2329
+
2330
+ switch (body.type) {
2331
+ case 'ObjectExpression':
2332
+ return true;
2333
+ case 'AssignmentExpression':
2334
+ return body.left.type === 'ObjectPattern';
2335
+ case 'LogicalExpression':
2336
+ return body.left.type === 'ObjectExpression';
2337
+ case 'ConditionalExpression':
2338
+ return body.test.type === 'ObjectExpression';
2339
+ case 'TSAsExpression':
2340
+ case 'TSSatisfiesExpression':
2341
+ case 'TSNonNullExpression':
2342
+ return body.expression ? arrow_concise_body_needs_wrap(body.expression) : false;
2343
+ default:
2344
+ return false;
2345
+ }
2346
+ }
2347
+
2324
2348
  /**
2325
2349
  *
2326
2350
  * @param {TSESTree.Expression | TSESTree.PrivateIdentifier} node