@ohos-ports/formatter 0.0.0-stage → 0.11.1-beta.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 +317 -2
- package/bin/herb-format +3 -0
- package/dist/herb-format.js +3772 -0
- package/dist/herb-format.js.map +1 -0
- package/dist/index.cjs +3222 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.esm.js +3218 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/types/attribute-renderer.d.ts +44 -0
- package/dist/types/cli/summary-reporter.d.ts +23 -0
- package/dist/types/cli.d.ts +12 -0
- package/dist/types/comment-helpers.d.ts +47 -0
- package/dist/types/format-helpers.d.ts +249 -0
- package/dist/types/format-ignore.d.ts +9 -0
- package/dist/types/format-printer.d.ts +288 -0
- package/dist/types/formatter.d.ts +48 -0
- package/dist/types/herb-disable-collector.d.ts +22 -0
- package/dist/types/herb-format.d.ts +2 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/options.d.ts +33 -0
- package/dist/types/scaffold-template-detector.d.ts +12 -0
- package/dist/types/spacing-analyzer.d.ts +51 -0
- package/dist/types/text-flow-analyzer.d.ts +33 -0
- package/dist/types/text-flow-engine.d.ts +37 -0
- package/dist/types/text-flow-helpers.d.ts +91 -0
- package/dist/types/types.d.ts +23 -0
- package/package.json +44 -4
- package/src/attribute-renderer.ts +305 -0
- package/src/cli/summary-reporter.ts +145 -0
- package/src/cli.ts +632 -0
- package/src/comment-helpers.ts +139 -0
- package/src/format-helpers.ts +679 -0
- package/src/format-ignore.ts +41 -0
- package/src/format-printer.ts +2012 -0
- package/src/formatter.ts +171 -0
- package/src/herb-disable-collector.ts +89 -0
- package/src/herb-format.ts +6 -0
- package/src/index.ts +7 -0
- package/src/options.ts +52 -0
- package/src/scaffold-template-detector.ts +25 -0
- package/src/spacing-analyzer.ts +262 -0
- package/src/text-flow-analyzer.ts +317 -0
- package/src/text-flow-engine.ts +327 -0
- package/src/text-flow-helpers.ts +462 -0
- package/src/types.ts +27 -0
package/README.md
CHANGED
|
@@ -1,3 +1,318 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Herb Formatter <Badge type="warning" text="experimental preview" />
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Package:** [`@herb-tools/formatter`](https://www.npmjs.com/package/@herb-tools/formatter)
|
|
4
|
+
|
|
5
|
+
> [!WARNING] Experimental Preview
|
|
6
|
+
> This formatter is currently in experimental preview. While it works for many common cases, it may potentially corrupt files in edge cases. Only use on files that can be restored via git or other version control systems.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
Auto-formatter for HTML+ERB templates with intelligent indentation, line wrapping, and ERB-aware pretty-printing.
|
|
11
|
+
|
|
12
|
+
Perfect for format-on-save in editors and formatting verification in CI/CD pipelines. Transforms templates into consistently formatted, readable code while preserving all functionality.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
### Global Installation
|
|
17
|
+
|
|
18
|
+
:::code-group
|
|
19
|
+
|
|
20
|
+
```shell [npm]
|
|
21
|
+
npm install -g @herb-tools/formatter
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```shell [pnpm]
|
|
25
|
+
pnpm add -g @herb-tools/formatter
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```shell [yarn 1]
|
|
29
|
+
yarn global add @herb-tools/formatter
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
```shell [yarn 4]
|
|
33
|
+
yarn dlx @herb-tools/formatter template.html.erb
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```shell [bun]
|
|
37
|
+
bun add -g @herb-tools/formatter
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
:::
|
|
41
|
+
|
|
42
|
+
### One-time Usage
|
|
43
|
+
For occasional use without installing:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npx @herb-tools/formatter template.html.erb
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Preview Releases
|
|
50
|
+
|
|
51
|
+
Want to try unreleased features? Use pkg.pr.new to run the formatter from any commit or PR:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npx https://pkg.pr.new/@herb-tools/formatter@{commit} template.html.erb
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Replace `{commit}` with a commit SHA (e.g., `0d2eabe`) or branch name (e.g., `main`). Find available previews at [pkg.pr.new/~/marcoroth/herb](https://pkg.pr.new/~/marcoroth/herb).
|
|
58
|
+
|
|
59
|
+
### Project Installation
|
|
60
|
+
|
|
61
|
+
:::code-group
|
|
62
|
+
|
|
63
|
+
```shell [npm]
|
|
64
|
+
npm add -D @herb-tools/formatter
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```shell [pnpm]
|
|
68
|
+
pnpm add -D @herb-tools/formatter
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```shell [yarn]
|
|
72
|
+
yarn add -D @herb-tools/formatter
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
```shell [bun]
|
|
76
|
+
bun add -D @herb-tools/formatter
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
:::
|
|
80
|
+
|
|
81
|
+
After installing as a dev dependency, initialize the configuration:
|
|
82
|
+
|
|
83
|
+
:::code-group
|
|
84
|
+
|
|
85
|
+
```shell [npm]
|
|
86
|
+
npx herb-format --init
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
```shell [pnpm]
|
|
90
|
+
pnpm herb-format --init
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```shell [yarn]
|
|
94
|
+
yarn herb-format --init
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
```shell [bun]
|
|
98
|
+
bunx herb-format --init
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
:::
|
|
102
|
+
|
|
103
|
+
Then add format scripts to your `package.json`:
|
|
104
|
+
```json [package.json]
|
|
105
|
+
{
|
|
106
|
+
"scripts": {
|
|
107
|
+
"herb:format": "herb-format",
|
|
108
|
+
"herb:format:check": "herb-format --check"
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Then run the scripts:
|
|
114
|
+
|
|
115
|
+
:::code-group
|
|
116
|
+
|
|
117
|
+
```shell [npm]
|
|
118
|
+
npm run herb:format
|
|
119
|
+
npm run herb:format:check
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```shell [pnpm]
|
|
123
|
+
pnpm herb:format
|
|
124
|
+
pnpm herb:format:check
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
```shell [yarn]
|
|
128
|
+
yarn herb:format
|
|
129
|
+
yarn herb:format:check
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
```shell [bun]
|
|
133
|
+
bun run herb:format
|
|
134
|
+
bun run herb:format:check
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
:::
|
|
138
|
+
|
|
139
|
+
## Usage
|
|
140
|
+
|
|
141
|
+
### Command Line
|
|
142
|
+
|
|
143
|
+
Basic usage:
|
|
144
|
+
```bash
|
|
145
|
+
herb-format
|
|
146
|
+
herb-format template.html.erb
|
|
147
|
+
herb-format templates/
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**Initialize configuration:**
|
|
151
|
+
```bash
|
|
152
|
+
# Create a .herb.yml configuration file
|
|
153
|
+
herb-format --init
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### Options
|
|
157
|
+
|
|
158
|
+
**Check Mode:**
|
|
159
|
+
```bash
|
|
160
|
+
# Check if files are formatted without modifying them
|
|
161
|
+
herb-format --check template.html.erb
|
|
162
|
+
|
|
163
|
+
# Check all files in current directory
|
|
164
|
+
herb-format --check
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**Input Sources:**
|
|
168
|
+
```bash
|
|
169
|
+
# Format specific file
|
|
170
|
+
herb-format templates/index.html.erb
|
|
171
|
+
|
|
172
|
+
# Format all .html.erb files in directory
|
|
173
|
+
herb-format templates/
|
|
174
|
+
|
|
175
|
+
# Format all .html.erb files in current directory (default)
|
|
176
|
+
herb-format
|
|
177
|
+
|
|
178
|
+
# Format from stdin
|
|
179
|
+
cat template.html.erb | herb-format
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
**Quiet Mode:**
|
|
183
|
+
```bash
|
|
184
|
+
# Suppress the experimental preview banner and the config file notice on stderr
|
|
185
|
+
herb-format --quiet
|
|
186
|
+
|
|
187
|
+
# Combine with other options
|
|
188
|
+
herb-format --check --quiet
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**Help and Version:**
|
|
192
|
+
```bash
|
|
193
|
+
# Show help
|
|
194
|
+
herb-format --help
|
|
195
|
+
|
|
196
|
+
# Show version information
|
|
197
|
+
herb-format --version
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Configuration
|
|
201
|
+
|
|
202
|
+
Create a `.herb.yml` file in your project root to configure the formatter:
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
herb-format --init
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Basic Configuration
|
|
209
|
+
|
|
210
|
+
```yaml [.herb.yml]
|
|
211
|
+
formatter:
|
|
212
|
+
enabled: true # Must be enabled for formatting to work
|
|
213
|
+
indentWidth: 2
|
|
214
|
+
indentStyle: space
|
|
215
|
+
maxLineLength: 80
|
|
216
|
+
|
|
217
|
+
# Additional glob patterns to include (additive to defaults)
|
|
218
|
+
include:
|
|
219
|
+
- '**/*.xml.erb'
|
|
220
|
+
|
|
221
|
+
# Glob patterns to exclude from formatting
|
|
222
|
+
exclude:
|
|
223
|
+
- 'vendor/**/*'
|
|
224
|
+
- 'node_modules/**/*'
|
|
225
|
+
- 'app/views/generated/**/*'
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Default File Patterns
|
|
229
|
+
|
|
230
|
+
By default, the formatter processes:
|
|
231
|
+
- `**/*.html`
|
|
232
|
+
- `**/*.rhtml`
|
|
233
|
+
- `**/*.html.erb`
|
|
234
|
+
- `**/*.html+*.erb`
|
|
235
|
+
- `**/*.turbo_stream.erb`
|
|
236
|
+
|
|
237
|
+
The `include` patterns are **additive** - they add to the defaults.
|
|
238
|
+
|
|
239
|
+
### Configuration Options
|
|
240
|
+
|
|
241
|
+
- **`enabled`**: `true` or `false` - Must be `true` to enable formatting
|
|
242
|
+
- **`indentWidth`**: Number (default: `2`) - Spaces per indentation level
|
|
243
|
+
- **`indentStyle`**: `"space"` or `"tab"` (default: `"space"`) - Character used for indentation
|
|
244
|
+
- **`maxLineLength`**: Number (default: `80`) - Maximum line length before wrapping. Tags whose attributes would exceed this length are wrapped one-per-line.
|
|
245
|
+
- **`include`**: Array of glob patterns - Additional patterns to format (additive to defaults)
|
|
246
|
+
- **`exclude`**: Array of glob patterns - Patterns to exclude from formatting
|
|
247
|
+
|
|
248
|
+
### Force Flag
|
|
249
|
+
|
|
250
|
+
Format files even when formatter is disabled:
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
# Force formatting when disabled in config
|
|
254
|
+
herb-format --force
|
|
255
|
+
|
|
256
|
+
# Force formatting on an excluded file
|
|
257
|
+
herb-format --force app/views/excluded-file.html.erb
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
When using `--force` on an excluded file, the formatter will show a warning but proceed with formatting.
|
|
261
|
+
|
|
262
|
+
### Disabling Formatting for Entire Files <Badge type="info" text="v0.8.2+" />
|
|
263
|
+
|
|
264
|
+
You can disable formatting for an entire file by adding the `ignore` directive anywhere in your template:
|
|
265
|
+
|
|
266
|
+
```erb
|
|
267
|
+
<%# herb:formatter ignore %>
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
**Example:**
|
|
271
|
+
|
|
272
|
+
:::code-group
|
|
273
|
+
```erb [ignored.html.erb]
|
|
274
|
+
<%# herb:formatter ignore %>
|
|
275
|
+
|
|
276
|
+
<div><div>This entire file will not be formatted</div></div>
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
```erb [formatted.html.erb]
|
|
280
|
+
<div>
|
|
281
|
+
<div>This file will be formatted</div>
|
|
282
|
+
</div>
|
|
283
|
+
```
|
|
284
|
+
:::
|
|
285
|
+
|
|
286
|
+
::: warning Important
|
|
287
|
+
The `<%# herb:formatter ignore %>` directive must be an exact match. Extra text or spacing will prevent it from working.
|
|
288
|
+
:::
|
|
289
|
+
|
|
290
|
+
## Rewriters
|
|
291
|
+
|
|
292
|
+
The formatter supports **rewriters** that allow you to transform templates before and after formatting.
|
|
293
|
+
|
|
294
|
+
Configure rewriters in your `.herb.yml`:
|
|
295
|
+
|
|
296
|
+
```yaml [.herb.yml]
|
|
297
|
+
formatter:
|
|
298
|
+
enabled: true
|
|
299
|
+
indentWidth: 2
|
|
300
|
+
|
|
301
|
+
rewriter:
|
|
302
|
+
# Pre-format rewriters (run before formatting)
|
|
303
|
+
pre:
|
|
304
|
+
- tailwind-class-sorter
|
|
305
|
+
|
|
306
|
+
# Post-format rewriters (run after formatting)
|
|
307
|
+
post: []
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### Built-in Rewriters
|
|
311
|
+
|
|
312
|
+
- **`tailwind-class-sorter`** - Automatically sorts Tailwind CSS classes according to the recommended order
|
|
313
|
+
|
|
314
|
+
### Custom Rewriters
|
|
315
|
+
|
|
316
|
+
You can create custom rewriters by placing them in `.herb/rewriters/` and referencing them in your config.
|
|
317
|
+
|
|
318
|
+
For detailed documentation on creating and using rewriters, see the [Rewriter Documentation](/projects/rewriter).
|
package/bin/herb-format
ADDED