mirror of
https://github.com/denoland/deno.git
synced 2024-11-22 15:06:54 -05:00
feat: add "--ignore" to deno lint (#6934)
This commit is contained in:
parent
fed70c9903
commit
6706eb5515
5 changed files with 55 additions and 9 deletions
32
cli/flags.rs
32
cli/flags.rs
|
@ -55,6 +55,7 @@ pub enum DenoSubcommand {
|
|||
},
|
||||
Lint {
|
||||
files: Vec<String>,
|
||||
ignore: Vec<String>,
|
||||
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()
|
||||
|
|
13
cli/lint.rs
13
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<String>) -> Result<(), ErrBox> {
|
||||
let target_files = collect_files(args)?;
|
||||
pub async fn lint_files(
|
||||
args: Vec<String>,
|
||||
ignore: Vec<String>,
|
||||
) -> 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));
|
||||
|
|
11
cli/main.rs
11
cli/main.rs
|
@ -345,6 +345,7 @@ async fn lint_command(
|
|||
flags: Flags,
|
||||
files: Vec<String>,
|
||||
list_rules: bool,
|
||||
ignore: Vec<String>,
|
||||
) -> 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<String>) -> 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 {
|
||||
|
|
|
@ -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",
|
||||
|
|
2
cli/tests/lint/expected_ignore.out
Normal file
2
cli/tests/lint/expected_ignore.out
Normal file
|
@ -0,0 +1,2 @@
|
|||
[WILDCARD]
|
||||
Found 1 problems
|
Loading…
Reference in a new issue