Skip to content

Commit

Permalink
Add --prefix option to lint sub-command
Browse files Browse the repository at this point in the history
Reviewed By: alanz

Differential Revision: D62016816

fbshipit-source-id: a84e4688bcb05552ed33389592334a1750260f78
  • Loading branch information
robertoaloi authored and facebook-github-bot committed Aug 30, 2024
1 parent 5f6ade1 commit c8aacf7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
2 changes: 2 additions & 0 deletions crates/elp/src/bin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ pub struct Lint {
/// Apply to all matching diagnostic occurrences at once, rather
/// than one at a time.
pub one_shot: bool,
/// Optional prefix to prepend to each fact. Only used when --format=json is set
pub prefix: Option<String>,
/// Ignore the specified diagnostic, by code or label
#[bpaf(argument("CODE"))]
pub diagnostic_ignore: Option<String>,
Expand Down
11 changes: 10 additions & 1 deletion crates/elp/src/bin/lint_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::str;
use std::sync::Arc;

Expand Down Expand Up @@ -273,11 +274,12 @@ pub fn do_codemod(cli: &mut dyn Cli, loaded: &mut LoadResult, args: &Lint) -> Re
.root_dir;
let relative_path =
reporting::get_relative_path(root_path, &vfs_path);
let prefix = args.prefix.as_ref();
print_diagnostic_json(
diag,
&analysis,
*file_id,
relative_path,
with_prefix(relative_path, prefix).as_path(),
cli,
)?;
}
Expand Down Expand Up @@ -779,6 +781,13 @@ fn get_form_id_at_offset(
Some(form_id)
}

fn with_prefix(path: &Path, prefix: Option<&String>) -> PathBuf {
match prefix {
Some(prefix) => Path::new(prefix).join(path),
None => path.into(),
}
}

#[cfg(test)]
mod tests {
use elp_ide::diagnostics::DiagnosticCode;
Expand Down
30 changes: 30 additions & 0 deletions crates/elp/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,36 @@ mod tests {
.expect("bad test");
}

#[test_case(false ; "rebar")]
#[test_case(true ; "buck")]
fn lint_json_output_prefix(buck: bool) {
let tmp_dir = TempDir::new().expect("Could not create temporary directory");
let tmp_path = tmp_dir.path();
fs::create_dir_all(tmp_path).expect("Could not create temporary directory path");
check_lint_fix(
args_vec![
"lint",
"--diagnostic-filter",
"W0010",
"--experimental",
"--format",
"json",
"--prefix",
"my/prefix"
],
"linter",
expect_file!("../resources/test/linter/parse_elp_lint_json_output_prefix.stdout"),
101,
buck,
None,
tmp_path,
Path::new("../resources/test/lint/lint_recursive"),
&[],
false,
)
.expect("bad test");
}

#[test_case(false ; "rebar")]
#[test_case(true ; "buck")]
fn lint_applies_fix_using_to_dir(buck: bool) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"path":"app_a/src/lints.erl","line":5,"char":1,"code":"ELP","severity":"error","name":"P1700 (head_mismatch)","original":null,"replacement":null,"description":"head mismatch 'head_mismatcX' vs 'head_mismatch'\n\nFor more information see: /erlang-error-index/p/P1700"}
3 changes: 2 additions & 1 deletion crates/elp/src/resources/test/lint_help.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Usage: [--project PROJECT] [--module MODULE] [--file FILE] [--to TO] [--no-diags] [--experimental] [--as PROFILE] [[--format FORMAT]] [--rebar] [--include-generated] [--include-erlc-diagnostics] [--include-ct-diagnostics] [--include-edoc-diagnostics] [--include-suppressed] [--include-tests] [--apply-fix] [--recursive] [--in-place] [--one-shot] [--diagnostic-ignore CODE] [--diagnostic-filter CODE] [--ignore-fix-only] [--read-config] [--config-file CONFIG_FILE] <IGNORED_APPS>...
Usage: [--project PROJECT] [--module MODULE] [--file FILE] [--to TO] [--no-diags] [--experimental] [--as PROFILE] [[--format FORMAT]] [--rebar] [--include-generated] [--include-erlc-diagnostics] [--include-ct-diagnostics] [--include-edoc-diagnostics] [--include-suppressed] [--include-tests] [--apply-fix] [--recursive] [--in-place] [--one-shot] [--prefix ARG] [--diagnostic-ignore CODE] [--diagnostic-filter CODE] [--ignore-fix-only] [--read-config] [--config-file CONFIG_FILE] <IGNORED_APPS>...

Available positional items:
<IGNORED_APPS> Rest of args are space separated list of apps to ignore
Expand Down Expand Up @@ -26,6 +26,7 @@ Available options:
--in-place When applying a fix, modify the original file.
--one-shot Apply to all matching diagnostic occurrences at once, rather
than one at a time.
--prefix <ARG> Optional prefix to prepend to each fact. Only used when --format=json is set
--diagnostic-ignore <CODE> Ignore the specified diagnostic, by code or label
--diagnostic-filter <CODE> Filter out all reported diagnostics except this one, by code or label
--ignore-fix-only Only apply elp:ignore fixes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{"path":"my/prefix/app_a/src/app_a.erl","line":9,"char":6,"code":"ELP","severity":"warning","name":"W0010 (unused_function_arg)","original":null,"replacement":null,"description":"this variable is unused\n\nFor more information see: /erlang-error-index/w/W0010"}
{"path":"my/prefix/app_a/src/app_a_unused_param.erl","line":5,"char":5,"code":"ELP","severity":"warning","name":"W0010 (unused_function_arg)","original":null,"replacement":null,"description":"this variable is unused\n\nFor more information see: /erlang-error-index/w/W0010"}
{"path":"my/prefix/app_b/src/app_b_unused_param.erl","line":5,"char":5,"code":"ELP","severity":"warning","name":"W0010 (unused_function_arg)","original":null,"replacement":null,"description":"this variable is unused\n\nFor more information see: /erlang-error-index/w/W0010"}

0 comments on commit c8aacf7

Please sign in to comment.