From 39f73631c8a60014044e2440cb709fffaa8d6ca8 Mon Sep 17 00:00:00 2001 From: leaysgur <6259812+leaysgur@users.noreply.github.com> Date: Wed, 11 Mar 2026 04:34:48 +0000 Subject: [PATCH] refactor(oxfmt): Limit auto discover `oxfmt.config."ts"` only (#20216) I noticed this while working on Oxlint, so I made the same change. Adding support later is easy, but reducing it is difficult. --- apps/oxfmt/src/core/config.rs | 17 ++++++------ apps/oxfmt/src/lsp/server_formatter.rs | 1 - apps/oxfmt/test/lsp/init/init.test.ts | 36 ++------------------------ 3 files changed, 11 insertions(+), 43 deletions(-) diff --git a/apps/oxfmt/src/core/config.rs b/apps/oxfmt/src/core/config.rs index 106086f4b25ab..b556a2a4595bf 100644 --- a/apps/oxfmt/src/core/config.rs +++ b/apps/oxfmt/src/core/config.rs @@ -26,19 +26,20 @@ use super::{ const JSON_CONFIG_FILES: &[&str] = &[".oxfmtrc.json", ".oxfmtrc.jsonc"]; /// JS/TS config file extensions. const JS_CONFIG_EXTENSIONS: &[&str] = &["ts", "mts", "cts", "js", "mjs", "cjs"]; -/// Oxfmt JS/TS config file prefix. -const OXFMT_JS_CONFIG_PREFIX: &str = "oxfmt.config."; -/// Vite+ config file prefix that may contain Oxfmt config under a `.fmt` field. -const VITE_PLUS_JS_CONFIG_PREFIX: &str = "vite.config."; +/// Oxfmt JS/TS config file name. +/// Only `.ts` extension is supported, matching oxlint's behavior. +const OXFMT_JS_CONFIG_NAME: &str = "oxfmt.config.ts"; +/// Vite+ config file name that may contain Oxfmt config under a `.fmt` field. +/// Only `.ts` extension is supported, matching oxlint's behavior. +const VITE_PLUS_CONFIG_NAME: &str = "vite.config.ts"; #[cfg(feature = "napi")] const VITE_PLUS_OXFMT_CONFIG_FIELD: &str = "fmt"; /// Returns an iterator of all supported config file names, in priority order. pub fn all_config_file_names() -> impl Iterator { let json = JSON_CONFIG_FILES.iter().map(|f| (*f).to_string()); - let oxfmt_js = JS_CONFIG_EXTENSIONS.iter().map(|ext| format!("{OXFMT_JS_CONFIG_PREFIX}{ext}")); - let vite_plus = - JS_CONFIG_EXTENSIONS.iter().map(|ext| format!("{VITE_PLUS_JS_CONFIG_PREFIX}{ext}")); + let oxfmt_js = std::iter::once(OXFMT_JS_CONFIG_NAME.to_string()); + let vite_plus = std::iter::once(VITE_PLUS_CONFIG_NAME.to_string()); json.chain(oxfmt_js).chain(vite_plus) } @@ -273,7 +274,7 @@ impl ConfigResolver { let is_vite_plus = path .file_name() .and_then(|f| f.to_str()) - .is_some_and(|name| name.starts_with(VITE_PLUS_JS_CONFIG_PREFIX)); + .is_some_and(|name| name == VITE_PLUS_CONFIG_NAME); let raw_config = if is_vite_plus { raw_config.get(VITE_PLUS_OXFMT_CONFIG_FIELD).cloned().ok_or_else(|| { format!("{}\nExpected a `{VITE_PLUS_OXFMT_CONFIG_FIELD}` field in the default export.", path.display()) diff --git a/apps/oxfmt/src/lsp/server_formatter.rs b/apps/oxfmt/src/lsp/server_formatter.rs index 74c535fa0ab7b..125181ed2f64a 100644 --- a/apps/oxfmt/src/lsp/server_formatter.rs +++ b/apps/oxfmt/src/lsp/server_formatter.rs @@ -233,7 +233,6 @@ impl Tool for ServerFormatter { if let Some(config_path) = options.config_path.as_ref().filter(|s| !s.is_empty()) { vec![config_path.clone()] } else { - // TODO: This can be glob patterns? all_config_file_names().collect() }; diff --git a/apps/oxfmt/test/lsp/init/init.test.ts b/apps/oxfmt/test/lsp/init/init.test.ts index 9a1dab5296cb0..a18659529c6dd 100644 --- a/apps/oxfmt/test/lsp/init/init.test.ts +++ b/apps/oxfmt/test/lsp/init/init.test.ts @@ -18,43 +18,11 @@ describe("LSP initialization", () => { it.each([ [ undefined, - [ - ".oxfmtrc.json", - ".oxfmtrc.jsonc", - "oxfmt.config.ts", - "oxfmt.config.mts", - "oxfmt.config.cts", - "oxfmt.config.js", - "oxfmt.config.mjs", - "oxfmt.config.cjs", - "vite.config.ts", - "vite.config.mts", - "vite.config.cts", - "vite.config.js", - "vite.config.mjs", - "vite.config.cjs", - ".editorconfig", - ], + [".oxfmtrc.json", ".oxfmtrc.jsonc", "oxfmt.config.ts", "vite.config.ts", ".editorconfig"], ], [ { "fmt.configPath": "" }, - [ - ".oxfmtrc.json", - ".oxfmtrc.jsonc", - "oxfmt.config.ts", - "oxfmt.config.mts", - "oxfmt.config.cts", - "oxfmt.config.js", - "oxfmt.config.mjs", - "oxfmt.config.cjs", - "vite.config.ts", - "vite.config.mts", - "vite.config.cts", - "vite.config.js", - "vite.config.mjs", - "vite.config.cjs", - ".editorconfig", - ], + [".oxfmtrc.json", ".oxfmtrc.jsonc", "oxfmt.config.ts", "vite.config.ts", ".editorconfig"], ], [{ "fmt.configPath": "./custom-config.json" }, ["./custom-config.json", ".editorconfig"]], ])(