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
4 changes: 2 additions & 2 deletions apps/oxfmt/src-js/config.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export interface Oxfmtrc {
* Specify the number of spaces per indentation-level.
*
* - Default: `2`
* - Overrides `.editorconfig.indent_size`
* - Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)
*/
tabWidth?: number;
/**
Expand Down Expand Up @@ -474,7 +474,7 @@ export interface FormatConfig {
* Specify the number of spaces per indentation-level.
*
* - Default: `2`
* - Overrides `.editorconfig.indent_size`
* - Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)
*/
tabWidth?: number;
/**
Expand Down
19 changes: 14 additions & 5 deletions apps/oxfmt/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ fn load_editorconfig(
/// - end_of_line
/// - indent_style
/// - indent_size
/// - tab_width
/// - insert_final_newline
fn has_editorconfig_overrides(editorconfig: &EditorConfig, path: &Path) -> bool {
let sections = editorconfig.sections();
Expand All @@ -632,6 +633,7 @@ fn has_editorconfig_overrides(editorconfig: &EditorConfig, path: &Path) -> bool
|| resolved.end_of_line != root.end_of_line
|| resolved.indent_style != root.indent_style
|| resolved.indent_size != root.indent_size
|| resolved.tab_width != root.tab_width
|| resolved.insert_final_newline != root.insert_final_newline
}
// No `[*]` section means any resolved property is an override
Expand All @@ -640,6 +642,7 @@ fn has_editorconfig_overrides(editorconfig: &EditorConfig, path: &Path) -> bool
|| resolved.end_of_line != EditorConfigProperty::Unset
|| resolved.indent_style != EditorConfigProperty::Unset
|| resolved.indent_size != EditorConfigProperty::Unset
|| resolved.tab_width != EditorConfigProperty::Unset
|| resolved.insert_final_newline != EditorConfigProperty::Unset
}
}
Expand Down Expand Up @@ -676,11 +679,17 @@ fn apply_editorconfig(config: &mut FormatConfig, props: &EditorConfigProperties)
});
}

#[expect(clippy::cast_possible_truncation)]
if config.tab_width.is_none()
&& let EditorConfigProperty::Value(size) = props.indent_size
{
config.tab_width = Some(size as u8);
if config.tab_width.is_none() {
// Match Prettier's behavior: Only use `indent_size` when `useTabs: false`.
// https://github.com/prettier/prettier/blob/90983f40dce5e20beea4e5618b5e0426a6a7f4f0/src/config/editorconfig/editorconfig-to-prettier.js#L25-L30
#[expect(clippy::cast_possible_truncation)]
if config.use_tabs == Some(false)
&& let EditorConfigProperty::Value(size) = props.indent_size
{
config.tab_width = Some(size as u8);
} else if let EditorConfigProperty::Value(size) = props.tab_width {
config.tab_width = Some(size as u8);
}
}

if config.insert_final_newline.is_none()
Expand Down
2 changes: 1 addition & 1 deletion apps/oxfmt/src/core/oxfmtrc/format_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct FormatConfig {
/// Specify the number of spaces per indentation-level.
///
/// - Default: `2`
/// - Overrides `.editorconfig.indent_size`
/// - Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)
#[serde(skip_serializing_if = "Option::is_none")]
pub tab_width: Option<u8>,
/// Which end of line characters to apply.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,17 @@ nested/deep/test.json

--------------------"
`;

exports[`editorconfig > tab_width fallback when indent_size is not set 1`] = `
"--- FILE -----------
test.js
--- BEFORE ---------
if (condition) { console.log("hello world"); }

--- AFTER ----------
if (condition) {
console.log("hello world");
}

--------------------"
`;
12 changes: 12 additions & 0 deletions apps/oxfmt/test/cli/editorconfig/editorconfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ describe("editorconfig", () => {
expect(snapshot).toMatchSnapshot();
});

// .editorconfig:
// [*] indent_style=space, tab_width=8
// (no indent_size)
//
// Expected: useTabs=false, tabWidth=8 (tab_width used as fallback for indent_size)
// - Indentation should use 8 spaces
it("tab_width fallback when indent_size is not set", async () => {
const cwd = join(fixturesDir, "tab_width_fallback");
const snapshot = await runWriteModeAndSnapshot(cwd, ["test.js"]);
expect(snapshot).toMatchSnapshot();
});

// .editorconfig: (empty file)
//
// Expected: default settings (useTabs=false, tabWidth=2)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*]
indent_style = space
tab_width = 8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (condition) { console.log("hello world"); }
8 changes: 4 additions & 4 deletions npm/oxfmt/configuration_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,11 @@
"markdownDescription": "Sort Tailwind CSS classes.\n\nUsing the same algorithm as [prettier-plugin-tailwindcss](https://github.com/tailwindlabs/prettier-plugin-tailwindcss).\nOption names omit the `tailwind` prefix used in the original plugin (e.g., `config` instead of `tailwindConfig`).\nFor details, see each field's documentation.\n\nPass `true` or an object to enable with defaults, or omit/set `false` to disable.\n\n- Default: Disabled"
},
"tabWidth": {
"description": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size`",
"description": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)",
"type": "integer",
"format": "uint8",
"minimum": 0.0,
"markdownDescription": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size`"
"markdownDescription": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)"
},
"trailingComma": {
"description": "Print trailing commas wherever possible in multi-line comma-separated syntactic structures.\n\nA single-line array, for example, never gets trailing commas.\n\n- Default: `\"all\"`",
Expand Down Expand Up @@ -391,11 +391,11 @@
"markdownDescription": "Sort Tailwind CSS classes.\n\nUsing the same algorithm as [prettier-plugin-tailwindcss](https://github.com/tailwindlabs/prettier-plugin-tailwindcss).\nOption names omit the `tailwind` prefix used in the original plugin (e.g., `config` instead of `tailwindConfig`).\nFor details, see each field's documentation.\n\nPass `true` or an object to enable with defaults, or omit/set `false` to disable.\n\n- Default: Disabled"
},
"tabWidth": {
"description": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size`",
"description": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)",
"type": "integer",
"format": "uint8",
"minimum": 0.0,
"markdownDescription": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size`"
"markdownDescription": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)"
},
"trailingComma": {
"description": "Print trailing commas wherever possible in multi-line comma-separated syntactic structures.\n\nA single-line array, for example, never gets trailing commas.\n\n- Default: `\"all\"`",
Expand Down
8 changes: 4 additions & 4 deletions tasks/website_formatter/src/snapshots/schema_json.snap
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ expression: json
"markdownDescription": "Sort Tailwind CSS classes.\n\nUsing the same algorithm as [prettier-plugin-tailwindcss](https://github.com/tailwindlabs/prettier-plugin-tailwindcss).\nOption names omit the `tailwind` prefix used in the original plugin (e.g., `config` instead of `tailwindConfig`).\nFor details, see each field's documentation.\n\nPass `true` or an object to enable with defaults, or omit/set `false` to disable.\n\n- Default: Disabled"
},
"tabWidth": {
"description": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size`",
"description": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)",
"type": "integer",
"format": "uint8",
"minimum": 0.0,
"markdownDescription": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size`"
"markdownDescription": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)"
},
"trailingComma": {
"description": "Print trailing commas wherever possible in multi-line comma-separated syntactic structures.\n\nA single-line array, for example, never gets trailing commas.\n\n- Default: `\"all\"`",
Expand Down Expand Up @@ -395,11 +395,11 @@ expression: json
"markdownDescription": "Sort Tailwind CSS classes.\n\nUsing the same algorithm as [prettier-plugin-tailwindcss](https://github.com/tailwindlabs/prettier-plugin-tailwindcss).\nOption names omit the `tailwind` prefix used in the original plugin (e.g., `config` instead of `tailwindConfig`).\nFor details, see each field's documentation.\n\nPass `true` or an object to enable with defaults, or omit/set `false` to disable.\n\n- Default: Disabled"
},
"tabWidth": {
"description": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size`",
"description": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)",
"type": "integer",
"format": "uint8",
"minimum": 0.0,
"markdownDescription": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size`"
"markdownDescription": "Specify the number of spaces per indentation-level.\n\n- Default: `2`\n- Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)"
},
"trailingComma": {
"description": "Print trailing commas wherever possible in multi-line comma-separated syntactic structures.\n\nA single-line array, for example, never gets trailing commas.\n\n- Default: `\"all\"`",
Expand Down
4 changes: 2 additions & 2 deletions tasks/website_formatter/src/snapshots/schema_markdown.snap
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ type: `integer`
Specify the number of spaces per indentation-level.

- Default: `2`
- Overrides `.editorconfig.indent_size`
- Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)


##### overrides[n].options.trailingComma
Expand Down Expand Up @@ -1443,7 +1443,7 @@ type: `integer`
Specify the number of spaces per indentation-level.

- Default: `2`
- Overrides `.editorconfig.indent_size`
- Overrides `.editorconfig.indent_size` (falls back to `.editorconfig.tab_width`)


## trailingComma
Expand Down
Loading