Skip to content
Merged
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
22 changes: 14 additions & 8 deletions apps/oxfmt/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,20 @@ impl ConfigResolver {
apply_editorconfig(&mut format_config, props);
}

// NOTE: Revisit this when adding Prettier plugin support.
// We use `format_config` directly instead of merging with `raw_config`.
// To preserve plugin-specific options,
// we would need to merge `raw_config` first, then apply `format_config` values on top.
// Or we could keep track of plugin options separately in `FormatConfig` itself,
// like how Tailwindcss options are handled currently.
let mut external_options = serde_json::to_value(&format_config)
.expect("FormatConfig serialization should not fail");

// Convert `FormatConfig` to `OxfmtOptions`, applying defaults where needed
let oxfmt_options = format_config
.into_oxfmt_options()
.map_err(|err| format!("Failed to parse configuration.\n{err}"))?;

// Apply our resolved defaults to Prettier options too
// e.g. set `printWidth: 100` if not specified (= Prettier default: 80)
// NOTE: `raw_config` is used to preserve unknown options for Prettier plugins.
// If we decide to support only known plugins, and keep their options inside `FormatConfig` like Tailwindcss,
// we can use `format_config` instead, which may be a bit efficient.
let mut external_options = self.raw_config.clone();
populate_prettier_config(&oxfmt_options.format_options, &mut external_options);

// Save cache for fast path: no per-file overrides
Expand Down Expand Up @@ -266,7 +269,7 @@ impl ConfigResolver {
}

// Slow path: reconstruct `FormatConfig` to apply overrides
// See `build_and_validate()` for why we cannot base this on cached options directly
// Overrides are merged at `FormatConfig` level, not `OxfmtOptions` level
let mut format_config: FormatConfig = serde_json::from_value(self.raw_config.clone())
.expect("`build_and_validate()` should catch this before");

Expand All @@ -282,11 +285,14 @@ impl ConfigResolver {
apply_editorconfig(&mut format_config, &props);
}

// NOTE: See `build_and_validate()` for details about `external_options` handling
let mut external_options = serde_json::to_value(&format_config)
.expect("FormatConfig serialization should not fail");

let oxfmt_options = format_config
.into_oxfmt_options()
.expect("If this fails, there is an issue with override values");

let mut external_options = self.raw_config.clone();
populate_prettier_config(&oxfmt_options.format_options, &mut external_options);

(oxfmt_options, external_options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ exports[`oxfmtrc overrides > Prettier options override - only enabled for specif
"--------------------
arguments: --check .
working directory: oxfmtrc_overrides/fixtures/prettier_overrides
exit code: 1
exit code: 0
--- STDOUT ---------
Checking formatting...

indented.vue (<variable>ms)

Format issues found in above 1 files. Run without \`--check\` to fix.
All matched files use the correct format.
Finished in <variable>ms on 5 files using 1 threads.
--- STDERR ---------

Expand Down
Loading