1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

feat(lint): add --compact flag for terse output (#15926)

This commit is contained in:
Brenley Dueck 2022-09-28 11:47:48 -05:00 committed by GitHub
parent 9c861ec430
commit 23125b275f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 105 additions and 0 deletions

View file

@ -154,6 +154,7 @@ pub struct LintFlags {
pub maybe_rules_include: Option<Vec<String>>, pub maybe_rules_include: Option<Vec<String>>,
pub maybe_rules_exclude: Option<Vec<String>>, pub maybe_rules_exclude: Option<Vec<String>>,
pub json: bool, pub json: bool,
pub compact: bool,
} }
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)] #[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
@ -1401,6 +1402,13 @@ Ignore linting a file by adding an ignore comment at the top of the file:
.help("Output lint result in JSON format") .help("Output lint result in JSON format")
.takes_value(false), .takes_value(false),
) )
.arg(
Arg::new("compact")
.long("compact")
.help("Output lint result in compact format")
.takes_value(false)
.conflicts_with("json"),
)
.arg( .arg(
Arg::new("files") Arg::new("files")
.takes_value(true) .takes_value(true)
@ -2584,6 +2592,7 @@ fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
.map(|f| f.map(String::from).collect()); .map(|f| f.map(String::from).collect());
let json = matches.is_present("json"); let json = matches.is_present("json");
let compact = matches.is_present("compact");
flags.subcommand = DenoSubcommand::Lint(LintFlags { flags.subcommand = DenoSubcommand::Lint(LintFlags {
files, files,
rules, rules,
@ -2592,6 +2601,7 @@ fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
maybe_rules_exclude, maybe_rules_exclude,
ignore, ignore,
json, json,
compact,
}); });
} }
@ -3686,6 +3696,7 @@ mod tests {
maybe_rules_include: None, maybe_rules_include: None,
maybe_rules_exclude: None, maybe_rules_exclude: None,
json: false, json: false,
compact: false,
ignore: vec![], ignore: vec![],
}), }),
..Flags::default() ..Flags::default()
@ -3712,6 +3723,7 @@ mod tests {
maybe_rules_include: None, maybe_rules_include: None,
maybe_rules_exclude: None, maybe_rules_exclude: None,
json: false, json: false,
compact: false,
ignore: vec![], ignore: vec![],
}), }),
watch: Some(vec![]), watch: Some(vec![]),
@ -3740,6 +3752,7 @@ mod tests {
maybe_rules_include: None, maybe_rules_include: None,
maybe_rules_exclude: None, maybe_rules_exclude: None,
json: false, json: false,
compact: false,
ignore: vec![], ignore: vec![],
}), }),
watch: Some(vec![]), watch: Some(vec![]),
@ -3760,6 +3773,7 @@ mod tests {
maybe_rules_include: None, maybe_rules_include: None,
maybe_rules_exclude: None, maybe_rules_exclude: None,
json: false, json: false,
compact: false,
ignore: vec![ ignore: vec![
PathBuf::from("script_1.ts"), PathBuf::from("script_1.ts"),
PathBuf::from("script_2.ts") PathBuf::from("script_2.ts")
@ -3780,6 +3794,7 @@ mod tests {
maybe_rules_include: None, maybe_rules_include: None,
maybe_rules_exclude: None, maybe_rules_exclude: None,
json: false, json: false,
compact: false,
ignore: vec![], ignore: vec![],
}), }),
..Flags::default() ..Flags::default()
@ -3803,6 +3818,7 @@ mod tests {
maybe_rules_include: Some(svec!["ban-untagged-todo", "no-undef"]), maybe_rules_include: Some(svec!["ban-untagged-todo", "no-undef"]),
maybe_rules_exclude: Some(svec!["no-const-assign"]), maybe_rules_exclude: Some(svec!["no-const-assign"]),
json: false, json: false,
compact: false,
ignore: vec![], ignore: vec![],
}), }),
..Flags::default() ..Flags::default()
@ -3820,6 +3836,7 @@ mod tests {
maybe_rules_include: None, maybe_rules_include: None,
maybe_rules_exclude: None, maybe_rules_exclude: None,
json: true, json: true,
compact: false,
ignore: vec![], ignore: vec![],
}), }),
..Flags::default() ..Flags::default()
@ -3844,6 +3861,33 @@ mod tests {
maybe_rules_include: None, maybe_rules_include: None,
maybe_rules_exclude: None, maybe_rules_exclude: None,
json: true, json: true,
compact: false,
ignore: vec![],
}),
config_flag: ConfigFlag::Path("Deno.jsonc".to_string()),
..Flags::default()
}
);
let r = flags_from_vec(svec![
"deno",
"lint",
"--config",
"Deno.jsonc",
"--compact",
"script_1.ts"
]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Lint(LintFlags {
files: vec![PathBuf::from("script_1.ts")],
rules: false,
maybe_rules_tags: None,
maybe_rules_include: None,
maybe_rules_exclude: None,
json: false,
compact: true,
ignore: vec![], ignore: vec![],
}), }),
config_flag: ConfigFlag::Path("Deno.jsonc".to_string()), config_flag: ConfigFlag::Path("Deno.jsonc".to_string()),

