@herb-tools/formatter 0.7.1 → 0.7.3
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/dist/herb-format.js +81 -24
- package/dist/herb-format.js.map +1 -1
- package/dist/index.cjs +17 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.esm.js +17 -6
- package/dist/index.esm.js.map +1 -1
- package/dist/types/cli.d.ts +1 -0
- package/package.json +3 -3
- package/src/cli.ts +69 -12
- package/src/format-printer.ts +14 -2
package/dist/types/cli.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@herb-tools/formatter",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Auto-formatter for HTML+ERB templates with intelligent indentation, line wrapping, and ERB-aware pretty-printing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://herb-tools.dev",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@herb-tools/core": "0.7.
|
|
39
|
-
"@herb-tools/printer": "0.7.
|
|
38
|
+
"@herb-tools/core": "0.7.3",
|
|
39
|
+
"@herb-tools/printer": "0.7.3"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"glob": "^11.0.3"
|
package/src/cli.ts
CHANGED
|
@@ -5,6 +5,8 @@ import { join, resolve } from "path"
|
|
|
5
5
|
|
|
6
6
|
import { Herb } from "@herb-tools/node-wasm"
|
|
7
7
|
import { Formatter } from "./formatter.js"
|
|
8
|
+
import { parseArgs } from "util"
|
|
9
|
+
import { resolveFormatOptions } from "./options.js"
|
|
8
10
|
|
|
9
11
|
import { name, version, dependencies } from "../package.json"
|
|
10
12
|
|
|
@@ -21,9 +23,11 @@ export class CLI {
|
|
|
21
23
|
glob pattern to match files, or '-' for stdin (omit to format all **/*.html.erb files in current directory)
|
|
22
24
|
|
|
23
25
|
Options:
|
|
24
|
-
-c, --check
|
|
25
|
-
-h, --help
|
|
26
|
-
-v, --version
|
|
26
|
+
-c, --check check if files are formatted without modifying them
|
|
27
|
+
-h, --help show help
|
|
28
|
+
-v, --version show version
|
|
29
|
+
--indent-width <number> number of spaces per indentation level (default: 2)
|
|
30
|
+
--max-line-length <number> maximum line length before wrapping (default: 80)
|
|
27
31
|
|
|
28
32
|
Examples:
|
|
29
33
|
herb-format # Format all **/*.html.erb files in current directory
|
|
@@ -35,22 +39,71 @@ export class CLI {
|
|
|
35
39
|
herb-format "**/*.xml.erb" # Format all .xml.erb files using glob pattern
|
|
36
40
|
herb-format --check # Check if all **/*.html.erb files are formatted
|
|
37
41
|
herb-format --check templates/ # Check if all **/*.html.erb files in templates/ are formatted
|
|
42
|
+
herb-format --indent-width 4 # Format with 4-space indentation
|
|
43
|
+
herb-format --max-line-length 100 # Format with 100-character line limit
|
|
38
44
|
cat template.html.erb | herb-format # Format from stdin to stdout
|
|
39
45
|
`
|
|
40
46
|
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
47
|
+
private parseArguments() {
|
|
48
|
+
const { values, positionals } = parseArgs({
|
|
49
|
+
args: process.argv.slice(2),
|
|
50
|
+
options: {
|
|
51
|
+
help: { type: "boolean", short: "h" },
|
|
52
|
+
version: { type: "boolean", short: "v" },
|
|
53
|
+
check: { type: "boolean", short: "c" },
|
|
54
|
+
"indent-width": { type: "string" },
|
|
55
|
+
"max-line-length": { type: "string" }
|
|
56
|
+
},
|
|
57
|
+
allowPositionals: true
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
if (values.help) {
|
|
45
61
|
console.log(this.usage)
|
|
46
|
-
|
|
47
62
|
process.exit(0)
|
|
48
63
|
}
|
|
49
64
|
|
|
65
|
+
let indentWidth: number | undefined
|
|
66
|
+
|
|
67
|
+
if (values["indent-width"]) {
|
|
68
|
+
const parsed = parseInt(values["indent-width"], 10)
|
|
69
|
+
if (isNaN(parsed) || parsed < 1) {
|
|
70
|
+
console.error(
|
|
71
|
+
`Invalid indent-width: ${values["indent-width"]}. Must be a positive integer.`,
|
|
72
|
+
)
|
|
73
|
+
process.exit(1)
|
|
74
|
+
}
|
|
75
|
+
indentWidth = parsed
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let maxLineLength: number | undefined
|
|
79
|
+
|
|
80
|
+
if (values["max-line-length"]) {
|
|
81
|
+
const parsed = parseInt(values["max-line-length"], 10)
|
|
82
|
+
if (isNaN(parsed) || parsed < 1) {
|
|
83
|
+
console.error(
|
|
84
|
+
`Invalid max-line-length: ${values["max-line-length"]}. Must be a positive integer.`,
|
|
85
|
+
)
|
|
86
|
+
process.exit(1)
|
|
87
|
+
}
|
|
88
|
+
maxLineLength = parsed
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
positionals,
|
|
93
|
+
isCheckMode: values.check,
|
|
94
|
+
isVersionMode: values.version,
|
|
95
|
+
indentWidth,
|
|
96
|
+
maxLineLength
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async run() {
|
|
101
|
+
const { positionals, isCheckMode, isVersionMode, indentWidth, maxLineLength } = this.parseArguments()
|
|
102
|
+
|
|
50
103
|
try {
|
|
51
104
|
await Herb.load()
|
|
52
105
|
|
|
53
|
-
if (
|
|
106
|
+
if (isVersionMode) {
|
|
54
107
|
console.log("Versions:")
|
|
55
108
|
console.log(` ${name}@${version}`)
|
|
56
109
|
console.log(` @herb-tools/printer@${dependencies['@herb-tools/printer']}`)
|
|
@@ -62,10 +115,14 @@ export class CLI {
|
|
|
62
115
|
console.log("⚠️ Experimental Preview: The formatter is in early development. Please report any unexpected behavior or bugs to https://github.com/marcoroth/herb/issues/new?template=formatting-issue.md")
|
|
63
116
|
console.log()
|
|
64
117
|
|
|
65
|
-
const
|
|
66
|
-
|
|
118
|
+
const formatOptions = resolveFormatOptions({
|
|
119
|
+
indentWidth,
|
|
120
|
+
maxLineLength
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
const formatter = new Formatter(Herb, formatOptions)
|
|
67
124
|
|
|
68
|
-
const file =
|
|
125
|
+
const file = positionals[0]
|
|
69
126
|
|
|
70
127
|
if (!file && !process.stdin.isTTY) {
|
|
71
128
|
if (isCheckMode) {
|
package/src/format-printer.ts
CHANGED
|
@@ -730,7 +730,19 @@ export class FormatPrinter extends Printer {
|
|
|
730
730
|
|
|
731
731
|
visitHTMLElementBody(body: Node[], element: HTMLElementNode) {
|
|
732
732
|
if (this.isContentPreserving(element)) {
|
|
733
|
-
element.body.map(child =>
|
|
733
|
+
element.body.map(child => {
|
|
734
|
+
if (isNode(child, HTMLElementNode)) {
|
|
735
|
+
const wasInlineMode = this.inlineMode
|
|
736
|
+
this.inlineMode = true
|
|
737
|
+
|
|
738
|
+
const formattedElement = this.capture(() => this.visit(child)).join("")
|
|
739
|
+
this.pushToLastLine(formattedElement)
|
|
740
|
+
|
|
741
|
+
this.inlineMode = wasInlineMode
|
|
742
|
+
} else {
|
|
743
|
+
this.pushToLastLine(IdentityPrinter.print(child))
|
|
744
|
+
}
|
|
745
|
+
})
|
|
734
746
|
|
|
735
747
|
return
|
|
736
748
|
}
|
|
@@ -1560,7 +1572,7 @@ export class FormatPrinter extends Printer {
|
|
|
1560
1572
|
|
|
1561
1573
|
return child.content
|
|
1562
1574
|
} else if (isNode(child, ERBContentNode)) {
|
|
1563
|
-
return
|
|
1575
|
+
return this.reconstructERBNode(child, true)
|
|
1564
1576
|
} else {
|
|
1565
1577
|
const printed = IdentityPrinter.print(child)
|
|
1566
1578
|
|