diff --git a/apps/oxfmt/test/__snapshots__/editorconfig.test.ts.snap b/apps/oxfmt/test/__snapshots__/editorconfig.test.ts.snap new file mode 100644 index 0000000000000..b8dcad46b9e3c --- /dev/null +++ b/apps/oxfmt/test/__snapshots__/editorconfig.test.ts.snap @@ -0,0 +1,83 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`editorconfig > .oxfmtrc overrides .editorconfig 1`] = ` +"--- FILE ----------- +test.js +--- BEFORE --------- +if (true) { console.log("hello"); } + +--- AFTER ---------- +if (true) { + console.log("hello"); +} + +--------------------" +`; + +exports[`editorconfig > basic settings from .editorconfig 1`] = ` +"--- FILE ----------- +test.js +--- BEFORE --------- +if (condition) { console.log("hello world"); } + +--- AFTER ---------- +if (condition) { + console.log("hello world"); +} + +--------------------" +`; + +exports[`editorconfig > empty .editorconfig 1`] = ` +"--- FILE ----------- +test.js +--- BEFORE --------- +if (true) { console.log("hello"); } + +--- AFTER ---------- +if (true) { + console.log("hello"); +} + +--------------------" +`; + +exports[`editorconfig > no root [*] section 1`] = ` +"--- FILE ----------- +test.js +--- BEFORE --------- +if (true) { console.log("js"); } + +--- AFTER ---------- +if (true) { + console.log("js"); +} + +-------------------- + +--- FILE ----------- +test.ts +--- BEFORE --------- +if (true) { console.log("ts"); } + +--- AFTER ---------- +if (true) { + console.log("ts"); +} + +--------------------" +`; + +exports[`editorconfig > partial override - settings from both files merged 1`] = ` +"--- FILE ----------- +test.js +--- BEFORE --------- +if (true) { console.log("hello"); } + +--- AFTER ---------- +if (true) { + console.log("hello"); +} + +--------------------" +`; diff --git a/apps/oxfmt/test/editorconfig.test.ts b/apps/oxfmt/test/editorconfig.test.ts new file mode 100644 index 0000000000000..b03a5592bac47 --- /dev/null +++ b/apps/oxfmt/test/editorconfig.test.ts @@ -0,0 +1,92 @@ +import { describe, expect, it } from "vitest"; +import { join } from "node:path"; +import { runWriteModeAndSnapshot } from "./utils"; + +const fixturesDir = join(__dirname, "fixtures", "editorconfig"); + +describe("editorconfig", () => { + // .editorconfig: + // [*] indent_style=tab, indent_size=4, max_line_length=40 + // + // Expected: useTabs=true, tabWidth=4, printWidth=40 + // - Code should be broken into multiple lines (printWidth=40) + // - Indentation should use tabs (not spaces) + it("basic settings from .editorconfig", async () => { + const cwd = join(fixturesDir, "basic"); + const snapshot = await runWriteModeAndSnapshot(cwd, ["test.js"]); + expect(snapshot).toMatchSnapshot(); + }); + + // .editorconfig: + // [*] indent_style=tab, indent_size=8 + // .oxfmtrc.json: + // useTabs=false, tabWidth=2 + // + // Expected: useTabs=false, tabWidth=2 (oxfmtrc wins) + // - Indentation should use 2 spaces, NOT tabs + it(".oxfmtrc overrides .editorconfig", async () => { + const cwd = join(fixturesDir, "with_oxfmtrc"); + const snapshot = await runWriteModeAndSnapshot(cwd, ["test.js"]); + expect(snapshot).toMatchSnapshot(); + }); + + // .editorconfig: + // [*] indent_style=space, indent_size=2 + // [*.ts] indent_size=4 + // [nested/**/*.js] indent_size=8 + // [nested/**/*.json] indent_size=8, max_line_length=40 + // + // Expected: + // - test.js: tabWidth=2 (from [*], not matched by [nested/**/*.js]) + // - test.ts: tabWidth=4 (from [*.ts]) + // - nested/deep/test.js: tabWidth=8 (from [nested/**/*.js], deep path glob) + // - nested/deep/test.json: tabWidth=8, printWidth=40 (from [nested/**/*.json], deep path glob for external formatter) + // TODO: Should fix `editor_config_parser` to accept `cwd` for `resolve()` + // oxlint-disable-next-line vitest/no-disabled-tests + it.skip("per-file overrides", async () => { + const cwd = join(fixturesDir, "per_file_override"); + const snapshot = await runWriteModeAndSnapshot(cwd, [ + "test.js", + "test.ts", + "nested/deep/test.js", + "nested/deep/test.json", + ]); + expect(snapshot).toMatchSnapshot(); + }); + + // .editorconfig: + // [*.js] indent_style=tab, indent_size=4 + // (no [*] section) + // + // Expected: + // - test.js: useTabs=true, tabWidth=4 (from [*.js]) + // - test.ts: default settings (no matching section) + it("no root [*] section", async () => { + const cwd = join(fixturesDir, "no_root_section"); + const snapshot = await runWriteModeAndSnapshot(cwd, ["test.js", "test.ts"]); + expect(snapshot).toMatchSnapshot(); + }); + + // .editorconfig: + // [*] indent_style=tab + // .oxfmtrc.json: + // tabWidth=8 + // + // Expected: useTabs=true (from editorconfig), tabWidth=8 (from oxfmtrc) + // - Both settings should be merged, not one overwriting the other entirely + it("partial override - settings from both files merged", async () => { + const cwd = join(fixturesDir, "partial_override"); + const snapshot = await runWriteModeAndSnapshot(cwd, ["test.js"]); + expect(snapshot).toMatchSnapshot(); + }); + + // .editorconfig: (empty file) + // + // Expected: default settings (useTabs=false, tabWidth=2) + // - Empty editorconfig should not cause errors + it("empty .editorconfig", async () => { + const cwd = join(fixturesDir, "empty"); + const snapshot = await runWriteModeAndSnapshot(cwd, ["test.js"]); + expect(snapshot).toMatchSnapshot(); + }); +}); diff --git a/apps/oxfmt/test/fixtures/editorconfig/basic/.editorconfig b/apps/oxfmt/test/fixtures/editorconfig/basic/.editorconfig new file mode 100644 index 0000000000000..a3ea1e0eca803 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/basic/.editorconfig @@ -0,0 +1,4 @@ +[*] +indent_style = tab +indent_size = 4 +max_line_length = 40 diff --git a/apps/oxfmt/test/fixtures/editorconfig/basic/test.js b/apps/oxfmt/test/fixtures/editorconfig/basic/test.js new file mode 100644 index 0000000000000..c62814b697ed6 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/basic/test.js @@ -0,0 +1 @@ +if (condition) { console.log("hello world"); } diff --git a/apps/oxfmt/test/fixtures/editorconfig/empty/.editorconfig b/apps/oxfmt/test/fixtures/editorconfig/empty/.editorconfig new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/apps/oxfmt/test/fixtures/editorconfig/empty/test.js b/apps/oxfmt/test/fixtures/editorconfig/empty/test.js new file mode 100644 index 0000000000000..656bdf0e446da --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/empty/test.js @@ -0,0 +1 @@ +if (true) { console.log("hello"); } diff --git a/apps/oxfmt/test/fixtures/editorconfig/no_root_section/.editorconfig b/apps/oxfmt/test/fixtures/editorconfig/no_root_section/.editorconfig new file mode 100644 index 0000000000000..a61df1942ac65 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/no_root_section/.editorconfig @@ -0,0 +1,3 @@ +[*.js] +indent_style = tab +indent_size = 4 diff --git a/apps/oxfmt/test/fixtures/editorconfig/no_root_section/test.js b/apps/oxfmt/test/fixtures/editorconfig/no_root_section/test.js new file mode 100644 index 0000000000000..e7db88e782e78 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/no_root_section/test.js @@ -0,0 +1 @@ +if (true) { console.log("js"); } diff --git a/apps/oxfmt/test/fixtures/editorconfig/no_root_section/test.ts b/apps/oxfmt/test/fixtures/editorconfig/no_root_section/test.ts new file mode 100644 index 0000000000000..13532e5078cc9 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/no_root_section/test.ts @@ -0,0 +1 @@ +if (true) { console.log("ts"); } diff --git a/apps/oxfmt/test/fixtures/editorconfig/partial_override/.editorconfig b/apps/oxfmt/test/fixtures/editorconfig/partial_override/.editorconfig new file mode 100644 index 0000000000000..0b225afd48019 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/partial_override/.editorconfig @@ -0,0 +1,2 @@ +[*] +indent_style = tab diff --git a/apps/oxfmt/test/fixtures/editorconfig/partial_override/.oxfmtrc.json b/apps/oxfmt/test/fixtures/editorconfig/partial_override/.oxfmtrc.json new file mode 100644 index 0000000000000..ba0fb2c580afd --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/partial_override/.oxfmtrc.json @@ -0,0 +1,3 @@ +{ + "tabWidth": 8 +} diff --git a/apps/oxfmt/test/fixtures/editorconfig/partial_override/test.js b/apps/oxfmt/test/fixtures/editorconfig/partial_override/test.js new file mode 100644 index 0000000000000..656bdf0e446da --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/partial_override/test.js @@ -0,0 +1 @@ +if (true) { console.log("hello"); } diff --git a/apps/oxfmt/test/fixtures/editorconfig/per_file_override/.editorconfig b/apps/oxfmt/test/fixtures/editorconfig/per_file_override/.editorconfig new file mode 100644 index 0000000000000..55c5601cad967 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/per_file_override/.editorconfig @@ -0,0 +1,13 @@ +[*] +indent_style = space +indent_size = 2 + +[*.ts] +indent_size = 4 + +[nested/**/*.js] +indent_size = 8 + +[nested/deep/*.json] +indent_size = 8 +max_line_length = 40 diff --git a/apps/oxfmt/test/fixtures/editorconfig/per_file_override/nested/deep/test.js b/apps/oxfmt/test/fixtures/editorconfig/per_file_override/nested/deep/test.js new file mode 100644 index 0000000000000..f9659dbfa67a3 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/per_file_override/nested/deep/test.js @@ -0,0 +1 @@ +if (true) { console.log("nested js"); } diff --git a/apps/oxfmt/test/fixtures/editorconfig/per_file_override/nested/deep/test.json b/apps/oxfmt/test/fixtures/editorconfig/per_file_override/nested/deep/test.json new file mode 100644 index 0000000000000..1fa028913d743 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/per_file_override/nested/deep/test.json @@ -0,0 +1 @@ +{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8} diff --git a/apps/oxfmt/test/fixtures/editorconfig/per_file_override/test.js b/apps/oxfmt/test/fixtures/editorconfig/per_file_override/test.js new file mode 100644 index 0000000000000..e7db88e782e78 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/per_file_override/test.js @@ -0,0 +1 @@ +if (true) { console.log("js"); } diff --git a/apps/oxfmt/test/fixtures/editorconfig/per_file_override/test.ts b/apps/oxfmt/test/fixtures/editorconfig/per_file_override/test.ts new file mode 100644 index 0000000000000..13532e5078cc9 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/per_file_override/test.ts @@ -0,0 +1 @@ +if (true) { console.log("ts"); } diff --git a/apps/oxfmt/test/fixtures/editorconfig/with_oxfmtrc/.editorconfig b/apps/oxfmt/test/fixtures/editorconfig/with_oxfmtrc/.editorconfig new file mode 100644 index 0000000000000..efd7997c93110 --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/with_oxfmtrc/.editorconfig @@ -0,0 +1,3 @@ +[*] +indent_style = tab +indent_size = 8 diff --git a/apps/oxfmt/test/fixtures/editorconfig/with_oxfmtrc/.oxfmtrc.json b/apps/oxfmt/test/fixtures/editorconfig/with_oxfmtrc/.oxfmtrc.json new file mode 100644 index 0000000000000..8973894db7f6f --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/with_oxfmtrc/.oxfmtrc.json @@ -0,0 +1,4 @@ +{ + "useTabs": false, + "tabWidth": 2 +} diff --git a/apps/oxfmt/test/fixtures/editorconfig/with_oxfmtrc/test.js b/apps/oxfmt/test/fixtures/editorconfig/with_oxfmtrc/test.js new file mode 100644 index 0000000000000..656bdf0e446da --- /dev/null +++ b/apps/oxfmt/test/fixtures/editorconfig/with_oxfmtrc/test.js @@ -0,0 +1 @@ +if (true) { console.log("hello"); }