2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
//! This module provides file linting utilities using
|
2020-06-10 17:29:48 -04:00
|
|
|
//! [`deno_lint`](https://github.com/denoland/deno_lint).
|
2023-01-07 15:22:09 -05:00
|
|
|
use crate::args::FilesConfig;
|
2023-06-14 18:29:19 -04:00
|
|
|
use crate::args::Flags;
|
|
|
|
use crate::args::LintFlags;
|
2023-01-07 15:22:09 -05:00
|
|
|
use crate::args::LintOptions;
|
|
|
|
use crate::args::LintReporterKind;
|
|
|
|
use crate::args::LintRulesConfig;
|
2022-06-28 16:45:55 -04:00
|
|
|
use crate::colors;
|
2023-05-01 14:35:23 -04:00
|
|
|
use crate::factory::CliFactory;
|
2020-11-19 13:19:34 -05:00
|
|
|
use crate::tools::fmt::run_parallelized;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::file_watcher;
|
2022-12-07 13:10:10 -05:00
|
|
|
use crate::util::fs::FileCollector;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::path::is_supported_ext;
|
2023-06-14 18:29:19 -04:00
|
|
|
use crate::util::sync::AtomicFlag;
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2023-01-07 15:22:09 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
|
|
|
use deno_core::error::JsStackFrame;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
2020-06-10 17:29:48 -04:00
|
|
|
use deno_lint::diagnostic::LintDiagnostic;
|
|
|
|
use deno_lint::linter::Linter;
|
2020-07-01 10:04:56 -04:00
|
|
|
use deno_lint::linter::LinterBuilder;
|
2020-06-10 17:29:48 -04:00
|
|
|
use deno_lint::rules;
|
2020-07-01 10:04:56 -04:00
|
|
|
use deno_lint::rules::LintRule;
|
2022-09-02 16:53:23 -04:00
|
|
|
use deno_runtime::fmt_errors::format_location;
|
2021-03-26 12:34:25 -04:00
|
|
|
use log::debug;
|
|
|
|
use log::info;
|
2020-08-13 11:30:46 -04:00
|
|
|
use serde::Serialize;
|
2020-06-10 17:29:48 -04:00
|
|
|
use std::fs;
|
2022-06-28 16:45:55 -04:00
|
|
|
use std::io::stdin;
|
|
|
|
use std::io::Read;
|
2023-03-21 11:46:40 -04:00
|
|
|
use std::path::Path;
|
2020-06-10 17:29:48 -04:00
|
|
|
use std::path::PathBuf;
|
2022-06-28 16:45:55 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2022-07-12 18:58:39 -04:00
|
|
|
use crate::cache::IncrementalCache;
|
2022-04-19 22:14:00 -04:00
|
|
|
|
2021-09-08 01:08:33 -04:00
|
|
|
static STDIN_FILE_NAME: &str = "_stdin.ts";
|
|
|
|
|
2020-08-13 11:30:46 -04:00
|
|
|
fn create_reporter(kind: LintReporterKind) -> Box<dyn LintReporter + Send> {
|
|
|
|
match kind {
|
|
|
|
LintReporterKind::Pretty => Box::new(PrettyLintReporter::new()),
|
|
|
|
LintReporterKind::Json => Box::new(JsonLintReporter::new()),
|
2022-09-28 12:47:48 -04:00
|
|
|
LintReporterKind::Compact => Box::new(CompactLintReporter::new()),
|
2020-08-13 11:30:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 18:29:19 -04:00
|
|
|
pub async fn lint(flags: Flags, lint_flags: LintFlags) -> Result<(), AnyError> {
|
|
|
|
if flags.watch.is_some() {
|
|
|
|
if lint_flags.is_stdin() {
|
2021-10-05 17:07:38 -04:00
|
|
|
return Err(generic_error(
|
|
|
|
"Lint watch on standard input is not supported.",
|
|
|
|
));
|
|
|
|
}
|
2023-06-14 18:29:19 -04:00
|
|
|
let clear_screen = !flags.no_clear_screen;
|
2022-01-31 11:39:39 -05:00
|
|
|
file_watcher::watch_func(
|
2023-06-14 18:29:19 -04:00
|
|
|
flags,
|
2022-01-31 11:39:39 -05:00
|
|
|
file_watcher::PrintConfig {
|
|
|
|
job_name: "Lint".to_string(),
|
2023-06-14 18:29:19 -04:00
|
|
|
clear_screen,
|
|
|
|
},
|
|
|
|
move |flags, sender, changed_paths| {
|
|
|
|
let lint_flags = lint_flags.clone();
|
|
|
|
Ok(async move {
|
|
|
|
let factory = CliFactory::from_flags(flags).await?;
|
|
|
|
let cli_options = factory.cli_options();
|
|
|
|
let lint_options = cli_options.resolve_lint_options(lint_flags)?;
|
|
|
|
let files =
|
|
|
|
collect_lint_files(&lint_options.files).and_then(|files| {
|
|
|
|
if files.is_empty() {
|
|
|
|
Err(generic_error("No target files found."))
|
|
|
|
} else {
|
|
|
|
Ok(files)
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
_ = sender.send(files.clone());
|
|
|
|
|
|
|
|
let lint_paths = if let Some(paths) = changed_paths {
|
|
|
|
// lint all files on any changed (https://github.com/denoland/deno/issues/12446)
|
|
|
|
files
|
|
|
|
.iter()
|
|
|
|
.any(|path| paths.contains(path))
|
|
|
|
.then_some(files)
|
|
|
|
.unwrap_or_else(|| [].to_vec())
|
|
|
|
} else {
|
|
|
|
files
|
|
|
|
};
|
|
|
|
|
|
|
|
lint_files(factory, lint_options, lint_paths).await?;
|
|
|
|
Ok(())
|
|
|
|
})
|
2022-01-31 11:39:39 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2021-10-05 17:07:38 -04:00
|
|
|
} else {
|
2023-06-14 18:29:19 -04:00
|
|
|
let factory = CliFactory::from_flags(flags).await?;
|
|
|
|
let cli_options = factory.cli_options();
|
|
|
|
let is_stdin = lint_flags.is_stdin();
|
|
|
|
let lint_options = cli_options.resolve_lint_options(lint_flags)?;
|
|
|
|
let files = &lint_options.files;
|
|
|
|
let success = if is_stdin {
|
|
|
|
let reporter_kind = lint_options.reporter_kind;
|
2023-01-13 16:39:19 -05:00
|
|
|
let reporter_lock = Arc::new(Mutex::new(create_reporter(reporter_kind)));
|
2023-06-14 18:29:19 -04:00
|
|
|
let lint_rules = get_config_rules_err_empty(lint_options.rules)?;
|
2021-09-08 01:08:33 -04:00
|
|
|
let r = lint_stdin(lint_rules);
|
2023-06-14 18:29:19 -04:00
|
|
|
let success =
|
|
|
|
handle_lint_result(STDIN_FILE_NAME, r, reporter_lock.clone());
|
2021-10-05 17:07:38 -04:00
|
|
|
reporter_lock.lock().unwrap().close(1);
|
2023-06-14 18:29:19 -04:00
|
|
|
success
|
2021-09-08 01:08:33 -04:00
|
|
|
} else {
|
2023-06-14 18:29:19 -04:00
|
|
|
let target_files = collect_lint_files(files).and_then(|files| {
|
2023-01-07 15:22:09 -05:00
|
|
|
if files.is_empty() {
|
|
|
|
Err(generic_error("No target files found."))
|
|
|
|
} else {
|
|
|
|
Ok(files)
|
|
|
|
}
|
|
|
|
})?;
|
2021-09-08 01:08:33 -04:00
|
|
|
debug!("Found {} files", target_files.len());
|
2023-06-14 18:29:19 -04:00
|
|
|
lint_files(factory, lint_options, target_files).await?
|
2021-09-08 01:08:33 -04:00
|
|
|
};
|
2023-06-14 18:29:19 -04:00
|
|
|
if !success {
|
2021-10-05 17:07:38 -04:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2020-06-10 17:29:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-06-14 18:29:19 -04:00
|
|
|
async fn lint_files(
|
|
|
|
factory: CliFactory,
|
|
|
|
lint_options: LintOptions,
|
|
|
|
paths: Vec<PathBuf>,
|
|
|
|
) -> Result<bool, AnyError> {
|
|
|
|
let caches = factory.caches()?;
|
|
|
|
let lint_rules = get_config_rules_err_empty(lint_options.rules)?;
|
|
|
|
let incremental_cache = Arc::new(IncrementalCache::new(
|
|
|
|
caches.lint_incremental_cache_db(),
|
|
|
|
// 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 = lint_options.reporter_kind;
|
|
|
|
let reporter_lock =
|
|
|
|
Arc::new(Mutex::new(create_reporter(reporter_kind.clone())));
|
|
|
|
let has_error = Arc::new(AtomicFlag::default());
|
|
|
|
|
|
|
|
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, file_text, lint_rules);
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let success = handle_lint_result(
|
|
|
|
&file_path.to_string_lossy(),
|
|
|
|
r,
|
|
|
|
reporter_lock.clone(),
|
|
|
|
);
|
|
|
|
if !success {
|
|
|
|
has_error.raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
incremental_cache.wait_completion().await;
|
|
|
|
reporter_lock.lock().unwrap().close(target_files_len);
|
|
|
|
|
|
|
|
Ok(!has_error.is_raised())
|
|
|
|
}
|
|
|
|
|
2023-01-07 15:22:09 -05:00
|
|
|
fn collect_lint_files(files: &FilesConfig) -> Result<Vec<PathBuf>, AnyError> {
|
2022-12-07 13:10:10 -05:00
|
|
|
FileCollector::new(is_supported_ext)
|
|
|
|
.ignore_git_folder()
|
|
|
|
.ignore_node_modules()
|
2023-01-07 15:22:09 -05:00
|
|
|
.add_ignore_paths(&files.exclude)
|
|
|
|
.collect_files(&files.include)
|
2022-12-07 13:10:10 -05:00
|
|
|
}
|
|
|
|
|
2020-11-14 14:51:30 -05:00
|
|
|
pub fn print_rules_list(json: bool) {
|
2020-08-29 06:58:20 -04:00
|
|
|
let lint_rules = rules::get_recommended_rules();
|
2020-06-12 10:42:12 -04:00
|
|
|
|
2020-11-14 14:51:30 -05:00
|
|
|
if json {
|
2021-09-08 01:08:33 -04:00
|
|
|
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();
|
2020-11-14 14:51:30 -05:00
|
|
|
let json_str = serde_json::to_string_pretty(&json_rules).unwrap();
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("{json_str}");
|
2020-11-14 14:51:30 -05:00
|
|
|
} else {
|
|
|
|
// The rules should still be printed even if `--quiet` option is enabled,
|
|
|
|
// so use `println!` here instead of `info!`.
|
|
|
|
println!("Available rules:");
|
2021-09-08 01:08:33 -04:00
|
|
|
for rule in lint_rules.iter() {
|
2020-11-14 14:51:30 -05:00
|
|
|
println!(" - {}", rule.code());
|
2021-08-12 13:15:31 -04:00
|
|
|
println!(" help: https://lint.deno.land/#{}", rule.code());
|
|
|
|
println!();
|
2020-11-14 14:51:30 -05:00
|
|
|
}
|
2020-06-12 10:42:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 01:08:33 -04:00
|
|
|
pub fn create_linter(
|
2021-10-12 09:58:04 -04:00
|
|
|
media_type: MediaType,
|
2023-03-02 16:50:17 -05:00
|
|
|
rules: Vec<&'static dyn LintRule>,
|
2021-09-08 01:08:33 -04:00
|
|
|
) -> Linter {
|
2020-07-01 10:04:56 -04:00
|
|
|
LinterBuilder::default()
|
2020-11-08 17:27:36 -05:00
|
|
|
.ignore_file_directive("deno-lint-ignore-file")
|
|
|
|
.ignore_diagnostic_directive("deno-lint-ignore")
|
2021-10-12 09:58:04 -04:00
|
|
|
.media_type(media_type)
|
2020-07-01 10:04:56 -04:00
|
|
|
.rules(rules)
|
|
|
|
.build()
|
2020-06-11 19:44:17 -04:00
|
|
|
}
|
|
|
|
|
2020-09-02 05:39:20 -04:00
|
|
|
fn lint_file(
|
2023-03-21 11:46:40 -04:00
|
|
|
file_path: &Path,
|
2022-04-19 22:14:00 -04:00
|
|
|
source_code: String,
|
2023-03-02 16:50:17 -05:00
|
|
|
lint_rules: Vec<&'static dyn LintRule>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(Vec<LintDiagnostic>, String), AnyError> {
|
2020-06-10 17:29:48 -04:00
|
|
|
let file_name = file_path.to_string_lossy().to_string();
|
2023-03-21 11:46:40 -04:00
|
|
|
let media_type = MediaType::from_path(file_path);
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2021-10-12 09:58:04 -04:00
|
|
|
let linter = create_linter(media_type, lint_rules);
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2020-11-08 17:27:36 -05:00
|
|
|
let (_, file_diagnostics) = linter.lint(file_name, source_code.clone())?;
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2020-09-02 05:39:20 -04:00
|
|
|
Ok((file_diagnostics, source_code))
|
2020-06-10 17:29:48 -04:00
|
|
|
}
|
|
|
|
|
2020-08-31 07:53:42 -04:00
|
|
|
/// Lint stdin and write result to stdout.
|
|
|
|
/// Treats input as TypeScript.
|
|
|
|
/// Compatible with `--json` flag.
|
2021-09-03 11:01:58 -04:00
|
|
|
fn lint_stdin(
|
2023-03-02 16:50:17 -05:00
|
|
|
lint_rules: Vec<&'static dyn LintRule>,
|
2021-09-08 01:08:33 -04:00
|
|
|
) -> Result<(Vec<LintDiagnostic>, String), AnyError> {
|
|
|
|
let mut source_code = String::new();
|
|
|
|
if stdin().read_to_string(&mut source_code).is_err() {
|
2020-09-14 12:48:57 -04:00
|
|
|
return Err(generic_error("Failed to read from stdin"));
|
2020-08-31 07:53:42 -04:00
|
|
|
}
|
|
|
|
|
2021-10-12 09:58:04 -04:00
|
|
|
let linter = create_linter(MediaType::TypeScript, lint_rules);
|
2021-09-08 01:08:33 -04:00
|
|
|
|
|
|
|
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>>>,
|
2023-06-14 18:29:19 -04:00
|
|
|
) -> bool {
|
2021-09-08 01:08:33 -04:00
|
|
|
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() {
|
|
|
|
reporter.visit_diagnostic(d, source.split('\n').collect());
|
2020-08-31 07:53:42 -04:00
|
|
|
}
|
2023-06-14 18:29:19 -04:00
|
|
|
file_diagnostics.is_empty()
|
2020-08-31 07:53:42 -04:00
|
|
|
}
|
|
|
|
Err(err) => {
|
2021-09-08 01:08:33 -04:00
|
|
|
reporter.visit_error(file_path, &err);
|
2023-06-14 18:29:19 -04:00
|
|
|
false
|
2020-08-31 07:53:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:30:46 -04:00
|
|
|
trait LintReporter {
|
2020-09-09 10:45:31 -04:00
|
|
|
fn visit_diagnostic(&mut self, d: &LintDiagnostic, source_lines: Vec<&str>);
|
2020-09-14 12:48:57 -04:00
|
|
|
fn visit_error(&mut self, file_path: &str, err: &AnyError);
|
2020-09-09 10:45:31 -04:00
|
|
|
fn close(&mut self, check_count: usize);
|
2020-08-13 11:30:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
2020-09-09 10:45:31 -04:00
|
|
|
fn visit_diagnostic(&mut self, d: &LintDiagnostic, source_lines: Vec<&str>) {
|
2020-08-13 11:30:46 -04:00
|
|
|
self.lint_count += 1;
|
|
|
|
|
2021-08-14 09:05:07 -04:00
|
|
|
let pretty_message = format!("({}) {}", colors::red(&d.code), &d.message);
|
2020-08-13 11:30:46 -04:00
|
|
|
|
2020-09-02 05:39:20 -04:00
|
|
|
let message = format_diagnostic(
|
2021-08-12 13:15:31 -04:00
|
|
|
&d.code,
|
2020-08-13 11:30:46 -04:00
|
|
|
&pretty_message,
|
2020-09-02 05:39:20 -04:00
|
|
|
&source_lines,
|
2023-01-13 16:39:19 -05:00
|
|
|
&d.range,
|
2020-10-18 10:26:05 -04:00
|
|
|
d.hint.as_ref(),
|
2022-09-02 16:53:23 -04:00
|
|
|
&format_location(&JsStackFrame::from_location(
|
2020-09-22 13:01:30 -04:00
|
|
|
Some(d.filename.clone()),
|
2022-12-17 16:00:33 -05:00
|
|
|
// todo(dsherret): these should use "display positions"
|
|
|
|
// which take into account the added column index of tab
|
|
|
|
// indentation
|
|
|
|
Some(d.range.start.line_index as i64 + 1),
|
|
|
|
Some(d.range.start.column_index as i64 + 1),
|
2020-09-22 13:01:30 -04:00
|
|
|
)),
|
2020-08-13 11:30:46 -04:00
|
|
|
);
|
|
|
|
|
2023-01-27 10:43:16 -05:00
|
|
|
eprintln!("{message}\n");
|
2020-08-13 11:30:46 -04:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
fn visit_error(&mut self, file_path: &str, err: &AnyError) {
|
2023-01-27 10:43:16 -05:00
|
|
|
eprintln!("Error linting: {file_path}");
|
|
|
|
eprintln!(" {err}");
|
2020-08-13 11:30:46 -04:00
|
|
|
}
|
|
|
|
|
2020-09-09 10:45:31 -04:00
|
|
|
fn close(&mut self, check_count: usize) {
|
2020-08-13 11:30:46 -04:00
|
|
|
match self.lint_count {
|
2020-09-20 07:49:22 -04:00
|
|
|
1 => info!("Found 1 problem"),
|
|
|
|
n if n > 1 => info!("Found {} problems", self.lint_count),
|
2020-08-13 11:30:46 -04:00
|
|
|
_ => (),
|
|
|
|
}
|
2020-09-09 10:45:31 -04:00
|
|
|
|
|
|
|
match check_count {
|
2020-09-20 07:49:22 -04:00
|
|
|
n if n <= 1 => info!("Checked {} file", n),
|
|
|
|
n if n > 1 => info!("Checked {} files", n),
|
|
|
|
_ => unreachable!(),
|
2020-09-09 10:45:31 -04:00
|
|
|
}
|
2020-08-13 11:30:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 12:47:48 -04:00
|
|
|
struct CompactLintReporter {
|
|
|
|
lint_count: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CompactLintReporter {
|
|
|
|
fn new() -> CompactLintReporter {
|
|
|
|
CompactLintReporter { lint_count: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintReporter for CompactLintReporter {
|
|
|
|
fn visit_diagnostic(&mut self, d: &LintDiagnostic, _source_lines: Vec<&str>) {
|
|
|
|
self.lint_count += 1;
|
|
|
|
|
|
|
|
eprintln!(
|
|
|
|
"{}: line {}, col {} - {} ({})",
|
|
|
|
d.filename,
|
|
|
|
d.range.start.line_index + 1,
|
|
|
|
d.range.start.column_index + 1,
|
|
|
|
d.message,
|
|
|
|
d.code
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_error(&mut self, file_path: &str, err: &AnyError) {
|
2023-01-27 10:43:16 -05:00
|
|
|
eprintln!("Error linting: {file_path}");
|
|
|
|
eprintln!(" {err}");
|
2022-09-28 12:47:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 05:39:20 -04:00
|
|
|
pub fn format_diagnostic(
|
2021-08-12 13:15:31 -04:00
|
|
|
diagnostic_code: &str,
|
2020-09-02 05:39:20 -04:00
|
|
|
message_line: &str,
|
|
|
|
source_lines: &[&str],
|
2023-01-13 16:39:19 -05:00
|
|
|
range: &deno_lint::diagnostic::Range,
|
2020-10-18 10:26:05 -04:00
|
|
|
maybe_hint: Option<&String>,
|
2020-09-02 05:39:20 -04:00
|
|
|
formatted_location: &str,
|
|
|
|
) -> String {
|
|
|
|
let mut lines = vec![];
|
|
|
|
|
2021-09-02 11:38:19 -04:00
|
|
|
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 {
|
2020-09-02 05:39:20 -04:00
|
|
|
lines.push(format!(
|
|
|
|
"{}{}",
|
2021-09-02 11:38:19 -04:00
|
|
|
" ".repeat(range.start.column_index),
|
|
|
|
colors::red(
|
|
|
|
&"^".repeat(range.end.column_index - range.start.column_index)
|
|
|
|
)
|
2020-09-02 05:39:20 -04:00
|
|
|
));
|
|
|
|
} else {
|
2021-09-02 11:38:19 -04:00
|
|
|
let line_len = line.len();
|
|
|
|
if range.start.line_index == i {
|
2020-09-02 05:39:20 -04:00
|
|
|
lines.push(format!(
|
|
|
|
"{}{}",
|
2021-09-02 11:38:19 -04:00
|
|
|
" ".repeat(range.start.column_index),
|
|
|
|
colors::red(&"^".repeat(line_len - range.start.column_index))
|
2020-09-02 05:39:20 -04:00
|
|
|
));
|
2021-09-02 11:38:19 -04:00
|
|
|
} else if range.end.line_index == i {
|
|
|
|
lines
|
|
|
|
.push(colors::red(&"^".repeat(range.end.column_index)).to_string());
|
2020-09-02 05:39:20 -04:00
|
|
|
} else if line_len != 0 {
|
2020-11-09 09:38:29 -05:00
|
|
|
lines.push(colors::red(&"^".repeat(line_len)).to_string());
|
2020-09-02 05:39:20 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-14 09:05:07 -04:00
|
|
|
let hint = if let Some(hint) = maybe_hint {
|
|
|
|
format!(" {} {}\n", colors::cyan("hint:"), hint)
|
2020-10-18 10:26:05 -04:00
|
|
|
} else {
|
2021-08-14 09:05:07 -04:00
|
|
|
"".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
|
|
|
|
)
|
2020-09-02 05:39:20 -04:00
|
|
|
}
|
|
|
|
|
2020-08-13 11:30:46 -04:00
|
|
|
#[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 {
|
2020-09-09 10:45:31 -04:00
|
|
|
fn visit_diagnostic(&mut self, d: &LintDiagnostic, _source_lines: Vec<&str>) {
|
2020-08-13 11:30:46 -04:00
|
|
|
self.diagnostics.push(d.clone());
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
fn visit_error(&mut self, file_path: &str, err: &AnyError) {
|
2020-08-13 11:30:46 -04:00
|
|
|
self.errors.push(LintError {
|
|
|
|
file_path: file_path.to_string(),
|
|
|
|
message: err.to_string(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-09 10:45:31 -04:00
|
|
|
fn close(&mut self, _check_count: usize) {
|
2020-09-02 05:39:20 -04:00
|
|
|
sort_diagnostics(&mut self.diagnostics);
|
2020-08-13 11:30:46 -04:00
|
|
|
let json = serde_json::to_string_pretty(&self);
|
2021-07-07 14:39:24 -04:00
|
|
|
println!("{}", json.unwrap());
|
2020-08-13 11:30:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 19:12:55 -04:00
|
|
|
fn sort_diagnostics(diagnostics: &mut [LintDiagnostic]) {
|
2020-09-02 05:39:20 -04:00
|
|
|
// 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 => {
|
2021-09-02 11:38:19 -04:00
|
|
|
let line_order =
|
|
|
|
a.range.start.line_index.cmp(&b.range.start.line_index);
|
2020-09-02 05:39:20 -04:00
|
|
|
match line_order {
|
2021-09-02 11:38:19 -04:00
|
|
|
Ordering::Equal => {
|
|
|
|
a.range.start.column_index.cmp(&b.range.start.column_index)
|
|
|
|
}
|
2020-09-02 05:39:20 -04:00
|
|
|
_ => line_order,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => file_order,
|
|
|
|
}
|
|
|
|
});
|
2020-06-10 17:29:48 -04:00
|
|
|
}
|
2021-09-03 11:01:58 -04:00
|
|
|
|
2023-06-14 18:29:19 -04:00
|
|
|
fn get_config_rules_err_empty(
|
|
|
|
rules: LintRulesConfig,
|
|
|
|
) -> Result<Vec<&'static dyn LintRule>, AnyError> {
|
|
|
|
let lint_rules = get_configured_rules(rules);
|
|
|
|
if lint_rules.is_empty() {
|
|
|
|
bail!("No rules have been configured")
|
|
|
|
}
|
|
|
|
Ok(lint_rules)
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:50:17 -05:00
|
|
|
pub fn get_configured_rules(
|
|
|
|
rules: LintRulesConfig,
|
|
|
|
) -> Vec<&'static dyn LintRule> {
|
2023-01-07 15:22:09 -05:00
|
|
|
if rules.tags.is_none() && rules.include.is_none() && rules.exclude.is_none()
|
2021-09-03 11:01:58 -04:00
|
|
|
{
|
2023-01-07 15:22:09 -05:00
|
|
|
rules::get_recommended_rules()
|
2021-09-03 11:01:58 -04:00
|
|
|
} else {
|
2023-01-07 15:22:09 -05:00
|
|
|
rules::get_filtered_rules(
|
|
|
|
rules.tags.or_else(|| Some(vec!["recommended".to_string()])),
|
|
|
|
rules.exclude,
|
|
|
|
rules.include,
|
|
|
|
)
|
2021-09-03 11:01:58 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-08 20:10:33 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use deno_lint::rules::get_recommended_rules;
|
|
|
|
|
|
|
|
use super::*;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::LintRulesConfig;
|
2021-11-08 20:10:33 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recommended_rules_when_no_tags_in_config() {
|
2023-01-07 15:22:09 -05:00
|
|
|
let rules_config = LintRulesConfig {
|
|
|
|
exclude: Some(vec!["no-debugger".to_string()]),
|
|
|
|
include: None,
|
|
|
|
tags: None,
|
2021-11-08 20:10:33 -05:00
|
|
|
};
|
2023-01-07 15:22:09 -05:00
|
|
|
let rules = get_configured_rules(rules_config);
|
2021-11-08 20:10:33 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|