Skip to content

Commit 656da7b

Browse files
committed
refactor(oxfmt): Minimize populate options for Prettier (#18022)
To invoke Prettier, there is a function that performs mappings to `populate_prettier_config()` results based on Oxfmt's defaults and `.editorconfig` values. However, - Doing this for every field would be wasteful, - Now, it's mostly the same as Prettier's default settings - Except for `endOfLine: auto` So it has been kept to the absolute minimum.
1 parent b1e9dec commit 656da7b

File tree

1 file changed

+12
-99
lines changed

1 file changed

+12
-99
lines changed

apps/oxfmt/src/core/oxfmtrc.rs

Lines changed: 12 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -758,26 +758,31 @@ fn build_toml_options(format_options: &FormatOptions) -> TomlFormatterOptions {
758758
/// Populates the raw config JSON with resolved `FormatOptions` values.
759759
/// This ensures `external_formatter`(Prettier) receives the same options that `oxc_formatter` uses.
760760
///
761-
/// Roughly the reverse of `into_options()`.
761+
/// Only options that meet one of these criteria need to be mapped:
762+
/// - 1. Different defaults between Prettier and oxc_formatter
763+
/// - e.g. `printWidth`: Prettier: 80, Oxfmt: 100
764+
/// - 2. Can be set via `.editorconfig` (values won't be in raw config JSON)
765+
/// - `max_line_length` -> `printWidth`
766+
/// - `end_of_line` -> `endOfLine`
767+
/// - `indent_style` -> `useTabs`
768+
/// - `indent_size` -> `tabWidth`
762769
pub fn populate_prettier_config(options: &FormatOptions, config: &mut Value) {
763770
let Some(obj) = config.as_object_mut() else {
764771
return;
765772
};
766773

767-
// [Prettier] useTabs: boolean
774+
// vs Prettier defaults and `.editorconfig` values
775+
obj.insert("printWidth".to_string(), Value::from(options.line_width.value()));
776+
777+
// vs `.editorconfig` values
768778
obj.insert(
769779
"useTabs".to_string(),
770780
Value::from(match options.indent_style {
771781
IndentStyle::Tab => true,
772782
IndentStyle::Space => false,
773783
}),
774784
);
775-
776-
// [Prettier] tabWidth: number
777785
obj.insert("tabWidth".to_string(), Value::from(options.indent_width.value()));
778-
779-
// [Prettier] endOfLine: "lf" | "cr" | "crlf" | "auto"
780-
// NOTE: "auto" is not supported by `oxc_formatter`
781786
obj.insert(
782787
"endOfLine".to_string(),
783788
Value::from(match options.line_ending {
@@ -787,98 +792,6 @@ pub fn populate_prettier_config(options: &FormatOptions, config: &mut Value) {
787792
}),
788793
);
789794

790-
// [Prettier] printWidth: number
791-
obj.insert("printWidth".to_string(), Value::from(options.line_width.value()));
792-
793-
// [Prettier] singleQuote: boolean
794-
obj.insert(
795-
"singleQuote".to_string(),
796-
Value::from(match options.quote_style {
797-
QuoteStyle::Single => true,
798-
QuoteStyle::Double => false,
799-
}),
800-
);
801-
802-
// [Prettier] jsxSingleQuote: boolean
803-
obj.insert(
804-
"jsxSingleQuote".to_string(),
805-
Value::from(match options.jsx_quote_style {
806-
QuoteStyle::Single => true,
807-
QuoteStyle::Double => false,
808-
}),
809-
);
810-
811-
// [Prettier] quoteProps: "as-needed" | "consistent" | "preserve"
812-
obj.insert(
813-
"quoteProps".to_string(),
814-
Value::from(match options.quote_properties {
815-
QuoteProperties::AsNeeded => "as-needed",
816-
QuoteProperties::Consistent => "consistent",
817-
QuoteProperties::Preserve => "preserve",
818-
}),
819-
);
820-
821-
// [Prettier] trailingComma: "all" | "es5" | "none"
822-
obj.insert(
823-
"trailingComma".to_string(),
824-
Value::from(match options.trailing_commas {
825-
TrailingCommas::All => "all",
826-
TrailingCommas::Es5 => "es5",
827-
TrailingCommas::None => "none",
828-
}),
829-
);
830-
831-
// [Prettier] semi: boolean
832-
obj.insert(
833-
"semi".to_string(),
834-
Value::from(match options.semicolons {
835-
Semicolons::Always => true,
836-
Semicolons::AsNeeded => false,
837-
}),
838-
);
839-
840-
// [Prettier] arrowParens: "avoid" | "always"
841-
obj.insert(
842-
"arrowParens".to_string(),
843-
Value::from(match options.arrow_parentheses {
844-
ArrowParentheses::AsNeeded => "avoid",
845-
ArrowParentheses::Always => "always",
846-
}),
847-
);
848-
849-
// [Prettier] bracketSpacing: boolean
850-
obj.insert("bracketSpacing".to_string(), Value::from(options.bracket_spacing.value()));
851-
852-
// [Prettier] bracketSameLine: boolean
853-
obj.insert("bracketSameLine".to_string(), Value::from(options.bracket_same_line.value()));
854-
855-
// [Prettier] singleAttributePerLine: boolean
856-
obj.insert(
857-
"singleAttributePerLine".to_string(),
858-
Value::from(match options.attribute_position {
859-
AttributePosition::Multiline => true,
860-
AttributePosition::Auto => false,
861-
}),
862-
);
863-
864-
// [Prettier] objectWrap: "preserve" | "collapse"
865-
obj.insert(
866-
"objectWrap".to_string(),
867-
Value::from(match options.expand {
868-
Expand::Auto => "preserve",
869-
Expand::Never => "collapse",
870-
}),
871-
);
872-
873-
// [Prettier] embeddedLanguageFormatting: "auto" | "off"
874-
obj.insert(
875-
"embeddedLanguageFormatting".to_string(),
876-
Value::from(match options.embedded_language_formatting {
877-
EmbeddedLanguageFormatting::Auto => "auto",
878-
EmbeddedLanguageFormatting::Off => "off",
879-
}),
880-
);
881-
882795
// Below are our own extensions, just remove them
883796
obj.remove("ignorePatterns");
884797
obj.remove("insertFinalNewline");

0 commit comments

Comments
 (0)