Skip to content

Commit c138fad

Browse files
committed
refactor(linter): avoid fs reads in TsGoLintState when --silent is used (#13199)
1 parent 845c6de commit c138fad

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

apps/oxlint/src/lint.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ impl LintRunner {
300300
// TODO: Add a warning message if `tsgolint` cannot be found, but type-aware rules are enabled
301301
if self.options.type_aware {
302302
if let Err(err) = TsGoLintState::new(options.cwd(), config_store.clone())
303+
.with_silent(misc_options.silent)
303304
.lint(&files_to_lint, tx_error.clone())
304305
{
305306
print_and_flush_stdout(stdout, &err);
@@ -1178,6 +1179,14 @@ mod test {
11781179
Tester::new().with_cwd("fixtures/tsgolint".into()).test_and_snapshot(args);
11791180
}
11801181

1182+
#[test]
1183+
#[cfg(not(target_endian = "big"))]
1184+
fn test_tsgolint_silent() {
1185+
// TODO: test with other rules as well once diagnostics are more stable
1186+
let args = &["--type-aware", "--silent", "no-floating-promises"];
1187+
Tester::new().with_cwd("fixtures/tsgolint".into()).test_and_snapshot(args);
1188+
}
1189+
11811190
#[test]
11821191
#[cfg(not(target_endian = "big"))]
11831192
fn test_tsgolint_config() {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
---
4+
##########
5+
arguments: --type-aware --silent no-floating-promises
6+
working directory: fixtures/tsgolint
7+
----------
8+
9+
Found 5 warnings and 11 errors.
10+
Finished in <variable>ms on 3 files using 1 threads.
11+
----------
12+
CLI result: LintFoundErrors
13+
----------

crates/oxc_linter/src/tsgolint.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ pub struct TsGoLintState {
2424
cwd: PathBuf,
2525
/// The configuration store for `tsgolint` (used to resolve configurations outside of `oxc_linter`)
2626
config_store: ConfigStore,
27+
/// If `oxlint` will output the diagnostics or not.
28+
/// When `silent` is true, we do not need to access the file system for nice diagnostics messages.
29+
silent: bool,
2730
}
2831

2932
impl TsGoLintState {
@@ -32,9 +35,20 @@ impl TsGoLintState {
3235
config_store,
3336
executable_path: try_find_tsgolint_executable(cwd).unwrap_or(PathBuf::from("tsgolint")),
3437
cwd: cwd.to_path_buf(),
38+
silent: false,
3539
}
3640
}
3741

42+
/// Set to `true` to skip file system reads.
43+
/// When `silent` is true, we do not need to access the file system for nice diagnostics messages.
44+
///
45+
/// Default is `false`.
46+
#[must_use]
47+
pub fn with_silent(mut self, yes: bool) -> Self {
48+
self.silent = yes;
49+
self
50+
}
51+
3852
/// # Panics
3953
/// - when `stdin` of subprocess cannot be opened
4054
/// - when `stdout` of subprocess cannot be opened
@@ -147,18 +161,22 @@ impl TsGoLintState {
147161
},
148162
);
149163

150-
let source_text: &str =
151-
if let Some(source_text) = source_text_map.get(&path) {
152-
source_text.as_str()
153-
} else {
154-
let source_text = read_to_string(&path)
155-
.unwrap_or_else(|_| String::new());
156-
// Insert and get a reference to the inserted string
157-
let entry = source_text_map
158-
.entry(path.clone())
159-
.or_insert(source_text);
160-
entry.as_str()
161-
};
164+
let source_text: &str = if self.silent {
165+
// The source text is not needed in silent mode.
166+
// The source text is only here to wrap the line before and after into a nice `oxc_diagnostic` Error
167+
""
168+
} else if let Some(source_text) = source_text_map.get(&path)
169+
{
170+
source_text.as_str()
171+
} else {
172+
let source_text = read_to_string(&path)
173+
.unwrap_or_else(|_| String::new());
174+
// Insert and get a reference to the inserted string
175+
let entry = source_text_map
176+
.entry(path.clone())
177+
.or_insert(source_text);
178+
entry.as_str()
179+
};
162180

163181
let diagnostics = DiagnosticService::wrap_diagnostics(
164182
cwd_clone.clone(),

0 commit comments

Comments
 (0)