Skip to content

Commit 8b66cd5

Browse files
camchenrycamc314
andcommitted
refactor(linter): update tsgolint payload
Co-authored-by: camc314 <18101008+camc314@users.noreply.github.com>
1 parent 93da5bc commit 8b66cd5

File tree

1 file changed

+51
-37
lines changed

1 file changed

+51
-37
lines changed

crates/oxc_linter/src/tsgolint.rs

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ impl TsGoLintState {
7676
let mut resolved_configs: FxHashMap<PathBuf, ResolvedLinterState> = FxHashMap::default();
7777

7878
let json_input = self.json_input(paths, &mut resolved_configs);
79-
80-
if json_input.files.is_empty() {
79+
if json_input.configs.is_empty() {
8180
return Ok(());
8281
}
8382

@@ -469,33 +468,39 @@ impl TsGoLintState {
469468
&self,
470469
paths: &[Arc<OsStr>],
471470
resolved_configs: &mut FxHashMap<PathBuf, ResolvedLinterState>,
472-
) -> TsGoLintInput {
473-
TsGoLintInput {
474-
files: paths
475-
.iter()
476-
.filter(|path| SourceType::from_path(Path::new(path)).is_ok())
477-
.map(|path| TsGoLintInputFile {
478-
file_path: path.to_string_lossy().to_string(),
479-
rules: {
480-
let path_buf = PathBuf::from(path);
481-
let resolved_config = resolved_configs
482-
.entry(path_buf.clone())
483-
.or_insert_with(|| self.config_store.resolve(&path_buf));
484-
485-
// Collect the rules that are enabled for this file
486-
resolved_config
487-
.rules
488-
.iter()
489-
.filter_map(|(rule, status)| {
490-
if status.is_warn_deny() && rule.is_tsgolint_rule() {
491-
Some(rule.name().to_string())
492-
} else {
493-
None
494-
}
495-
})
496-
.collect()
497-
},
498-
})
471+
) -> Payload {
472+
let mut config_groups: FxHashMap<Vec<Rule>, Vec<String>> = FxHashMap::default();
473+
474+
for path in paths {
475+
if SourceType::from_path(Path::new(path)).is_ok() {
476+
let path_buf = PathBuf::from(path);
477+
let file_path = path.to_string_lossy().to_string();
478+
479+
let resolved_config = resolved_configs
480+
.entry(path_buf.clone())
481+
.or_insert_with(|| self.config_store.resolve(&path_buf));
482+
483+
let rules: Vec<Rule> = resolved_config
484+
.rules
485+
.iter()
486+
.filter_map(|(rule, status)| {
487+
if status.is_warn_deny() && rule.is_tsgolint_rule() {
488+
Some(Rule { name: rule.name().to_string() })
489+
} else {
490+
None
491+
}
492+
})
493+
.collect();
494+
495+
config_groups.entry(rules).or_default().push(file_path);
496+
}
497+
}
498+
499+
Payload {
500+
version: 2,
501+
configs: config_groups
502+
.into_iter()
503+
.map(|(rules, file_paths)| Config { file_paths, rules })
499504
.collect(),
500505
}
501506
}
@@ -505,26 +510,35 @@ impl TsGoLintState {
505510
///
506511
/// ```json
507512
/// {
508-
/// "files": [
513+
/// "configs": [
509514
/// {
510-
/// "file_path": "/absolute/path/to/file.ts",
511-
/// "rules": ["rule-1", "another-rule"]
515+
/// "file_paths": ["/absolute/path/to/file.ts", "/another/file.ts"],
516+
/// "rules": [
517+
/// { "name": "rule-1" },
518+
/// { "name": "another-rule" },
519+
/// ]
512520
/// }
513521
/// ]
514522
/// }
515523
/// ```
516524
#[derive(Debug, Clone, Serialize, Deserialize)]
517-
pub struct TsGoLintInput {
518-
pub files: Vec<TsGoLintInputFile>,
525+
pub struct Payload {
526+
pub version: i32,
527+
pub configs: Vec<Config>,
519528
}
520529

521530
#[derive(Debug, Clone, Serialize, Deserialize)]
522-
pub struct TsGoLintInputFile {
531+
pub struct Config {
523532
/// Absolute path to the file to lint
524-
pub file_path: String,
533+
pub file_paths: Vec<String>,
525534
/// List of rules to apply to this file
526535
/// Example: `["no-floating-promises"]`
527-
pub rules: Vec<String>,
536+
pub rules: Vec<Rule>,
537+
}
538+
539+
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
540+
pub struct Rule {
541+
pub name: String,
528542
}
529543

530544
/// Represents the raw output binary data from `tsgolint`.

0 commit comments

Comments
 (0)