diff --git a/apps/oxfmt/src/command.rs b/apps/oxfmt/src/command.rs index ebe6ca7253338..68c4113a25fea 100644 --- a/apps/oxfmt/src/command.rs +++ b/apps/oxfmt/src/command.rs @@ -21,7 +21,7 @@ const PATHS_ERROR_MESSAGE: &str = "PATH must not contain \"..\""; #[derive(Debug, Clone, Bpaf)] #[bpaf(options, version(VERSION))] pub struct FormatCommand { - #[bpaf(external, fallback(OutputOptions::DefaultWrite), hide_usage)] + #[bpaf(external, fallback(OutputOptions::Write), hide_usage)] pub output_options: OutputOptions, #[bpaf(external)] pub basic_options: BasicOptions, @@ -43,7 +43,7 @@ pub struct FormatCommand { pub enum OutputOptions { /// Default - when no output option is specified, behaves like `--write` mode in Prettier #[bpaf(hide)] - DefaultWrite, + Write, /// Check mode - check if files are formatted, also show statistics #[bpaf(long)] Check, diff --git a/apps/oxfmt/src/format.rs b/apps/oxfmt/src/format.rs index 4cd8c4c69b862..a11f4244d42ac 100644 --- a/apps/oxfmt/src/format.rs +++ b/apps/oxfmt/src/format.rs @@ -194,7 +194,7 @@ impl FormatRunner { CliRunResult::FormatMismatch } // Default (write) does not output anything - (OutputOptions::DefaultWrite, changed_count) => { + (OutputOptions::Write, changed_count) => { // Each changed file is also NOT printed debug_assert_eq!( changed_count, 0, diff --git a/apps/oxfmt/src/service.rs b/apps/oxfmt/src/service.rs index d4e8e20715b12..df9df79e9b383 100644 --- a/apps/oxfmt/src/service.rs +++ b/apps/oxfmt/src/service.rs @@ -152,7 +152,7 @@ impl FormatService { let is_changed = source_text != code; // Write back if needed - if matches!(self.output_options, OutputOptions::DefaultWrite) && is_changed { + if matches!(self.output_options, OutputOptions::Write) && is_changed { fs::write(path, code) .map_err(|_| format!("Failed to write to '{}'", path.to_string_lossy())) .unwrap(); diff --git a/apps/oxfmt/test/__snapshots__/write_mode.test.ts.snap b/apps/oxfmt/test/__snapshots__/write_mode.test.ts.snap index c16258c9300fa..ada65cd316ecb 100644 --- a/apps/oxfmt/test/__snapshots__/write_mode.test.ts.snap +++ b/apps/oxfmt/test/__snapshots__/write_mode.test.ts.snap @@ -28,6 +28,34 @@ const obj = { --------------------" `; +exports[`write_mode > should format unformatted files with explicit --write 1`] = ` +"--- FILE ----------- +unformatted.js +--- BEFORE --------- + class Foo {} + +--- AFTER ---------- +class Foo {} + +-------------------- + +--- FILE ----------- +complex.js +--- BEFORE --------- +const obj = { + foo: "bar", + baz: 123 +}; + +--- AFTER ---------- +const obj = { + foo: "bar", + baz: 123, +}; + +--------------------" +`; + exports[`write_mode > should not modify already formatted files 1`] = ` "--- FILE ----------- already_formatted.js diff --git a/apps/oxfmt/test/write_mode.test.ts b/apps/oxfmt/test/write_mode.test.ts index 86fad96468a08..90d48e55396b7 100644 --- a/apps/oxfmt/test/write_mode.test.ts +++ b/apps/oxfmt/test/write_mode.test.ts @@ -13,6 +13,14 @@ describe("write_mode", () => { expect(snapshot).toMatchSnapshot(); }); + it("should format unformatted files with explicit --write", async () => { + const fixtureDir = join(fixturesDir, "write_mode"); + const files = ["unformatted.js", "complex.js"]; + + const snapshot = await runWriteModeAndSnapshot(fixtureDir, files, ["--write"]); + expect(snapshot).toMatchSnapshot(); + }); + it("should not modify already formatted files", async () => { const fixtureDir = join(fixturesDir, "write_mode"); const files = ["already_formatted.js"];