@tsrx/prettier-plugin 0.5.1 → 0.6.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/README.md +38 -0
- package/package.json +2 -2
- package/src/printer.js +4 -5
package/README.md
CHANGED
|
@@ -6,6 +6,44 @@ The goal is Prettier's output wherever TSRX is TSX, so TSRX only differs where i
|
|
|
6
6
|
syntax does: `@{ … }` blocks, `@if`/`@for`/`@switch`/`@try`, `{value}` shorthand
|
|
7
7
|
props, `<style>`/`<script>` bodies, and comments between JSX children.
|
|
8
8
|
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
1. Install Prettier and the plugin as dev dependencies:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install -D prettier @tsrx/prettier-plugin
|
|
15
|
+
# or
|
|
16
|
+
pnpm add -D prettier @tsrx/prettier-plugin
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
2. Add the plugin to your Prettier config, for example `.prettierrc`:
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{ "plugins": ["@tsrx/prettier-plugin"] }
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
3. Format with Prettier:
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npx prettier --write "src/**/*.tsrx"
|
|
29
|
+
# or
|
|
30
|
+
pnpm exec prettier --write "src/**/*.tsrx"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### In your editor
|
|
34
|
+
|
|
35
|
+
The
|
|
36
|
+
[TSRX language server](https://github.com/tsrx-org/tsrx/tree/main/packages/language-server#formatting)
|
|
37
|
+
formats `.tsrx` files with your project's Prettier and this plugin. The
|
|
38
|
+
[TSRX Syntax for VS Code](https://github.com/tsrx-org/tsrx/tree/main/packages/vscode-plugin#formatting)
|
|
39
|
+
extension uses it, and so does any editor that formats through the language
|
|
40
|
+
server. You do not need the Prettier extension for VS Code.
|
|
41
|
+
|
|
42
|
+
The language server adds the plugin itself, so a `.tsrx` file formats in the
|
|
43
|
+
editor even when your Prettier config does not list the plugin. The `prettier`
|
|
44
|
+
command does not do this: for the command line, keep the plugin in your Prettier
|
|
45
|
+
config (step 2).
|
|
46
|
+
|
|
9
47
|
## How it works
|
|
10
48
|
|
|
11
49
|
- **Parser** (`src/parse.js`) parses with `@tsrx/core` in `collect` mode, so
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsrx/prettier-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.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.5.
|
|
30
|
+
"@tsrx/core": "0.5.3"
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
33
33
|
"src/"
|
package/src/printer.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* @import { Node } from './parse.js'
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import { isLayoutWhitespace } from '@tsrx/core';
|
|
6
7
|
import { builders, utils } from 'prettier/doc';
|
|
7
8
|
import * as estreePlugin from 'prettier/plugins/estree';
|
|
8
9
|
import * as standalone from 'prettier/standalone';
|
|
@@ -1086,17 +1087,15 @@ function printRawTextAsWritten(path, options, print) {
|
|
|
1086
1087
|
|
|
1087
1088
|
/**
|
|
1088
1089
|
* The index of the `@{ … }` block that is an element's or fragment's only
|
|
1089
|
-
* child, apart from
|
|
1090
|
-
* -1.
|
|
1090
|
+
* child, apart from text that renders nothing (core's `isLayoutWhitespace`:
|
|
1091
|
+
* spaces, tabs and line breaks with a line break among them), or -1.
|
|
1091
1092
|
* @param {Node} node
|
|
1092
1093
|
* @returns {number}
|
|
1093
1094
|
*/
|
|
1094
1095
|
function huggedCodeBlock(node) {
|
|
1095
1096
|
let index = -1;
|
|
1096
1097
|
for (const [i, child] of node.children.entries()) {
|
|
1097
|
-
if (
|
|
1098
|
-
continue;
|
|
1099
|
-
}
|
|
1098
|
+
if (isLayoutWhitespace(child)) continue;
|
|
1100
1099
|
if (index !== -1 || !child.tsrxCodeBlock || hasOwnComments(child)) return -1;
|
|
1101
1100
|
index = i;
|
|
1102
1101
|
}
|