2020-06-10 17:29:48 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
//! This module provides file formating 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::colors;
|
|
|
|
use crate::file_fetcher::map_file_extension;
|
|
|
|
use crate::fmt::collect_files;
|
2020-06-11 19:44:17 -04:00
|
|
|
use crate::fmt::run_parallelized;
|
2020-06-10 17:29:48 -04:00
|
|
|
use crate::fmt_errors;
|
|
|
|
use crate::swc_util;
|
|
|
|
use deno_core::ErrBox;
|
|
|
|
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;
|
2020-08-13 11:30:46 -04:00
|
|
|
use serde::Serialize;
|
2020-06-10 17:29:48 -04:00
|
|
|
use std::fs;
|
|
|
|
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(
|
|
|
|
args: Vec<String>,
|
|
|
|
ignore: Vec<String>,
|
2020-08-13 11:30:46 -04:00
|
|
|
json: bool,
|
2020-08-12 09:47:44 -04:00
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
let mut target_files = collect_files(args)?;
|
|
|
|
if !ignore.is_empty() {
|
|
|
|
// collect all files to be ignored
|
|
|
|
// and retain only files that should be linted.
|
|
|
|
let ignore_files = collect_files(ignore)?;
|
|
|
|
target_files.retain(|f| !ignore_files.contains(&f));
|
|
|
|
}
|
2020-06-11 19:44:17 -04:00
|
|
|
debug!("Found {} files", 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 {
|
|
|
|
Ok(file_diagnostics) => {
|
|
|
|
for d in file_diagnostics.iter() {
|
2020-08-13 11:30:46 -04:00
|
|
|
has_error.store(true, Ordering::Relaxed);
|
|
|
|
reporter.visit(&d);
|
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);
|
|
|
|
|
|
|
|
reporter_lock.lock().unwrap().close();
|
|
|
|
|
|
|
|
if has_error {
|
2020-06-10 17:29:48 -04:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-06-12 10:42:12 -04:00
|
|
|
pub fn print_rules_list() {
|
2020-08-29 06:58:20 -04:00
|
|
|
let lint_rules = rules::get_recommended_rules();
|
2020-06-12 10:42:12 -04:00
|
|
|
|
|
|
|
println!("Available rules:");
|
|
|
|
for rule in lint_rules {
|
|
|
|
println!(" - {}", rule.code());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 10:04:56 -04:00
|
|
|
fn create_linter(syntax: Syntax, rules: Vec<Box<dyn LintRule>>) -> Linter {
|
|
|
|
LinterBuilder::default()
|
|
|
|
.ignore_file_directives(vec!["deno-lint-ignore-file"])
|
|
|
|
.ignore_diagnostic_directives(vec![
|
|
|
|
"deno-lint-ignore",
|
|
|
|
"eslint-disable-next-line",
|
|
|
|
])
|
|
|
|
.lint_unused_ignore_directives(true)
|
|
|
|
// TODO(bartlomieju): switch to true
|
|
|
|
.lint_unknown_rules(false)
|
|
|
|
.syntax(syntax)
|
|
|
|
.rules(rules)
|
|
|
|
.build()
|
2020-06-11 19:44:17 -04:00
|
|
|
}
|
|
|
|
|
2020-06-10 17:29:48 -04:00
|
|
|
fn lint_file(file_path: PathBuf) -> Result<Vec<LintDiagnostic>, ErrBox> {
|
|
|
|
let file_name = file_path.to_string_lossy().to_string();
|
|
|
|
let source_code = fs::read_to_string(&file_path)?;
|
|
|
|
let media_type = map_file_extension(&file_path);
|
|
|
|
let syntax = swc_util::get_syntax_for_media_type(media_type);
|
|
|
|
|
2020-08-29 06:58:20 -04:00
|
|
|
let lint_rules = rules::get_recommended_rules();
|
2020-07-01 10:04:56 -04:00
|
|
|
let mut linter = create_linter(syntax, lint_rules);
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2020-07-01 10:04:56 -04:00
|
|
|
let file_diagnostics = linter.lint(file_name, source_code)?;
|
2020-06-10 17:29:48 -04:00
|
|
|
|
|
|
|
Ok(file_diagnostics)
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:30:46 -04:00
|
|
|
trait LintReporter {
|
|
|
|
fn visit(&mut self, d: &LintDiagnostic);
|
|
|
|
fn visit_error(&mut self, file_path: &str, err: &ErrBox);
|
|
|
|
fn close(&mut self);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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(&mut self, d: &LintDiagnostic) {
|
|
|
|
self.lint_count += 1;
|
|
|
|
|
|
|
|
let pretty_message =
|
|
|
|
format!("({}) {}", colors::gray(&d.code), d.message.clone());
|
|
|
|
|
|
|
|
let message = fmt_errors::format_stack(
|
|
|
|
true,
|
|
|
|
&pretty_message,
|
|
|
|
Some(&d.line_src),
|
|
|
|
Some(d.location.col as i64),
|
|
|
|
Some((d.location.col + d.snippet_length) as i64),
|
|
|
|
&[fmt_errors::format_location(
|
2020-08-26 14:43:24 -04:00
|
|
|
&d.filename,
|
2020-08-13 11:30:46 -04:00
|
|
|
d.location.line as i64,
|
|
|
|
d.location.col as i64,
|
|
|
|
)],
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
|
|
|
|
eprintln!("{}\n", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_error(&mut self, file_path: &str, err: &ErrBox) {
|
|
|
|
eprintln!("Error linting: {}", file_path);
|
|
|
|
eprintln!(" {}", err);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(&mut self) {
|
|
|
|
match self.lint_count {
|
|
|
|
1 => eprintln!("Found 1 problem"),
|
|
|
|
n if n > 1 => eprintln!("Found {} problems", self.lint_count),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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(&mut self, d: &LintDiagnostic) {
|
|
|
|
self.diagnostics.push(d.clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_error(&mut self, file_path: &str, err: &ErrBox) {
|
|
|
|
self.errors.push(LintError {
|
|
|
|
file_path: file_path.to_string(),
|
|
|
|
message: err.to_string(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(&mut self) {
|
|
|
|
// Sort so that we guarantee a deterministic output which is useful for tests
|
2020-08-14 06:40:29 -04:00
|
|
|
self.diagnostics.sort_by_key(|key| get_sort_key(&key));
|
2020-08-13 11:30:46 -04:00
|
|
|
|
|
|
|
let json = serde_json::to_string_pretty(&self);
|
|
|
|
eprintln!("{}", json.unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_sort_key(a: &LintDiagnostic) -> String {
|
|
|
|
let location = &a.location;
|
|
|
|
|
2020-08-26 14:43:24 -04:00
|
|
|
return format!("{}:{}:{}", a.filename, location.line, location.col);
|
2020-06-10 17:29:48 -04:00
|
|
|
}
|