@tsrx/prettier-plugin 0.6.2 → 0.7.0
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 +2 -2
- package/src/parse.js +75 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsrx/prettier-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
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.
|
|
30
|
+
"@tsrx/core": "0.6.0"
|
|
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,
|
|
30
|
-
*
|
|
31
|
-
*
|
|
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,7 +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,
|
|
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.
|
|
51
61
|
*
|
|
52
62
|
* It formats the others, and leaves out what its tree has no place for, as the
|
|
53
63
|
* output does: a repeated modifier, a modifier in a block, `declare` before an
|
|
@@ -74,6 +84,12 @@ const REJECTED_CODES = new Set([
|
|
|
74
84
|
'TS1491',
|
|
75
85
|
'TS1495',
|
|
76
86
|
'TS17012',
|
|
87
|
+
'TS18061',
|
|
88
|
+
'TS18058',
|
|
89
|
+
'TS18059',
|
|
90
|
+
'TS1363',
|
|
91
|
+
'TS1392',
|
|
92
|
+
'TS1183',
|
|
77
93
|
]);
|
|
78
94
|
|
|
79
95
|
/**
|
|
@@ -91,6 +107,45 @@ function isModifierBeforeExport(error, text) {
|
|
|
91
107
|
return error.code === 'TS1029' && error.pos !== undefined && text.startsWith('export', error.pos);
|
|
92
108
|
}
|
|
93
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
|
+
|
|
94
149
|
/**
|
|
95
150
|
* @param {ParseError} error
|
|
96
151
|
* @param {string} text
|
|
@@ -100,8 +155,9 @@ function isRejected(error, text) {
|
|
|
100
155
|
return (
|
|
101
156
|
!!error.code &&
|
|
102
157
|
(BROKEN_MARKUP_CODES.has(error.code) ||
|
|
103
|
-
REJECTED_CODES.has(error.code) ||
|
|
104
|
-
isModifierBeforeExport(error, text)
|
|
158
|
+
(REJECTED_CODES.has(error.code) && !isStaticBlockDecorator(error)) ||
|
|
159
|
+
isModifierBeforeExport(error, text) ||
|
|
160
|
+
isAbstractInBlock(error, text))
|
|
105
161
|
);
|
|
106
162
|
}
|
|
107
163
|
|
|
@@ -356,6 +412,15 @@ class Adapter {
|
|
|
356
412
|
return this.visit(node.typeAnnotation);
|
|
357
413
|
}
|
|
358
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
|
+
|
|
359
424
|
// The core parser attaches comments itself; Prettier attaches them again
|
|
360
425
|
// from `program.comments`.
|
|
361
426
|
delete node.leadingComments;
|
|
@@ -417,6 +482,9 @@ class Adapter {
|
|
|
417
482
|
case 'Program':
|
|
418
483
|
case 'StaticBlock':
|
|
419
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;
|
|
420
488
|
node.body = node.body.map(plainStatement);
|
|
421
489
|
break;
|
|
422
490
|
case 'IfStatement':
|