diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index d34bd46b059ef..22fb052561c93 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -300,6 +300,7 @@ impl LintRunner { // TODO: Add a warning message if `tsgolint` cannot be found, but type-aware rules are enabled if self.options.type_aware { if let Err(err) = TsGoLintState::new(options.cwd(), config_store.clone()) + .with_silent(misc_options.silent) .lint(&files_to_lint, tx_error.clone()) { print_and_flush_stdout(stdout, &err); @@ -1178,6 +1179,14 @@ mod test { Tester::new().with_cwd("fixtures/tsgolint".into()).test_and_snapshot(args); } + #[test] + #[cfg(not(target_endian = "big"))] + fn test_tsgolint_silent() { + // TODO: test with other rules as well once diagnostics are more stable + let args = &["--type-aware", "--silent", "no-floating-promises"]; + Tester::new().with_cwd("fixtures/tsgolint".into()).test_and_snapshot(args); + } + #[test] #[cfg(not(target_endian = "big"))] fn test_tsgolint_config() { diff --git a/apps/oxlint/src/snapshots/fixtures__tsgolint_--type-aware --silent no-floating-promises@oxlint.snap b/apps/oxlint/src/snapshots/fixtures__tsgolint_--type-aware --silent no-floating-promises@oxlint.snap new file mode 100644 index 0000000000000..05319905d1daa --- /dev/null +++ b/apps/oxlint/src/snapshots/fixtures__tsgolint_--type-aware --silent no-floating-promises@oxlint.snap @@ -0,0 +1,13 @@ +--- +source: apps/oxlint/src/tester.rs +--- +########## +arguments: --type-aware --silent no-floating-promises +working directory: fixtures/tsgolint +---------- + +Found 5 warnings and 11 errors. +Finished in ms on 3 files using 1 threads. +---------- +CLI result: LintFoundErrors +---------- diff --git a/crates/oxc_linter/src/tsgolint.rs b/crates/oxc_linter/src/tsgolint.rs index 2fd5ec950d5f5..c2c916e5fd959 100644 --- a/crates/oxc_linter/src/tsgolint.rs +++ b/crates/oxc_linter/src/tsgolint.rs @@ -24,6 +24,9 @@ pub struct TsGoLintState { cwd: PathBuf, /// The configuration store for `tsgolint` (used to resolve configurations outside of `oxc_linter`) config_store: ConfigStore, + /// If `oxlint` will output the diagnostics or not. + /// When `silent` is true, we do not need to access the file system for nice diagnostics messages. + silent: bool, } impl TsGoLintState { @@ -32,9 +35,20 @@ impl TsGoLintState { config_store, executable_path: try_find_tsgolint_executable(cwd).unwrap_or(PathBuf::from("tsgolint")), cwd: cwd.to_path_buf(), + silent: false, } } + /// Set to `true` to skip file system reads. + /// When `silent` is true, we do not need to access the file system for nice diagnostics messages. + /// + /// Default is `false`. + #[must_use] + pub fn with_silent(mut self, yes: bool) -> Self { + self.silent = yes; + self + } + /// # Panics /// - when `stdin` of subprocess cannot be opened /// - when `stdout` of subprocess cannot be opened @@ -147,18 +161,22 @@ impl TsGoLintState { }, ); - let source_text: &str = - if let Some(source_text) = source_text_map.get(&path) { - source_text.as_str() - } else { - let source_text = read_to_string(&path) - .unwrap_or_else(|_| String::new()); - // Insert and get a reference to the inserted string - let entry = source_text_map - .entry(path.clone()) - .or_insert(source_text); - entry.as_str() - }; + let source_text: &str = if self.silent { + // The source text is not needed in silent mode. + // The source text is only here to wrap the line before and after into a nice `oxc_diagnostic` Error + "" + } else if let Some(source_text) = source_text_map.get(&path) + { + source_text.as_str() + } else { + let source_text = read_to_string(&path) + .unwrap_or_else(|_| String::new()); + // Insert and get a reference to the inserted string + let entry = source_text_map + .entry(path.clone()) + .or_insert(source_text); + entry.as_str() + }; let diagnostics = DiagnosticService::wrap_diagnostics( cwd_clone.clone(),