Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fmt): add TOML formatter #28104

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 83 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ dotenvy = "0.15.7"
dprint-plugin-json = "=0.19.4"
dprint-plugin-jupyter = "=0.1.5"
dprint-plugin-markdown = "=0.17.8"
dprint-plugin-toml = "=0.6.4"
dprint-plugin-typescript = "=0.93.3"
fancy-regex = "=0.10.0"
faster-hex.workspace = true
Expand Down
36 changes: 36 additions & 0 deletions cli/tools/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ fn format_markdown(
| "yml"
| "yaml"
| "sql"
| "toml"
) {
// It's important to tell dprint proper file extension, otherwise
// it might parse the file twice.
Expand Down Expand Up @@ -305,6 +306,7 @@ fn format_markdown(
}
}
"yml" | "yaml" => format_yaml(text, fmt_options),
"toml" => format_toml(text, fmt_options),
"sql" => {
if unstable_options.sql {
format_sql(text, fmt_options)
Expand Down Expand Up @@ -362,6 +364,38 @@ pub fn format_css(
})
}

fn format_toml(
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 = dprint_plugin_toml::format_text(
&PathBuf::from(""),
file_text,
&dprint_plugin_toml::configuration::ConfigurationBuilder::new()
.line_width(fmt_options.line_width.unwrap_or(80))
.use_tabs(fmt_options.use_tabs.unwrap_or_default())
.indent_width(fmt_options.indent_width.unwrap_or(2))
.build(),
)?;

Ok(formatted_str)
}

fn format_yaml(
file_text: &str,
fmt_options: &FmtOptionsConfig,
Expand Down Expand Up @@ -590,6 +624,7 @@ pub fn format_file(
}
}
"yml" | "yaml" => format_yaml(file_text, fmt_options),
"toml" => format_toml(file_text, fmt_options),
"ipynb" => dprint_plugin_jupyter::format_text(
file_text,
|file_path: &Path, file_text: String| {
Expand Down Expand Up @@ -1285,6 +1320,7 @@ fn is_supported_ext_fmt(path: &Path) -> bool {
| "yaml"
| "ipynb"
| "sql"
| "toml"
)
})
}
Expand Down
37 changes: 37 additions & 0 deletions tests/specs/fmt/toml/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"tempDir": true,
"tests": {
"badly_formatted": {
"args": "fmt badly_formatted.toml",
"output": "[WILDLINE]badly_formatted.toml\nChecked 1 file\n"
},
"well_formatted": {
"args": "fmt --check well_formatted.toml",
"output": "Checked 1 file\n"
},
"ignore_line": {
"args": "fmt --check ignore_line.toml",
"output": "Checked 1 file\n"
},
"ignore_file": {
"args": "fmt ignore_file.toml",
"output": "Checked 1 file\n"
},
"ignore_file2": {
"args": "fmt ignore_file2.toml",
"output": "Checked 1 file\n"
},
"ignore_file3": {
"args": "fmt ignore_file3.toml",
"output": "Checked 1 file\n"
},
"ignore_file4": {
"args": "fmt ignore_file4.toml",
"output": "Checked 1 file\n"
},
"wrong_file_ignore": {
"args": "fmt wrong_file_ignore.toml",
"output": "wrong_file_ignore.out"
}
}
}
15 changes: 15 additions & 0 deletions tests/specs/fmt/toml/badly_formatted.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
data = [ ["delta", "phi"], [3.14] ]
temp_targets = { cpu = 79.5, case = 72.0 }

[servers]

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"
2 changes: 2 additions & 0 deletions tests/specs/fmt/toml/ignore_file.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# deno-fmt-ignore-file
enabled = true
2 changes: 2 additions & 0 deletions tests/specs/fmt/toml/ignore_file2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#deno-fmt-ignore-file
enabled = true
8 changes: 8 additions & 0 deletions tests/specs/fmt/toml/ignore_file3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 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



enabled = true
4 changes: 4 additions & 0 deletions tests/specs/fmt/toml/ignore_file4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# deno-fmt-ignore-file Because this is templated yaml file


enabled = true
2 changes: 2 additions & 0 deletions tests/specs/fmt/toml/ignore_line.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# deno-fmt-ignore
enabled = true
15 changes: 15 additions & 0 deletions tests/specs/fmt/toml/well_formatted.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[database]
enabled = true
ports = [8000, 8001, 8002]
data = [["delta", "phi"], [3.14]]
temp_targets = { cpu = 79.5, case = 72.0 }

[servers]

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"
5 changes: 5 additions & 0 deletions tests/specs/fmt/toml/wrong_file_ignore.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Error formatting: [WILDCARD]wrong_file_ignore.toml
Line 5, column 10: expected value

enabled =
Checked 1 file
5 changes: 5 additions & 0 deletions tests/specs/fmt/toml/wrong_file_ignore.toml
Original file line number Diff line number Diff line change
@@ -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
enabled =
Loading