Skip to content

Commit 845c6de

Browse files
committed
refactor(linter): store source_text of already opened files for TsGoLintState (#13197)
When a file has multiple `tsgolint` diagnostics, `TsGoLintState` will now cache the source text in memory, instead of accessing the file system again.
1 parent b2d59a2 commit 845c6de

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

crates/oxc_linter/src/tsgolint.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ impl TsGoLintState {
9292
let mut buffer = Vec::with_capacity(8192);
9393
let mut read_buf = [0u8; 8192];
9494

95+
let mut source_text_map: FxHashMap<PathBuf, String> = FxHashMap::default();
96+
9597
loop {
9698
match stdout.read(&mut read_buf) {
9799
Ok(0) => break, // EOF
@@ -145,11 +147,23 @@ impl TsGoLintState {
145147
},
146148
);
147149

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+
};
162+
148163
let diagnostics = DiagnosticService::wrap_diagnostics(
149164
cwd_clone.clone(),
150165
path.clone(),
151-
&read_to_string(&path)
152-
.unwrap_or_else(|_| String::new()),
166+
source_text,
153167
vec![oxc_diagnostic],
154168
);
155169

0 commit comments

Comments
 (0)