mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(fmt): ignore file directive for YAML files (#26717)
Closes https://github.com/denoland/deno/issues/26712 Support `# deno-fmt-ignore-file` directive for YAML files. Also added tests for single line ignores.
This commit is contained in:
parent
1bdecc8c73
commit
a119ead8a0
9 changed files with 64 additions and 0 deletions
|
@ -353,6 +353,21 @@ fn format_yaml(
|
|||
file_text: &str,
|
||||
fmt_options: &FmtOptionsConfig,
|
||||
) -> Result<Option<String>, AnyError> {
|
||||
let ignore_file = file_text
|
||||
.lines()
|
||||
.take_while(|line| line.starts_with('#'))
|
||||
.any(|line| {
|
||||
line
|
||||
.strip_prefix('#')
|
||||
.unwrap()
|
||||
.trim()
|
||||
.starts_with("deno-fmt-ignore-file")
|
||||
});
|
||||
|
||||
if ignore_file {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let formatted_str =
|
||||
pretty_yaml::format_text(file_text, &get_resolved_yaml_config(fmt_options))
|
||||
.map_err(AnyError::from)?;
|
||||
|
|
|
@ -8,6 +8,30 @@
|
|||
"well_formatted": {
|
||||
"args": "fmt --check well_formatted.yml",
|
||||
"output": "Checked 1 file\n"
|
||||
},
|
||||
"ignore_line": {
|
||||
"args": "fmt --check ignore_line.yml",
|
||||
"output": "Checked 1 file\n"
|
||||
},
|
||||
"ignore_file": {
|
||||
"args": "fmt ignore_file.yaml",
|
||||
"output": "Checked 1 file\n"
|
||||
},
|
||||
"ignore_file2": {
|
||||
"args": "fmt ignore_file2.yaml",
|
||||
"output": "Checked 1 file\n"
|
||||
},
|
||||
"ignore_file3": {
|
||||
"args": "fmt ignore_file3.yaml",
|
||||
"output": "Checked 1 file\n"
|
||||
},
|
||||
"ignore_file4": {
|
||||
"args": "fmt ignore_file4.yaml",
|
||||
"output": "Checked 1 file\n"
|
||||
},
|
||||
"wrong_file_ignore": {
|
||||
"args": "fmt wrong_file_ignore.yaml",
|
||||
"output": "wrong_file_ignore.out"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
2
tests/specs/fmt/yaml/ignore_file.yaml
Normal file
2
tests/specs/fmt/yaml/ignore_file.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
# deno-fmt-ignore-file
|
||||
{{something crazy
|
2
tests/specs/fmt/yaml/ignore_file2.yaml
Normal file
2
tests/specs/fmt/yaml/ignore_file2.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
#deno-fmt-ignore-file
|
||||
{{something crazy
|
5
tests/specs/fmt/yaml/ignore_file3.yaml
Normal file
5
tests/specs/fmt/yaml/ignore_file3.yaml
Normal file
|
@ -0,0 +1,5 @@
|
|||
# Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
|
||||
# incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
|
||||
# quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
||||
# deno-fmt-ignore-file
|
||||
{{something crazy
|
2
tests/specs/fmt/yaml/ignore_file4.yaml
Normal file
2
tests/specs/fmt/yaml/ignore_file4.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
# deno-fmt-ignore-file Because this is templated yaml file
|
||||
{{something crazy
|
2
tests/specs/fmt/yaml/ignore_line.yml
Normal file
2
tests/specs/fmt/yaml/ignore_line.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
# deno-fmt-ignore
|
||||
- Test
|
7
tests/specs/fmt/yaml/wrong_file_ignore.out
Normal file
7
tests/specs/fmt/yaml/wrong_file_ignore.out
Normal file
|
@ -0,0 +1,7 @@
|
|||
Error formatting: [WILDCARD]wrong_file_ignore.yaml
|
||||
parse error at line 5, column 1
|
||||
|
|
||||
5 | {{something crazy
|
||||
| ^
|
||||
|
||||
Checked 1 file
|
5
tests/specs/fmt/yaml/wrong_file_ignore.yaml
Normal file
5
tests/specs/fmt/yaml/wrong_file_ignore.yaml
Normal file
|
@ -0,0 +1,5 @@
|
|||
# File ignore directive only works if it's in the first cluster
|
||||
# of comment, ie. there are no empty lines after the first n-leading lines.
|
||||
|
||||
# deno-fmt-ignore-file
|
||||
{{something crazy
|
Loading…
Reference in a new issue