Skip to content

Commit

Permalink
feat: Add LintOptionsConfig.plugins (#145)
Browse files Browse the repository at this point in the history
Ref denoland/deno#27203

---------

Co-authored-by: Marvin Hagemeister <[email protected]>
  • Loading branch information
bartlomieju and marvinhagemeister authored Jan 9, 2025
1 parent 00bac96 commit 3b5eb8f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/deno_json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ struct SerializedLintConfig {
#[serde(rename = "files")]
pub deprecated_files: serde_json::Value,
pub report: Option<String>,
pub plugins: Vec<String>,
}

impl SerializedLintConfig {
Expand All @@ -125,7 +126,10 @@ impl SerializedLintConfig {
log::warn!( "Warning: \"files\" configuration in \"lint\" was removed in Deno 2, use \"include\" and \"exclude\" instead.");
}
Ok(LintConfig {
options: LintOptionsConfig { rules: self.rules },
options: LintOptionsConfig {
rules: self.rules,
plugins: self.plugins,
},
files: files.into_resolved(config_file_specifier)?,
})
}
Expand All @@ -134,6 +138,7 @@ impl SerializedLintConfig {
#[derive(Clone, Debug, Default, Hash, PartialEq)]
pub struct LintOptionsConfig {
pub rules: LintRulesConfig,
pub plugins: Vec<String>,
}

#[derive(Clone, Debug, Hash, PartialEq)]
Expand Down Expand Up @@ -1970,6 +1975,7 @@ mod tests {
exclude: None,
tags: Some(vec!["recommended".to_string()]),
},
plugins: vec![],
}
}
);
Expand Down
48 changes: 45 additions & 3 deletions src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use discovery::ConfigFileDiscovery;
use discovery::ConfigFolder;
use discovery::DenoOrPkgJson;
use indexmap::IndexMap;
use indexmap::IndexSet;
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::collections::HashSet;
Expand Down Expand Up @@ -1442,11 +1443,38 @@ impl WorkspaceDirectory {
None => return Ok(member_config),
};
// combine the configs
let root_opts = root_config.options;
let member_opts = member_config.options;

// 1. Merge workspace root + member plugins
// 2. Workspace member can filter out plugins by negating
// like this: `!my-plugin`
// 3. Remove duplicates in case a plugin was defined in both
// workspace root and member.
let excluded_plugins: HashSet<String> = HashSet::from_iter(
member_opts
.plugins
.iter()
.filter(|plugin| plugin.starts_with('!'))
.map(|plugin| plugin[1..].to_string()),
);

let filtered_plugins = IndexSet::<String>::from_iter(
root_opts
.plugins
.into_iter()
.chain(member_opts.plugins)
.filter(|plugin| {
!plugin.starts_with('!') && !excluded_plugins.contains(plugin)
}),
)
.into_iter()
.collect::<Vec<_>>();

Ok(LintConfig {
options: LintOptionsConfig {
plugins: filtered_plugins,
rules: {
let root_opts = root_config.options;
let member_opts = member_config.options;
LintRulesConfig {
tags: combine_option_vecs(
root_opts.rules.tags,
Expand Down Expand Up @@ -2761,6 +2789,7 @@ mod test {
"include": ["rule1"],
"exclude": ["rule2"],
},
"plugins": ["jsr:@deno/test-plugin1", "jsr:@deno/test-plugin3"]
}
}),
json!({
Expand All @@ -2770,7 +2799,12 @@ mod test {
"rules": {
"tags": ["tag1"],
"include": ["rule2"],
}
},
"plugins": [
"jsr:@deno/test-plugin1",
"jsr:@deno/test-plugin2",
"!jsr:@deno/test-plugin3"
]
}
}),
);
Expand Down Expand Up @@ -2800,6 +2834,10 @@ mod test {
include: Some(vec!["rule1".to_string(), "rule2".to_string()]),
exclude: Some(vec![])
},
plugins: vec![
"jsr:@deno/test-plugin1".to_string(),
"jsr:@deno/test-plugin2".to_string()
],
},
files: FilePatterns {
base: root_dir().join("member"),
Expand Down Expand Up @@ -2827,6 +2865,10 @@ mod test {
include: Some(vec!["rule1".to_string()]),
exclude: Some(vec!["rule2".to_string()])
},
plugins: vec![
"jsr:@deno/test-plugin1".to_string(),
"jsr:@deno/test-plugin3".to_string()
]
},
files: FilePatterns {
base: root_dir(),
Expand Down

0 comments on commit 3b5eb8f

Please sign in to comment.