@tsrx/prettier-plugin 0.6.3 → 0.7.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsrx/prettier-plugin",
3
- "version": "0.6.3",
3
+ "version": "0.7.1",
4
4
  "description": "TSRX plugin for Prettier",
5
5
  "type": "module",
6
6
  "module": "src/index.js",
@@ -27,7 +27,7 @@
27
27
  "prettier-3-6": "npm:prettier@3.6.0"
28
28
  },
29
29
  "dependencies": {
30
- "@tsrx/core": "0.5.5"
30
+ "@tsrx/core": "0.6.1"
31
31
  },
32
32
  "files": [
33
33
  "src/"
package/src/parse.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * @import { ParserOptions } from 'prettier'
3
3
  */
4
4
 
5
- import { DIAGNOSTIC_CODES, parseModule } from '@tsrx/core';
5
+ import { DIAGNOSTIC_CODES, parseModule, TS_ERRORS } from '@tsrx/core';
6
6
  import { parsers as acornParsers } from 'prettier/plugins/acorn';
7
7
 
8
8
  /**
@@ -26,14 +26,16 @@ export const TSRX_DIRECTIVES = new Set([
26
26
 
27
27
  /**
28
28
  * Markup that breaks TSRX's grammar: an unclosed or mismatched tag, after which
29
- * the parser guessed at the markup's structure, and a dynamic tag whose
30
- * expression isn't an identifier, a member access or a string literal
31
- * (`<{getTag()} />`, TSRX2014, decision 58 of #852).
29
+ * the parser guessed at the markup's structure, a dynamic tag whose expression
30
+ * isn't an identifier, a member access or a string literal (`<{getTag()} />`,
31
+ * TSRX2014, decision 58 of #852), and a dynamic tag with type arguments
32
+ * (`<{Tag}<T> />`, TSRX2033).
32
33
  */
33
34
  const BROKEN_MARKUP_CODES = new Set([
34
35
  DIAGNOSTIC_CODES.UNCLOSED_TAG,
35
36
  DIAGNOSTIC_CODES.MISMATCHED_CLOSING_TAG,
36
37
  DIAGNOSTIC_CODES.DYNAMIC_TAG_EXPRESSION,
38
+ DIAGNOSTIC_CODES.DYNAMIC_TAG_TYPE_ARGUMENTS,
37
39
  ]);
38
40
 
39
41
  /**
@@ -47,9 +49,15 @@ const BROKEN_MARKUP_CODES = new Set([
47
49
  * class member, a class member's modifier before a module element
48
50
  * (`public class A {}`), `readonly`, `accessor` or `async` where they can't
49
51
  * appear, `async` with `declare` or in a `declare` context (TS1040),
50
- * `declare` before a `using` declaration, a call of `import.<name>` other
51
- * than `import.defer()` or `import.source()`, and a default or named binding in
52
- * a deferred import (`import defer a from "m"`, TS18058 and TS18059).
52
+ * `declare` before a `using` declaration, `import.<name>` other than
53
+ * `import.meta`, or a call of it other than `import.defer()` or
54
+ * `import.source()` (TS17012, TS18061), a default or named binding in
55
+ * a deferred import (`import defer a from "m"`, TS18058 and TS18059), a
56
+ * type-only import with both (`import type a, { b } from "m"`, TS1363),
57
+ * `import type` on an import alias (TS1392), and a function body in an
58
+ * ambient context (TS1183). Prettier rejects that one after `declare`
59
+ * (`declare function f() {}`, which its tree prints wrong), and the plugin in
60
+ * a `declare namespace` too.
53
61
  *
54
62
  * It formats the others, and leaves out what its tree has no place for, as the
55
63
  * output does: a repeated modifier, a modifier in a block, `declare` before an
@@ -76,8 +84,12 @@ const REJECTED_CODES = new Set([
76
84
  'TS1491',
77
85
  'TS1495',
78
86
  'TS17012',
87
+ 'TS18061',
79
88
  'TS18058',
80
89
  'TS18059',
90
+ 'TS1363',
91
+ 'TS1392',
92
+ 'TS1183',
81
93
  ]);
82
94
 
83
95
  /**
@@ -95,6 +107,45 @@ function isModifierBeforeExport(error, text) {
95
107
  return error.code === 'TS1029' && error.pos !== undefined && text.startsWith('export', error.pos);
96
108
  }
97
109
 
110
+ /**
111
+ * Whether `error` is for `abstract` before a declaration other than a class in
112
+ * a block (`function f() { abstract interface I {} }`): TS1184, which TypeScript
113
+ * reports there before TS1242. Prettier's `typescript` parser rejects
114
+ * `abstract` before anything but a class wherever it is, as the plugin does
115
+ * outside a block (TS1242).
116
+ * @param {ParseError} error
117
+ * @param {string} text
118
+ * @returns {boolean}
119
+ */
120
+ function isAbstractInBlock(error, text) {
121
+ return (
122
+ error.code === 'TS1184' &&
123
+ error.pos !== undefined &&
124
+ text.startsWith('abstract', error.pos) &&
125
+ // Not modifiers before a class `static` block, which the tree leaves out,
126
+ // as Prettier's does.
127
+ !regex_static_block_modifiers.test(text.slice(error.pos))
128
+ );
129
+ }
130
+
131
+ /** Modifiers, then a class `static` block's `static {`, with comments between them. */
132
+ const regex_static_block_modifiers =
133
+ /^(?:(?:abstract|accessor|declare|override|private|protected|public|readonly)(?:\s|\/\*[\s\S]*?\*\/|\/\/.*)+)+static(?:\s|\/\*[\s\S]*?\*\/|\/\/.*)*\{/u;
134
+
135
+ /**
136
+ * Whether `error` is for decorators on a class `static` block (`@dec static
137
+ * {}`, TS1206), which Prettier's `typescript` parser leaves out, as the plugin
138
+ * does, instead of rejecting them as it does decorators elsewhere.
139
+ * @param {ParseError} error
140
+ * @returns {boolean}
141
+ */
142
+ function isStaticBlockDecorator(error) {
143
+ return (
144
+ error.code === TS_ERRORS.DECORATORS_NOT_VALID_HERE.code &&
145
+ error.message === TS_ERRORS.DECORATORS_NOT_VALID_HERE.message
146
+ );
147
+ }
148
+
98
149
  /**
99
150
  * @param {ParseError} error
100
151
  * @param {string} text
@@ -104,8 +155,9 @@ function isRejected(error, text) {
104
155
  return (
105
156
  !!error.code &&
106
157
  (BROKEN_MARKUP_CODES.has(error.code) ||
107
- REJECTED_CODES.has(error.code) ||
108
- isModifierBeforeExport(error, text))
158
+ (REJECTED_CODES.has(error.code) && !isStaticBlockDecorator(error)) ||
159
+ isModifierBeforeExport(error, text) ||
160
+ isAbstractInBlock(error, text))
109
161
  );
110
162
  }
111
163
 
@@ -360,6 +412,15 @@ class Adapter {
360
412
  return this.visit(node.typeAnnotation);
361
413
  }
362
414
 
415
+ // A modifier the core parser's tree leaves out, which Prettier's own
416
+ // parser rejects (`declare public class A {}` in a `declare namespace`).
417
+ // TypeScript reports only the declaration's first modifier error, which
418
+ // the parser records, and keeps the others in the metadata.
419
+ const dropped = node.metadata?.dropped_modifiers?.find((/** @type {ParseError} */ error) =>
420
+ isRejected(error, this.text),
421
+ );
422
+ if (dropped) throw createParseError(dropped);
423
+
363
424
  // The core parser attaches comments itself; Prettier attaches them again
