summaryrefslogtreecommitdiff
path: root/cli/tools/fmt.rs
diff options
context:
space:
mode:
authorJoão Baptista <15786310+M4RC3L05@users.noreply.github.com>2024-11-19 21:01:16 +0000
committerGitHub <noreply@github.com>2024-11-19 21:01:16 +0000
commitc55e936be03a3a023330789f903e2fbd12f4a308 (patch)
tree0dc406b0cfb82a6c0683edb9152c6450aa3abf52 /cli/tools/fmt.rs
parent628816448ea45807762fd8eb0c59302c9f2aac9a (diff)
feat(fmt): support SQL (#26750)
This commit adds support for .sql files in "deno fmt" subcommand. Closes: https://github.com/denoland/deno/issues/25024 --------- Signed-off-by: m4rc3l05 <15786310+M4RC3L05@users.noreply.github.com> Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli/tools/fmt.rs')
-rw-r--r--cli/tools/fmt.rs64
1 files changed, 63 insertions, 1 deletions
diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs
index d40abd5f5..9c2c70912 100644
--- a/cli/tools/fmt.rs
+++ b/cli/tools/fmt.rs
@@ -272,6 +272,7 @@ fn format_markdown(
| "njk"
| "yml"
| "yaml"
+ | "sql"
) {
// It's important to tell dprint proper file extension, otherwise
// it might parse the file twice.
@@ -301,6 +302,13 @@ fn format_markdown(
}
}
"yml" | "yaml" => format_yaml(text, fmt_options),
+ "sql" => {
+ if unstable_options.sql {
+ format_sql(text, fmt_options)
+ } else {
+ Ok(None)
+ }
+ }
_ => {
let mut codeblock_config =
get_resolved_typescript_config(fmt_options);
@@ -503,7 +511,48 @@ pub fn format_html(
})
}
-/// Formats a single TS, TSX, JS, JSX, JSONC, JSON, MD, or IPYNB file.
+pub fn format_sql(
+ 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 mut formatted_str = sqlformat::format(
+ file_text,
+ &sqlformat::QueryParams::None,
+ &sqlformat::FormatOptions {
+ ignore_case_convert: None,
+ indent: if fmt_options.use_tabs.unwrap_or_default() {
+ sqlformat::Indent::Tabs
+ } else {
+ sqlformat::Indent::Spaces(fmt_options.indent_width.unwrap_or(2))
+ },
+ // leave one blank line between queries.
+ lines_between_queries: 2,
+ uppercase: Some(true),
+ },
+ );
+
+ // Add single new line to the end of file.
+ formatted_str.push('\n');
+
+ Ok(Some(formatted_str))
+}
+
+/// Formats a single TS, TSX, JS, JSX, JSONC, JSON, MD, IPYNB or SQL file.
pub fn format_file(
file_path: &Path,
file_text: &str,
@@ -538,6 +587,13 @@ pub fn format_file(
format_file(file_path, &file_text, fmt_options, unstable_options, None)
},
),
+ "sql" => {
+ if unstable_options.sql {
+ format_sql(file_text, fmt_options)
+ } else {
+ Ok(None)
+ }
+ }
_ => {
let config = get_resolved_typescript_config(fmt_options);
dprint_plugin_typescript::format_text(
@@ -1209,6 +1265,7 @@ fn is_supported_ext_fmt(path: &Path) -> bool {
| "yml"
| "yaml"
| "ipynb"
+ | "sql"
)
})
}
@@ -1269,6 +1326,11 @@ mod test {
assert!(is_supported_ext_fmt(Path::new("foo.yaml")));
assert!(is_supported_ext_fmt(Path::new("foo.YaML")));
assert!(is_supported_ext_fmt(Path::new("foo.ipynb")));
+ assert!(is_supported_ext_fmt(Path::new("foo.sql")));
+ assert!(is_supported_ext_fmt(Path::new("foo.Sql")));
+ assert!(is_supported_ext_fmt(Path::new("foo.sQl")));
+ assert!(is_supported_ext_fmt(Path::new("foo.sqL")));
+ assert!(is_supported_ext_fmt(Path::new("foo.SQL")));
}
#[test]