2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 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).
|
2024-02-08 20:40:26 -05:00
|
|
|
use deno_ast::diagnostics::Diagnostic;
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2024-02-19 10:28:41 -05:00
|
|
|
use deno_ast::ModuleSpecifier;
|
2024-01-23 10:37:43 -05:00
|
|
|
use deno_ast::ParsedSource;
|
2024-02-19 10:28:41 -05:00
|
|
|
use deno_ast::SourceRange;
|
|
|
|
use deno_ast::SourceTextInfo;
|
2024-01-15 19:15:39 -05:00
|
|
|
use deno_config::glob::FilePatterns;
|
2023-01-07 15:22:09 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2024-03-21 17:18:59 -04:00
|
|
|
use deno_core::anyhow::Context;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::error::generic_error;
|
|
|
|
use deno_core::error::AnyError;
|
2024-02-19 10:28:41 -05:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json;
|
2024-02-19 10:28:41 -05:00
|
|
|
use deno_graph::FastCheckDiagnostic;
|
2020-06-10 17:29:48 -04:00
|
|
|
use deno_lint::diagnostic::LintDiagnostic;
|
2024-05-29 20:09:16 -04:00
|
|
|
use deno_lint::linter::LintConfig;
|
2024-01-09 18:20:52 -05:00
|
|
|
use deno_lint::linter::LintFileOptions;
|
2020-06-10 17:29:48 -04:00
|
|
|
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;
|
2024-02-19 10:28:41 -05:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::collections::HashSet;
|
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;
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2024-03-27 14:25:39 -04:00
|
|
|
use crate::args::CliOptions;
|
2024-02-19 10:28:41 -05:00
|
|
|
use crate::args::Flags;
|
|
|
|
use crate::args::LintFlags;
|
|
|
|
use crate::args::LintOptions;
|
|
|
|
use crate::args::LintReporterKind;
|
|
|
|
use crate::args::LintRulesConfig;
|
2022-07-12 18:58:39 -04:00
|
|
|
use crate::cache::IncrementalCache;
|
2024-02-19 10:28:41 -05:00
|
|
|
use crate::colors;
|
|
|
|
use crate::factory::CliFactory;
|
|
|
|
use crate::tools::fmt::run_parallelized;
|
|
|
|
use crate::util::file_watcher;
|
|
|
|
use crate::util::fs::canonicalize_path;
|
|
|
|
use crate::util::fs::specifier_from_file_path;
|
|
|
|
use crate::util::fs::FileCollector;
|
|
|
|
use crate::util::path::is_script_ext;
|
|
|
|
use crate::util::sync::AtomicFlag;
|
|
|
|
|
|
|
|
pub mod no_slow_types;
|
2022-04-19 22:14:00 -04:00
|
|
|
|
2024-01-23 10:37:43 -05:00
|
|
|
static STDIN_FILE_NAME: &str = "$deno$stdin.ts";
|
2021-09-08 01:08:33 -04:00
|
|
|
|
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> {
|
2023-06-15 13:09:37 -04:00
|
|
|
if let Some(watch_flags) = &lint_flags.watch {
|
2023-06-14 18:29:19 -04:00
|
|
|
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.",
|
|
|
|
));
|
|
|
|
}
|
2022-01-31 11:39:39 -05:00
|
|
|
file_watcher::watch_func(
|
2023-06-14 18:29:19 -04:00
|
|
|
flags,
|
2023-10-30 20:25:58 -04:00
|
|
|
file_watcher::PrintConfig::new("Lint", !watch_flags.no_clear_screen),
|
2023-10-19 01:05:00 -04:00
|
|
|
move |flags, watcher_communicator, changed_paths| {
|
2023-06-14 18:29:19 -04:00
|
|
|
let lint_flags = lint_flags.clone();
|
|
|
|
Ok(async move {
|
2024-03-11 23:48:00 -04:00
|
|
|
let factory = CliFactory::from_flags(flags)?;
|
2023-06-14 18:29:19 -04:00
|
|
|
let cli_options = factory.cli_options();
|
|
|
|
let lint_options = cli_options.resolve_lint_options(lint_flags)?;
|
2024-05-29 20:09:16 -04:00
|
|
|
let lint_config = cli_options.resolve_lint_config()?;
|
2024-03-27 14:25:39 -04:00
|
|
|
let files =
|
|
|
|
collect_lint_files(cli_options, lint_options.files.clone())
|
|
|
|
.and_then(|files| {
|
|
|
|
if files.is_empty() {
|
|
|
|
Err(generic_error("No target files found."))
|
|
|
|
} else {
|
|
|
|
Ok(files)
|
|
|
|
}
|
|
|
|
})?;
|
2023-10-19 01:05:00 -04:00
|
|
|
_ = watcher_communicator.watch_paths(files.clone());
|
2023-06-14 18:29:19 -04:00
|
|
|
|
|
|
|
let lint_paths = if let Some(paths) = changed_paths {
|
|
|
|
// lint all files on any changed (https://github.com/denoland/deno/issues/12446)
|
|
|
|
files
|
|
|
|
.iter()
|
2024-01-08 12:18:42 -05:00
|
|
|
.any(|path| {
|
|
|
|
canonicalize_path(path)
|
|
|
|
.map(|p| paths.contains(&p))
|
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
2023-06-14 18:29:19 -04:00
|
|
|
.then_some(files)
|
|
|
|
.unwrap_or_else(|| [].to_vec())
|
|
|
|
} else {
|
|
|
|
files
|
|
|
|
};
|
|
|
|
|
2024-05-29 20:09:16 -04:00
|
|
|
lint_files(factory, lint_options, lint_config, lint_paths).await?;
|
2023-06-14 18:29:19 -04:00
|
|
|
Ok(())
|
|
|
|
})
|
2022-01-31 11:39:39 -05:00
|
|
|
},
|
|
|
|
)
|
|
|
|
.await?;
|
2021-10-05 17:07:38 -04:00
|
|
|
} else {
|
2024-03-11 23:48:00 -04:00
|
|
|
let factory = CliFactory::from_flags(flags)?;
|
2023-06-14 18:29:19 -04:00
|
|
|
let cli_options = factory.cli_options();
|
|
|
|
let is_stdin = lint_flags.is_stdin();
|
|
|
|
let lint_options = cli_options.resolve_lint_options(lint_flags)?;
|
2024-05-29 20:09:16 -04:00
|
|
|
let lint_config = cli_options.resolve_lint_config()?;
|
2023-06-14 18:29:19 -04:00
|
|
|
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)));
|
2024-02-19 10:28:41 -05:00
|
|
|
let lint_rules = get_config_rules_err_empty(
|
|
|
|
lint_options.rules,
|
|
|
|
cli_options.maybe_config_file().as_ref(),
|
|
|
|
)?;
|
2024-01-23 10:37:43 -05:00
|
|
|
let file_path = cli_options.initial_cwd().join(STDIN_FILE_NAME);
|
2024-05-29 20:09:16 -04:00
|
|
|
let r = lint_stdin(&file_path, lint_rules.rules, lint_config);
|
2024-02-08 20:40:26 -05:00
|
|
|
let success = handle_lint_result(
|
|
|
|
&file_path.to_string_lossy(),
|
|
|
|
r,
|
|
|
|
reporter_lock.clone(),
|
|
|
|
);
|
2024-02-19 10:28:41 -05:00
|
|
|
reporter_lock.lock().close(1);
|
2023-06-14 18:29:19 -04:00
|
|
|
success
|
2021-09-08 01:08:33 -04:00
|
|
|
} else {
|
2024-03-27 14:25:39 -04:00
|
|
|
let target_files = collect_lint_files(cli_options, files.clone())
|
|
|
|
.and_then(|files| {
|
2024-01-08 12:18:42 -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());
|
2024-05-29 20:09:16 -04:00
|
|
|
lint_files(factory, lint_options, lint_config, 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,
|
2024-05-29 20:09:16 -04:00
|
|
|
lint_config: LintConfig,
|
2023-06-14 18:29:19 -04:00
|
|
|
paths: Vec<PathBuf>,
|
|
|
|
) -> Result<bool, AnyError> {
|
|
|
|
let caches = factory.caches()?;
|
2024-02-19 10:28:41 -05:00
|
|
|
let maybe_config_file = factory.cli_options().maybe_config_file().as_ref();
|
|
|
|
let lint_rules =
|
|
|
|
get_config_rules_err_empty(lint_options.rules, maybe_config_file)?;
|
2023-06-14 18:29:19 -04:00
|
|
|
let incremental_cache = Arc::new(IncrementalCache::new(
|
|
|
|
caches.lint_incremental_cache_db(),
|
2024-02-19 10:28:41 -05:00
|
|
|
&lint_rules.incremental_cache_state(),
|
2023-06-14 18:29:19 -04:00
|
|
|
&paths,
|
|
|
|
));
|
|
|
|
let target_files_len = paths.len();
|
|
|
|
let reporter_kind = lint_options.reporter_kind;
|
2024-02-19 10:28:41 -05:00
|
|
|
// todo(dsherret): abstract away this lock behind a performant interface
|
2023-06-14 18:29:19 -04:00
|
|
|
let reporter_lock =
|
|
|
|
Arc::new(Mutex::new(create_reporter(reporter_kind.clone())));
|
|
|
|
let has_error = Arc::new(AtomicFlag::default());
|
|
|
|
|
2024-02-19 10:28:41 -05:00
|
|
|
let mut futures = Vec::with_capacity(2);
|
|
|
|
if lint_rules.no_slow_types {
|
|
|
|
if let Some(config_file) = maybe_config_file {
|
|
|
|
let members = config_file.to_workspace_members()?;
|
|
|
|
let has_error = has_error.clone();
|
|
|
|
let reporter_lock = reporter_lock.clone();
|
2024-02-20 16:29:57 -05:00
|
|
|
let module_graph_creator = factory.module_graph_creator().await?.clone();
|
2024-02-19 10:28:41 -05:00
|
|
|
let path_urls = paths
|
|
|
|
.iter()
|
|
|
|
.filter_map(|p| ModuleSpecifier::from_file_path(p).ok())
|
|
|
|
.collect::<HashSet<_>>();
|
|
|
|
futures.push(deno_core::unsync::spawn(async move {
|
2024-03-07 11:30:30 -05:00
|
|
|
let graph = module_graph_creator
|
|
|
|
.create_and_validate_publish_graph(&members, true)
|
|
|
|
.await?;
|
2024-02-19 10:28:41 -05:00
|
|
|
// todo(dsherret): this isn't exactly correct as linting isn't properly
|
|
|
|
// setup to handle workspaces. Iterating over the workspace members
|
|
|
|
// should be done at a higher level because it also needs to take into
|
|
|
|
// account the config per workspace member.
|
|
|
|
for member in &members {
|
|
|
|
let export_urls = member.config_file.resolve_export_value_urls()?;
|
|
|
|
if !export_urls.iter().any(|url| path_urls.contains(url)) {
|
|
|
|
continue; // entrypoint is not specified, so skip
|
|
|
|
}
|
|
|
|
let diagnostics = no_slow_types::collect_no_slow_type_diagnostics(
|
|
|
|
&export_urls,
|
|
|
|
&graph,
|
|
|
|
);
|
|
|
|
if !diagnostics.is_empty() {
|
|
|
|
has_error.raise();
|
|
|
|
let mut reporter = reporter_lock.lock();
|
|
|
|
for diagnostic in &diagnostics {
|
|
|
|
reporter
|
|
|
|
.visit_diagnostic(LintOrCliDiagnostic::FastCheck(diagnostic));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
futures.push({
|
2023-06-14 18:29:19 -04:00
|
|
|
let has_error = has_error.clone();
|
2024-03-21 17:18:59 -04:00
|
|
|
let linter = create_linter(lint_rules.rules);
|
2023-06-14 18:29:19 -04:00
|
|
|
let reporter_lock = reporter_lock.clone();
|
|
|
|
let incremental_cache = incremental_cache.clone();
|
2024-05-29 20:09:16 -04:00
|
|
|
let lint_config = lint_config.clone();
|
2024-03-21 17:18:59 -04:00
|
|
|
let fix = lint_options.fix;
|
2024-02-19 10:28:41 -05:00
|
|
|
deno_core::unsync::spawn(async move {
|
|
|
|
run_parallelized(paths, {
|
|
|
|
move |file_path| {
|
2024-06-07 13:02:47 -04:00
|
|
|
let file_text = deno_ast::strip_bom(fs::read_to_string(&file_path)?);
|
2024-02-19 10:28:41 -05:00
|
|
|
|
|
|
|
// 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(());
|
|
|
|
}
|
2023-06-14 18:29:19 -04:00
|
|
|
|
2024-05-29 20:09:16 -04:00
|
|
|
let r = lint_file(&linter, &file_path, file_text, lint_config, fix);
|
2024-03-21 17:18:59 -04:00
|
|
|
if let Ok((file_source, file_diagnostics)) = &r {
|
2024-02-19 10:28:41 -05:00
|
|
|
if file_diagnostics.is_empty() {
|
|
|
|
// update the incremental cache if there were no diagnostics
|
2024-03-21 17:18:59 -04:00
|
|
|
incremental_cache.update_file(
|
|
|
|
&file_path,
|
|
|
|
// ensure the returned text is used here as it may have been modified via --fix
|
2024-06-05 11:04:16 -04:00
|
|
|
file_source.text(),
|
2024-03-21 17:18:59 -04:00
|
|
|
)
|
2024-02-19 10:28:41 -05:00
|
|
|
}
|
|
|
|
}
|
2023-06-14 18:29:19 -04:00
|
|
|
|
2024-02-19 10:28:41 -05:00
|
|
|
let success = handle_lint_result(
|
|
|
|
&file_path.to_string_lossy(),
|
|
|
|
r,
|
|
|
|
reporter_lock.clone(),
|
|
|
|
);
|
|
|
|
if !success {
|
|
|
|
has_error.raise();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2023-06-14 18:29:19 -04:00
|
|
|
}
|
2024-02-19 10:28:41 -05:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
})
|
|
|
|
});
|
2023-06-14 18:29:19 -04:00
|
|
|
|
2024-02-19 10:28:41 -05:00
|
|
|
deno_core::futures::future::try_join_all(futures).await?;
|
2023-06-14 18:29:19 -04:00
|
|
|
|
|
|
|
incremental_cache.wait_completion().await;
|
2024-02-19 10:28:41 -05:00
|
|
|
reporter_lock.lock().close(target_files_len);
|
2023-06-14 18:29:19 -04:00
|
|
|
|
|
|
|
Ok(!has_error.is_raised())
|
|
|
|
}
|
|
|
|
|
2024-03-27 14:25:39 -04:00
|
|
|
fn collect_lint_files(
|
|
|
|
cli_options: &CliOptions,
|
|
|
|
files: FilePatterns,
|
|
|
|
) -> Result<Vec<PathBuf>, AnyError> {
|
2024-03-07 20:16:32 -05:00
|
|
|
FileCollector::new(|e| is_script_ext(e.path))
|
2022-12-07 13:10:10 -05:00
|
|
|
.ignore_git_folder()
|
|
|
|
.ignore_node_modules()
|
2024-03-27 14:25:39 -04:00
|
|
|
.set_vendor_folder(cli_options.vendor_dir_path().map(ToOwned::to_owned))
|
2024-01-08 12:18:42 -05:00
|
|
|
.collect_file_patterns(files)
|
2022-12-07 13:10:10 -05:00
|
|
|
}
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stdout)]
|
2023-07-25 17:24:06 -04:00
|
|
|
pub fn print_rules_list(json: bool, maybe_rules_tags: Option<Vec<String>>) {
|
2023-08-27 05:17:41 -04:00
|
|
|
let lint_rules = if maybe_rules_tags.is_none() {
|
|
|
|
rules::get_all_rules()
|
|
|
|
} else {
|
|
|
|
rules::get_filtered_rules(maybe_rules_tags, None, None)
|
|
|
|
};
|
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() {
|
2023-08-27 05:17:41 -04:00
|
|
|
print!(" - {}", colors::cyan(rule.code()));
|
|
|
|
if rule.tags().is_empty() {
|
|
|
|
println!();
|
|
|
|
} else {
|
|
|
|
println!(" [{}]", colors::gray(rule.tags().join(", ")))
|
|
|
|
}
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
colors::gray(format!(
|
|
|
|
" help: https://lint.deno.land/#{}",
|
|
|
|
rule.code()
|
|
|
|
))
|
|
|
|
);
|
2021-08-12 13:15:31 -04:00
|
|
|
println!();
|
2020-11-14 14:51:30 -05:00
|
|
|
}
|
2020-06-12 10:42:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 18:20:52 -05:00
|
|
|
pub fn create_linter(rules: Vec<&'static 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
|
|
|
.rules(rules)
|
|
|
|
.build()
|
2020-06-11 19:44:17 -04:00
|
|
|
}
|
|
|
|
|
2020-09-02 05:39:20 -04:00
|
|
|
fn lint_file(
|
2024-03-21 17:18:59 -04:00
|
|
|
linter: &Linter,
|
2023-03-21 11:46:40 -04:00
|
|
|
file_path: &Path,
|
2022-04-19 22:14:00 -04:00
|
|
|
source_code: String,
|
2024-05-29 20:09:16 -04:00
|
|
|
config: LintConfig,
|
2024-03-21 17:18:59 -04:00
|
|
|
fix: bool,
|
|
|
|
) -> Result<(ParsedSource, Vec<LintDiagnostic>), AnyError> {
|
2024-02-08 20:40:26 -05:00
|
|
|
let specifier = specifier_from_file_path(file_path)?;
|
|
|
|
let media_type = MediaType::from_specifier(&specifier);
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2024-03-21 17:18:59 -04:00
|
|
|
if fix {
|
2024-05-29 20:09:16 -04:00
|
|
|
lint_file_and_fix(
|
|
|
|
linter,
|
|
|
|
&specifier,
|
|
|
|
media_type,
|
|
|
|
source_code,
|
|
|
|
file_path,
|
|
|
|
config,
|
|
|
|
)
|
2024-03-21 17:18:59 -04:00
|
|
|
} else {
|
|
|
|
linter
|
|
|
|
.lint_file(LintFileOptions {
|
|
|
|
specifier,
|
|
|
|
media_type,
|
|
|
|
source_code,
|
2024-05-29 20:09:16 -04:00
|
|
|
config,
|
2024-03-21 17:18:59 -04:00
|
|
|
})
|
|
|
|
.map_err(AnyError::from)
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2024-03-21 17:18:59 -04:00
|
|
|
fn lint_file_and_fix(
|
|
|
|
linter: &Linter,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
media_type: MediaType,
|
|
|
|
source_code: String,
|
|
|
|
file_path: &Path,
|
2024-05-29 20:09:16 -04:00
|
|
|
config: LintConfig,
|
2024-03-21 17:18:59 -04:00
|
|
|
) -> Result<(ParsedSource, Vec<LintDiagnostic>), deno_core::anyhow::Error> {
|
|
|
|
// initial lint
|
|
|
|
let (source, diagnostics) = linter.lint_file(LintFileOptions {
|
|
|
|
specifier: specifier.clone(),
|
2024-01-09 18:20:52 -05:00
|
|
|
media_type,
|
2024-03-21 17:18:59 -04:00
|
|
|
source_code,
|
2024-05-29 20:09:16 -04:00
|
|
|
config: config.clone(),
|
2024-01-09 18:20:52 -05:00
|
|
|
})?;
|
2020-06-10 17:29:48 -04:00
|
|
|
|
2024-03-21 17:18:59 -04:00
|
|
|
// Try applying fixes repeatedly until the file has none left or
|
|
|
|
// a maximum number of iterations is reached. This is necessary
|
|
|
|
// because lint fixes may overlap and so we can't always apply
|
|
|
|
// them in one pass.
|
|
|
|
let mut source = source;
|
|
|
|
let mut diagnostics = diagnostics;
|
|
|
|
let mut fix_iterations = 0;
|
|
|
|
loop {
|
|
|
|
let change = apply_lint_fixes_and_relint(
|
|
|
|
specifier,
|
|
|
|
media_type,
|
|
|
|
linter,
|
2024-05-29 20:09:16 -04:00
|
|
|
config.clone(),
|
2024-06-05 11:04:16 -04:00
|
|
|
source.text_info_lazy(),
|
2024-03-21 17:18:59 -04:00
|
|
|
&diagnostics,
|
|
|
|
)?;
|
|
|
|
match change {
|
|
|
|
Some(change) => {
|
|
|
|
source = change.0;
|
|
|
|
diagnostics = change.1;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fix_iterations += 1;
|
|
|
|
if fix_iterations > 5 {
|
|
|
|
log::warn!(
|
|
|
|
concat!(
|
|
|
|
"Reached maximum number of fix iterations for '{}'. There's ",
|
|
|
|
"probably a bug in Deno. Please fix this file manually.",
|
|
|
|
),
|
|
|
|
specifier,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fix_iterations > 0 {
|
|
|
|
// everything looks good and the file still parses, so write it out
|
2024-06-05 11:04:16 -04:00
|
|
|
fs::write(file_path, source.text().as_ref())
|
2024-03-21 17:18:59 -04:00
|
|
|
.context("Failed writing fix to file.")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((source, diagnostics))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_lint_fixes_and_relint(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
media_type: MediaType,
|
|
|
|
linter: &Linter,
|
2024-05-29 20:09:16 -04:00
|
|
|
config: LintConfig,
|
2024-03-21 17:18:59 -04:00
|
|
|
text_info: &SourceTextInfo,
|
|
|
|
diagnostics: &[LintDiagnostic],
|
|
|
|
) -> Result<Option<(ParsedSource, Vec<LintDiagnostic>)>, AnyError> {
|
|
|
|
let Some(new_text) = apply_lint_fixes(text_info, diagnostics) else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
linter
|
|
|
|
.lint_file(LintFileOptions {
|
|
|
|
specifier: specifier.clone(),
|
|
|
|
source_code: new_text,
|
|
|
|
media_type,
|
2024-05-29 20:09:16 -04:00
|
|
|
config,
|
2024-03-21 17:18:59 -04:00
|
|
|
})
|
|
|
|
.map(Some)
|
|
|
|
.context(
|
|
|
|
"An applied lint fix caused a syntax error. Please report this bug.",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_lint_fixes(
|
|
|
|
text_info: &SourceTextInfo,
|
|
|
|
diagnostics: &[LintDiagnostic],
|
|
|
|
) -> Option<String> {
|
|
|
|
if diagnostics.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let file_start = text_info.range().start;
|
|
|
|
let mut quick_fixes = diagnostics
|
|
|
|
.iter()
|
|
|
|
// use the first quick fix
|
|
|
|
.filter_map(|d| d.fixes.first())
|
|
|
|
.flat_map(|fix| fix.changes.iter())
|
|
|
|
.map(|change| deno_ast::TextChange {
|
|
|
|
range: change.range.as_byte_range(file_start),
|
|
|
|
new_text: change.new_text.to_string(),
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
if quick_fixes.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
// remove any overlapping text changes, we'll circle
|
|
|
|
// back for another pass to fix the remaining
|
|
|
|
quick_fixes.sort_by_key(|change| change.range.start);
|
|
|
|
for i in (1..quick_fixes.len()).rev() {
|
|
|
|
let cur = &quick_fixes[i];
|
|
|
|
let previous = &quick_fixes[i - 1];
|
|
|
|
let is_overlapping = cur.range.start < previous.range.end;
|
|
|
|
if is_overlapping {
|
|
|
|
quick_fixes.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let new_text =
|
|
|
|
deno_ast::apply_text_changes(text_info.text_str(), quick_fixes);
|
|
|
|
Some(new_text)
|
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(
|
2024-02-08 20:40:26 -05:00
|
|
|
file_path: &Path,
|
2023-03-02 16:50:17 -05:00
|
|
|
lint_rules: Vec<&'static dyn LintRule>,
|
2024-05-29 20:09:16 -04:00
|
|
|
config: LintConfig,
|
2024-03-21 17:18:59 -04:00
|
|
|
) -> Result<(ParsedSource, Vec<LintDiagnostic>), AnyError> {
|
2021-09-08 01:08:33 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-09 18:20:52 -05:00
|
|
|
let linter = create_linter(lint_rules);
|
2021-09-08 01:08:33 -04:00
|
|
|
|
2024-03-21 17:18:59 -04:00
|
|
|
linter
|
|
|
|
.lint_file(LintFileOptions {
|
|
|
|
specifier: specifier_from_file_path(file_path)?,
|
2024-06-07 13:02:47 -04:00
|
|
|
source_code: deno_ast::strip_bom(source_code),
|
2024-03-21 17:18:59 -04:00
|
|
|
media_type: MediaType::TypeScript,
|
2024-05-29 20:09:16 -04:00
|
|
|
config,
|
2024-03-21 17:18:59 -04:00
|
|
|
})
|
|
|
|
.map_err(AnyError::from)
|
2021-09-08 01:08:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_lint_result(
|
|
|
|
file_path: &str,
|
2024-03-21 17:18:59 -04:00
|
|
|
result: Result<(ParsedSource, Vec<LintDiagnostic>), AnyError>,
|
2021-09-08 01:08:33 -04:00
|
|
|
reporter_lock: Arc<Mutex<Box<dyn LintReporter + Send>>>,
|
2023-06-14 18:29:19 -04:00
|
|
|
) -> bool {
|
2024-02-19 10:28:41 -05:00
|
|
|
let mut reporter = reporter_lock.lock();
|
2021-09-08 01:08:33 -04:00
|
|
|
|
|
|
|
match result {
|
2024-03-21 17:18:59 -04:00
|
|
|
Ok((_source, mut file_diagnostics)) => {
|
2024-02-08 20:40:26 -05:00
|
|
|
file_diagnostics.sort_by(|a, b| match a.specifier.cmp(&b.specifier) {
|
|
|
|
std::cmp::Ordering::Equal => a.range.start.cmp(&b.range.start),
|
|
|
|
file_order => file_order,
|
|
|
|
});
|
2024-02-19 10:28:41 -05:00
|
|
|
for d in &file_diagnostics {
|
|
|
|
reporter.visit_diagnostic(LintOrCliDiagnostic::Lint(d));
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 10:28:41 -05:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum LintOrCliDiagnostic<'a> {
|
|
|
|
Lint(&'a LintDiagnostic),
|
|
|
|
FastCheck(&'a FastCheckDiagnostic),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> LintOrCliDiagnostic<'a> {
|
|
|
|
pub fn specifier(&self) -> &ModuleSpecifier {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => &d.specifier,
|
|
|
|
LintOrCliDiagnostic::FastCheck(d) => d.specifier(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn range(&self) -> Option<(&SourceTextInfo, SourceRange)> {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => Some((&d.text_info, d.range)),
|
|
|
|
LintOrCliDiagnostic::FastCheck(d) => {
|
|
|
|
d.range().map(|r| (&r.text_info, r.range))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> deno_ast::diagnostics::Diagnostic for LintOrCliDiagnostic<'a> {
|
|
|
|
fn level(&self) -> deno_ast::diagnostics::DiagnosticLevel {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => d.level(),
|
|
|
|
LintOrCliDiagnostic::FastCheck(d) => d.level(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn code(&self) -> Cow<'_, str> {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => d.code(),
|
|
|
|
LintOrCliDiagnostic::FastCheck(_) => Cow::Borrowed("no-slow-types"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn message(&self) -> Cow<'_, str> {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => d.message(),
|
|
|
|
LintOrCliDiagnostic::FastCheck(d) => d.message(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn location(&self) -> deno_ast::diagnostics::DiagnosticLocation {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => d.location(),
|
|
|
|
LintOrCliDiagnostic::FastCheck(d) => d.location(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn snippet(&self) -> Option<deno_ast::diagnostics::DiagnosticSnippet<'_>> {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => d.snippet(),
|
|
|
|
LintOrCliDiagnostic::FastCheck(d) => d.snippet(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hint(&self) -> Option<Cow<'_, str>> {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => d.hint(),
|
|
|
|
LintOrCliDiagnostic::FastCheck(d) => d.hint(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn snippet_fixed(
|
|
|
|
&self,
|
|
|
|
) -> Option<deno_ast::diagnostics::DiagnosticSnippet<'_>> {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => d.snippet_fixed(),
|
|
|
|
LintOrCliDiagnostic::FastCheck(d) => d.snippet_fixed(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn info(&self) -> Cow<'_, [Cow<'_, str>]> {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => d.info(),
|
|
|
|
LintOrCliDiagnostic::FastCheck(d) => d.info(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn docs_url(&self) -> Option<Cow<'_, str>> {
|
|
|
|
match self {
|
|
|
|
LintOrCliDiagnostic::Lint(d) => d.docs_url(),
|
|
|
|
LintOrCliDiagnostic::FastCheck(d) => d.docs_url(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:30:46 -04:00
|
|
|
trait LintReporter {
|
2024-02-19 10:28:41 -05:00
|
|
|
fn visit_diagnostic(&mut self, d: LintOrCliDiagnostic);
|
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,
|
2024-03-21 17:18:59 -04:00
|
|
|
fixable_diagnostics: u32,
|
2020-08-13 11:30:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PrettyLintReporter {
|
|
|
|
fn new() -> PrettyLintReporter {
|
2024-03-21 17:18:59 -04:00
|
|
|
PrettyLintReporter {
|
|
|
|
lint_count: 0,
|
|
|
|
fixable_diagnostics: 0,
|
|
|
|
}
|
2020-08-13 11:30:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintReporter for PrettyLintReporter {
|
2024-02-19 10:28:41 -05:00
|
|
|
fn visit_diagnostic(&mut self, d: LintOrCliDiagnostic) {
|
2020-08-13 11:30:46 -04:00
|
|
|
self.lint_count += 1;
|
2024-03-21 17:18:59 -04:00
|
|
|
if let LintOrCliDiagnostic::Lint(d) = d {
|
|
|
|
if !d.fixes.is_empty() {
|
|
|
|
self.fixable_diagnostics += 1;
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 11:30:46 -04:00
|
|
|
|
2024-06-05 11:04:16 -04:00
|
|
|
log::error!("{}\n", d.display());
|
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) {
|
2024-05-08 22:45:06 -04:00
|
|
|
log::error!("Error linting: {file_path}");
|
|
|
|
log::error!(" {err}");
|
2020-08-13 11:30:46 -04:00
|
|
|
}
|
|
|
|
|
2020-09-09 10:45:31 -04:00
|
|
|
fn close(&mut self, check_count: usize) {
|
2024-03-21 17:18:59 -04:00
|
|
|
let fixable_suffix = if self.fixable_diagnostics > 0 {
|
|
|
|
colors::gray(format!(" ({} fixable via --fix)", self.fixable_diagnostics))
|
|
|
|
.to_string()
|
|
|
|
} else {
|
|
|
|
"".to_string()
|
|
|
|
};
|
2020-08-13 11:30:46 -04:00
|
|
|
match self.lint_count {
|
2024-03-21 17:18:59 -04:00
|
|
|
1 => info!("Found 1 problem{}", fixable_suffix),
|
|
|
|
n if n > 1 => {
|
|
|
|
info!("Found {} problems{}", self.lint_count, fixable_suffix)
|
|
|
|
}
|
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 {
|
2024-02-19 10:28:41 -05:00
|
|
|
fn visit_diagnostic(&mut self, d: LintOrCliDiagnostic) {
|
2022-09-28 12:47:48 -04:00
|
|
|
self.lint_count += 1;
|
|
|
|
|
2024-02-19 10:28:41 -05:00
|
|
|
match d.range() {
|
|
|
|
Some((text_info, range)) => {
|
|
|
|
let line_and_column = text_info.line_and_column_display(range.start);
|
2024-05-08 22:45:06 -04:00
|
|
|
log::error!(
|
2024-02-19 10:28:41 -05:00
|
|
|
"{}: line {}, col {} - {} ({})",
|
|
|
|
d.specifier(),
|
|
|
|
line_and_column.line_number,
|
|
|
|
line_and_column.column_number,
|
|
|
|
d.message(),
|
|
|
|
d.code(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
None => {
|
2024-05-08 22:45:06 -04:00
|
|
|
log::error!("{}: {} ({})", d.specifier(), d.message(), d.code())
|
2024-02-19 10:28:41 -05:00
|
|
|
}
|
|
|
|
}
|
2022-09-28 12:47:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_error(&mut self, file_path: &str, err: &AnyError) {
|
2024-05-08 22:45:06 -04:00
|
|
|
log::error!("Error linting: {file_path}");
|
|
|
|
log::error!(" {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!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 20:40:26 -05:00
|
|
|
// WARNING: Ensure doesn't change because it's used in the JSON output
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct JsonDiagnosticLintPosition {
|
|
|
|
/// The 1-indexed line number.
|
|
|
|
pub line: usize,
|
|
|
|
/// The 0-indexed column index.
|
|
|
|
pub col: usize,
|
|
|
|
pub byte_pos: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl JsonDiagnosticLintPosition {
|
|
|
|
pub fn new(byte_index: usize, loc: deno_ast::LineAndColumnIndex) -> Self {
|
|
|
|
JsonDiagnosticLintPosition {
|
|
|
|
line: loc.line_index + 1,
|
|
|
|
col: loc.column_index,
|
|
|
|
byte_pos: byte_index,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING: Ensure doesn't change because it's used in the JSON output
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
|
|
|
struct JsonLintDiagnosticRange {
|
|
|
|
pub start: JsonDiagnosticLintPosition,
|
|
|
|
pub end: JsonDiagnosticLintPosition,
|
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING: Ensure doesn't change because it's used in the JSON output
|
|
|
|
#[derive(Clone, Serialize)]
|
|
|
|
struct JsonLintDiagnostic {
|
|
|
|
pub filename: String,
|
2024-02-19 10:28:41 -05:00
|
|
|
pub range: Option<JsonLintDiagnosticRange>,
|
2024-02-08 20:40:26 -05:00
|
|
|
pub message: String,
|
|
|
|
pub code: String,
|
|
|
|
pub hint: Option<String>,
|
|
|
|
}
|
|
|
|
|
2020-08-13 11:30:46 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct JsonLintReporter {
|
2024-02-08 20:40:26 -05:00
|
|
|
diagnostics: Vec<JsonLintDiagnostic>,
|
2020-08-13 11:30:46 -04:00
|
|
|
errors: Vec<LintError>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl JsonLintReporter {
|
|
|
|
fn new() -> JsonLintReporter {
|
|
|
|
JsonLintReporter {
|
|
|
|
diagnostics: Vec::new(),
|
|
|
|
errors: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintReporter for JsonLintReporter {
|
2024-02-19 10:28:41 -05:00
|
|
|
fn visit_diagnostic(&mut self, d: LintOrCliDiagnostic) {
|
2024-02-08 20:40:26 -05:00
|
|
|
self.diagnostics.push(JsonLintDiagnostic {
|
2024-02-19 10:28:41 -05:00
|
|
|
filename: d.specifier().to_string(),
|
|
|
|
range: d.range().map(|(text_info, range)| JsonLintDiagnosticRange {
|
2024-02-08 20:40:26 -05:00
|
|
|
start: JsonDiagnosticLintPosition::new(
|
2024-02-19 10:28:41 -05:00
|
|
|
range.start.as_byte_index(text_info.range().start),
|
|
|
|
text_info.line_and_column_index(range.start),
|
2024-02-08 20:40:26 -05:00
|
|
|
),
|
|
|
|
end: JsonDiagnosticLintPosition::new(
|
2024-02-19 10:28:41 -05:00
|
|
|
range.end.as_byte_index(text_info.range().start),
|
|
|
|
text_info.line_and_column_index(range.end),
|
2024-02-08 20:40:26 -05:00
|
|
|
),
|
2024-02-19 10:28:41 -05:00
|
|
|
}),
|
|
|
|
message: d.message().to_string(),
|
|
|
|
code: d.code().to_string(),
|
|
|
|
hint: d.hint().map(|h| h.to_string()),
|
2024-02-08 20:40:26 -05:00
|
|
|
});
|
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) {
|
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);
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stdout)]
|
|
|
|
{
|
|
|
|
println!("{}", json.unwrap());
|
|
|
|
}
|
2020-08-13 11:30:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 20:40:26 -05:00
|
|
|
fn sort_diagnostics(diagnostics: &mut [JsonLintDiagnostic]) {
|
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 {
|
2024-02-19 10:28:41 -05:00
|
|
|
Ordering::Equal => match &a.range {
|
|
|
|
Some(a_range) => match &b.range {
|
|
|
|
Some(b_range) => {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Ordering::Less,
|
|
|
|
},
|
|
|
|
None => match &b.range {
|
|
|
|
Some(_) => Ordering::Greater,
|
|
|
|
None => Ordering::Equal,
|
|
|
|
},
|
|
|
|
},
|
2020-09-02 05:39:20 -04:00
|
|
|
_ => 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,
|
2024-02-19 10:28:41 -05:00
|
|
|
maybe_config_file: Option<&deno_config::ConfigFile>,
|
|
|
|
) -> Result<ConfiguredRules, AnyError> {
|
|
|
|
let lint_rules = get_configured_rules(rules, maybe_config_file);
|
|
|
|
if lint_rules.rules.is_empty() {
|
2023-06-14 18:29:19 -04:00
|
|
|
bail!("No rules have been configured")
|
|
|
|
}
|
|
|
|
Ok(lint_rules)
|
|
|
|
}
|
|
|
|
|
2024-02-19 10:28:41 -05:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct ConfiguredRules {
|
|
|
|
pub rules: Vec<&'static dyn LintRule>,
|
|
|
|
// cli specific rules
|
|
|
|
pub no_slow_types: bool,
|
|
|
|
}
|
|
|
|
|
2024-03-26 11:52:20 -04:00
|
|
|
impl Default for ConfiguredRules {
|
|
|
|
fn default() -> Self {
|
|
|
|
get_configured_rules(Default::default(), None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 10:28:41 -05:00
|
|
|
impl ConfiguredRules {
|
|
|
|
fn incremental_cache_state(&self) -> Vec<&str> {
|
|
|
|
// use a hash of the rule names in order to bust the cache
|
|
|
|
let mut names = self.rules.iter().map(|r| r.code()).collect::<Vec<_>>();
|
|
|
|
// ensure this is stable by sorting it
|
|
|
|
names.sort_unstable();
|
|
|
|
if self.no_slow_types {
|
|
|
|
names.push("no-slow-types");
|
|
|
|
}
|
|
|
|
names
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-02 16:50:17 -05:00
|
|
|
pub fn get_configured_rules(
|
|
|
|
rules: LintRulesConfig,
|
2024-02-19 10:28:41 -05:00
|
|
|
maybe_config_file: Option<&deno_config::ConfigFile>,
|
|
|
|
) -> ConfiguredRules {
|
|
|
|
const NO_SLOW_TYPES_NAME: &str = "no-slow-types";
|
|
|
|
let implicit_no_slow_types = maybe_config_file
|
2024-06-26 17:24:10 -04:00
|
|
|
.map(|c| c.is_package() || c.json.workspace.is_some())
|
2024-02-19 10:28:41 -05:00
|
|
|
.unwrap_or(false);
|
2024-03-25 18:20:15 -04:00
|
|
|
let no_slow_types = implicit_no_slow_types
|
|
|
|
&& !rules
|
|
|
|
.exclude
|
|
|
|
.as_ref()
|
|
|
|
.map(|exclude| exclude.iter().any(|i| i == NO_SLOW_TYPES_NAME))
|
|
|
|
.unwrap_or(false);
|
|
|
|
let rules = rules::get_filtered_rules(
|
|
|
|
rules
|
|
|
|
.tags
|
|
|
|
.or_else(|| Some(get_default_tags(maybe_config_file))),
|
|
|
|
rules.exclude.map(|exclude| {
|
|
|
|
exclude
|
|
|
|
.into_iter()
|
|
|
|
.filter(|c| c != NO_SLOW_TYPES_NAME)
|
|
|
|
.collect()
|
|
|
|
}),
|
|
|
|
rules.include.map(|include| {
|
|
|
|
include
|
|
|
|
.into_iter()
|
|
|
|
.filter(|c| c != NO_SLOW_TYPES_NAME)
|
|
|
|
.collect()
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
ConfiguredRules {
|
|
|
|
rules,
|
|
|
|
no_slow_types,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_default_tags(
|
|
|
|
maybe_config_file: Option<&deno_config::ConfigFile>,
|
|
|
|
) -> Vec<String> {
|
|
|
|
let mut tags = Vec::with_capacity(2);
|
|
|
|
tags.push("recommended".to_string());
|
|
|
|
if maybe_config_file.map(|c| c.is_package()).unwrap_or(false) {
|
|
|
|
tags.push("jsr".to_string());
|
2021-09-03 11:01:58 -04:00
|
|
|
}
|
2024-03-25 18:20:15 -04:00
|
|
|
tags
|
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
|
|
|
};
|
2024-02-19 10:28:41 -05:00
|
|
|
let rules = get_configured_rules(rules_config, None);
|
2021-11-08 20:10:33 -05:00
|
|
|
let mut rule_names = rules
|
2024-02-19 10:28:41 -05:00
|
|
|
.rules
|
2021-11-08 20:10:33 -05:00
|
|
|
.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);
|
|
|
|
}
|
|
|
|
}
|