esrap 2.2.8 → 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 +1 -1
- package/src/languages/ts/index.js +29 -6
package/package.json
CHANGED
|
@@ -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(')');
|
|
@@ -2322,6 +2317,34 @@ export default (options = {}) => {
|
|
|
2322
2317
|
|
|
2323
2318
|
/** @satisfies {Visitors} */
|
|
2324
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
|
+
|
|
2325
2348
|
/**
|
|
2326
2349
|
*
|
|
2327
2350
|
* @param {TSESTree.Expression | TSESTree.PrivateIdentifier} node
|