Skip to main content
Module

x/deno/cli/tools/lint.rs

A modern runtime for JavaScript and TypeScript.
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
//! This module provides file linting utilities using//! [`deno_lint`](https://github.com/denoland/deno_lint).//!//! At the moment it is only consumed using CLI but in//! the future it can be easily extended to provide//! the same functions as ops available in JS runtime.use crate::config_file::LintConfig;use crate::file_watcher::ResolutionResult;use crate::flags::{Flags, LintFlags};use crate::fmt_errors;use crate::fs_util::{collect_files, is_supported_ext, specifier_to_file_path};use crate::proc_state::ProcState;use crate::tools::fmt::run_parallelized;use crate::{colors, file_watcher};use deno_ast::MediaType;use deno_core::anyhow::anyhow;use deno_core::error::generic_error;use deno_core::error::AnyError;use deno_core::error::JsStackFrame;use deno_core::serde_json;use deno_lint::diagnostic::LintDiagnostic;use deno_lint::linter::Linter;use deno_lint::linter::LinterBuilder;use deno_lint::rules;use deno_lint::rules::LintRule;use log::debug;use log::info;use serde::Serialize;use std::fs;use std::io::{stdin, Read};use std::path::PathBuf;use std::sync::atomic::{AtomicBool, Ordering};use std::sync::{Arc, Mutex};
use super::incremental_cache::IncrementalCache;
static STDIN_FILE_NAME: &str = "_stdin.ts";
#[derive(Clone, Debug)]pub enum LintReporterKind { Pretty, Json,}
fn create_reporter(kind: LintReporterKind) -> Box<dyn LintReporter + Send> { match kind { LintReporterKind::Pretty => Box::new(PrettyLintReporter::new()), LintReporterKind::Json => Box::new(JsonLintReporter::new()), }}
pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> { let flags = Arc::new(flags); let LintFlags { maybe_rules_tags, maybe_rules_include, maybe_rules_exclude, files: args, ignore, json, .. } = lint_flags; // First, prepare final configuration. // Collect included and ignored files. CLI flags take precendence // over config file, ie. if there's `files.ignore` in config file // and `--ignore` CLI flag, only the flag value is taken into account. let mut include_files = args.clone(); let mut exclude_files = ignore.clone();
let ps = ProcState::build(flags.clone()).await?; let maybe_lint_config = if let Some(config_file) = &ps.maybe_config_file { config_file.to_lint_config()? } else { None };
if let Some(lint_config) = maybe_lint_config.as_ref() { if include_files.is_empty() { include_files = lint_config .files .include .iter() .filter_map(|s| specifier_to_file_path(s).ok()) .collect::<Vec<_>>(); }
if exclude_files.is_empty() { exclude_files = lint_config .files .exclude .iter() .filter_map(|s| specifier_to_file_path(s).ok()) .collect::<Vec<_>>(); } }
if include_files.is_empty() { include_files = [std::env::current_dir()?].to_vec(); }
let reporter_kind = if json { LintReporterKind::Json } else { LintReporterKind::Pretty };
let has_error = Arc::new(AtomicBool::new(false)); // Try to get configured rules. CLI flags take precendence // over config file, ie. if there's `rules.include` in config file // and `--rules-include` CLI flag, only the flag value is taken into account. let lint_rules = get_configured_rules( maybe_lint_config.as_ref(), maybe_rules_tags, maybe_rules_include, maybe_rules_exclude, )?;
let resolver = |changed: Option<Vec<PathBuf>>| { let files_changed = changed.is_some(); let result = collect_files(&include_files, &exclude_files, is_supported_ext).map( |files| { if let Some(paths) = changed { files .iter() .any(|path| paths.contains(path)) .then(|| files) .unwrap_or_else(|| [].to_vec()) } else { files } }, );
let paths_to_watch = include_files.clone();
async move { if files_changed && matches!(result, Ok(ref files) if files.is_empty()) { ResolutionResult::Ignore } else { ResolutionResult::Restart { paths_to_watch, result, } } } };
let operation = |paths: Vec<PathBuf>| async { let incremental_cache = Arc::new(IncrementalCache::new( &ps.dir.lint_incremental_cache_db_file_path(), // use a hash of the rule names in order to bust the cache &{ // ensure this is stable by sorting it let mut names = lint_rules.iter().map(|r| r.code()).collect::<Vec<_>>(); names.sort_unstable(); names }, &paths, )); let target_files_len = paths.len(); let reporter_kind = reporter_kind.clone(); let reporter_lock = Arc::new(Mutex::new(create_reporter(reporter_kind))); run_parallelized(paths, { let has_error = has_error.clone(); let lint_rules = lint_rules.clone(); let reporter_lock = reporter_lock.clone(); let incremental_cache = incremental_cache.clone(); move |file_path| { let file_text = fs::read_to_string(&file_path)?;
// don't bother rechecking this file if it didn't have any diagnostics before if incremental_cache.is_file_same(&file_path, &file_text) { return Ok(()); }
let r = lint_file(file_path.clone(), file_text, lint_rules.clone()); if let Ok((file_diagnostics, file_text)) = &r { if file_diagnostics.is_empty() { // update the incremental cache if there were no diagnostics incremental_cache.update_file(&file_path, file_text) } }
handle_lint_result( &file_path.to_string_lossy(), r, reporter_lock.clone(), has_error, );
Ok(()) } }) .await?; incremental_cache.wait_completion().await; reporter_lock.lock().unwrap().close(target_files_len);
Ok(()) }; if flags.watch.is_some() { if args.len() == 1 && args[0].to_string_lossy() == "-" { return Err(generic_error( "Lint watch on standard input is not supported.", )); } file_watcher::watch_func( resolver, operation, file_watcher::PrintConfig { job_name: "Lint".to_string(), clear_screen: !flags.no_clear_screen, }, ) .await?; } else { if args.len() == 1 && args[0].to_string_lossy() == "-" { let reporter_lock = Arc::new(Mutex::new(create_reporter(reporter_kind.clone()))); let r = lint_stdin(lint_rules); handle_lint_result( STDIN_FILE_NAME, r, reporter_lock.clone(), has_error.clone(), ); reporter_lock.lock().unwrap().close(1); } else { let target_files = collect_files(&include_files, &exclude_files, is_supported_ext) .and_then(|files| { if files.is_empty() { Err(generic_error("No target files found.")) } else { Ok(files) } })?; debug!("Found {} files", target_files.len()); operation(target_files).await?; }; let has_error = has_error.load(Ordering::Relaxed); if has_error { std::process::exit(1); } }
Ok(())}
pub fn print_rules_list(json: bool) { let lint_rules = rules::get_recommended_rules();
if json { let json_rules: Vec<serde_json::Value> = lint_rules .iter() .map(|rule| { serde_json::json!({ "code": rule.code(), "tags": rule.tags(), "docs": rule.docs(), }) }) .collect(); let json_str = serde_json::to_string_pretty(&json_rules).unwrap(); println!("{}", json_str); } else { // The rules should still be printed even if `--quiet` option is enabled, // so use `println!` here instead of `info!`. println!("Available rules:"); for rule in lint_rules.iter() { println!(" - {}", rule.code()); println!(" help: https://lint.deno.land/#{}", rule.code()); println!(); } }}
pub fn create_linter( media_type: MediaType, rules: Vec<Arc<dyn LintRule>>,) -> Linter { LinterBuilder::default() .ignore_file_directive("deno-lint-ignore-file") .ignore_diagnostic_directive("deno-lint-ignore") .media_type(media_type) .rules(rules) .build()}
fn lint_file( file_path: PathBuf, source_code: String, lint_rules: Vec<Arc<dyn LintRule>>,) -> Result<(Vec<LintDiagnostic>, String), AnyError> { let file_name = file_path.to_string_lossy().to_string(); let media_type = MediaType::from(&file_path);
let linter = create_linter(media_type, lint_rules);
let (_, file_diagnostics) = linter.lint(file_name, source_code.clone())?;
Ok((file_diagnostics, source_code))}
/// Lint stdin and write result to stdout./// Treats input as TypeScript./// Compatible with `--json` flag.fn lint_stdin( lint_rules: Vec<Arc<dyn LintRule>>,) -> Result<(Vec<LintDiagnostic>, String), AnyError> { let mut source_code = String::new(); if stdin().read_to_string(&mut source_code).is_err() { return Err(generic_error("Failed to read from stdin")); }
let linter = create_linter(MediaType::TypeScript, lint_rules);
let (_, file_diagnostics) = linter.lint(STDIN_FILE_NAME.to_string(), source_code.clone())?;
Ok((file_diagnostics, source_code))}
fn handle_lint_result( file_path: &str, result: Result<(Vec<LintDiagnostic>, String), AnyError>, reporter_lock: Arc<Mutex<Box<dyn LintReporter + Send>>>, has_error: Arc<AtomicBool>,) { let mut reporter = reporter_lock.lock().unwrap();
match result { Ok((mut file_diagnostics, source)) => { sort_diagnostics(&mut file_diagnostics); for d in file_diagnostics.iter() { has_error.store(true, Ordering::Relaxed); reporter.visit_diagnostic(d, source.split('\n').collect()); } } Err(err) => { has_error.store(true, Ordering::Relaxed); reporter.visit_error(file_path, &err); } }}
trait LintReporter { fn visit_diagnostic(&mut self, d: &LintDiagnostic, source_lines: Vec<&str>); fn visit_error(&mut self, file_path: &str, err: &AnyError); fn close(&mut self, check_count: usize);}
#[derive(Serialize)]struct LintError { file_path: String, message: String,}
struct PrettyLintReporter { lint_count: u32,}
impl PrettyLintReporter { fn new() -> PrettyLintReporter { PrettyLintReporter { lint_count: 0 } }}
impl LintReporter for PrettyLintReporter { fn visit_diagnostic(&mut self, d: &LintDiagnostic, source_lines: Vec<&str>) { self.lint_count += 1;
let pretty_message = format!("({}) {}", colors::red(&d.code), &d.message);
let message = format_diagnostic( &d.code, &pretty_message, &source_lines, d.range.clone(), d.hint.as_ref(), &fmt_errors::format_location(&JsStackFrame::from_location( Some(d.filename.clone()), Some(d.range.start.line_index as i64 + 1), // 1-indexed // todo(#11111): make 1-indexed as well Some(d.range.start.column_index as i64), )), );
eprintln!("{}\n", message); }
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( diagnostic_code: &str, message_line: &str, source_lines: &[&str], range: deno_lint::diagnostic::Range, maybe_hint: Option<&String>, formatted_location: &str,) -> String { let mut lines = vec![];
for (i, line) in source_lines .iter() .enumerate() .take(range.end.line_index + 1) .skip(range.start.line_index) { lines.push(line.to_string()); if range.start.line_index == range.end.line_index { lines.push(format!( "{}{}", " ".repeat(range.start.column_index), colors::red( &"^".repeat(range.end.column_index - range.start.column_index) ) )); } else { let line_len = line.len(); if range.start.line_index == i { lines.push(format!( "{}{}", " ".repeat(range.start.column_index), colors::red(&"^".repeat(line_len - range.start.column_index)) )); } else if range.end.line_index == i { lines .push(colors::red(&"^".repeat(range.end.column_index)).to_string()); } else if line_len != 0 { lines.push(colors::red(&"^".repeat(line_len)).to_string()); } } }
let hint = if let Some(hint) = maybe_hint { format!(" {} {}\n", colors::cyan("hint:"), hint) } else { "".to_string() }; let help = format!( " {} for further information visit https://lint.deno.land/#{}", colors::cyan("help:"), diagnostic_code );
format!( "{message_line}\n{snippets}\n at {formatted_location}\n\n{hint}{help}", message_line = message_line, snippets = lines.join("\n"), formatted_location = formatted_location, hint = hint, help = help )}
#[derive(Serialize)]struct JsonLintReporter { diagnostics: Vec<LintDiagnostic>, errors: Vec<LintError>,}
impl JsonLintReporter { fn new() -> JsonLintReporter { JsonLintReporter { diagnostics: Vec::new(), errors: Vec::new(), } }}
impl LintReporter for JsonLintReporter { fn visit_diagnostic(&mut self, d: &LintDiagnostic, _source_lines: Vec<&str>) { self.diagnostics.push(d.clone()); }
fn visit_error(&mut self, file_path: &str, err: &AnyError) { self.errors.push(LintError { file_path: file_path.to_string(), message: err.to_string(), }); }
fn close(&mut self, _check_count: usize) { sort_diagnostics(&mut self.diagnostics); let json = serde_json::to_string_pretty(&self); println!("{}", json.unwrap()); }}
fn sort_diagnostics(diagnostics: &mut Vec<LintDiagnostic>) { // Sort so that we guarantee a deterministic output which is useful for tests diagnostics.sort_by(|a, b| { use std::cmp::Ordering; let file_order = a.filename.cmp(&b.filename); match file_order { Ordering::Equal => { let line_order = a.range.start.line_index.cmp(&b.range.start.line_index); match line_order { Ordering::Equal => { a.range.start.column_index.cmp(&b.range.start.column_index) } _ => line_order, } } _ => file_order, } });}
pub fn get_configured_rules( maybe_lint_config: Option<&LintConfig>, maybe_rules_tags: Option<Vec<String>>, maybe_rules_include: Option<Vec<String>>, maybe_rules_exclude: Option<Vec<String>>,) -> Result<Vec<Arc<dyn LintRule>>, AnyError> { if maybe_lint_config.is_none() && maybe_rules_tags.is_none() && maybe_rules_include.is_none() && maybe_rules_exclude.is_none() { return Ok(rules::get_recommended_rules()); }
let (config_file_tags, config_file_include, config_file_exclude) = if let Some(lint_config) = maybe_lint_config { ( lint_config.rules.tags.clone(), lint_config.rules.include.clone(), lint_config.rules.exclude.clone(), ) } else { (None, None, None) };
let maybe_configured_include = if maybe_rules_include.is_some() { maybe_rules_include } else { config_file_include };
let maybe_configured_exclude = if maybe_rules_exclude.is_some() { maybe_rules_exclude } else { config_file_exclude };
let maybe_configured_tags = if maybe_rules_tags.is_some() { maybe_rules_tags } else { config_file_tags };
let configured_rules = rules::get_filtered_rules( maybe_configured_tags.or_else(|| Some(vec!["recommended".to_string()])), maybe_configured_exclude, maybe_configured_include, );
if configured_rules.is_empty() { return Err(anyhow!("No rules have been configured")); }
Ok(configured_rules)}
#[cfg(test)]mod test { use deno_lint::rules::get_recommended_rules;
use super::*; use crate::config_file::LintRulesConfig;
#[test] fn recommended_rules_when_no_tags_in_config() { let lint_config = LintConfig { rules: LintRulesConfig { exclude: Some(vec!["no-debugger".to_string()]), ..Default::default() }, ..Default::default() }; let rules = get_configured_rules(Some(&lint_config), None, None, None).unwrap(); let mut rule_names = rules .into_iter() .map(|r| r.code().to_string()) .collect::<Vec<_>>(); rule_names.sort(); let mut recommended_rule_names = get_recommended_rules() .into_iter() .map(|r| r.code().to_string()) .filter(|n| n != "no-debugger") .collect::<Vec<_>>(); recommended_rule_names.sort(); assert_eq!(rule_names, recommended_rule_names); }}