364
425
  // from `program.comments`.
365
426
  delete node.leadingComments;
@@ -421,6 +482,9 @@ class Adapter {
421
482
  case 'Program':
422
483
  case 'StaticBlock':
423
484
  case 'TSModuleBlock':
485
+ // Prettier's `typescript` parser leaves out a `static` block's
486
+ // decorators (see `isStaticBlockDecorator`).
487
+ if (node.type === 'StaticBlock') delete node.decorators;
424
488
  node.body = node.body.map(plainStatement);
425
489
  break;
426
490
  case 'IfStatement':
package/src/printer.js CHANGED
@@ -80,6 +80,9 @@ const TSRX_VISITOR_KEYS = /** @type {Record<string, string[]>} */ ({
80
80
  /** `@catch (error, reset)` has a second parameter. */
81
81
  const CATCH_CLAUSE_KEYS = ['param', 'resetParam', 'body'];
82
82
 
83
+ /** A plain `try` with a `pending` block (see `printTsrx`). */
84
+ const PENDING_TRY_KEYS = ['block', 'pending', 'handler', 'finalizer'];
85
+
83
86
  /**
84
87
  * A shorthand attribute (`{name}`) prints only its value: its `name` is the
85
88
  * same identifier as the value's expression, so no comment may attach to it.
@@ -169,6 +172,7 @@ export const printer = {
169
172
  const keys = TSRX_VISITOR_KEYS[node.tsrxType ?? node.type];
170
173
  if (keys) return keys;
171
174
  if (node.type === 'CatchClause' && node.resetParam) return CATCH_CLAUSE_KEYS;
175
+ if (node.type === 'TryStatement' && node.pending) return PENDING_TRY_KEYS;
172
176
  if (node.type === 'JSXAttribute' && node.shorthand) return SHORTHAND_ATTRIBUTE_KEYS;
173
177
  return /** @type {NonNullable<Printer<Node>['getVisitorKeys']>} */ (estree.getVisitorKeys)(
174
178
  node,
@@ -836,6 +840,21 @@ function printTsrx(path, options, print) {
836
840
  ];
837
841
  }
838
842
 
843
+ // A `pending` block on a plain `try`, which the parser reads for
844
+ // `@tsrx/ripple`, and Prettier's `try` would leave out (#1303). It prints
845
+ // as Prettier prints a `try`, with the block after the `try` block.
846
+ case 'TryStatement':
847
+ return node.pending
848
+ ? [
849
+ 'try ',
850
+ print('block'),
851
+ ' pending ',
852
+ print('pending'),
853
+ node.handler ? [' ', print('handler')] : '',
854
+ node.finalizer ? [' finally ', print('finalizer')] : '',
855
+ ]
856
+ : null;
857
+
839
858
  case 'JSXForExpression':
840
859
  return printFor(path, options, print);
841
860