Skip to content

Commit

Permalink
Merge pull request #598 from gauge-sh/external-unused-dep-rule
Browse files Browse the repository at this point in the history
Add configurable rule for severity of unused external dependency
  • Loading branch information
emdoyle authored Feb 5, 2025
2 parents 6ea439f + a1c18d8 commit d080cc4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
33 changes: 19 additions & 14 deletions src/commands/check/check_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,26 @@ pub fn check(
})
.collect();

let all_seen_dependencies: HashSet<String> =
pipeline.seen_dependencies.into_iter().collect();
let unused_dependency_diagnostics = project_info
.dependencies
.difference(&all_seen_dependencies)
.filter(|&dep| !pipeline.excluded_external_modules.contains(dep)) // 'exclude' should hide unused errors unconditionally
.map(|dep| {
Diagnostic::new_global_error(DiagnosticDetails::Code(
CodeDiagnostic::UnusedExternalDependency {
package_module_name: dep.clone(),
},
))
});
if !project_config.rules.unused_external_dependencies.is_off() {
let all_seen_dependencies: HashSet<String> =
pipeline.seen_dependencies.into_iter().collect();
let unused_dependency_diagnostics = project_info
.dependencies
.difference(&all_seen_dependencies)
.filter(|&dep| !pipeline.excluded_external_modules.contains(dep)) // 'exclude' should hide unused errors unconditionally
.map(|dep| {
Diagnostic::new_global(
(&project_config.rules.unused_external_dependencies)
.try_into()
.unwrap(),
DiagnosticDetails::Code(CodeDiagnostic::UnusedExternalDependency {
package_module_name: dep.clone(),
}),
)
});

project_diagnostics.extend(unused_dependency_diagnostics);
project_diagnostics.extend(unused_dependency_diagnostics);
}
project_diagnostics
});

Expand Down
12 changes: 9 additions & 3 deletions src/config/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ impl RuleSetting {
*self == Self::Warn
}

fn _error() -> Self {
fn error() -> Self {
Self::Error
}

fn _is_error(&self) -> bool {
fn is_error(&self) -> bool {
*self == Self::Error
}

fn off() -> Self {
Self::Off
}

fn is_off(&self) -> bool {
pub fn is_off(&self) -> bool {
*self == Self::Off
}
}
Expand Down Expand Up @@ -59,13 +59,19 @@ pub struct RulesConfig {
skip_serializing_if = "RuleSetting::is_off"
)]
pub require_ignore_directive_reasons: RuleSetting,
#[serde(
default = "RuleSetting::error",
skip_serializing_if = "RuleSetting::is_error"
)]
pub unused_external_dependencies: RuleSetting,
}

impl Default for RulesConfig {
fn default() -> Self {
Self {
unused_ignore_directives: RuleSetting::warn(),
require_ignore_directive_reasons: RuleSetting::off(),
unused_external_dependencies: RuleSetting::error(),
}
}
}
Expand Down

0 comments on commit d080cc4

Please sign in to comment.