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
12 changes: 8 additions & 4 deletions apps/oxfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ impl FormatRunner {
let FormatCommand { paths, output_options, basic_options, misc_options } = self.options;

// Find and load config
// NOTE: Currently, we only load single config file.
// - from `--config` if specified
// - else, search nearest for the nearest `.oxfmtrc.json` from cwd upwards
let format_options = match load_config(&cwd, basic_options.config.as_ref()) {
Ok(options) => options,
Err(err) => {
Expand All @@ -58,13 +61,14 @@ impl FormatRunner {
}
};

// Default to current working directory if no paths are provided
let paths = if paths.is_empty() { vec![cwd.clone()] } else { paths };

// Instead of `--ignore-pattern=PAT`, we support `!` prefix in paths
// Instead of `oxlint`'s `--ignore-pattern=PAT`, `oxfmt` supports `!` prefix in paths like Prettier
let (exclude_patterns, target_paths): (Vec<_>, Vec<_>) =
paths.into_iter().partition(|p| p.to_string_lossy().starts_with('!'));

// Default to cwd if no `target_paths` are provided
// NOTE: Do not specify `.` as cwd here, it behaves differently
let target_paths = if target_paths.is_empty() { vec![cwd.clone()] } else { target_paths };

// Resolve relative paths against the current working directory
let target_paths: Vec<PathBuf> = target_paths
.into_iter()
Expand Down
2 changes: 2 additions & 0 deletions apps/oxfmt/tests/fixtures/exclude_nested/foo/bar/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file should be excluded
class {
1 change: 1 addition & 0 deletions apps/oxfmt/tests/fixtures/exclude_nested/foo/bar/ok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 1;
1 change: 1 addition & 0 deletions apps/oxfmt/tests/fixtures/exclude_nested/foo/ok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 1;
1 change: 1 addition & 0 deletions apps/oxfmt/tests/fixtures/exclude_nested/ok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = 1;
37 changes: 37 additions & 0 deletions apps/oxfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,40 @@ fn vcs_dirs_ignored() {
.with_cwd(PathBuf::from("tests/fixtures/vcs_dirs"))
.test_and_snapshot_multiple(&[&["--check"]]);
}

#[test]
fn exclude_nested_paths() {
// Test that nested path exclusion works correctly
// See: https://github.com/oxc-project/oxc/issues/14684
// All these cases should not report parse error from `foo/bar/error.js`
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/exclude_nested"))
.test_and_snapshot_multiple(&[
&["--check", "!foo/bar/error.js"],
&["--check", "!foo/bar"],
&["--check", "!foo"],
&["--check", "!**/error.js"],
&["--check", "foo", "!foo/bar/error.js"],
&["--check", "foo", "!foo/bar"],
&["--check", "foo", "!**/bar/error.js"],
&["--check", "foo", "!**/bar/*"],
]);
}
#[test]
fn exclude_nested_paths_with_dot() {
// All these cases should not report parse error from `foo/bar/error.js`
// TODO: Make the commented cases work as well.
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/exclude_nested"))
.test_and_snapshot_multiple(&[
// &["--check", ".", "!foo/bar/error.js"],
// &["--check", ".", "!foo/bar"],
&["--check", ".", "!foo"],
&["--check", ".", "!**/error.js"],
&["--check", "./foo", "!**/bar/error.js"],
&["--check", "./foo", "!**/error.js"],
&["--check", "./foo", "!**/bar/*"],
// &["--check", "./foo", "!foo/bar/error.js"],
// &["--check", "./foo", "!foo/bar"],
]);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
source: apps/oxfmt/tests/tester.rs
---
##########
arguments: --check !foo/bar/error.js
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 3 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check !foo/bar
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 2 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check !foo
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 1 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check !**/error.js
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 3 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check foo !foo/bar/error.js
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 2 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check foo !foo/bar
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 1 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check foo !**/bar/error.js
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 2 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check foo !**/bar/*
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 1 files using 1 threads.
----------
CLI result: FormatSucceeded
----------
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
source: apps/oxfmt/tests/tester.rs
---
##########
arguments: --check . !foo
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 1 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check . !**/error.js
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 3 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check ./foo !**/bar/error.js
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 2 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check ./foo !**/error.js
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 2 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check ./foo !**/bar/*
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...

All matched files use the correct format.
Finished in <variable>ms on 1 files using 1 threads.
----------
CLI result: FormatSucceeded
----------
Loading