From 6706eb551532ee6c84ad013377ac2cd91681424a Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 12 Aug 2020 19:17:44 +0530 Subject: [PATCH] feat: add "--ignore" to deno lint (#6934) --- cli/flags.rs | 32 +++++++++++++++++++++++++++--- cli/lint.rs | 13 ++++++++++-- cli/main.rs | 11 ++++++---- cli/tests/integration_tests.rs | 6 ++++++ cli/tests/lint/expected_ignore.out | 2 ++ 5 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 cli/tests/lint/expected_ignore.out diff --git a/cli/flags.rs b/cli/flags.rs index 993f681b58..49b5776969 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -55,6 +55,7 @@ pub enum DenoSubcommand { }, Lint { files: Vec, + ignore: Vec, rules: bool, }, Repl, @@ -628,8 +629,16 @@ fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) { Some(f) => f.map(String::from).collect(), None => vec![], }; + let ignore = match matches.values_of("ignore") { + Some(f) => f.map(String::from).collect(), + None => vec![], + }; let rules = matches.is_present("rules"); - flags.subcommand = DenoSubcommand::Lint { files, rules }; + flags.subcommand = DenoSubcommand::Lint { + files, + rules, + ignore, + }; } fn types_subcommand<'a, 'b>() -> App<'a, 'b> { @@ -1019,6 +1028,15 @@ Ignore linting a file by adding an ignore comment at the top of the file: .long("rules") .help("List available rules"), ) + .arg( + Arg::with_name("ignore") + .long("ignore") + .requires("unstable") + .takes_value(true) + .use_delimiter(true) + .require_equals(true) + .help("Ignore linting particular source files."), + ) .arg( Arg::with_name("files") .takes_value(true) @@ -1739,19 +1757,26 @@ mod tests { subcommand: DenoSubcommand::Lint { files: vec!["script_1.ts".to_string(), "script_2.ts".to_string()], rules: false, + ignore: vec![], }, unstable: true, ..Flags::default() } ); - let r = flags_from_vec_safe(svec!["deno", "lint", "--unstable"]); + let r = flags_from_vec_safe(svec![ + "deno", + "lint", + "--unstable", + "--ignore=script_1.ts,script_2.ts" + ]); assert_eq!( r.unwrap(), Flags { subcommand: DenoSubcommand::Lint { files: vec![], rules: false, + ignore: svec!["script_1.ts", "script_2.ts"], }, unstable: true, ..Flags::default() @@ -1764,7 +1789,8 @@ mod tests { Flags { subcommand: DenoSubcommand::Lint { files: vec![], - rules: true + rules: true, + ignore: vec![], }, unstable: true, ..Flags::default() diff --git a/cli/lint.rs b/cli/lint.rs index e3013f302d..3e9380d9d9 100644 --- a/cli/lint.rs +++ b/cli/lint.rs @@ -25,8 +25,17 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use swc_ecmascript::parser::Syntax; -pub async fn lint_files(args: Vec) -> Result<(), ErrBox> { - let target_files = collect_files(args)?; +pub async fn lint_files( + args: Vec, + ignore: Vec, +) -> Result<(), ErrBox> { + let mut target_files = collect_files(args)?; + if !ignore.is_empty() { + // collect all files to be ignored + // and retain only files that should be linted. + let ignore_files = collect_files(ignore)?; + target_files.retain(|f| !ignore_files.contains(&f)); + } debug!("Found {} files", target_files.len()); let error_count = Arc::new(AtomicUsize::new(0)); diff --git a/cli/main.rs b/cli/main.rs index 5400d3f65b..27948e14f3 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -345,6 +345,7 @@ async fn lint_command( flags: Flags, files: Vec, list_rules: bool, + ignore: Vec, ) -> Result<(), ErrBox> { if !flags.unstable { exit_unstable("lint"); @@ -355,7 +356,7 @@ async fn lint_command( return Ok(()); } - lint::lint_files(files).await + lint::lint_files(files, ignore).await } async fn cache_command(flags: Flags, files: Vec) -> Result<(), ErrBox> { @@ -733,9 +734,11 @@ pub fn main() { } => { install_command(flags, module_url, args, name, root, force).boxed_local() } - DenoSubcommand::Lint { files, rules } => { - lint_command(flags, files, rules).boxed_local() - } + DenoSubcommand::Lint { + files, + rules, + ignore, + } => lint_command(flags, files, rules, ignore).boxed_local(), DenoSubcommand::Repl => run_repl(flags).boxed_local(), DenoSubcommand::Run { script } => run_command(flags, script).boxed_local(), DenoSubcommand::Test { diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 9ffcad1846..bb7ba0d6f6 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -2216,6 +2216,12 @@ itest!(deno_lint { exit_code: 1, }); +itest!(deno_lint_ignore { + args: "lint --unstable --ignore=lint/file1.js lint/", + output: "lint/expected_ignore.out", + exit_code: 1, +}); + itest!(deno_lint_glob { args: "lint --unstable lint/", output: "lint/expected_glob.out", diff --git a/cli/tests/lint/expected_ignore.out b/cli/tests/lint/expected_ignore.out new file mode 100644 index 0000000000..6041d1c6af --- /dev/null +++ b/cli/tests/lint/expected_ignore.out @@ -0,0 +1,2 @@ +[WILDCARD] +Found 1 problems