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
62 changes: 47 additions & 15 deletions apps/oxfmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,15 @@ impl FormatRunner {
}
};

// 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()
.map(|path| if path.is_relative() { cwd.join(path) } else { path })
.collect();
// Normalize user input paths
let (target_paths, exclude_patterns) = normalize_paths(&cwd, &paths);

// Build exclude patterns if any exist
let override_builder = (!exclude_patterns.is_empty())
.then(|| {
let mut builder = OverrideBuilder::new(&cwd);
for pattern in exclude_patterns {
builder.add(&pattern.to_string_lossy()).ok()?;
for pattern_str in exclude_patterns {
builder.add(&pattern_str).ok()?;
}
builder.build().ok()
})
Expand Down Expand Up @@ -213,6 +202,49 @@ fn load_config(cwd: &Path, config: Option<&PathBuf>) -> Result<FormatOptions, St
Ok(FormatOptions::default())
}

/// Normalize user input paths into `target_paths` and `exclude_patterns`.
/// - `target_paths`: Absolute paths to format
/// - `exclude_patterns`: Pattern strings to exclude (with `!` prefix)
fn normalize_paths(cwd: &Path, input_paths: &[PathBuf]) -> (Vec<PathBuf>, Vec<String>) {
let mut target_paths = vec![];
let mut exclude_patterns = vec![];

for path in input_paths {
let path_str = path.to_string_lossy();

// Instead of `oxlint`'s `--ignore-pattern=PAT`,
// `oxfmt` supports `!` prefix in paths like Prettier.
if path_str.starts_with('!') {
exclude_patterns.push(path_str.to_string());
continue;
}

// Otherwise, treat as target path

if path.is_absolute() {
target_paths.push(path.clone());
continue;
}

// NOTE: `.` and cwd behaves differently, need to normalize
let path = if path_str == "." {
cwd.to_path_buf()
} else if let Some(stripped) = path_str.strip_prefix("./") {
cwd.join(stripped)
} else {
cwd.join(path)
};
target_paths.push(path);
}

// Default to cwd if no `target_paths` are provided
if target_paths.is_empty() {
target_paths.push(cwd.into());
}

(target_paths, exclude_patterns)
}

fn print_and_flush_stdout(stdout: &mut dyn Write, message: &str) {
use std::io::{Error, ErrorKind};
fn check_for_writer_error(error: Error) -> Result<(), Error> {
Expand Down
2 changes: 0 additions & 2 deletions apps/oxfmt/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ impl Walk {
/// Will not canonicalize paths.
/// # Panics
pub fn new(paths: &[PathBuf], override_builder: Option<Override>) -> Self {
assert!(!paths.is_empty(), "At least one path must be provided to Walk::new");

let mut inner = ignore::WalkBuilder::new(
paths
.iter()
Expand Down
19 changes: 12 additions & 7 deletions apps/oxfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ fn multiple_files() {
.test_and_snapshot_multiple(&[
// Explicit file list
&["--check", "simple.js", "arrow.js"],
// Directory
&["--check", "."],
// Default to current directory
&["--check"],
// Explicit cwd
&["--check", "."],
&["--check", "./"],
]);
}

Expand Down Expand Up @@ -106,18 +107,22 @@ fn exclude_nested_paths() {
#[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/bar/error.js"],
&["--check", ".", "!foo/bar"],
&["--check", ".", "!foo"],
&["--check", ".", "!**/error.js"],
]);
// Split to avoid too long file name error...
Tester::new()
.with_cwd(PathBuf::from("tests/fixtures/exclude_nested"))
.test_and_snapshot_multiple(&[
&["--check", "./foo", "!**/bar/error.js"],
&["--check", "./foo", "!**/error.js"],
&["--check", "./foo", "!**/bar/*"],
// &["--check", "./foo", "!foo/bar/error.js"],
// &["--check", "./foo", "!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,50 @@
---
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
----------
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@
source: apps/oxfmt/tests/tester.rs
---
##########
arguments: --check . !foo
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 1 files using 1 threads.
Finished in <variable>ms on 2 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check . !**/error.js
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 3 files using 1 threads.
Finished in <variable>ms on 2 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check ./foo !**/bar/error.js
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.
Finished in <variable>ms on 1 files using 1 threads.
----------
CLI result: FormatSucceeded
----------

##########
arguments: --check ./foo !**/error.js
arguments: --check ./foo !foo/bar/error.js
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...
Expand All @@ -50,7 +50,7 @@ CLI result: FormatSucceeded
----------

##########
arguments: --check ./foo !**/bar/*
arguments: --check ./foo !foo/bar
working directory: tests/fixtures/exclude_nested
----------
Checking formatting...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ Finished in <variable>ms on 2 files using 1 threads.
CLI result: FormatMismatch
----------

##########
arguments: --check
working directory: tests/fixtures/multiple_files
----------
Checking formatting...
arrow.js (<variable>ms)
simple.js (<variable>ms)

Format issues found in above 2 files. Run without `--check` to fix.
Finished in <variable>ms on 2 files using 1 threads.
----------
CLI result: FormatMismatch
----------

##########
arguments: --check .
working directory: tests/fixtures/multiple_files
Expand All @@ -30,7 +44,7 @@ CLI result: FormatMismatch
----------

##########
arguments: --check
arguments: --check ./
working directory: tests/fixtures/multiple_files
----------
Checking formatting...
Expand Down
Loading