Skip to content

Commit 4227771

Browse files
alanzmeta-codesync[bot]
authored andcommitted
CLI ssr: Add --dump-config for .elp_lint.toml config snippet
Summary: We can use the command line `elp ssr` command to search through files in a project for matches, but it is also convenient to be able to examine them in an IDE context. This diff adds this capability, with two new command line options - `--info` upgrades the reported diagnostic from weak warning to info - `--dump-config` emits a toml snippet suitable for placement at the end of an `.elp_lint.toml` file. ```bash elp ssr --parens --dump-config --info "(3)" "{_A}" # Add this to your .elp_lint.toml [[ad_hoc_lints.lints]] type = "LintMatchSsr" ssr_pattern = "ssr: (3)." severity = "info" macro_strategy = "expand" paren_strategy = "visible" [[ad_hoc_lints.lints]] type = "LintMatchSsr" ssr_pattern = "ssr: {_A}." severity = "info" macro_strategy = "expand" paren_strategy = "visible" ``` The edit to `.elp_lint.toml` will be immediately picked up, and you will see the matches as normal diagnostics with a blue squiggle for info. Reviewed By: TD5 Differential Revision: D85960380 fbshipit-source-id: 803c19f1917d939d13e6406e440057632b489ea6
1 parent 9e99f96 commit 4227771

File tree

7 files changed

+241
-5
lines changed

7 files changed

+241
-5
lines changed

crates/elp/src/bin/args.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ pub struct Ssr {
372372
)]
373373
pub format: Option<String>,
374374

375+
/// Report severity as Info instead of WeakWarning
376+
#[bpaf(long("info"))]
377+
pub info_severity: bool,
378+
375379
/// Macro expansion strategy: expand | no-expand | visible-expand (default expand)
376380
#[bpaf(
377381
long("macros"),
@@ -386,6 +390,9 @@ pub struct Ssr {
386390
#[bpaf(long("parens"))]
387391
pub paren_strategy: bool,
388392

393+
/// Dump a configuration snippet that can be put in .elp_lint.toml to match the given SSR patterns
394+
pub dump_config: bool,
395+
389396
/// Report system memory usage and other statistics
390397
#[bpaf(long("report-system-stats"))]
391398
pub report_system_stats: bool,

crates/elp/src/bin/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,6 +2114,17 @@ mod tests {
21142114
)
21152115
}
21162116

2117+
#[test]
2118+
fn lint_ssr_as_cli_dump_config() {
2119+
simple_snapshot(
2120+
args_vec!["ssr", "--dump-config", "--info", "?BAR(_@AA)", "{4}"],
2121+
"linter",
2122+
expect_file!("../resources/test/linter/ssr_ad_hoc_cli_dump_config.stdout"),
2123+
true,
2124+
None,
2125+
)
2126+
}
2127+
21172128
#[test_case(false ; "rebar")]
21182129
#[test_case(true ; "buck")]
21192130
fn eqwalizer_tests_check(buck: bool) {

crates/elp/src/bin/ssr_cli.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use elp_ide::diagnostics;
2828
use elp_ide::diagnostics::DiagnosticsConfig;
2929
use elp_ide::diagnostics::FallBackToAll;
3030
use elp_ide::diagnostics::LintConfig;
31+
use elp_ide::diagnostics::LintsFromConfig;
3132
use elp_ide::diagnostics::MatchSsr;
3233
use elp_ide::elp_ide_db::elp_base_db::AbsPath;
3334
use elp_ide::elp_ide_db::elp_base_db::FileId;
@@ -85,10 +86,16 @@ pub fn run_ssr_command(
8586
let mut lint_config = LintConfig::default();
8687
for pattern in &args.ssr_specs {
8788
let normalized_pattern = normalize_ssr_pattern(pattern);
89+
let severity = if args.info_severity {
90+
Some(diagnostics::Severity::Information)
91+
} else {
92+
None
93+
};
8894
let ssr_lint = diagnostics::Lint::LintMatchSsr(MatchSsr {
8995
ssr_pattern: normalized_pattern,
9096
message: None,
9197
strategy: Some(strategy),
98+
severity,
9299
});
93100
lint_config.ad_hoc_lints.lints.push(ssr_lint);
94101
}
@@ -105,8 +112,13 @@ pub fn run_ssr_command(
105112
.set_experimental(false)
106113
.set_use_cli_severity(false);
107114

108-
if diagnostics_config.enabled.all_enabled() && args.is_format_normal() {
109-
writeln!(cli, "Reporting all diagnostics codes")?;
115+
if args.dump_config {
116+
let result = toml::to_string::<LintsFromConfig>(&diagnostics_config.lints_from_config)?;
117+
// This is a subsection of .elp_lint.toml, add subsection prefix
118+
let result = result.replace("[[lints]]", "[[ad_hoc_lints.lints]]");
119+
writeln!(cli, "\n# Add this to your .elp_lint.toml")?;
120+
writeln!(cli, "{}", result)?;
121+
return Ok(());
110122
}
111123

112124
// Load the project

crates/elp/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ mod tests {
193193
ssr_pattern: "ssr: _@A = 10.".to_string(),
194194
message: None,
195195
strategy: None,
196+
severity: None,
196197
}),
197198
],
198199
},
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
# Add this to your .elp_lint.toml
3+
[[ad_hoc_lints.lints]]
4+
type = "LintMatchSsr"
5+
ssr_pattern = "ssr: ?BAR(_@AA)."
6+
severity = "info"
7+
8+
[[ad_hoc_lints.lints]]
9+
type = "LintMatchSsr"
10+
ssr_pattern = "ssr: {4}."
11+
severity = "info"
12+

crates/elp/src/resources/test/ssr_help.stdout

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Usage: [--project PROJECT] [--module MODULE] [--app APP] [--file FILE] [--rebar] [--as PROFILE] [--include-generated] [--include-tests] [[--format FORMAT]] [[--macros STRATEGY]] [--parens] [--report-system-stats] <SSR_SPECS>...
1+
Usage: [--project PROJECT] [--module MODULE] [--app APP] [--file FILE] [--rebar] [--as PROFILE] [--include-generated] [--include-tests] [[--format FORMAT]] [--info] [[--macros STRATEGY]] [--parens] [--dump-config] [--report-system-stats] <SSR_SPECS>...
22

33
Available positional items:
44
<SSR_SPECS> SSR specs to use
@@ -13,7 +13,9 @@ Available options:
1313
--include-generated Also generate diagnostics for generated files
1414
--include-tests Also generate diagnostics for test files
1515
--format <FORMAT> Show diagnostics in JSON format
16+
--info Report severity as Info instead of WeakWarning
1617
--macros <STRATEGY> Macro expansion strategy: expand | no-expand | visible-expand (default expand)
1718
--parens Explicitly match parentheses. If omitted, they are ignored.
19+
--dump-config Dump a configuration snippet that can be put in .elp_lint.toml to match the given SSR patterns
1820
--report-system-stats Report system memory usage and other statistics
1921
-h, --help Prints help information

0 commit comments

Comments
 (0)