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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apps/oxfmt/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl ConfigResolver {
FormatFileStrategy::ExternalFormatterPackageJson { .. } => {
ResolvedOptions::ExternalFormatterPackageJson {
external_options,
sort_package_json: oxfmt_options.sort_package_json,
sort_package_json: oxfmt_options.sort_package_json.is_some(),
insert_final_newline,
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_formatter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ rustc-hash = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
sort-package-json = { workspace = true }
unicode-width = { workspace = true }

[dev-dependencies]
Expand Down
68 changes: 62 additions & 6 deletions crates/oxc_formatter/src/oxfmtrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ pub struct Oxfmtrc {
#[serde(skip_serializing_if = "Option::is_none")]
pub insert_final_newline: Option<bool>,

/// Experimental: Sort import statements. Disabled by default.
/// Experimental: Sort import statements. (Default: disabled)
#[serde(skip_serializing_if = "Option::is_none")]
pub experimental_sort_imports: Option<SortImportsConfig>,

/// Experimental: Sort `package.json` keys. (Default: `true`)
#[serde(skip_serializing_if = "Option::is_none")]
pub experimental_sort_package_json: Option<bool>,
pub experimental_sort_package_json: Option<SortPackageJsonUserConfig>,

/// Experimental: Enable Tailwind CSS class sorting in JSX class/className attributes.
/// When enabled, class strings will be collected and passed to a callback for sorting.
Expand Down Expand Up @@ -242,6 +242,55 @@ pub enum SortOrderConfig {
Desc,
}

/// User-provided configuration for `package.json` sorting.
///
/// - `true`: Enable sorting with default options
/// - `false`: Disable sorting
/// - `{ sortScripts: true }`: Enable sorting with custom options
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
#[serde(untagged)]
pub enum SortPackageJsonUserConfig {
Bool(bool),
Object(SortPackageJsonConfig),
}

impl Default for SortPackageJsonUserConfig {
fn default() -> Self {
Self::Bool(true)
}
}

impl SortPackageJsonUserConfig {
/// Convert to `sort_package_json::SortOptions`.
/// Returns `None` if sorting is disabled.
pub fn to_sort_options(&self) -> Option<sort_package_json::SortOptions> {
match self {
Self::Bool(false) => None,
Self::Bool(true) => Some(SortPackageJsonConfig::default().to_sort_options()),
Self::Object(config) => Some(config.to_sort_options()),
}
}
}

/// Configuration for `package.json` sorting.
#[derive(Debug, Clone, Default, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase", default)]
pub struct SortPackageJsonConfig {
/// Sort the `scripts` field alphabetically. (Default: `false`)
#[serde(skip_serializing_if = "Option::is_none")]
pub sort_scripts: Option<bool>,
}

impl SortPackageJsonConfig {
fn to_sort_options(&self) -> sort_package_json::SortOptions {
sort_package_json::SortOptions {
sort_scripts: self.sort_scripts.unwrap_or(false),
// Small optimization: Prettier will reformat anyway
pretty: false,
}
}
}

/// Configuration for Tailwind CSS class sorting.
/// Based on options from `prettier-plugin-tailwindcss`.
///
Expand Down Expand Up @@ -305,13 +354,17 @@ pub struct TailwindcssConfig {
#[derive(Debug, Clone)]
pub struct OxfmtOptions {
pub ignore_patterns: Vec<String>,
pub sort_package_json: bool,
pub sort_package_json: Option<sort_package_json::SortOptions>,
pub insert_final_newline: bool,
}

impl Default for OxfmtOptions {
fn default() -> Self {
Self { ignore_patterns: vec![], sort_package_json: true, insert_final_newline: true }
Self {
ignore_patterns: vec![],
sort_package_json: Some(SortPackageJsonConfig::default().to_sort_options()),
insert_final_newline: true,
}
}
}

Expand Down Expand Up @@ -500,12 +553,15 @@ impl Oxfmtrc {
}

let mut oxfmt_options = OxfmtOptions::default();

if let Some(patterns) = self.ignore_patterns {
oxfmt_options.ignore_patterns = patterns;
}
if let Some(sort_package_json) = self.experimental_sort_package_json {
oxfmt_options.sort_package_json = sort_package_json;

if let Some(config) = self.experimental_sort_package_json {
oxfmt_options.sort_package_json = config.to_sort_options();
}

if let Some(insert_final_newline) = self.insert_final_newline {
oxfmt_options.insert_final_newline = insert_final_newline;
}
Expand Down
46 changes: 39 additions & 7 deletions crates/oxc_formatter/tests/snapshots/schema_json.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/oxc_formatter/tests/schema.rs
assertion_line: 17
expression: json
---
{
Expand Down Expand Up @@ -134,6 +135,33 @@ expression: json
],
"type": "string"
},
"SortPackageJsonConfig": {
"description": "Configuration for `package.json` sorting.",
"markdownDescription": "Configuration for `package.json` sorting.",
"properties": {
"sortScripts": {
"description": "Sort the `scripts` field alphabetically. (Default: `false`)",
"markdownDescription": "Sort the `scripts` field alphabetically. (Default: `false`)",
"type": [
"boolean",
"null"
]
}
},
"type": "object"
},
"SortPackageJsonUserConfig": {
"anyOf": [
{
"type": "boolean"
},
{
"$ref": "#/definitions/SortPackageJsonConfig"
}
],
"description": "User-provided configuration for `package.json` sorting.\n\n- `true`: Enable sorting with default options\n- `false`: Disable sorting\n- `{ sortScripts: true }`: Enable sorting with custom options",
"markdownDescription": "User-provided configuration for `package.json` sorting.\n\n- `true`: Enable sorting with default options\n- `false`: Disable sorting\n- `{ sortScripts: true }`: Enable sorting with custom options"
},
"TailwindcssConfig": {
"description": "Configuration for Tailwind CSS class sorting.\nBased on options from `prettier-plugin-tailwindcss`.\n\nNote: All `tailwind` prefixes have been removed from option names.\nFor example, use `config` instead of `tailwindConfig`.\n\nSee <https://github.com/tailwindlabs/prettier-plugin-tailwindcss#options>",
"markdownDescription": "Configuration for Tailwind CSS class sorting.\nBased on options from `prettier-plugin-tailwindcss`.\n\nNote: All `tailwind` prefixes have been removed from option names.\nFor example, use `config` instead of `tailwindConfig`.\n\nSee <https://github.com/tailwindlabs/prettier-plugin-tailwindcss#options>",
Expand Down Expand Up @@ -268,16 +296,20 @@ expression: json
"type": "null"
}
],
"description": "Experimental: Sort import statements. Disabled by default.",
"markdownDescription": "Experimental: Sort import statements. Disabled by default."
"description": "Experimental: Sort import statements. (Default: disabled)",
"markdownDescription": "Experimental: Sort import statements. (Default: disabled)"
},
"experimentalSortPackageJson": {
"anyOf": [
{
"$ref": "#/definitions/SortPackageJsonUserConfig"
},
{
"type": "null"
}
],
"description": "Experimental: Sort `package.json` keys. (Default: `true`)",
"markdownDescription": "Experimental: Sort `package.json` keys. (Default: `true`)",
"type": [
"boolean",
"null"
]
"markdownDescription": "Experimental: Sort `package.json` keys. (Default: `true`)"
},
"experimentalTailwindcss": {
"anyOf": [
Expand Down
45 changes: 38 additions & 7 deletions npm/oxfmt/configuration_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,33 @@
],
"type": "string"
},
"SortPackageJsonConfig": {
"description": "Configuration for `package.json` sorting.",
"markdownDescription": "Configuration for `package.json` sorting.",
"properties": {
"sortScripts": {
"description": "Sort the `scripts` field alphabetically. (Default: `false`)",
"markdownDescription": "Sort the `scripts` field alphabetically. (Default: `false`)",
"type": [
"boolean",
"null"
]
}
},
"type": "object"
},
"SortPackageJsonUserConfig": {
"anyOf": [
{
"type": "boolean"
},
{
"$ref": "#/definitions/SortPackageJsonConfig"
}
],
"description": "User-provided configuration for `package.json` sorting.\n\n- `true`: Enable sorting with default options\n- `false`: Disable sorting\n- `{ sortScripts: true }`: Enable sorting with custom options",
"markdownDescription": "User-provided configuration for `package.json` sorting.\n\n- `true`: Enable sorting with default options\n- `false`: Disable sorting\n- `{ sortScripts: true }`: Enable sorting with custom options"
},
"TailwindcssConfig": {
"description": "Configuration for Tailwind CSS class sorting.\nBased on options from `prettier-plugin-tailwindcss`.\n\nNote: All `tailwind` prefixes have been removed from option names.\nFor example, use `config` instead of `tailwindConfig`.\n\nSee <https://github.com/tailwindlabs/prettier-plugin-tailwindcss#options>",
"markdownDescription": "Configuration for Tailwind CSS class sorting.\nBased on options from `prettier-plugin-tailwindcss`.\n\nNote: All `tailwind` prefixes have been removed from option names.\nFor example, use `config` instead of `tailwindConfig`.\n\nSee <https://github.com/tailwindlabs/prettier-plugin-tailwindcss#options>",
Expand Down Expand Up @@ -264,16 +291,20 @@
"type": "null"
}
],
"description": "Experimental: Sort import statements. Disabled by default.",
"markdownDescription": "Experimental: Sort import statements. Disabled by default."
"description": "Experimental: Sort import statements. (Default: disabled)",
"markdownDescription": "Experimental: Sort import statements. (Default: disabled)"
},
"experimentalSortPackageJson": {
"anyOf": [
{
"$ref": "#/definitions/SortPackageJsonUserConfig"
},
{
"type": "null"
}
],
"description": "Experimental: Sort `package.json` keys. (Default: `true`)",
"markdownDescription": "Experimental: Sort `package.json` keys. (Default: `true`)",
"type": [
"boolean",
"null"
]
"markdownDescription": "Experimental: Sort `package.json` keys. (Default: `true`)"
},
"experimentalTailwindcss": {
"anyOf": [
Expand Down
12 changes: 10 additions & 2 deletions tasks/website_formatter/src/snapshots/schema_markdown.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Which end of line characters to apply. (Default: `"lf"`)
type: `object | null`


Experimental: Sort import statements. Disabled by default.
Experimental: Sort import statements. (Default: disabled)


### experimentalSortImports.groups
Expand Down Expand Up @@ -140,12 +140,20 @@ Sort side-effect imports. (Default: `false`)

## experimentalSortPackageJson

type: `boolean | null`
type: `object | boolean | null`


Experimental: Sort `package.json` keys. (Default: `true`)


### experimentalSortPackageJson.sortScripts

type: `boolean | null`


Sort the `scripts` field alphabetically. (Default: `false`)


## experimentalTailwindcss

type: `object | null`
Expand Down
Loading