Skip to content

Commit 23e5642

Browse files
committed
refactor(linter): move TsGoLintInput creation into own function (#13118)
Move the creation into an own function. This would be later helpful when implementing it for the language server.
1 parent 896c3ba commit 23e5642

File tree

1 file changed

+49
-40
lines changed

1 file changed

+49
-40
lines changed

apps/oxlint/src/tsgolint.rs

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -57,46 +57,7 @@ impl<'a> TsGoLintState<'a> {
5757

5858
let mut resolved_configs: FxHashMap<PathBuf, ResolvedLinterState> = FxHashMap::default();
5959

60-
// Feed JSON into STDIN of tsgolint in this format:
61-
// ```
62-
// {
63-
// "files": [
64-
// {
65-
// "file_path": "/absolute/path/to/file.ts",
66-
// "rules": ["rule-1", "another-rule"]
67-
// }
68-
// ]
69-
// }
70-
// ```
71-
let json_input = TsGoLintInput {
72-
files: self
73-
.paths
74-
.iter()
75-
.filter(|path| SourceType::from_path(Path::new(path)).is_ok())
76-
.map(|path| TsGoLintInputFile {
77-
file_path: path.to_string_lossy().to_string(),
78-
rules: {
79-
let path_buf = PathBuf::from(path);
80-
let resolved_config = resolved_configs
81-
.entry(path_buf.clone())
82-
.or_insert_with(|| self.config_store.resolve(&path_buf));
83-
84-
// Collect the rules that are enabled for this file
85-
resolved_config
86-
.rules
87-
.iter()
88-
.filter_map(|(rule, status)| {
89-
if status.is_warn_deny() && rule.is_tsgolint_rule() {
90-
Some(rule.name().to_string())
91-
} else {
92-
None
93-
}
94-
})
95-
.collect()
96-
},
97-
})
98-
.collect(),
99-
};
60+
let json_input = self.json_input(&mut resolved_configs);
10061

10162
let handler = std::thread::spawn(move || {
10263
let child = std::process::Command::new(&self.executable_path)
@@ -262,6 +223,54 @@ impl<'a> TsGoLintState<'a> {
262223
}
263224
}
264225
}
226+
227+
/// Create a JSON input for STDIN of tsgolint in this format:
228+
///
229+
/// ```json
230+
/// {
231+
/// "files": [
232+
/// {
233+
/// "file_path": "/absolute/path/to/file.ts",
234+
/// "rules": ["rule-1", "another-rule"]
235+
/// }
236+
/// ]
237+
/// }
238+
/// ```
239+
#[inline]
240+
fn json_input(
241+
&self,
242+
resolved_configs: &mut FxHashMap<PathBuf, ResolvedLinterState>,
243+
) -> TsGoLintInput {
244+
TsGoLintInput {
245+
files: self
246+
.paths
247+
.iter()
248+
.filter(|path| SourceType::from_path(Path::new(path)).is_ok())
249+
.map(|path| TsGoLintInputFile {
250+
file_path: path.to_string_lossy().to_string(),
251+
rules: {
252+
let path_buf = PathBuf::from(path);
253+
let resolved_config = resolved_configs
254+
.entry(path_buf.clone())
255+
.or_insert_with(|| self.config_store.resolve(&path_buf));
256+
257+
// Collect the rules that are enabled for this file
258+
resolved_config
259+
.rules
260+
.iter()
261+
.filter_map(|(rule, status)| {
262+
if status.is_warn_deny() && rule.is_tsgolint_rule() {
263+
Some(rule.name().to_string())
264+
} else {
265+
None
266+
}
267+
})
268+
.collect()
269+
},
270+
})
271+
.collect(),
272+
}
273+
}
265274
}
266275

267276
/// Represents the input JSON to `tsgolint`, like:

0 commit comments

Comments
 (0)