2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-02-06 07:39:01 -05:00
|
|
|
use super::language_server;
|
2021-02-04 13:53:02 -05:00
|
|
|
use super::tsc;
|
|
|
|
|
2021-10-11 18:02:33 -04:00
|
|
|
use crate::config_file::LintConfig;
|
2020-12-07 05:46:39 -05:00
|
|
|
use crate::tools::lint::create_linter;
|
2021-10-11 18:02:33 -04:00
|
|
|
use crate::tools::lint::get_configured_rules;
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::SourceTextInfo;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
2021-02-04 13:53:02 -05:00
|
|
|
use deno_core::error::custom_error;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::error::AnyError;
|
2021-01-31 22:30:41 -05:00
|
|
|
use deno_core::serde::Deserialize;
|
2021-02-11 23:17:48 -05:00
|
|
|
use deno_core::serde_json;
|
2021-02-05 15:10:53 -05:00
|
|
|
use deno_core::serde_json::json;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2021-01-29 14:34:33 -05:00
|
|
|
use lspower::lsp;
|
|
|
|
use lspower::lsp::Position;
|
|
|
|
use lspower::lsp::Range;
|
2021-12-18 16:14:42 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2021-05-29 07:21:11 -04:00
|
|
|
use regex::Regex;
|
2021-02-04 13:53:02 -05:00
|
|
|
use std::cmp::Ordering;
|
2020-12-07 05:46:39 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
/// Diagnostic error codes which actually are the same, and so when grouping
|
|
|
|
/// fixes we treat them the same.
|
|
|
|
static FIX_ALL_ERROR_CODES: Lazy<HashMap<&'static str, &'static str>> =
|
|
|
|
Lazy::new(|| {
|
|
|
|
(&[("2339", "2339"), ("2345", "2339")])
|
2021-02-04 13:53:02 -05:00
|
|
|
.iter()
|
2021-02-12 06:49:42 -05:00
|
|
|
.cloned()
|
2021-12-18 16:14:42 -05:00
|
|
|
.collect()
|
|
|
|
});
|
2021-02-04 13:53:02 -05:00
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
/// Fixes which help determine if there is a preferred fix when there are
|
|
|
|
/// multiple fixes available.
|
|
|
|
static PREFERRED_FIXES: Lazy<HashMap<&'static str, (u32, bool)>> =
|
|
|
|
Lazy::new(|| {
|
|
|
|
(&[
|
|
|
|
("annotateWithTypeFromJSDoc", (1, false)),
|
|
|
|
("constructorForDerivedNeedSuperCall", (1, false)),
|
|
|
|
("extendsInterfaceBecomesImplements", (1, false)),
|
|
|
|
("awaitInSyncFunction", (1, false)),
|
|
|
|
("classIncorrectlyImplementsInterface", (3, false)),
|
|
|
|
("classDoesntImplementInheritedAbstractMember", (3, false)),
|
|
|
|
("unreachableCode", (1, false)),
|
|
|
|
("unusedIdentifier", (1, false)),
|
|
|
|
("forgottenThisPropertyAccess", (1, false)),
|
|
|
|
("spelling", (2, false)),
|
|
|
|
("addMissingAwait", (1, false)),
|
|
|
|
("fixImport", (0, true)),
|
|
|
|
])
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
static IMPORT_SPECIFIER_RE: Lazy<Regex> =
|
|
|
|
Lazy::new(|| Regex::new(r#"\sfrom\s+["']([^"']*)["']"#).unwrap());
|
2021-05-29 07:21:11 -04:00
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
static DENO_TYPES_RE: Lazy<Regex> = Lazy::new(|| {
|
|
|
|
Regex::new(r#"(?i)^\s*@deno-types\s*=\s*(?:["']([^"']+)["']|(\S+))"#).unwrap()
|
|
|
|
});
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
static TRIPLE_SLASH_REFERENCE_RE: Lazy<Regex> =
|
|
|
|
Lazy::new(|| Regex::new(r"(?i)^/\s*<reference\s.*?/>").unwrap());
|
2021-10-10 17:26:22 -04:00
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
static PATH_REFERENCE_RE: Lazy<Regex> =
|
|
|
|
Lazy::new(|| Regex::new(r#"(?i)\spath\s*=\s*["']([^"']*)["']"#).unwrap());
|
|
|
|
static TYPES_REFERENCE_RE: Lazy<Regex> =
|
|
|
|
Lazy::new(|| Regex::new(r#"(?i)\stypes\s*=\s*["']([^"']*)["']"#).unwrap());
|
2021-02-04 13:53:02 -05:00
|
|
|
|
2021-05-29 07:21:11 -04:00
|
|
|
const SUPPORTED_EXTENSIONS: &[&str] = &[".ts", ".tsx", ".js", ".jsx", ".mjs"];
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
/// Category of self-generated diagnostic messages (those not coming from)
|
|
|
|
/// TypeScript.
|
2021-05-25 18:15:09 -04:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub enum Category {
|
|
|
|
/// A lint diagnostic, where the first element is the message.
|
|
|
|
Lint {
|
|
|
|
message: String,
|
|
|
|
code: String,
|
|
|
|
hint: Option<String>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A structure to hold a reference to a diagnostic message.
|
2021-05-25 18:15:09 -04:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub struct Reference {
|
|
|
|
category: Category,
|
|
|
|
range: Range,
|
|
|
|
}
|
|
|
|
|
2021-05-11 10:43:00 -04:00
|
|
|
impl Reference {
|
|
|
|
pub fn to_diagnostic(&self) -> lsp::Diagnostic {
|
|
|
|
match &self.category {
|
2021-05-25 18:15:09 -04:00
|
|
|
Category::Lint {
|
|
|
|
message,
|
|
|
|
code,
|
|
|
|
hint,
|
|
|
|
} => lsp::Diagnostic {
|
2021-05-11 10:43:00 -04:00
|
|
|
range: self.range,
|
2021-11-24 20:10:12 -05:00
|
|
|
severity: Some(lsp::DiagnosticSeverity::WARNING),
|
2021-05-11 10:43:00 -04:00
|
|
|
code: Some(lsp::NumberOrString::String(code.to_string())),
|
|
|
|
code_description: None,
|
|
|
|
source: Some("deno-lint".to_string()),
|
2021-05-25 18:15:09 -04:00
|
|
|
message: {
|
|
|
|
let mut msg = message.to_string();
|
|
|
|
if let Some(hint) = hint {
|
|
|
|
msg.push('\n');
|
|
|
|
msg.push_str(hint);
|
|
|
|
}
|
|
|
|
msg
|
|
|
|
},
|
2021-05-11 10:43:00 -04:00
|
|
|
related_information: None,
|
|
|
|
tags: None, // we should tag unused code
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
fn as_lsp_range(range: &deno_lint::diagnostic::Range) -> Range {
|
|
|
|
Range {
|
|
|
|
start: Position {
|
2021-09-02 11:38:19 -04:00
|
|
|
line: range.start.line_index as u32,
|
|
|
|
character: range.start.column_index as u32,
|
2020-12-07 05:46:39 -05:00
|
|
|
},
|
|
|
|
end: Position {
|
2021-09-02 11:38:19 -04:00
|
|
|
line: range.end.line_index as u32,
|
|
|
|
character: range.end.column_index as u32,
|
2020-12-07 05:46:39 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_lint_references(
|
2021-09-07 10:39:32 -04:00
|
|
|
parsed_source: &deno_ast::ParsedSource,
|
2021-10-11 18:02:33 -04:00
|
|
|
maybe_lint_config: Option<&LintConfig>,
|
2020-12-07 05:46:39 -05:00
|
|
|
) -> Result<Vec<Reference>, AnyError> {
|
2021-11-04 11:12:12 -04:00
|
|
|
let lint_rules = get_configured_rules(maybe_lint_config, None, None, None)?;
|
2021-10-12 09:58:04 -04:00
|
|
|
let linter = create_linter(parsed_source.media_type(), lint_rules);
|
|
|
|
let lint_diagnostics = linter.lint_with_ast(parsed_source);
|
2020-12-07 05:46:39 -05:00
|
|
|
|
|
|
|
Ok(
|
|
|
|
lint_diagnostics
|
|
|
|
.into_iter()
|
|
|
|
.map(|d| Reference {
|
|
|
|
category: Category::Lint {
|
|
|
|
message: d.message,
|
|
|
|
code: d.code,
|
|
|
|
hint: d.hint,
|
|
|
|
},
|
|
|
|
range: as_lsp_range(&d.range),
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:53:02 -05:00
|
|
|
fn code_as_string(code: &Option<lsp::NumberOrString>) -> String {
|
|
|
|
match code {
|
|
|
|
Some(lsp::NumberOrString::String(str)) => str.clone(),
|
|
|
|
Some(lsp::NumberOrString::Number(num)) => num.to_string(),
|
|
|
|
_ => "".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-29 07:21:11 -04:00
|
|
|
/// Iterate over the supported extensions, concatenating the extension on the
|
|
|
|
/// specifier, returning the first specifier that is resolve-able, otherwise
|
|
|
|
/// None if none match.
|
|
|
|
fn check_specifier(
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
snapshot: &language_server::StateSnapshot,
|
|
|
|
) -> Option<String> {
|
|
|
|
for ext in SUPPORTED_EXTENSIONS {
|
|
|
|
let specifier_with_ext = format!("{}{}", specifier, ext);
|
2021-10-28 19:56:01 -04:00
|
|
|
if snapshot
|
|
|
|
.documents
|
|
|
|
.contains_import(&specifier_with_ext, referrer)
|
2021-05-29 07:21:11 -04:00
|
|
|
{
|
2021-10-28 19:56:01 -04:00
|
|
|
return Some(specifier_with_ext);
|
2021-05-29 07:21:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For a set of tsc changes, can them for any that contain something that looks
|
|
|
|
/// like an import and rewrite the import specifier to include the extension
|
|
|
|
pub(crate) fn fix_ts_import_changes(
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
changes: &[tsc::FileTextChanges],
|
|
|
|
language_server: &language_server::Inner,
|
|
|
|
) -> Result<Vec<tsc::FileTextChanges>, AnyError> {
|
|
|
|
let mut r = Vec::new();
|
|
|
|
let snapshot = language_server.snapshot()?;
|
|
|
|
for change in changes {
|
|
|
|
let mut text_changes = Vec::new();
|
|
|
|
for text_change in &change.text_changes {
|
|
|
|
if let Some(captures) =
|
|
|
|
IMPORT_SPECIFIER_RE.captures(&text_change.new_text)
|
|
|
|
{
|
|
|
|
let specifier = captures
|
|
|
|
.get(1)
|
|
|
|
.ok_or_else(|| anyhow!("Missing capture."))?
|
|
|
|
.as_str();
|
2021-10-28 19:56:01 -04:00
|
|
|
if let Some(new_specifier) =
|
|
|
|
check_specifier(specifier, referrer, &snapshot)
|
|
|
|
{
|
2021-05-29 07:21:11 -04:00
|
|
|
let new_text =
|
|
|
|
text_change.new_text.replace(specifier, &new_specifier);
|
|
|
|
text_changes.push(tsc::TextChange {
|
|
|
|
span: text_change.span.clone(),
|
|
|
|
new_text,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
text_changes.push(text_change.clone());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
text_changes.push(text_change.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.push(tsc::FileTextChanges {
|
|
|
|
file_name: change.file_name.clone(),
|
|
|
|
text_changes,
|
|
|
|
is_new_file: change.is_new_file,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fix tsc import code actions so that the module specifier is correct for
|
|
|
|
/// resolution by Deno (includes the extension).
|
|
|
|
fn fix_ts_import_action(
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
action: &tsc::CodeFixAction,
|
|
|
|
language_server: &language_server::Inner,
|
|
|
|
) -> Result<tsc::CodeFixAction, AnyError> {
|
|
|
|
if action.fix_name == "import" {
|
|
|
|
let change = action
|
|
|
|
.changes
|
|
|
|
.get(0)
|
|
|
|
.ok_or_else(|| anyhow!("Unexpected action changes."))?;
|
|
|
|
let text_change = change
|
|
|
|
.text_changes
|
|
|
|
.get(0)
|
|
|
|
.ok_or_else(|| anyhow!("Missing text change."))?;
|
|
|
|
if let Some(captures) = IMPORT_SPECIFIER_RE.captures(&text_change.new_text)
|
|
|
|
{
|
|
|
|
let specifier = captures
|
|
|
|
.get(1)
|
|
|
|
.ok_or_else(|| anyhow!("Missing capture."))?
|
|
|
|
.as_str();
|
|
|
|
let snapshot = language_server.snapshot()?;
|
2021-10-28 19:56:01 -04:00
|
|
|
if let Some(new_specifier) =
|
|
|
|
check_specifier(specifier, referrer, &snapshot)
|
|
|
|
{
|
2021-05-29 07:21:11 -04:00
|
|
|
let description = action.description.replace(specifier, &new_specifier);
|
|
|
|
let changes = action
|
|
|
|
.changes
|
|
|
|
.iter()
|
|
|
|
.map(|c| {
|
|
|
|
let text_changes = c
|
|
|
|
.text_changes
|
|
|
|
.iter()
|
|
|
|
.map(|tc| tsc::TextChange {
|
|
|
|
span: tc.span.clone(),
|
|
|
|
new_text: tc.new_text.replace(specifier, &new_specifier),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
tsc::FileTextChanges {
|
|
|
|
file_name: c.file_name.clone(),
|
|
|
|
text_changes,
|
|
|
|
is_new_file: c.is_new_file,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
return Ok(tsc::CodeFixAction {
|
|
|
|
description,
|
|
|
|
changes,
|
|
|
|
commands: None,
|
|
|
|
fix_name: action.fix_name.clone(),
|
|
|
|
fix_id: None,
|
|
|
|
fix_all_description: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(action.clone())
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:53:02 -05:00
|
|
|
/// Determines if two TypeScript diagnostic codes are effectively equivalent.
|
|
|
|
fn is_equivalent_code(
|
|
|
|
a: &Option<lsp::NumberOrString>,
|
|
|
|
b: &Option<lsp::NumberOrString>,
|
|
|
|
) -> bool {
|
|
|
|
let a_code = code_as_string(a);
|
|
|
|
let b_code = code_as_string(b);
|
|
|
|
FIX_ALL_ERROR_CODES.get(a_code.as_str())
|
|
|
|
== FIX_ALL_ERROR_CODES.get(b_code.as_str())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a boolean flag to indicate if the specified action is the preferred
|
|
|
|
/// action for a given set of actions.
|
|
|
|
fn is_preferred(
|
|
|
|
action: &tsc::CodeFixAction,
|
2021-02-11 23:17:48 -05:00
|
|
|
actions: &[CodeActionKind],
|
2021-02-04 13:53:02 -05:00
|
|
|
fix_priority: u32,
|
|
|
|
only_one: bool,
|
|
|
|
) -> bool {
|
2021-02-11 23:17:48 -05:00
|
|
|
actions.iter().all(|i| {
|
|
|
|
if let CodeActionKind::Tsc(_, a) = i {
|
|
|
|
if action == a {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if a.fix_id.is_some() {
|
|
|
|
return true;
|
2021-02-04 13:53:02 -05:00
|
|
|
}
|
2021-02-11 23:17:48 -05:00
|
|
|
if let Some((other_fix_priority, _)) =
|
|
|
|
PREFERRED_FIXES.get(a.fix_name.as_str())
|
|
|
|
{
|
|
|
|
match other_fix_priority.cmp(&fix_priority) {
|
|
|
|
Ordering::Less => return true,
|
|
|
|
Ordering::Greater => return false,
|
|
|
|
Ordering::Equal => (),
|
|
|
|
}
|
|
|
|
if only_one && action.fix_name == a.fix_name {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-04 13:53:02 -05:00
|
|
|
}
|
2021-02-11 23:17:48 -05:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
true
|
2021-02-04 13:53:02 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert changes returned from a TypeScript quick fix action into edits
|
|
|
|
/// for an LSP CodeAction.
|
2021-02-06 07:39:01 -05:00
|
|
|
pub(crate) async fn ts_changes_to_edit(
|
2021-02-04 13:53:02 -05:00
|
|
|
changes: &[tsc::FileTextChanges],
|
2021-02-06 07:39:01 -05:00
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> Result<Option<lsp::WorkspaceEdit>, AnyError> {
|
2021-02-04 13:53:02 -05:00
|
|
|
let mut text_document_edits = Vec::new();
|
|
|
|
for change in changes {
|
2021-02-06 07:39:01 -05:00
|
|
|
let text_document_edit =
|
|
|
|
change.to_text_document_edit(language_server).await?;
|
2021-02-04 13:53:02 -05:00
|
|
|
text_document_edits.push(text_document_edit);
|
|
|
|
}
|
|
|
|
Ok(Some(lsp::WorkspaceEdit {
|
|
|
|
changes: None,
|
|
|
|
document_changes: Some(lsp::DocumentChanges::Edits(text_document_edits)),
|
|
|
|
change_annotations: None,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2021-02-05 15:10:53 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CodeActionData {
|
|
|
|
pub specifier: ModuleSpecifier,
|
|
|
|
pub fix_id: String,
|
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct DenoFixData {
|
|
|
|
pub specifier: ModuleSpecifier,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
enum CodeActionKind {
|
|
|
|
Deno(lsp::CodeAction),
|
2021-06-21 02:43:35 -04:00
|
|
|
DenoLint(lsp::CodeAction),
|
2021-02-11 23:17:48 -05:00
|
|
|
Tsc(lsp::CodeAction, tsc::CodeFixAction),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Hash, PartialEq, Eq)]
|
|
|
|
enum FixAllKind {
|
|
|
|
Tsc(String),
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:53:02 -05:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct CodeActionCollection {
|
2021-02-11 23:17:48 -05:00
|
|
|
actions: Vec<CodeActionKind>,
|
|
|
|
fix_all_actions: HashMap<FixAllKind, CodeActionKind>,
|
2021-02-04 13:53:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CodeActionCollection {
|
2021-02-11 23:17:48 -05:00
|
|
|
pub(crate) fn add_deno_fix_action(
|
|
|
|
&mut self,
|
2021-12-15 22:53:17 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2021-02-11 23:17:48 -05:00
|
|
|
diagnostic: &lsp::Diagnostic,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-12-15 22:53:17 -05:00
|
|
|
if let Some(lsp::NumberOrString::String(code)) = &diagnostic.code {
|
|
|
|
if code == "no-assert-type" {
|
|
|
|
let code_action = lsp::CodeAction {
|
|
|
|
title: "Insert import assertion.".to_string(),
|
|
|
|
kind: Some(lsp::CodeActionKind::QUICKFIX),
|
|
|
|
diagnostics: Some(vec![diagnostic.clone()]),
|
|
|
|
edit: Some(lsp::WorkspaceEdit {
|
|
|
|
changes: Some(HashMap::from([(
|
|
|
|
specifier.clone(),
|
|
|
|
vec![lsp::TextEdit {
|
|
|
|
new_text: " assert { type: \"json\" }".to_string(),
|
|
|
|
range: lsp::Range {
|
|
|
|
start: diagnostic.range.end,
|
|
|
|
end: diagnostic.range.end,
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
)])),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
self.actions.push(CodeActionKind::Deno(code_action));
|
|
|
|
} else if let Some(data) = diagnostic.data.clone() {
|
|
|
|
let fix_data: DenoFixData = serde_json::from_value(data)?;
|
|
|
|
let title = if code == "no-cache-data" {
|
|
|
|
"Cache the data URL and its dependencies.".to_string()
|
|
|
|
} else {
|
|
|
|
format!("Cache \"{}\" and its dependencies.", fix_data.specifier)
|
|
|
|
};
|
|
|
|
let code_action = lsp::CodeAction {
|
|
|
|
title,
|
|
|
|
kind: Some(lsp::CodeActionKind::QUICKFIX),
|
|
|
|
diagnostics: Some(vec![diagnostic.clone()]),
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "".to_string(),
|
|
|
|
command: "deno.cache".to_string(),
|
|
|
|
arguments: Some(vec![json!([fix_data.specifier])]),
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
self.actions.push(CodeActionKind::Deno(code_action));
|
|
|
|
}
|
2021-02-11 23:17:48 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-06-21 02:43:35 -04:00
|
|
|
pub(crate) fn add_deno_lint_ignore_action(
|
|
|
|
&mut self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
diagnostic: &lsp::Diagnostic,
|
2021-10-28 19:56:01 -04:00
|
|
|
maybe_text_info: Option<SourceTextInfo>,
|
|
|
|
maybe_parsed_source: Option<deno_ast::ParsedSource>,
|
2021-06-21 02:43:35 -04:00
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
let code = diagnostic
|
|
|
|
.code
|
|
|
|
.as_ref()
|
|
|
|
.map(|v| match v {
|
|
|
|
lsp::NumberOrString::String(v) => v.to_owned(),
|
|
|
|
_ => "".to_string(),
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
|
2021-10-28 19:56:01 -04:00
|
|
|
let line_content = maybe_text_info.map(|ti| {
|
|
|
|
ti.line_text(diagnostic.range.start.line as usize)
|
2021-09-07 10:39:32 -04:00
|
|
|
.to_string()
|
|
|
|
});
|
2021-06-21 02:43:35 -04:00
|
|
|
|
|
|
|
let mut changes = HashMap::new();
|
|
|
|
changes.insert(
|
|
|
|
specifier.clone(),
|
|
|
|
vec![lsp::TextEdit {
|
|
|
|
new_text: prepend_whitespace(
|
|
|
|
format!("// deno-lint-ignore {}\n", code),
|
|
|
|
line_content,
|
|
|
|
),
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: diagnostic.range.start.line,
|
|
|
|
character: 0,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: diagnostic.range.start.line,
|
|
|
|
character: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
);
|
|
|
|
let ignore_error_action = lsp::CodeAction {
|
|
|
|
title: format!("Disable {} for this line", code),
|
|
|
|
kind: Some(lsp::CodeActionKind::QUICKFIX),
|
|
|
|
diagnostics: Some(vec![diagnostic.clone()]),
|
|
|
|
command: None,
|
|
|
|
is_preferred: None,
|
|
|
|
disabled: None,
|
|
|
|
data: None,
|
|
|
|
edit: Some(lsp::WorkspaceEdit {
|
|
|
|
changes: Some(changes),
|
|
|
|
change_annotations: None,
|
|
|
|
document_changes: None,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
self
|
|
|
|
.actions
|
|
|
|
.push(CodeActionKind::DenoLint(ignore_error_action));
|
|
|
|
|
2021-09-14 08:16:51 -04:00
|
|
|
// Disable a lint error for the entire file.
|
2021-10-28 19:56:01 -04:00
|
|
|
let maybe_ignore_comment = maybe_parsed_source.clone().and_then(|ps| {
|
2021-09-14 08:16:51 -04:00
|
|
|
// Note: we can use ps.get_leading_comments() but it doesn't
|
|
|
|
// work when shebang is present at the top of the file.
|
|
|
|
ps.comments().get_vec().iter().find_map(|c| {
|
|
|
|
let comment_text = c.text.trim();
|
|
|
|
comment_text.split_whitespace().next().and_then(|prefix| {
|
|
|
|
if prefix == "deno-lint-ignore-file" {
|
|
|
|
Some(c.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut new_text = format!("// deno-lint-ignore-file {}\n", code);
|
|
|
|
let mut range = lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 0,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 0,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// If ignore file comment already exists, append the lint code
|
|
|
|
// to the existing comment.
|
|
|
|
if let Some(ignore_comment) = maybe_ignore_comment {
|
|
|
|
new_text = format!(" {}", code);
|
|
|
|
// Get the end position of the comment.
|
2021-10-28 19:56:01 -04:00
|
|
|
let line = maybe_parsed_source
|
2021-09-14 08:16:51 -04:00
|
|
|
.unwrap()
|
|
|
|
.source()
|
|
|
|
.line_and_column_index(ignore_comment.span.hi());
|
|
|
|
let position = lsp::Position {
|
|
|
|
line: line.line_index as u32,
|
|
|
|
character: line.column_index as u32,
|
|
|
|
};
|
|
|
|
// Set the edit range to the end of the comment.
|
|
|
|
range.start = position;
|
|
|
|
range.end = position;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut changes = HashMap::new();
|
|
|
|
changes.insert(specifier.clone(), vec![lsp::TextEdit { new_text, range }]);
|
|
|
|
let ignore_file_action = lsp::CodeAction {
|
|
|
|
title: format!("Disable {} for the entire file", code),
|
|
|
|
kind: Some(lsp::CodeActionKind::QUICKFIX),
|
|
|
|
diagnostics: Some(vec![diagnostic.clone()]),
|
|
|
|
command: None,
|
|
|
|
is_preferred: None,
|
|
|
|
disabled: None,
|
|
|
|
data: None,
|
|
|
|
edit: Some(lsp::WorkspaceEdit {
|
|
|
|
changes: Some(changes),
|
|
|
|
change_annotations: None,
|
|
|
|
document_changes: None,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
self
|
|
|
|
.actions
|
|
|
|
.push(CodeActionKind::DenoLint(ignore_file_action));
|
|
|
|
|
2021-06-21 02:43:35 -04:00
|
|
|
let mut changes = HashMap::new();
|
|
|
|
changes.insert(
|
|
|
|
specifier.clone(),
|
|
|
|
vec![lsp::TextEdit {
|
|
|
|
new_text: "// deno-lint-ignore-file\n".to_string(),
|
|
|
|
range: lsp::Range {
|
|
|
|
start: lsp::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 0,
|
|
|
|
},
|
|
|
|
end: lsp::Position {
|
|
|
|
line: 0,
|
|
|
|
character: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
);
|
|
|
|
let ignore_file_action = lsp::CodeAction {
|
|
|
|
title: "Ignore lint errors for the entire file".to_string(),
|
|
|
|
kind: Some(lsp::CodeActionKind::QUICKFIX),
|
|
|
|
diagnostics: Some(vec![diagnostic.clone()]),
|
|
|
|
command: None,
|
|
|
|
is_preferred: None,
|
|
|
|
disabled: None,
|
|
|
|
data: None,
|
|
|
|
edit: Some(lsp::WorkspaceEdit {
|
|
|
|
changes: Some(changes),
|
|
|
|
change_annotations: None,
|
|
|
|
document_changes: None,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
self
|
|
|
|
.actions
|
|
|
|
.push(CodeActionKind::DenoLint(ignore_file_action));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-02-04 13:53:02 -05:00
|
|
|
/// Add a TypeScript code fix action to the code actions collection.
|
2021-02-06 07:39:01 -05:00
|
|
|
pub(crate) async fn add_ts_fix_action(
|
2021-02-04 13:53:02 -05:00
|
|
|
&mut self,
|
2021-05-29 07:21:11 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2021-02-04 13:53:02 -05:00
|
|
|
action: &tsc::CodeFixAction,
|
|
|
|
diagnostic: &lsp::Diagnostic,
|
2021-02-06 07:39:01 -05:00
|
|
|
language_server: &mut language_server::Inner,
|
|
|
|
) -> Result<(), AnyError> {
|
2021-02-04 13:53:02 -05:00
|
|
|
if action.commands.is_some() {
|
|
|
|
// In theory, tsc can return actions that require "commands" to be applied
|
|
|
|
// back into TypeScript. Currently there is only one command, `install
|
|
|
|
// package` but Deno doesn't support that. The problem is that the
|
|
|
|
// `.applyCodeActionCommand()` returns a promise, and with the current way
|
|
|
|
// we wrap tsc, we can't handle the asynchronous response, so it is
|
|
|
|
// actually easier to return errors if we ever encounter one of these,
|
|
|
|
// which we really wouldn't expect from the Deno lsp.
|
|
|
|
return Err(custom_error(
|
|
|
|
"UnsupportedFix",
|
|
|
|
"The action returned from TypeScript is unsupported.",
|
|
|
|
));
|
|
|
|
}
|
2021-05-29 07:21:11 -04:00
|
|
|
let action = fix_ts_import_action(specifier, action, language_server)?;
|
2021-02-06 07:39:01 -05:00
|
|
|
let edit = ts_changes_to_edit(&action.changes, language_server).await?;
|
2021-02-04 13:53:02 -05:00
|
|
|
let code_action = lsp::CodeAction {
|
|
|
|
title: action.description.clone(),
|
|
|
|
kind: Some(lsp::CodeActionKind::QUICKFIX),
|
|
|
|
diagnostics: Some(vec![diagnostic.clone()]),
|
|
|
|
edit,
|
|
|
|
command: None,
|
|
|
|
is_preferred: None,
|
|
|
|
disabled: None,
|
|
|
|
data: None,
|
|
|
|
};
|
2021-02-11 23:17:48 -05:00
|
|
|
self.actions.retain(|i| match i {
|
|
|
|
CodeActionKind::Tsc(c, a) => {
|
|
|
|
!(action.fix_name == a.fix_name && code_action.edit == c.edit)
|
|
|
|
}
|
|
|
|
_ => true,
|
2021-02-04 13:53:02 -05:00
|
|
|
});
|
2021-02-11 23:17:48 -05:00
|
|
|
self
|
|
|
|
.actions
|
|
|
|
.push(CodeActionKind::Tsc(code_action, action.clone()));
|
2021-02-04 13:53:02 -05:00
|
|
|
|
|
|
|
if let Some(fix_id) = &action.fix_id {
|
2021-02-11 23:17:48 -05:00
|
|
|
if let Some(CodeActionKind::Tsc(existing_fix_all, existing_action)) =
|
|
|
|
self.fix_all_actions.get(&FixAllKind::Tsc(fix_id.clone()))
|
2021-02-04 13:53:02 -05:00
|
|
|
{
|
2021-02-11 23:17:48 -05:00
|
|
|
self.actions.retain(|i| match i {
|
|
|
|
CodeActionKind::Tsc(c, _) => c != existing_fix_all,
|
|
|
|
_ => true,
|
|
|
|
});
|
|
|
|
self.actions.push(CodeActionKind::Tsc(
|
|
|
|
existing_fix_all.clone(),
|
|
|
|
existing_action.clone(),
|
|
|
|
));
|
2021-02-04 13:53:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a TypeScript action to the actions as a "fix all" action, where it
|
|
|
|
/// will fix all occurrences of the diagnostic in the file.
|
2021-02-05 15:10:53 -05:00
|
|
|
pub fn add_ts_fix_all_action(
|
2021-02-04 13:53:02 -05:00
|
|
|
&mut self,
|
|
|
|
action: &tsc::CodeFixAction,
|
2021-02-05 15:10:53 -05:00
|
|
|
specifier: &ModuleSpecifier,
|
2021-02-04 13:53:02 -05:00
|
|
|
diagnostic: &lsp::Diagnostic,
|
2021-02-05 15:10:53 -05:00
|
|
|
) {
|
|
|
|
let data = Some(json!({
|
|
|
|
"specifier": specifier,
|
|
|
|
"fixId": action.fix_id,
|
|
|
|
}));
|
2021-02-04 13:53:02 -05:00
|
|
|
let title = if let Some(description) = &action.fix_all_description {
|
|
|
|
description.clone()
|
|
|
|
} else {
|
|
|
|
format!("{} (Fix all in file)", action.description)
|
|
|
|
};
|
|
|
|
|
|
|
|
let code_action = lsp::CodeAction {
|
|
|
|
title,
|
|
|
|
kind: Some(lsp::CodeActionKind::QUICKFIX),
|
|
|
|
diagnostics: Some(vec![diagnostic.clone()]),
|
2021-02-05 15:10:53 -05:00
|
|
|
edit: None,
|
2021-02-04 13:53:02 -05:00
|
|
|
command: None,
|
|
|
|
is_preferred: None,
|
|
|
|
disabled: None,
|
2021-02-05 15:10:53 -05:00
|
|
|
data,
|
2021-02-04 13:53:02 -05:00
|
|
|
};
|
2021-02-11 23:17:48 -05:00
|
|
|
if let Some(CodeActionKind::Tsc(existing, _)) = self
|
|
|
|
.fix_all_actions
|
|
|
|
.get(&FixAllKind::Tsc(action.fix_id.clone().unwrap()))
|
2021-02-04 13:53:02 -05:00
|
|
|
{
|
2021-02-11 23:17:48 -05:00
|
|
|
self.actions.retain(|i| match i {
|
|
|
|
CodeActionKind::Tsc(c, _) => c != existing,
|
|
|
|
_ => true,
|
|
|
|
});
|
2021-02-04 13:53:02 -05:00
|
|
|
}
|
2021-02-11 23:17:48 -05:00
|
|
|
self
|
|
|
|
.actions
|
|
|
|
.push(CodeActionKind::Tsc(code_action.clone(), action.clone()));
|
2021-02-04 13:53:02 -05:00
|
|
|
self.fix_all_actions.insert(
|
2021-02-11 23:17:48 -05:00
|
|
|
FixAllKind::Tsc(action.fix_id.clone().unwrap()),
|
|
|
|
CodeActionKind::Tsc(code_action, action.clone()),
|
2021-02-04 13:53:02 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Move out the code actions and return them as a `CodeActionResponse`.
|
|
|
|
pub fn get_response(self) -> lsp::CodeActionResponse {
|
|
|
|
self
|
|
|
|
.actions
|
|
|
|
.into_iter()
|
2021-02-11 23:17:48 -05:00
|
|
|
.map(|i| match i {
|
|
|
|
CodeActionKind::Tsc(c, _) => lsp::CodeActionOrCommand::CodeAction(c),
|
|
|
|
CodeActionKind::Deno(c) => lsp::CodeActionOrCommand::CodeAction(c),
|
2021-06-21 02:43:35 -04:00
|
|
|
CodeActionKind::DenoLint(c) => lsp::CodeActionOrCommand::CodeAction(c),
|
2021-02-11 23:17:48 -05:00
|
|
|
})
|
2021-02-04 13:53:02 -05:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Determine if a action can be converted into a "fix all" action.
|
|
|
|
pub fn is_fix_all_action(
|
|
|
|
&self,
|
|
|
|
action: &tsc::CodeFixAction,
|
|
|
|
diagnostic: &lsp::Diagnostic,
|
2021-02-06 07:39:01 -05:00
|
|
|
file_diagnostics: &[lsp::Diagnostic],
|
2021-02-04 13:53:02 -05:00
|
|
|
) -> bool {
|
|
|
|
// If the action does not have a fix id (indicating it can be "bundled up")
|
|
|
|
// or if the collection already contains a "bundled" action return false
|
|
|
|
if action.fix_id.is_none()
|
|
|
|
|| self
|
|
|
|
.fix_all_actions
|
2021-02-11 23:17:48 -05:00
|
|
|
.contains_key(&FixAllKind::Tsc(action.fix_id.clone().unwrap()))
|
2021-02-04 13:53:02 -05:00
|
|
|
{
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
// else iterate over the diagnostic in the file and see if there are any
|
|
|
|
// other diagnostics that could be bundled together in a "fix all" code
|
|
|
|
// action
|
|
|
|
file_diagnostics.iter().any(|d| {
|
2021-02-06 07:39:01 -05:00
|
|
|
if d == diagnostic || d.code.is_none() || diagnostic.code.is_none() {
|
2021-02-04 13:53:02 -05:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
d.code == diagnostic.code
|
|
|
|
|| is_equivalent_code(&d.code, &diagnostic.code)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the `.is_preferred` flag on code actions, this should be only executed
|
|
|
|
/// when all actions are added to the collection.
|
|
|
|
pub fn set_preferred_fixes(&mut self) {
|
|
|
|
let actions = self.actions.clone();
|
2021-02-11 23:17:48 -05:00
|
|
|
for entry in self.actions.iter_mut() {
|
|
|
|
if let CodeActionKind::Tsc(code_action, action) = entry {
|
|
|
|
if action.fix_id.is_some() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if let Some((fix_priority, only_one)) =
|
|
|
|
PREFERRED_FIXES.get(action.fix_name.as_str())
|
|
|
|
{
|
|
|
|
code_action.is_preferred =
|
|
|
|
Some(is_preferred(action, &actions, *fix_priority, *only_one));
|
|
|
|
}
|
2021-02-04 13:53:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 02:43:35 -04:00
|
|
|
/// Prepend the whitespace characters found at the start of line_content to content.
|
|
|
|
fn prepend_whitespace(content: String, line_content: Option<String>) -> String {
|
|
|
|
if let Some(line) = line_content {
|
|
|
|
let whitespaces =
|
|
|
|
line.chars().position(|c| !c.is_whitespace()).unwrap_or(0);
|
|
|
|
let whitespace = &line[0..whitespaces];
|
|
|
|
format!("{}{}", &whitespace, content)
|
|
|
|
} else {
|
|
|
|
content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2021-05-25 18:15:09 -04:00
|
|
|
#[test]
|
|
|
|
fn test_reference_to_diagnostic() {
|
|
|
|
let range = Range {
|
|
|
|
start: Position {
|
|
|
|
line: 1,
|
|
|
|
character: 1,
|
|
|
|
},
|
|
|
|
end: Position {
|
|
|
|
line: 2,
|
|
|
|
character: 2,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let test_cases = [
|
|
|
|
(
|
|
|
|
Reference {
|
|
|
|
category: Category::Lint {
|
|
|
|
message: "message1".to_string(),
|
|
|
|
code: "code1".to_string(),
|
|
|
|
hint: None,
|
|
|
|
},
|
|
|
|
range,
|
|
|
|
},
|
|
|
|
lsp::Diagnostic {
|
|
|
|
range,
|
2021-11-24 20:10:12 -05:00
|
|
|
severity: Some(lsp::DiagnosticSeverity::WARNING),
|
2021-05-25 18:15:09 -04:00
|
|
|
code: Some(lsp::NumberOrString::String("code1".to_string())),
|
|
|
|
source: Some("deno-lint".to_string()),
|
|
|
|
message: "message1".to_string(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Reference {
|
|
|
|
category: Category::Lint {
|
|
|
|
message: "message2".to_string(),
|
|
|
|
code: "code2".to_string(),
|
|
|
|
hint: Some("hint2".to_string()),
|
|
|
|
},
|
|
|
|
range,
|
|
|
|
},
|
|
|
|
lsp::Diagnostic {
|
|
|
|
range,
|
2021-11-24 20:10:12 -05:00
|
|
|
severity: Some(lsp::DiagnosticSeverity::WARNING),
|
2021-05-25 18:15:09 -04:00
|
|
|
code: Some(lsp::NumberOrString::String("code2".to_string())),
|
|
|
|
source: Some("deno-lint".to_string()),
|
|
|
|
message: "message2\nhint2".to_string(),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (input, expected) in test_cases.iter() {
|
|
|
|
let actual = input.to_diagnostic();
|
|
|
|
assert_eq!(&actual, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[test]
|
|
|
|
fn test_as_lsp_range() {
|
|
|
|
let fixture = deno_lint::diagnostic::Range {
|
|
|
|
start: deno_lint::diagnostic::Position {
|
2021-09-02 11:38:19 -04:00
|
|
|
line_index: 0,
|
|
|
|
column_index: 2,
|
2020-12-07 05:46:39 -05:00
|
|
|
byte_pos: 23,
|
|
|
|
},
|
|
|
|
end: deno_lint::diagnostic::Position {
|
2021-09-02 11:38:19 -04:00
|
|
|
line_index: 1,
|
|
|
|
column_index: 0,
|
2020-12-07 05:46:39 -05:00
|
|
|
byte_pos: 33,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let actual = as_lsp_range(&fixture);
|
|
|
|
assert_eq!(
|
|
|
|
actual,
|
2021-01-29 14:34:33 -05:00
|
|
|
lsp::Range {
|
|
|
|
start: lsp::Position {
|
2020-12-07 05:46:39 -05:00
|
|
|
line: 0,
|
|
|
|
character: 2,
|
|
|
|
},
|
2021-01-29 14:34:33 -05:00
|
|
|
end: lsp::Position {
|
2020-12-07 05:46:39 -05:00
|
|
|
line: 1,
|
|
|
|
character: 0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|