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
83 changes: 83 additions & 0 deletions apps/oxfmt/test/__snapshots__/editorconfig.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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");
}

--------------------"
`;
92 changes: 92 additions & 0 deletions apps/oxfmt/test/editorconfig.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
4 changes: 4 additions & 0 deletions apps/oxfmt/test/fixtures/editorconfig/basic/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*]
indent_style = tab
indent_size = 4
max_line_length = 40
1 change: 1 addition & 0 deletions apps/oxfmt/test/fixtures/editorconfig/basic/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (condition) { console.log("hello world"); }
Empty file.
1 change: 1 addition & 0 deletions apps/oxfmt/test/fixtures/editorconfig/empty/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (true) { console.log("hello"); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*.js]
indent_style = tab
indent_size = 4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (true) { console.log("js"); }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (true) { console.log("ts"); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[*]
indent_style = tab
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"tabWidth": 8
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (true) { console.log("hello"); }
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (true) { console.log("nested js"); }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7,"h":8}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (true) { console.log("js"); }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (true) { console.log("ts"); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[*]
indent_style = tab
indent_size = 8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"useTabs": false,
"tabWidth": 2
}
1 change: 1 addition & 0 deletions apps/oxfmt/test/fixtures/editorconfig/with_oxfmtrc/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
if (true) { console.log("hello"); }
Loading