Skip to main content

crate_cli/fmt/web/
fn.rs

1use super::*;
2
3/// Formats JavaScript (`.js`, `.mjs`, `.cjs`, `.wxs`) matching VS Code Prettier 3.7.4 (`singleQuote: true`, `semi: true`, `tabWidth: 2`).
4///
5/// # Arguments
6///
7/// - `&str` - Raw JavaScript source text.
8///
9/// # Returns
10///
11/// - `String` - Formatted JavaScript text, or unchanged input if syntax errors occur.
12pub fn format_javascript(input: &str) -> String {
13    format_javascript_with_path(input, Path::new(WEB_HINT_JS))
14}
15
16/// Formats TypeScript (`.ts`, `.mts`, `.cts`) matching VS Code Prettier 3.7.4 (`singleQuote: true`, `semi: true`, `tabWidth: 2`).
17///
18/// # Arguments
19///
20/// - `&str` - Raw TypeScript source text.
21///
22/// # Returns
23///
24/// - `String` - Formatted TypeScript text, or unchanged input if syntax errors occur.
25pub fn format_typescript(input: &str) -> String {
26    format_typescript_with_path(input, Path::new(WEB_HINT_TS))
27}
28
29/// Formats TypeScript JSX (`.tsx`) matching VS Code Prettier 3.7.4 (`singleQuote: true`, `jsxSingleQuote: false`, `semi: true`, `tabWidth: 2`).
30///
31/// # Arguments
32///
33/// - `&str` - Raw TSX source text.
34///
35/// # Returns
36///
37/// - `String` - Formatted TSX text, or unchanged input if syntax errors occur.
38pub fn format_tsx(input: &str) -> String {
39    format_tsx_with_path(input, Path::new(WEB_HINT_TSX))
40}
41
42/// Formats CSS (`.css`, `.wxss`) matching VS Code Prettier 3.7.4 (`singleQuote: true`, `tabWidth: 2`).
43///
44/// # Arguments
45///
46/// - `&str` - Raw CSS stylesheet text.
47///
48/// # Returns
49///
50/// - `String` - Formatted CSS text, or unchanged input if syntax errors occur.
51pub fn format_css(input: &str) -> String {
52    format_stylesheet(input, Dialect::Css)
53}
54
55/// Formats SCSS (`.scss`) matching VS Code Prettier 3.7.4 (`singleQuote: true`, `tabWidth: 2`).
56///
57/// # Arguments
58///
59/// - `&str` - Raw SCSS stylesheet text.
60///
61/// # Returns
62///
63/// - `String` - Formatted SCSS text, or unchanged input if syntax errors occur.
64pub fn format_scss(input: &str) -> String {
65    format_stylesheet(input, Dialect::Scss)
66}
67
68/// Formats Less (`.less`) matching VS Code Prettier 3.7.4 (`singleQuote: true`, `tabWidth: 2`).
69///
70/// # Arguments
71///
72/// - `&str` - Raw Less stylesheet text.
73///
74/// # Returns
75///
76/// - `String` - Formatted Less text, or unchanged input if syntax errors occur.
77pub fn format_less(input: &str) -> String {
78    format_stylesheet(input, Dialect::Less)
79}
80
81/// Formats HTML (`.html`, `.htm`) including embedded `<script>` and `<style>` blocks matching VS Code Prettier 3.7.4.
82///
83/// # Arguments
84///
85/// - `&str` - Raw HTML document text.
86///
87/// # Returns
88///
89/// - `String` - Formatted HTML text, or unchanged input if syntax errors occur.
90pub fn format_html(input: &str) -> String {
91    format_html_with_path(input, Path::new(WEB_HINT_HTML))
92}
93
94/// Formats Vue Single-File Components (`.vue`) including embedded `<script>` and `<style>` blocks matching VS Code Prettier 3.7.4.
95///
96/// # Arguments
97///
98/// - `&str` - Raw Vue SFC source text.
99///
100/// # Returns
101///
102/// - `String` - Formatted Vue SFC text, or unchanged input if syntax errors occur.
103pub fn format_vue(input: &str) -> String {
104    format_vue_with_path(input, Path::new(WEB_HINT_VUE))
105}
106
107/// Formats JavaScript with an explicit file path so `.cjs` CommonJS semantics are preserved.
108///
109/// # Arguments
110///
111/// - `&str` - Raw JavaScript source text.
112/// - `&Path` - Source file path hint.
113///
114/// # Returns
115///
116/// - `String` - Formatted JavaScript text, or unchanged input on syntax error.
117pub(crate) fn format_javascript_with_path(input: &str, path: &Path) -> String {
118    let opts: Options = vscode_tidy_options();
119    let path_str: String = path.to_string_lossy().into_owned();
120    let mut embed: Verbatim = Verbatim;
121    tidy::format(
122        input,
123        Language::Js(tidy::js::Dialect::Js),
124        &opts,
125        Some(path_str.as_str()),
126        &mut embed,
127    )
128    .unwrap_or_else(|_: tidy::Error| input.to_string())
129}
130
131/// Formats TypeScript with an explicit file path so `.cts` and `.d.ts` semantics are preserved.
132///
133/// # Arguments
134///
135/// - `&str` - Raw TypeScript source text.
136/// - `&Path` - Source file path hint.
137///
138/// # Returns
139///
140/// - `String` - Formatted TypeScript text, or unchanged input on syntax error.
141pub(crate) fn format_typescript_with_path(input: &str, path: &Path) -> String {
142    let opts: Options = vscode_tidy_options();
143    let path_str: String = path.to_string_lossy().into_owned();
144    let mut embed: Verbatim = Verbatim;
145    tidy::format(
146        input,
147        Language::Js(tidy::js::Dialect::Ts),
148        &opts,
149        Some(path_str.as_str()),
150        &mut embed,
151    )
152    .unwrap_or_else(|_: tidy::Error| input.to_string())
153}
154
155/// Formats TSX with an explicit file path so JSX-in-TypeScript parsing is enabled.
156///
157/// # Arguments
158///
159/// - `&str` - Raw TSX source text.
160/// - `&Path` - Source file path hint.
161///
162/// # Returns
163///
164/// - `String` - Formatted TSX text, or unchanged input on syntax error.
165pub(crate) fn format_tsx_with_path(input: &str, path: &Path) -> String {
166    let opts: Options = vscode_tidy_options();
167    let path_str: String = path.to_string_lossy().into_owned();
168    let mut embed: Verbatim = Verbatim;
169    tidy::format(
170        input,
171        Language::Js(tidy::js::Dialect::Ts),
172        &opts,
173        Some(path_str.as_str()),
174        &mut embed,
175    )
176    .unwrap_or_else(|_: tidy::Error| input.to_string())
177}
178
179/// Formats HTML with an explicit file path so HTML5 `<!doctype html>` casing matches Prettier 3.7.4.
180///
181/// # Arguments
182///
183/// - `&str` - Raw HTML document text.
184/// - `&Path` - Source file path hint.
185///
186/// # Returns
187///
188/// - `String` - Formatted HTML text, or unchanged input on syntax error.
189pub(crate) fn format_html_with_path(input: &str, path: &Path) -> String {
190    let opts: Options = vscode_tidy_options();
191    let path_str: String = path.to_string_lossy().into_owned();
192    tidy::format(
193        input,
194        Language::Html,
195        &opts,
196        Some(path_str.as_str()),
197        &mut |req: Request<'_>| format_markup_embedded(req, &opts),
198    )
199    .unwrap_or_else(|_: tidy::Error| input.to_string())
200}
201
202/// Formats Vue SFC with an explicit file path and embedded CSS/JS Prettier parity.
203///
204/// # Arguments
205///
206/// - `&str` - Raw Vue SFC source text.
207/// - `&Path` - Source file path hint.
208///
209/// # Returns
210///
211/// - `String` - Formatted Vue SFC text, or unchanged input on syntax error.
212pub(crate) fn format_vue_with_path(input: &str, path: &Path) -> String {
213    let opts: Options = vscode_tidy_options();
214    let path_str: String = path.to_string_lossy().into_owned();
215    tidy::format(
216        input,
217        Language::Vue,
218        &opts,
219        Some(path_str.as_str()),
220        &mut |req: Request<'_>| format_markup_embedded(req, &opts),
221    )
222    .unwrap_or_else(|_: tidy::Error| input.to_string())
223}
224
225/// Formats a stylesheet in the requested `Dialect` with VS Code Prettier 3.7.4 options.
226///
227/// # Arguments
228///
229/// - `&str` - Raw stylesheet source text.
230/// - `Dialect` - Target stylesheet dialect (`Css`, `Scss`, or `Less`).
231///
232/// # Returns
233///
234/// - `String` - Formatted stylesheet text, or unchanged input on syntax error.
235fn format_stylesheet(input: &str, dialect: Dialect) -> String {
236    let opts: Options = vscode_tidy_options();
237    tidy::format(
238        input,
239        Language::Css(dialect),
240        &opts,
241        None,
242        &mut |req: Request<'_>| format_markup_embedded(req, &opts),
243    )
244    .unwrap_or_else(|_: tidy::Error| input.to_string())
245}