Skip to content

Commit 5412625

Browse files
robertoaloimeta-codesync[bot]
authored andcommitted
Allow overriding is_enabled property via config for trait linters
Summary: # Context For linters based on [traits](https://whatsapp.github.io/erlang-language-platform/docs/contributing/linters/linter-traits/) we can customize their behaviour via config. Here we extend the configuration mechanism we already use for `severity` and other properties to the `is_enabled` flag. This mechanism will eventually supersede the `enabled_lints` and `disabled_lints` [configuration properties](https://whatsapp.github.io/erlang-language-platform/docs/get-started/configure-project/elp-lint-toml/#enabled_lints). # Why having a new way to enable and disable linters? Currently, the `.elp_lint.toml` allows users to specify which linters are enabled or disabled. But the configuration file works in a very counter-intuitive way: say a linter is "disabled by default". To enable it, a user cannot simply add it to the `enabled_list`, since that would cause **only that linter** to run. A user would need to add all available linters to the config, which is clearly not a maintainable solution. By supporting an override mechanism, it will be possible to - for example - have the following configuration: ``` [linters.my_linter] enabled = true ``` Which would keep the *enabled* status for all other linters to their default value, ensuring `my_linter` is enabled. # This diff This diff introduces the new overriding mechanism. We still keep it undocumented, until all linters are migrated to use a trait. Reviewed By: TheGeorge Differential Revision: D85055745 fbshipit-source-id: f84d203c380a45dcbafae5d09dc9141d42a12e91
1 parent dab22ff commit 5412625

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

crates/ide/src/diagnostics.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,17 @@ fn should_run(
550550
is_generated: bool,
551551
is_test: bool,
552552
) -> bool {
553+
let is_enabled = if let Some(lint_config) = config.lint_config.as_ref() {
554+
lint_config
555+
.get_is_enabled_override(&linter.id())
556+
.unwrap_or_else(|| linter.is_enabled())
557+
} else {
558+
linter.is_enabled()
559+
};
560+
if !is_enabled {
561+
return false;
562+
}
563+
553564
let include_tests = if let Some(lint_config) = config.lint_config.as_ref() {
554565
lint_config
555566
.get_include_tests_override(&linter.id())
@@ -1129,6 +1140,11 @@ impl DiagnosticsConfig {
11291140
}
11301141

11311142
impl LintConfig {
1143+
/// Get the is_enabled override for a linter based on its diagnostic code
1144+
pub fn get_is_enabled_override(&self, diagnostic_code: &DiagnosticCode) -> Option<bool> {
1145+
self.linters.get(diagnostic_code)?.is_enabled
1146+
}
1147+
11321148
/// Get the severity override for a linter based on its diagnostic code
11331149
pub fn get_severity_override(&self, diagnostic_code: &DiagnosticCode) -> Option<Severity> {
11341150
self.linters.get(diagnostic_code)?.severity
@@ -1213,6 +1229,8 @@ pub enum LinterTraitConfig {
12131229
/// Configuration for a specific linter that allows overriding default settings
12141230
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
12151231
pub struct LinterConfig {
1232+
#[serde(rename = "enabled")]
1233+
pub is_enabled: Option<bool>,
12161234
pub severity: Option<Severity>,
12171235
pub include_tests: Option<bool>,
12181236
pub include_generated: Option<bool>,
@@ -3486,6 +3504,7 @@ baz(1)->4.
34863504
lint_config.linters.insert(
34873505
DiagnosticCode::NoGarbageCollect,
34883506
LinterConfig {
3507+
is_enabled: None,
34893508
severity: Some(Severity::Error),
34903509
include_tests: None,
34913510
include_generated: None,
@@ -3527,6 +3546,7 @@ baz(1)->4.
35273546
lint_config.linters.insert(
35283547
DiagnosticCode::NoGarbageCollect,
35293548
LinterConfig {
3549+
is_enabled: None,
35303550
severity: None,
35313551
include_tests: Some(true),
35323552
include_generated: None,
@@ -3567,6 +3587,7 @@ baz(1)->4.
35673587
lint_config.linters.insert(
35683588
DiagnosticCode::NoGarbageCollect,
35693589
LinterConfig {
3590+
is_enabled: None,
35703591
severity: None,
35713592
include_tests: None,
35723593
include_generated: Some(true),
@@ -3608,6 +3629,7 @@ baz(1)->4.
36083629
lint_config.linters.insert(
36093630
DiagnosticCode::NoGarbageCollect,
36103631
LinterConfig {
3632+
is_enabled: None,
36113633
severity: None,
36123634
include_tests: None,
36133635
include_generated: None,
@@ -3644,4 +3666,44 @@ baz(1)->4.
36443666
),
36453667
);
36463668
}
3669+
3670+
#[test]
3671+
fn test_linter_is_enabled_override() {
3672+
let mut lint_config = LintConfig::default();
3673+
lint_config.linters.insert(
3674+
DiagnosticCode::NoGarbageCollect,
3675+
LinterConfig {
3676+
is_enabled: Some(false),
3677+
severity: None,
3678+
include_tests: None,
3679+
include_generated: None,
3680+
experimental: None,
3681+
config: None,
3682+
},
3683+
);
3684+
3685+
let config = DiagnosticsConfig::default()
3686+
.configure_diagnostics(
3687+
&lint_config,
3688+
&Some("no_garbage_collect".to_string()),
3689+
&None,
3690+
FallBackToAll::No,
3691+
)
3692+
.unwrap();
3693+
check_diagnostics_with_config(
3694+
config,
3695+
r#"
3696+
//- /src/main.erl
3697+
-module(main).
3698+
-export([warning/0]).
3699+
3700+
warning() ->
3701+
erlang:garbage_collect().
3702+
//- /opt/lib/stdlib-3.17/src/erlang.erl otp_app:/opt/lib/stdlib-3.17
3703+
-module(erlang).
3704+
-export([garbage_collect/0]).
3705+
garbage_collect() -> ok.
3706+
"#,
3707+
);
3708+
}
36473709
}

0 commit comments

Comments
 (0)