2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2021-01-26 22:41:48 -05:00
|
|
|
//! This module provides file formatting utilities using
|
2020-06-10 17:29:48 -04:00
|
|
|
//! [`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.
|
2020-09-14 08:27:44 -04:00
|
|
|
use crate::ast;
|
2020-06-10 17:29:48 -04:00
|
|
|
use crate::colors;
|
|
|
|
use crate::fmt_errors;
|
2020-11-16 14:48:50 -05:00
|
|
|
use crate::fs_util::{collect_files, is_supported_ext};
|
2020-09-15 10:33:14 -04:00
|
|
|
use crate::media_type::MediaType;
|
2020-11-19 13:19:34 -05:00
|
|
|
use crate::tools::fmt::run_parallelized;
|
2020-09-22 13:01:30 -04:00
|
|
|
use deno_core::error::{generic_error, AnyError, 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;
|
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;
|
2020-08-31 07:53:42 -04:00
|
|
|
use std::io::{stdin, Read};
|
2020-06-10 17:29:48 -04:00
|
|
|
use std::path::PathBuf;
|
2020-08-13 11:30:46 -04:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
2020-06-11 19:44:17 -04:00
|
|
|
use std::sync::{Arc, Mutex};
|
2020-07-31 10:59:22 -04:00
|
|
|
use swc_ecmascript::parser::Syntax;
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2020-08-13 11:30:46 -04:00
|
|
|
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()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-12 09:47:44 -04:00
|
|
|
pub async fn lint_files(
|
2020-10-21 07:12:01 -04:00
|
|
|
args: Vec<PathBuf>,
|
|
|
|
ignore: Vec<PathBuf>,
|
2020-08-13 11:30:46 -04:00
|
|
|
json: bool,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-10-21 07:12:01 -04:00
|
|
|
if args.len() == 1 && args[0].to_string_lossy() == "-" {
|
2020-08-31 07:53:42 -04:00
|
|
|
return lint_stdin(json);
|
|
|
|
}
|
2021-02-19 07:18:16 -05:00
|
|
|
let target_files =
|
|
|
|
collect_files(&args, &ignore, is_supported_ext).and_then(|files| {
|
|
|
|
if files.is_empty() {
|
|
|
|
Err(generic_error("No target files found."))
|
|
|
|
} else {
|
|
|
|
Ok(files)
|
|
|
|
}
|
|
|
|
})?;
|
2020-06-11 19:44:17 -04:00
|
|
|
debug!("Found {} files", target_files.len());
|
2020-09-09 10:45:31 -04:00
|
|
|
let target_files_len = target_files.len();
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2020-08-13 11:30:46 -04:00
|
|
|
let has_error = Arc::new(AtomicBool::new(false));
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2020-08-13 11:30:46 -04:00
|
|
|
let reporter_kind = if json {
|
|
|
|
LintReporterKind::Json
|
|
|
|
} else {
|
|
|
|
LintReporterKind::Pretty
|
|
|
|
};
|
|
|
|
let reporter_lock = Arc::new(Mutex::new(create_reporter(reporter_kind)));
|
2020-06-11 19:44:17 -04:00
|
|
|
|
|
|
|
run_parallelized(target_files, {
|
2020-08-13 11:30:46 -04:00
|
|
|
let reporter_lock = reporter_lock.clone();
|
|
|
|
let has_error = has_error.clone();
|
2020-06-11 19:44:17 -04:00
|
|
|
move |file_path| {
|
|
|
|
let r = lint_file(file_path.clone());
|
2020-08-13 11:30:46 -04:00
|
|
|
let mut reporter = reporter_lock.lock().unwrap();
|
2020-06-11 19:44:17 -04:00
|
|
|
|
|
|
|
match r {
|
2020-09-02 05:39:20 -04:00
|
|
|
Ok((mut file_diagnostics, source)) => {
|
|
|
|
sort_diagnostics(&mut file_diagnostics);
|
2020-06-11 19:44:17 -04:00
|
|
|
for d in file_diagnostics.iter() {
|
2020-08-13 11:30:46 -04:00
|
|
|
has_error.store(true, Ordering::Relaxed);
|
2021-07-30 09:03:41 -04:00
|
|
|
reporter.visit_diagnostic(d, source.split('\n').collect());
|
2020-06-11 19:44:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2020-08-13 11:30:46 -04:00
|
|
|
has_error.store(true, Ordering::Relaxed);
|
|
|
|
reporter.visit_error(&file_path.to_string_lossy().to_string(), &err);
|
2020-06-11 19:44:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2020-06-10 17:29:48 -04:00
|
|
|
}
|
2020-06-11 19:44:17 -04:00
|
|
|
})
|
|
|
|
.await?;
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2020-08-13 11:30:46 -04:00
|
|
|
let has_error = has_error.load(Ordering::Relaxed);
|
|
|
|
|
2020-09-09 10:45:31 -04:00
|
|
|
reporter_lock.lock().unwrap().close(target_files_len);
|
2020-08-13 11:30:46 -04:00
|
|
|
|
|
|
|
if has_error {
|
2020-06-10 17:29:48 -04:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-14 14:51:30 -05:00
|
|
|
fn rule_to_json(rule: Box<dyn LintRule>) -> serde_json::Value {
|
|
|
|
serde_json::json!({
|
|
|
|
"code": rule.code(),
|
|
|
|
"tags": rule.tags(),
|
|
|
|
"docs": rule.docs(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
let json_rules: Vec<serde_json::Value> =
|
|
|
|
lint_rules.into_iter().map(rule_to_json).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 {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
pub fn create_linter(syntax: Syntax, rules: Vec<Box<dyn LintRule>>) -> 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")
|
2020-07-01 10:04:56 -04:00
|
|
|
.syntax(syntax)
|
|
|
|
.rules(rules)
|
|
|
|
.build()
|
2020-06-11 19:44:17 -04:00
|
|
|
}
|
|
|
|
|
2020-09-02 05:39:20 -04:00
|
|
|
fn lint_file(
|
|
|
|
file_path: PathBuf,
|
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();
|
|
|
|
let source_code = fs::read_to_string(&file_path)?;
|
2020-09-15 10:33:14 -04:00
|
|
|
let media_type = MediaType::from(&file_path);
|
2020-09-14 08:27:44 -04:00
|
|
|
let syntax = ast::get_syntax(&media_type);
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2020-08-29 06:58:20 -04:00
|
|
|
let lint_rules = rules::get_recommended_rules();
|
2021-05-10 16:39:16 -04:00
|
|
|
let linter = create_linter(syntax, 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.
|
2020-09-14 12:48:57 -04:00
|
|
|
fn lint_stdin(json: bool) -> Result<(), AnyError> {
|
2020-08-31 07:53:42 -04:00
|
|
|
let mut source = String::new();
|
|
|
|
if stdin().read_to_string(&mut source).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
|
|
|
}
|
|
|
|
|
|
|
|
let reporter_kind = if json {
|
|
|
|
LintReporterKind::Json
|
|
|
|
} else {
|
|
|
|
LintReporterKind::Pretty
|
|
|
|
};
|
|
|
|
let mut reporter = create_reporter(reporter_kind);
|
|
|
|
let lint_rules = rules::get_recommended_rules();
|
2020-09-15 10:33:14 -04:00
|
|
|
let syntax = ast::get_syntax(&MediaType::TypeScript);
|
2021-05-10 16:39:16 -04:00
|
|
|
let linter = create_linter(syntax, lint_rules);
|
2020-08-31 07:53:42 -04:00
|
|
|
let mut has_error = false;
|
|
|
|
let pseudo_file_name = "_stdin.ts";
|
|
|
|
match linter
|
2020-11-08 17:27:36 -05:00
|
|
|
.lint(pseudo_file_name.to_string(), source.clone())
|
2020-08-31 07:53:42 -04:00
|
|
|
.map_err(|e| e.into())
|
|
|
|
{
|
2020-11-08 17:27:36 -05:00
|
|
|
Ok((_, diagnostics)) => {
|
2020-08-31 07:53:42 -04:00
|
|
|
for d in diagnostics {
|
|
|
|
has_error = true;
|
2020-09-09 10:45:31 -04:00
|
|
|
reporter.visit_diagnostic(&d, source.split('\n').collect());
|
2020-08-31 07:53:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
has_error = true;
|
|
|
|
reporter.visit_error(pseudo_file_name, &err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 10:45:31 -04:00
|
|
|
reporter.close(1);
|
2020-08-31 07:53:42 -04:00
|
|
|
|
|
|
|
if has_error {
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
d.range.clone(),
|
2020-10-18 10:26:05 -04:00
|
|
|
d.hint.as_ref(),
|
2020-09-22 13:01:30 -04:00
|
|
|
&fmt_errors::format_location(&JsStackFrame::from_location(
|
|
|
|
Some(d.filename.clone()),
|
|
|
|
Some(d.range.start.line as i64),
|
|
|
|
Some(d.range.start.col as i64),
|
|
|
|
)),
|
2020-08-13 11:30:46 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
eprintln!("{}\n", message);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
eprintln!("Error linting: {}", file_path);
|
|
|
|
eprintln!(" {}", err);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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],
|
|
|
|
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![];
|
|
|
|
|
|
|
|
for i in range.start.line..=range.end.line {
|
|
|
|
lines.push(source_lines[i - 1].to_string());
|
|
|
|
if range.start.line == range.end.line {
|
|
|
|
lines.push(format!(
|
|
|
|
"{}{}",
|
|
|
|
" ".repeat(range.start.col),
|
|
|
|
colors::red(&"^".repeat(range.end.col - range.start.col))
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
let line_len = source_lines[i - 1].len();
|
|
|
|
if range.start.line == i {
|
|
|
|
lines.push(format!(
|
|
|
|
"{}{}",
|
|
|
|
" ".repeat(range.start.col),
|
|
|
|
colors::red(&"^".repeat(line_len - range.start.col))
|
|
|
|
));
|
|
|
|
} else if range.end.line == i {
|
2020-11-09 09:38:29 -05:00
|
|
|
lines.push(colors::red(&"^".repeat(range.end.col)).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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 05:39:20 -04:00
|
|
|
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.cmp(&b.range.start.line);
|
|
|
|
match line_order {
|
|
|
|
Ordering::Equal => a.range.start.col.cmp(&b.range.start.col),
|
|
|
|
_ => line_order,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => file_order,
|
|
|
|
}
|
|
|
|
});
|
2020-06-10 17:29:48 -04:00
|
|
|
}
|