Skip to content

Commit a77ca8b

Browse files
committed
feat(oxfmt): Support ignorePatterns in oxfmtrc
1 parent 64b8226 commit a77ca8b

File tree

11 files changed

+92
-9
lines changed

11 files changed

+92
-9
lines changed

apps/oxfmt/src/format.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
};
88

99
use oxc_diagnostics::DiagnosticService;
10-
use oxc_formatter::{FormatOptions, Oxfmtrc};
10+
use oxc_formatter::Oxfmtrc;
1111

1212
use crate::{
1313
cli::{CliRunResult, FormatCommand},
@@ -49,8 +49,8 @@ impl FormatRunner {
4949
// NOTE: Currently, we only load single config file.
5050
// - from `--config` if specified
5151
// - else, search nearest for the nearest `.oxfmtrc.json` from cwd upwards
52-
let format_options = match load_config(&cwd, basic_options.config.as_ref()) {
53-
Ok(options) => options,
52+
let config = match load_config(&cwd, basic_options.config.as_ref()) {
53+
Ok(config) => config,
5454
Err(err) => {
5555
print_and_flush_stdout(
5656
stdout,
@@ -60,11 +60,21 @@ impl FormatRunner {
6060
}
6161
};
6262

63+
let ignore_patterns = config.ignore_patterns.clone().unwrap_or_default();
64+
let format_options = match config.into_format_options() {
65+
Ok(options) => options,
66+
Err(err) => {
67+
print_and_flush_stdout(stdout, &format!("Failed to parse configuration.\n{err}\n"));
68+
return CliRunResult::InvalidOptionConfig;
69+
}
70+
};
71+
6372
let walker = match Walk::build(
6473
&cwd,
6574
&paths,
6675
&ignore_options.ignore_path,
6776
ignore_options.with_node_modules,
77+
&ignore_patterns,
6878
) {
6979
Ok(walker) => walker,
7080
Err(err) => {
@@ -171,7 +181,7 @@ impl FormatRunner {
171181
/// Returns error if:
172182
/// - Config file is specified but not found or invalid
173183
/// - Config file parsing fails
174-
fn load_config(cwd: &Path, config_path: Option<&PathBuf>) -> Result<FormatOptions, String> {
184+
fn load_config(cwd: &Path, config_path: Option<&PathBuf>) -> Result<Oxfmtrc, String> {
175185
let config_path = if let Some(config_path) = config_path {
176186
// If `--config` is explicitly specified, use that path
177187
Some(if config_path.is_absolute() {
@@ -194,9 +204,9 @@ fn load_config(cwd: &Path, config_path: Option<&PathBuf>) -> Result<FormatOption
194204
};
195205

196206
match config_path {
197-
Some(ref path) => Oxfmtrc::from_file(path)?.into_format_options(),
207+
Some(ref path) => Oxfmtrc::from_file(path),
198208
// Default if not specified and not found
199-
None => Ok(FormatOptions::default()),
209+
None => Ok(Oxfmtrc::default()),
200210
}
201211
}
202212

apps/oxfmt/src/walk.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl Walk {
1818
paths: &[PathBuf],
1919
ignore_paths: &[PathBuf],
2020
with_node_modules: bool,
21+
ignore_patterns: &[String],
2122
) -> Result<Self, String> {
2223
let (target_paths, exclude_patterns) = normalize_paths(cwd, paths);
2324

@@ -55,8 +56,12 @@ impl Walk {
5556
return Err(format!("Failed to add ignore file: {}", ignore_path.display()));
5657
}
5758
}
58-
// TODO: Support config.ignorePatterns
59-
// Use `builder.add_line(None, pattern_str)` here
59+
// Handle `config.ignorePatterns`
60+
for pattern in ignore_patterns {
61+
if builder.add_line(None, pattern).is_err() {
62+
return Err(format!("Failed to add ignore pattern `{pattern}`"));
63+
}
64+
}
6065
let ignores = builder.build().map_err(|_| "Failed to build ignores".to_string())?;
6166

6267
// NOTE: If return `false` here, it will not be `visit()`ed at all
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ignorePatterns": ["not-formatted/"]
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ignorePatterns": ["*.js"],
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class Bar {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class

apps/oxfmt/tests/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,22 @@ fn ignore_patterns() {
183183
);
184184
}
185185

186+
#[test]
187+
fn config_ignore_patterns() {
188+
Tester::new()
189+
.with_cwd(PathBuf::from("tests/fixtures/config_ignore_patterns"))
190+
.test_and_snapshot_multiple(
191+
"config_ignore_patterns",
192+
&[
193+
// .oxfmtrc.json contains: ignorePatterns: ["not-formatted/"]
194+
// Should not find any files
195+
&["--check"],
196+
// fmtrc.jsonc also ignores every .js files
197+
&["--check", "--config", "fmtrc.jsonc"],
198+
],
199+
);
200+
}
201+
186202
#[test]
187203
fn ignore_and_override() {
188204
Tester::new()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: apps/oxfmt/tests/tester.rs
3+
---
4+
##########
5+
arguments: --check
6+
working directory: tests/fixtures/config_ignore_patterns
7+
----------
8+
Checking formatting...
9+
10+
Expected at least one target file
11+
----------
12+
CLI result: NoFilesFound
13+
----------
14+
15+
##########
16+
arguments: --check --config fmtrc.jsonc
17+
working directory: tests/fixtures/config_ignore_patterns
18+
----------
19+
Checking formatting...
20+
21+
Expected at least one target file
22+
----------
23+
CLI result: NoFilesFound
24+
----------

crates/oxc_formatter/src/service/oxfmtrc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pub struct Oxfmtrc {
4848
// TODO: experimental_ternaries
4949
#[serde(skip_serializing_if = "Option::is_none")]
5050
pub experimental_sort_imports: Option<SortImportsConfig>,
51+
#[serde(skip_serializing_if = "Option::is_none")]
52+
pub ignore_patterns: Option<Vec<String>>,
5153
}
5254

5355
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]

crates/oxc_formatter/tests/snapshots/schema_json.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ expression: json
4848
}
4949
]
5050
},
51+
"ignorePatterns": {
52+
"type": [
53+
"array",
54+
"null"
55+
],
56+
"items": {
57+
"type": "string"
58+
}
59+
},
5160
"jsxSingleQuote": {
5261
"type": [
5362
"boolean",

0 commit comments

Comments
 (0)