summaryrefslogtreecommitdiff
path: root/cli/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/main.rs')
-rw-r--r--cli/main.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/cli/main.rs b/cli/main.rs
index cf819ce5f..3c16df974 100644
--- a/cli/main.rs
+++ b/cli/main.rs
@@ -314,6 +314,71 @@ async fn install_command(
.map_err(ErrBox::from)
}
+async fn lint_command(flags: Flags, files: Vec<String>) -> Result<(), ErrBox> {
+ let global_state = GlobalState::new(flags)?;
+
+ // TODO(bartlomieju): refactor, it's non-sense to create
+ // state just to perform unstable check...
+ use crate::state::State;
+ let state = State::new(
+ global_state.clone(),
+ None,
+ ModuleSpecifier::resolve_url("file:///dummy.ts").unwrap(),
+ None,
+ true,
+ )?;
+
+ state.check_unstable("lint");
+
+ let mut error_counts = 0;
+
+ for file in files {
+ let specifier = ModuleSpecifier::resolve_url_or_path(&file)?;
+ let source_file = global_state
+ .file_fetcher
+ .fetch_source_file(&specifier, None, Permissions::allow_all())
+ .await?;
+ let source_code = String::from_utf8(source_file.source_code)?;
+
+ let mut linter = deno_lint::linter::Linter::default();
+ let lint_rules = deno_lint::rules::get_all_rules();
+
+ let file_diagnostics = linter.lint(file, source_code, lint_rules)?;
+
+ error_counts += file_diagnostics.len();
+ for d in file_diagnostics.iter() {
+ let pretty_message = format!(
+ "({}) {}",
+ colors::gray(d.code.to_string()),
+ d.message.clone()
+ );
+ eprintln!(
+ "{}\n",
+ fmt_errors::format_stack(
+ true,
+ pretty_message,
+ Some(d.line_src.clone()),
+ Some(d.location.col as i64),
+ Some((d.location.col + d.snippet_length) as i64),
+ &[fmt_errors::format_location(
+ d.location.filename.clone(),
+ d.location.line as i64,
+ d.location.col as i64,
+ )],
+ 0
+ )
+ );
+ }
+ }
+
+ if error_counts > 0 {
+ eprintln!("Found {} problems", error_counts);
+ std::process::exit(1);
+ }
+
+ Ok(())
+}
+
async fn cache_command(flags: Flags, files: Vec<String>) -> Result<(), ErrBox> {
let main_module =
ModuleSpecifier::resolve_url_or_path("./__$deno$fetch.ts").unwrap();
@@ -657,6 +722,7 @@ pub fn main() {
} => {
install_command(flags, module_url, args, name, root, force).boxed_local()
}
+ DenoSubcommand::Lint { files } => lint_command(flags, files).boxed_local(),
DenoSubcommand::Repl => run_repl(flags).boxed_local(),
DenoSubcommand::Run { script } => run_command(flags, script).boxed_local(),
DenoSubcommand::Test {