View file

@ -42,6 +42,13 @@ itest!(json {
exit_code: 1, exit_code: 1,
}); });
itest!(compact {
args:
"lint --compact lint/without_config/file1.js lint/without_config/file2.ts lint/without_config/ignored_file.tss",
output: "lint/expected_compact.out",
exit_code: 1,
});
itest!(ignore { itest!(ignore {
args: args:
"lint --ignore=lint/without_config/file1.js,lint/without_config/malformed.js,lint/without_config/lint_with_config/ lint/without_config/", "lint --ignore=lint/without_config/file1.js,lint/without_config/malformed.js,lint/without_config/lint_with_config/ lint/without_config/",

View file

@ -0,0 +1,5 @@
[WILDCARD]file1.js: line 1, col 1 - Ignore directive requires lint rule name(s) (ban-untagged-ignore)
[WILDCARD]file1.js: line 2, col 15 - Empty block statement (no-empty)
[WILDCARD]file2.ts: line 3, col 14 - Empty block statement (no-empty)
Found 3 problems
Checked 2 files

View file

@ -49,12 +49,14 @@ static STDIN_FILE_NAME: &str = "_stdin.ts";
pub enum LintReporterKind { pub enum LintReporterKind {
Pretty, Pretty,
Json, Json,
Compact,
} }
fn create_reporter(kind: LintReporterKind) -> Box<dyn LintReporter + Send> { fn create_reporter(kind: LintReporterKind) -> Box<dyn LintReporter + Send> {
match kind { match kind {
LintReporterKind::Pretty => Box::new(PrettyLintReporter::new()), LintReporterKind::Pretty => Box::new(PrettyLintReporter::new()),
LintReporterKind::Json => Box::new(JsonLintReporter::new()), LintReporterKind::Json => Box::new(JsonLintReporter::new()),
LintReporterKind::Compact => Box::new(CompactLintReporter::new()),
} }
} }
@ -66,6 +68,7 @@ pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> {
files: args, files: args,
ignore, ignore,
json, json,
compact,
.. ..
} = lint_flags; } = lint_flags;
// First, prepare final configuration. // First, prepare final configuration.
@ -104,6 +107,8 @@ pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> {
let reporter_kind = if json { let reporter_kind = if json {
LintReporterKind::Json LintReporterKind::Json
} else if compact {
LintReporterKind::Compact
} else { } else {
LintReporterKind::Pretty LintReporterKind::Pretty
}; };
@ -413,6 +418,50 @@ impl LintReporter for PrettyLintReporter {
} }
} }
struct CompactLintReporter {
lint_count: u32,
}
impl CompactLintReporter {
fn new() -> CompactLintReporter {
CompactLintReporter { lint_count: 0 }
}
}
impl LintReporter for CompactLintReporter {
fn visit_diagnostic(&mut self, d: &LintDiagnostic, _source_lines: Vec<&str>) {
self.lint_count += 1;
eprintln!(
"{}: line {}, col {} - {} ({})",
d.filename,
d.range.start.line_index + 1,
d.range.start.column_index + 1,
d.message,
d.code
)
}
fn visit_error(&mut self, file_path: &str, err: &AnyError) {
eprintln!("Error linting: {}", file_path);
eprintln!(" {}", err);
}
fn close(&mut self, check_count: usize) {
match self.lint_count {
1 => info!("Found 1 problem"),
n if n > 1 => info!("Found {} problems", self.lint_count),
_ => (),
}
match check_count {
n if n <= 1 => info!("Checked {} file", n),
n if n > 1 => info!("Checked {} files", n),
_ => unreachable!(),
}
}
}
pub fn format_diagnostic( pub fn format_diagnostic(
diagnostic_code: &str, diagnostic_code: &str,
message_line: &str, message_line: &str,