1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 12:58:54 -05:00

feat(lint): add --rules flag (#6264)

This commit is contained in:
Bartek Iwańczuk 2020-06-12 16:42:12 +02:00 committed by GitHub
parent e9424bf6b5
commit 07bf90779b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 5 deletions

View file

@ -52,6 +52,7 @@ pub enum DenoSubcommand {
}, },
Lint { Lint {
files: Vec<String>, files: Vec<String>,
rules: bool,
}, },
Repl, Repl,
Run { Run {
@ -592,7 +593,8 @@ fn lint_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
Some(f) => f.map(String::from).collect(), Some(f) => f.map(String::from).collect(),
None => vec![], None => vec![],
}; };
flags.subcommand = DenoSubcommand::Lint { files }; let rules = matches.is_present("rules");
flags.subcommand = DenoSubcommand::Lint { files, rules };
} }
fn types_subcommand<'a, 'b>() -> App<'a, 'b> { fn types_subcommand<'a, 'b>() -> App<'a, 'b> {
@ -913,6 +915,9 @@ fn lint_subcommand<'a, 'b>() -> App<'a, 'b> {
deno lint --unstable deno lint --unstable
deno lint --unstable myfile1.ts myfile2.js deno lint --unstable myfile1.ts myfile2.js
List available rules:
deno lint --unstable --rules
Ignore diagnostics on the next line by preceding it with an ignore comment and Ignore diagnostics on the next line by preceding it with an ignore comment and
rule name: rule name:
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
@ -929,6 +934,11 @@ Ignore linting a file by adding an ignore comment at the top of the file:
", ",
) )
.arg(unstable_arg()) .arg(unstable_arg())
.arg(
Arg::with_name("rules")
.long("rules")
.help("List available rules"),
)
.arg( .arg(
Arg::with_name("files") Arg::with_name("files")
.takes_value(true) .takes_value(true)
@ -1658,7 +1668,8 @@ mod tests {
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Lint { subcommand: DenoSubcommand::Lint {
files: vec!["script_1.ts".to_string(), "script_2.ts".to_string()] files: vec!["script_1.ts".to_string(), "script_2.ts".to_string()],
rules: false,
}, },
unstable: true, unstable: true,
..Flags::default() ..Flags::default()
@ -1669,7 +1680,23 @@ mod tests {
assert_eq!( assert_eq!(
r.unwrap(), r.unwrap(),
Flags { Flags {
subcommand: DenoSubcommand::Lint { files: vec![] }, subcommand: DenoSubcommand::Lint {
files: vec![],
rules: false,
},
unstable: true,
..Flags::default()
}
);
let r = flags_from_vec_safe(svec!["deno", "lint", "--unstable", "--rules"]);
assert_eq!(
r.unwrap(),
Flags {
subcommand: DenoSubcommand::Lint {
files: vec![],
rules: true
},
unstable: true, unstable: true,
..Flags::default() ..Flags::default()
} }

View file

@ -64,6 +64,15 @@ pub async fn lint_files(args: Vec<String>) -> Result<(), ErrBox> {
Ok(()) Ok(())
} }
pub fn print_rules_list() {
let lint_rules = rules::get_recommended_rules();
println!("Available rules:");
for rule in lint_rules {
println!(" - {}", rule.code());
}
}
fn create_linter() -> Linter { fn create_linter() -> Linter {
Linter::new( Linter::new(
"deno-lint-ignore-file".to_string(), "deno-lint-ignore-file".to_string(),

View file

@ -316,7 +316,11 @@ async fn install_command(
.map_err(ErrBox::from) .map_err(ErrBox::from)
} }
async fn lint_command(flags: Flags, files: Vec<String>) -> Result<(), ErrBox> { async fn lint_command(
flags: Flags,
files: Vec<String>,
list_rules: bool,
) -> Result<(), ErrBox> {
let global_state = GlobalState::new(flags)?; let global_state = GlobalState::new(flags)?;
// TODO(bartlomieju): refactor, it's non-sense to create // TODO(bartlomieju): refactor, it's non-sense to create
@ -331,6 +335,12 @@ async fn lint_command(flags: Flags, files: Vec<String>) -> Result<(), ErrBox> {
)?; )?;
state.check_unstable("lint"); state.check_unstable("lint");
if list_rules {
lint::print_rules_list();
return Ok(());
}
lint::lint_files(files).await lint::lint_files(files).await
} }
@ -708,7 +718,9 @@ pub fn main() {
} => { } => {
install_command(flags, module_url, args, name, root, force).boxed_local() install_command(flags, module_url, args, name, root, force).boxed_local()
} }
DenoSubcommand::Lint { files } => lint_command(flags, files).boxed_local(), DenoSubcommand::Lint { files, rules } => {
lint_command(flags, files, rules).boxed_local()
}
DenoSubcommand::Repl => run_repl(flags).boxed_local(), DenoSubcommand::Repl => run_repl(flags).boxed_local(),
DenoSubcommand::Run { script } => run_command(flags, script).boxed_local(), DenoSubcommand::Run { script } => run_command(flags, script).boxed_local(),
DenoSubcommand::Test { DenoSubcommand::Test {