summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2024-09-24 14:54:48 +0100
committerGitHub <noreply@github.com>2024-09-24 15:54:48 +0200
commit3242550f5f0b33769a4e0a24a7dc96773647ca75 (patch)
tree3ca7a167c4ed833310f2c396073548cd5181aa5b
parent7d7e54172477203f50e553304b0980e17b5fd441 (diff)
fix(fmt): --check was broken for CSS, YAML and HTML (#25848)
`deno fmt --check` was broken for CSS, YAML and HTML files. Before this PR, formatting any of these file types would return a string, even though the contract in `cli/tools/fmt.rs` is to only return a string if the formatting changed. This causes wrong flagging of these files as being badly formatted even though diffs showed nothing (because they were in fact formatted properly). Closes https://github.com/denoland/deno/issues/25840
-rw-r--r--cli/tools/fmt.rs51
-rw-r--r--tests/specs/fmt/css/__test__.jsonc12
-rw-r--r--tests/specs/fmt/css/well_formatted.css3
-rw-r--r--tests/specs/fmt/html/__test__.jsonc12
-rw-r--r--tests/specs/fmt/html/well_formatted.html11
-rw-r--r--tests/specs/fmt/yaml/__test__.jsonc14
-rw-r--r--tests/specs/fmt/yaml/well_formatted.yml3
7 files changed, 82 insertions, 24 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index 489f36f53..7a3c0de4b 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -297,12 +297,7 @@ fn format_markdown(
Ok(None)
}
}
- "yml" | "yaml" => pretty_yaml::format_text(
- text,
- &get_resolved_yaml_config(fmt_options),
- )
- .map(Some)
- .map_err(AnyError::from),
+ "yml" | "yaml" => format_yaml(text, fmt_options),
_ => {
let mut codeblock_config =
get_resolved_typescript_config(fmt_options);
@@ -339,13 +334,33 @@ pub fn format_css(
file_text: &str,
fmt_options: &FmtOptionsConfig,
) -> Result<Option<String>, AnyError> {
- malva::format_text(
+ let formatted_str = malva::format_text(
file_text,
malva::detect_syntax(file_path).unwrap_or(malva::Syntax::Css),
&get_resolved_malva_config(fmt_options),
)
- .map(Some)
- .map_err(AnyError::from)
+ .map_err(AnyError::from)?;
+
+ Ok(if formatted_str == file_text {
+ None
+ } else {
+ Some(formatted_str)
+ })
+}
+
+fn format_yaml(
+ file_text: &str,
+ fmt_options: &FmtOptionsConfig,
+) -> Result<Option<String>, AnyError> {
+ let formatted_str =
+ pretty_yaml::format_text(file_text, &get_resolved_yaml_config(fmt_options))
+ .map_err(AnyError::from)?;
+
+ Ok(if formatted_str == file_text {
+ None
+ } else {
+ Some(formatted_str)
+ })
}
pub fn format_html(
@@ -353,7 +368,7 @@ pub fn format_html(
file_text: &str,
fmt_options: &FmtOptionsConfig,
) -> Result<Option<String>, AnyError> {
- markup_fmt::format_text(
+ let format_result = markup_fmt::format_text(
file_text,
markup_fmt::detect_language(file_path)
.unwrap_or(markup_fmt::Language::Html),
@@ -419,7 +434,6 @@ pub fn format_html(
}
},
)
- .map(Some)
.map_err(|error| match error {
markup_fmt::FormatError::Syntax(error) => AnyError::from(error),
markup_fmt::FormatError::External(errors) => {
@@ -438,6 +452,14 @@ pub fn format_html(
.collect::<String>(),
)
}
+ });
+
+ let formatted_str = format_result?;
+
+ Ok(if formatted_str == file_text {
+ None
+ } else {
+ Some(formatted_str)
})
}
@@ -469,12 +491,7 @@ pub fn format_file(
Ok(None)
}
}
- "yml" | "yaml" => pretty_yaml::format_text(
- file_text,
- &get_resolved_yaml_config(fmt_options),
- )
- .map(Some)
- .map_err(AnyError::from),
+ "yml" | "yaml" => format_yaml(file_text, fmt_options),
"ipynb" => dprint_plugin_jupyter::format_text(
file_text,
|file_path: &Path, file_text: String| {
diff --git a/tests/specs/fmt/css/__test__.jsonc b/tests/specs/fmt/css/__test__.jsonc
index e58818fbe..a3ded6d43 100644
--- a/tests/specs/fmt/css/__test__.jsonc
+++ b/tests/specs/fmt/css/__test__.jsonc
@@ -1,5 +1,13 @@
{
"tempDir": true,
- "args": "fmt",
- "output": "[WILDLINE]badly_formatted.css\nChecked 1 file\n"
+ "tests": {
+ "badly_formatted": {
+ "args": "fmt badly_formatted.css",
+ "output": "[WILDLINE]badly_formatted.css\nChecked 1 file\n"
+ },
+ "well_formatted": {
+ "args": "fmt --check well_formatted.css",
+ "output": "Checked 1 file\n"
+ }
+ }
}
diff --git a/tests/specs/fmt/css/well_formatted.css b/tests/specs/fmt/css/well_formatted.css
new file mode 100644
index 000000000..1653551f4
--- /dev/null
+++ b/tests/specs/fmt/css/well_formatted.css
@@ -0,0 +1,3 @@
+#app > .btn {
+ color: #000;
+}
diff --git a/tests/specs/fmt/html/__test__.jsonc b/tests/specs/fmt/html/__test__.jsonc
index cec29d1b3..7f3169423 100644
--- a/tests/specs/fmt/html/__test__.jsonc
+++ b/tests/specs/fmt/html/__test__.jsonc
@@ -1,5 +1,13 @@
{
"tempDir": true,
- "args": "fmt --unstable-html",
- "output": "[WILDLINE]badly_formatted.html\nChecked 1 file\n"
+ "tests": {
+ "badly_formatted": {
+ "args": "fmt badly_formatted.html",
+ "output": "[WILDLINE]badly_formatted.html\nChecked 1 file\n"
+ },
+ "well_formatted": {
+ "args": "fmt --check well_formatted.html",
+ "output": "Checked 1 file\n"
+ }
+ }
}
diff --git a/tests/specs/fmt/html/well_formatted.html b/tests/specs/fmt/html/well_formatted.html
new file mode 100644
index 000000000..c0c06cd9b
--- /dev/null
+++ b/tests/specs/fmt/html/well_formatted.html
@@ -0,0 +1,11 @@
+<div class="container">content</div>
+
+<script>
+ let counter = 0;
+</script>
+
+<style>
+ .container {
+ padding: 8px;
+ }
+</style>
diff --git a/tests/specs/fmt/yaml/__test__.jsonc b/tests/specs/fmt/yaml/__test__.jsonc
index 3cef276b8..499b4144b 100644
--- a/tests/specs/fmt/yaml/__test__.jsonc
+++ b/tests/specs/fmt/yaml/__test__.jsonc
@@ -1,5 +1,13 @@
{
"tempDir": true,
- "args": "fmt",
- "output": "[WILDLINE]badly_formatted.yml\nChecked 1 file\n"
-} \ No newline at end of file
+ "tests": {
+ "badly_formatted": {
+ "args": "fmt badly_formatted.yml",
+ "output": "[WILDLINE]badly_formatted.yml\nChecked 1 file\n"
+ },
+ "well_formatted": {
+ "args": "fmt --check well_formatted.yml",
+ "output": "Checked 1 file\n"
+ }
+ }
+}
diff --git a/tests/specs/fmt/yaml/well_formatted.yml b/tests/specs/fmt/yaml/well_formatted.yml
new file mode 100644
index 000000000..016e0b951
--- /dev/null
+++ b/tests/specs/fmt/yaml/well_formatted.yml
@@ -0,0 +1,3 @@
+- Test
+- Test
+- Test