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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 8 additions & 50 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,6 @@ impl LintRunner {
builder.add(&pattern).unwrap();
}
}
if !oxlintrc.ignore_patterns.is_empty() {
let oxlint_wd = oxlintrc.path.parent().unwrap_or(&self.cwd).to_path_buf();
oxlintrc.ignore_patterns =
Self::adjust_ignore_patterns(&self.cwd, &oxlint_wd, oxlintrc.ignore_patterns);
for pattern in &oxlintrc.ignore_patterns {
let pattern = format!("!{pattern}");
builder.add(&pattern).unwrap();
}
}

let builder = builder.build().unwrap();

Expand Down Expand Up @@ -179,7 +170,6 @@ impl LintRunner {

let walker = Walk::new(&paths, &ignore_options, override_builder);
let paths = walker.paths();
let number_of_files = paths.len();

let mut external_plugin_store = ExternalPluginStore::default();

Expand Down Expand Up @@ -312,10 +302,17 @@ impl LintRunner {
}
}

let files_to_lint = paths
.into_iter()
.filter(|path| !config_store.should_ignore(Path::new(path)))
.collect::<Vec<Arc<OsStr>>>();

let linter = Linter::new(LintOptions::default(), config_store, self.external_linter)
.with_fix(fix_options.fix_kind())
.with_report_unused_directives(report_unused_directives);

let number_of_files = files_to_lint.len();

