esrap 2.2.9 → 2.2.10

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.9",
3
+ "version": "2.2.10",
4
4
  "description": "Parse in reverse",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,7 +25,6 @@ export const EXPRESSIONS_PRECEDENCE = {
25
25
  ImportExpression: 19,
26
26
  NewExpression: 19,
27
27
  Literal: 18,
28
- TSSatisfiesExpression: 18,
29
28
  TSInstantiationExpression: 18,
30
29
  TSNonNullExpression: 18,
31
30
  TSTypeAssertion: 18,
@@ -33,11 +32,13 @@ export const EXPRESSIONS_PRECEDENCE = {
33
32
  ClassExpression: 17,
34
33
  FunctionExpression: 17,
35
34
  ObjectExpression: 17,
36
- TSAsExpression: 16,
37
35
  UpdateExpression: 16,
38
36
  UnaryExpression: 15,
39
37
  BinaryExpression: 14,
40
- LogicalExpression: 13,
38
+ // `as`/`satisfies` sit between binary and logical operators
39
+ TSAsExpression: 13,
40
+ TSSatisfiesExpression: 13,
41
+ LogicalExpression: 12,
41
42
  ConditionalExpression: 4,
42
43
  ArrowFunctionExpression: 3,
43
44
  AssignmentExpression: 3,
@@ -360,7 +361,7 @@ export default (options = {}) => {
360
361
  * @param {{ line: number, column: number }} until
361
362
  * @param {boolean} pad
362
363
  */
363
- function sequence(context, nodes, until, pad, separator = ',') {
364
+ function sequence(context, nodes, until, pad, separator = ',', trailing_newline = true) {
364
365
  let multiline = false;
365
366
  let length = -1;
366
367
 
@@ -426,7 +427,7 @@ export default (options = {}) => {
426
427
 
427
428
  if (multiline) {
428
429
  context.dedent();
429
- context.newline();
430
+ if (trailing_newline) context.newline();
430
431
  } else if (pad && length > 0) {
431
432
  context.write(' ');
432
433
  }
@@ -2106,11 +2107,13 @@ export default (options = {}) => {
2106
2107
  },
2107
2108
 
2108
2109
  TSUnionType(node, context) {
2109
- sequence(context, node.types, node.loc?.end ?? null, false, ' |');
2110
+ // no trailing newline, so a following `=>` stays on the same line
2111
+ sequence(context, node.types, node.loc?.end ?? null, false, ' |', false);
2110
2112
  },
2111
2113
 
2112
2114
  TSIntersectionType(node, context) {
2113
- sequence(context, node.types, node.loc?.end ?? null, false, ' &');
2115
+ // no trailing newline, so a following `=>` stays on the same line
2116
+ sequence(context, node.types, node.loc?.end ?? null, false, ' &', false);
2114
2117
  },
2115
2118
 
2116
2119
  TSInferType(node, context) {
@@ -2241,7 +2244,16 @@ export default (options = {}) => {
2241
2244
  },
2242
2245
 
2243
2246
  TSNonNullExpression(node, context) {
2244
- context.visit(node.expression);
2247
+ // operator expressions can't take a postfix `!` directly: `(0 as number)!`, `(await x)!`
2248
+ if (
2249
+ EXPRESSIONS_PRECEDENCE[node.expression.type] < EXPRESSIONS_PRECEDENCE.TSNonNullExpression
2250
+ ) {
2251
+ context.write('(');
2252
+ context.visit(node.expression);
2253
+ context.write(')');
2254
+ } else {
2255
+ context.visit(node.expression);
2256
+ }
2245
2257
  context.write('!');
2246
2258
  },
2247
2259
 
@@ -2355,6 +2367,11 @@ function arrow_concise_body_needs_wrap(body) {
2355
2367
  function needs_parens(node, parent, is_right) {
2356
2368
  if (node.type === 'PrivateIdentifier') return false;
2357
2369
 
2370
+ if (!is_right && (node.type === 'TSAsExpression' || node.type === 'TSSatisfiesExpression')) {
2371
+ // `**` would be invalid, `&`/`|` would be swallowed into the trailing type
2372
+ return parent.operator === '**' || parent.operator === '&' || parent.operator === '|';
2373
+ }
2374
+
2358
2375
  // special case where logical expressions and coalesce expressions cannot be mixed,
2359
2376
  // either of them need to be wrapped with parentheses
2360
2377
  if (