let tsconfig = basic_options.tsconfig;
if let Some(path) = tsconfig.as_ref() {
if path.is_file() {
Expand All @@ -340,7 +337,7 @@ impl LintRunner {
// Spawn linting in another thread so diagnostics can be printed immediately from diagnostic_service.run.
rayon::spawn(move || {
let mut lint_service = LintService::new(linter, options);
lint_service.with_paths(paths);
lint_service.with_paths(files_to_lint);

// Use `RawTransferFileSystem` if `oxlint2` feature is enabled.
// This reads the source text into start of allocator, instead of the end.
Expand Down Expand Up @@ -552,30 +549,6 @@ impl LintRunner {
Ok(None)
}
}

fn adjust_ignore_patterns(
base: &PathBuf,
path: &PathBuf,
ignore_patterns: Vec<String>,
) -> Vec<String> {
if base == path {
ignore_patterns
} else {
let relative_ignore_path =
path.strip_prefix(base).map_or_else(|_| PathBuf::from("."), Path::to_path_buf);

ignore_patterns
.into_iter()
.map(|pattern| {
let prefix_len = pattern.bytes().take_while(|&c| c == b'!').count();
let (prefix, pattern) = pattern.split_at(prefix_len);

let adjusted_path = relative_ignore_path.join(pattern);
format!("{prefix}{}", adjusted_path.to_string_lossy().cow_replace('\\', "/"))
})
.collect()
}
}
}

pub fn print_and_flush_stdout(stdout: &mut dyn Write, message: &str) {
Expand Down Expand Up @@ -1048,21 +1021,6 @@ mod test {
Tester::new().with_cwd("fixtures/report_unused_directives".into()).test_and_snapshot(args);
}

#[test]
fn test_adjust_ignore_patterns() {
let base = PathBuf::from("/project/root");
let path = PathBuf::from("/project/root/src");
let ignore_patterns =
vec![String::from("target"), String::from("!dist"), String::from("!!dist")];

let adjusted_patterns = LintRunner::adjust_ignore_patterns(&base, &path, ignore_patterns);

assert_eq!(
adjusted_patterns,
vec![String::from("src/target"), String::from("!src/dist"), String::from("!!src/dist")]
);
}

#[test]
fn test_nested_config() {
let args = &[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ source: apps/oxlint/src/tester.rs
arguments: -c fixtures/config_ignore_patterns/ignore_extension/eslintrc.json fixtures/config_ignore_patterns/ignore_extension/main.js
working directory:
----------
Finished in <variable>ms on 0 files using 1 threads.
Found 0 warnings and 0 errors.
Finished in <variable>ms on 0 files with 88 rules using 1 threads.
----------
CLI result: LintNoFilesFound
CLI result: LintSucceeded
----------
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ source: apps/oxlint/src/tester.rs
arguments: -c ./test/eslintrc.json --ignore-pattern *.ts .
working directory: fixtures/config_ignore_patterns/with_oxlintrc
----------

! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/unicorn/no-empty-file.html\eslint-plugin-unicorn(no-empty-file)]8;;\: Empty files are not allowed.
,-[main.js:1:1]
`----
help: Delete this file or add some code to it.

Found 1 warning and 0 errors.
Finished in <variable>ms on 1 file with 88 rules using 1 threads.
Found 0 warnings and 0 errors.
Finished in <variable>ms on 0 files with 88 rules using 1 threads.
----------
CLI result: LintSucceeded
----------
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ arguments:
working directory: fixtures/ignore_file_current_dir
----------
Found 0 warnings and 0 errors.
Finished in <variable>ms on 0 files with 88 rules using 1 threads.
Finished in <variable>ms on 0 files using 1 threads.
----------
CLI result: LintSucceeded
----------
Expand All @@ -16,7 +16,7 @@ arguments: .
working directory: fixtures/ignore_file_current_dir
----------
Found 0 warnings and 0 errors.
Finished in <variable>ms on 0 files with 88 rules using 1 threads.
Finished in <variable>ms on 0 files using 1 threads.
----------
CLI result: LintSucceeded
----------
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-debugger": "error"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
debugger;

Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ impl IsolatedLintHandler {
return None;
}

if self.service.should_ignore(&path) {
return None;
}

let mut allocator = Allocator::default();
let source_text = content.or_else(|| read_to_string(&path).ok())?;
let errors = self.lint_path(&mut allocator, &path, source_text);
Expand Down
26 changes: 6 additions & 20 deletions crates/oxc_language_server/src/linter/server_linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ impl ServerLinter {
Oxlintrc::default()
};

// clone because we are returning it for ignore builder
let config_builder = ConfigStoreBuilder::from_oxlintrc(
false,
oxlintrc.clone(),
oxlintrc,
None,
&mut ExternalPluginStore::default(),
)
Expand Down Expand Up @@ -111,7 +110,7 @@ impl ServerLinter {

Self {
isolated_linter: Arc::new(Mutex::new(isolated_linter)),
gitignore_glob: Self::create_ignore_glob(&root_path, &oxlintrc),
gitignore_glob: Self::create_ignore_glob(&root_path),
extended_paths,
}
}
Expand Down Expand Up @@ -164,7 +163,7 @@ impl ServerLinter {
}

#[expect(clippy::filetype_is_file)]
fn create_ignore_glob(root_path: &Path, oxlintrc: &Oxlintrc) -> Vec<Gitignore> {
fn create_ignore_glob(root_path: &Path) -> Vec<Gitignore> {
let walk = ignore::WalkBuilder::new(root_path)
.ignore(true)
.hidden(false)
Expand Down Expand Up @@ -194,20 +193,6 @@ impl ServerLinter {
}
}

if oxlintrc.ignore_patterns.is_empty() {
return gitignore_globs;
}

let Some(oxlintrc_dir) = oxlintrc.path.parent() else {
warn!("Oxlintrc path has no parent, skipping inline ignore patterns");
return gitignore_globs;
};

let mut builder = ignore::gitignore::GitignoreBuilder::new(oxlintrc_dir);
for entry in &oxlintrc.ignore_patterns {
builder.add_line(None, entry).expect("Failed to add ignore line");
}
gitignore_globs.push(builder.build().unwrap());
gitignore_globs
}

Expand Down Expand Up @@ -409,8 +394,9 @@ mod test {

#[test]
fn test_root_ignore_patterns() {
Tester::new("fixtures/linter/root_ignore_patterns", None)
.test_and_snapshot_single_file("ignored-file.ts");
let tester = Tester::new("fixtures/linter/ignore_patterns", None);
tester.test_and_snapshot_single_file("ignored-file.ts");
tester.test_and_snapshot_single_file("another_config/not-ignored-file.ts");
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: crates/oxc_language_server/src/tester.rs
input_file: crates/oxc_language_server/fixtures/linter/ignore_patterns/another_config/not-ignored-file.ts
---
code: "eslint(no-debugger)"
code_description.href: "https://oxc.rs/docs/guide/usage/linter/rules/eslint/no-debugger.html"
message: "`debugger` statement is not allowed\nhelp: Remove the debugger statement"
range: Range { start: Position { line: 0, character: 0 }, end: Position { line: 0, character: 9 } }
related_information[0].message: ""
related_information[0].location.uri: "file://<variable>/fixtures/linter/ignore_patterns/another_config/not-ignored-file.ts"
related_information[0].location.range: Range { start: Position { line: 0, character: 0 }, end: Position { line: 0, character: 9 } }
severity: Some(Error)
source: Some("oxc")
tags: None
fixed: Single(FixedContent { message: Some("Remove the debugger statement"), code: "", range: Range { start: Position { line: 0, character: 0 }, end: Position { line: 0, character: 9 } } })
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
source: crates/oxc_language_server/src/tester.rs
input_file: crates/oxc_language_server/fixtures/linter/ignore_patterns/ignored-file.ts
---
File is ignored

This file was deleted.

1 change: 1 addition & 0 deletions crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ convert_case = { workspace = true }
cow-utils = { workspace = true }
fast-glob = { workspace = true }
icu_segmenter = { workspace = true }
ignore = { workspace = true }
indexmap = { workspace = true, features = ["rayon"] }
itertools = { workspace = true }
javascript-globals = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/config/config_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ impl ConfigStoreBuilder {
settings: oxlintrc.settings,
env: oxlintrc.env,
globals: oxlintrc.globals,
ignore_patterns: LintConfig::resolve_oxlintrc_ignore_patterns(
&oxlintrc.ignore_patterns,
&oxlintrc.path,
),
path: Some(oxlintrc.path),
};

Expand Down
Loading
Loading