2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 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;
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
use crate::ast;
|
|
|
|
use crate::import_map::ImportMap;
|
|
|
|
use crate::media_type::MediaType;
|
|
|
|
use crate::module_graph::parse_deno_types;
|
|
|
|
use crate::module_graph::parse_ts_reference;
|
|
|
|
use crate::module_graph::TypeScriptReference;
|
|
|
|
use crate::tools::lint::create_linter;
|
|
|
|
|
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;
|
|
|
|
use deno_core::serde::Serialize;
|
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;
|
2021-02-09 17:46:12 -05:00
|
|
|
use deno_core::ModuleResolutionError;
|
2020-12-07 05:46:39 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_lint::rules;
|
2021-01-29 14:34:33 -05:00
|
|
|
use lspower::lsp;
|
|
|
|
use lspower::lsp::Position;
|
|
|
|
use lspower::lsp::Range;
|
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-02-09 17:46:12 -05:00
|
|
|
use std::fmt;
|
2020-12-07 05:46:39 -05:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2021-02-04 13:53:02 -05:00
|
|
|
lazy_static! {
|
|
|
|
/// Diagnostic error codes which actually are the same, and so when grouping
|
|
|
|
/// fixes we treat them the same.
|
|
|
|
static ref FIX_ALL_ERROR_CODES: HashMap<&'static str, &'static str> =
|
2021-02-12 06:49:42 -05:00
|
|
|
(&[("2339", "2339"), ("2345", "2339"),])
|
2021-02-04 13:53:02 -05:00
|
|
|
.iter()
|
2021-02-12 06:49:42 -05:00
|
|
|
.cloned()
|
2021-02-04 13:53:02 -05:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
/// Fixes which help determine if there is a preferred fix when there are
|
|
|
|
/// multiple fixes available.
|
2021-02-12 06:49:42 -05:00
|
|
|
static ref PREFERRED_FIXES: HashMap<&'static str, (u32, bool)> = (&[
|
2021-02-04 13:53:02 -05:00
|
|
|
("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)),
|
2021-02-12 06:49:42 -05:00
|
|
|
])
|
2021-02-04 13:53:02 -05:00
|
|
|
.iter()
|
2021-02-12 06:49:42 -05:00
|
|
|
.cloned()
|
2021-02-04 13:53:02 -05:00
|
|
|
.collect();
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
/// Category of self-generated diagnostic messages (those not coming from)
|
|
|
|
/// TypeScript.
|
|
|
|
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.
|
|
|
|
pub struct Reference {
|
|
|
|
category: Category,
|
|
|
|
range: Range,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_lsp_range(range: &deno_lint::diagnostic::Range) -> Range {
|
|
|
|
Range {
|
|
|
|
start: Position {
|
|
|
|
line: (range.start.line - 1) as u32,
|
|
|
|
character: range.start.col as u32,
|
|
|
|
},
|
|
|
|
end: Position {
|
|
|
|
line: (range.end.line - 1) as u32,
|
|
|
|
character: range.end.col as u32,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_lint_references(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
media_type: &MediaType,
|
|
|
|
source_code: &str,
|
|
|
|
) -> Result<Vec<Reference>, AnyError> {
|
|
|
|
let syntax = ast::get_syntax(media_type);
|
|
|
|
let lint_rules = rules::get_recommended_rules();
|
|
|
|
let mut linter = create_linter(syntax, lint_rules);
|
|
|
|
// TODO(@kitsonk) we should consider caching the swc source file versions for
|
|
|
|
// reuse by other processes
|
|
|
|
let (_, lint_diagnostics) =
|
|
|
|
linter.lint(specifier.to_string(), source_code.to_string())?;
|
|
|
|
|
|
|
|
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(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn references_to_diagnostics(
|
|
|
|
references: Vec<Reference>,
|
2021-01-29 14:34:33 -05:00
|
|
|
) -> Vec<lsp::Diagnostic> {
|
2020-12-07 05:46:39 -05:00
|
|
|
references
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| match r.category {
|
2021-01-29 14:34:33 -05:00
|
|
|
Category::Lint { message, code, .. } => lsp::Diagnostic {
|
2020-12-07 05:46:39 -05:00
|
|
|
range: r.range,
|
2021-01-29 14:34:33 -05:00
|
|
|
severity: Some(lsp::DiagnosticSeverity::Warning),
|
|
|
|
code: Some(lsp::NumberOrString::String(code)),
|
2020-12-07 05:46:39 -05:00
|
|
|
code_description: None,
|
|
|
|
source: Some("deno-lint".to_string()),
|
|
|
|
message,
|
|
|
|
related_information: None,
|
|
|
|
tags: None, // we should tag unused code
|
|
|
|
data: None,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
|
|
|
pub struct Dependency {
|
|
|
|
pub is_dynamic: bool,
|
2020-12-24 05:53:03 -05:00
|
|
|
pub maybe_code: Option<ResolvedDependency>,
|
|
|
|
pub maybe_code_specifier_range: Option<Range>,
|
|
|
|
pub maybe_type: Option<ResolvedDependency>,
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:46:12 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum ResolvedDependencyErr {
|
|
|
|
InvalidDowngrade,
|
|
|
|
InvalidLocalImport,
|
|
|
|
InvalidSpecifier(ModuleResolutionError),
|
|
|
|
Missing,
|
|
|
|
}
|
|
|
|
|
2021-02-11 23:17:48 -05:00
|
|
|
impl ResolvedDependencyErr {
|
|
|
|
pub fn as_code(&self) -> lsp::NumberOrString {
|
|
|
|
match self {
|
|
|
|
Self::InvalidDowngrade => {
|
|
|
|
lsp::NumberOrString::String("invalid-downgrade".to_string())
|
|
|
|
}
|
|
|
|
Self::InvalidLocalImport => {
|
|
|
|
lsp::NumberOrString::String("invalid-local-import".to_string())
|
|
|
|
}
|
|
|
|
Self::InvalidSpecifier(_) => {
|
|
|
|
lsp::NumberOrString::String("invalid-specifier".to_string())
|
|
|
|
}
|
|
|
|
Self::Missing => lsp::NumberOrString::String("missing".to_string()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:46:12 -05:00
|
|
|
impl fmt::Display for ResolvedDependencyErr {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::InvalidDowngrade => {
|
|
|
|
write!(f, "HTTPS modules cannot import HTTP modules.")
|
|
|
|
}
|
|
|
|
Self::InvalidLocalImport => {
|
|
|
|
write!(f, "Remote modules cannot import local modules.")
|
|
|
|
}
|
|
|
|
Self::InvalidSpecifier(err) => write!(f, "{}", err),
|
|
|
|
Self::Missing => write!(f, "The module is unexpectedly missing."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2020-12-24 05:53:03 -05:00
|
|
|
pub enum ResolvedDependency {
|
2020-12-07 05:46:39 -05:00
|
|
|
Resolved(ModuleSpecifier),
|
2021-02-09 17:46:12 -05:00
|
|
|
Err(ResolvedDependencyErr),
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_import(
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &ModuleSpecifier,
|
2020-12-21 08:44:26 -05:00
|
|
|
maybe_import_map: &Option<ImportMap>,
|
2020-12-24 05:53:03 -05:00
|
|
|
) -> ResolvedDependency {
|
2020-12-07 05:46:39 -05:00
|
|
|
let maybe_mapped = if let Some(import_map) = maybe_import_map {
|
2020-12-21 08:44:26 -05:00
|
|
|
if let Ok(maybe_specifier) =
|
|
|
|
import_map.resolve(specifier, referrer.as_str())
|
2020-12-07 05:46:39 -05:00
|
|
|
{
|
|
|
|
maybe_specifier
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let remapped = maybe_mapped.is_some();
|
|
|
|
let specifier = if let Some(remapped) = maybe_mapped {
|
|
|
|
remapped
|
|
|
|
} else {
|
2021-02-17 13:47:18 -05:00
|
|
|
match deno_core::resolve_import(specifier, referrer.as_str()) {
|
2020-12-07 05:46:39 -05:00
|
|
|
Ok(resolved) => resolved,
|
2021-02-09 17:46:12 -05:00
|
|
|
Err(err) => {
|
|
|
|
return ResolvedDependency::Err(
|
|
|
|
ResolvedDependencyErr::InvalidSpecifier(err),
|
|
|
|
)
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
};
|
2021-02-17 13:47:18 -05:00
|
|
|
let referrer_scheme = referrer.scheme();
|
|
|
|
let specifier_scheme = specifier.scheme();
|
2020-12-07 05:46:39 -05:00
|
|
|
if referrer_scheme == "https" && specifier_scheme == "http" {
|
2021-02-09 17:46:12 -05:00
|
|
|
return ResolvedDependency::Err(ResolvedDependencyErr::InvalidDowngrade);
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
if (referrer_scheme == "https" || referrer_scheme == "http")
|
|
|
|
&& !(specifier_scheme == "https" || specifier_scheme == "http")
|
|
|
|
&& !remapped
|
|
|
|
{
|
2021-02-09 17:46:12 -05:00
|
|
|
return ResolvedDependency::Err(ResolvedDependencyErr::InvalidLocalImport);
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
2020-12-24 05:53:03 -05:00
|
|
|
ResolvedDependency::Resolved(specifier)
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@kitsonk) a lot of this logic is duplicated in module_graph.rs in
|
|
|
|
// Module::parse() and should be refactored out to a common function.
|
|
|
|
pub fn analyze_dependencies(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
source: &str,
|
|
|
|
media_type: &MediaType,
|
2020-12-21 08:44:26 -05:00
|
|
|
maybe_import_map: &Option<ImportMap>,
|
2020-12-24 05:53:03 -05:00
|
|
|
) -> Option<(HashMap<String, Dependency>, Option<ResolvedDependency>)> {
|
2020-12-07 05:46:39 -05:00
|
|
|
let specifier_str = specifier.to_string();
|
|
|
|
let source_map = Rc::new(swc_common::SourceMap::default());
|
|
|
|
let mut maybe_type = None;
|
|
|
|
if let Ok(parsed_module) =
|
|
|
|
ast::parse_with_source_map(&specifier_str, source, &media_type, source_map)
|
|
|
|
{
|
|
|
|
let mut dependencies = HashMap::<String, Dependency>::new();
|
|
|
|
|
|
|
|
// Parse leading comments for supported triple slash references.
|
|
|
|
for comment in parsed_module.get_leading_comments().iter() {
|
|
|
|
if let Some(ts_reference) = parse_ts_reference(&comment.text) {
|
|
|
|
match ts_reference {
|
|
|
|
TypeScriptReference::Path(import) => {
|
|
|
|
let dep = dependencies.entry(import.clone()).or_default();
|
|
|
|
let resolved_import =
|
2020-12-21 08:44:26 -05:00
|
|
|
resolve_import(&import, specifier, maybe_import_map);
|
2020-12-07 05:46:39 -05:00
|
|
|
dep.maybe_code = Some(resolved_import);
|
|
|
|
}
|
|
|
|
TypeScriptReference::Types(import) => {
|
|
|
|
let resolved_import =
|
2020-12-21 08:44:26 -05:00
|
|
|
resolve_import(&import, specifier, maybe_import_map);
|
2020-12-07 05:46:39 -05:00
|
|
|
if media_type == &MediaType::JavaScript
|
|
|
|
|| media_type == &MediaType::JSX
|
|
|
|
{
|
|
|
|
maybe_type = Some(resolved_import)
|
|
|
|
} else {
|
|
|
|
let dep = dependencies.entry(import).or_default();
|
|
|
|
dep.maybe_type = Some(resolved_import);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse ES and type only imports
|
|
|
|
let descriptors = parsed_module.analyze_dependencies();
|
|
|
|
for desc in descriptors.into_iter().filter(|desc| {
|
|
|
|
desc.kind != swc_ecmascript::dep_graph::DependencyKind::Require
|
|
|
|
}) {
|
|
|
|
let resolved_import =
|
2020-12-21 08:44:26 -05:00
|
|
|
resolve_import(&desc.specifier, specifier, maybe_import_map);
|
2020-12-07 05:46:39 -05:00
|
|
|
|
2021-02-09 17:46:12 -05:00
|
|
|
let maybe_resolved_type_dependency =
|
|
|
|
// Check for `@deno-types` pragmas that affect the import
|
2020-12-07 05:46:39 -05:00
|
|
|
if let Some(comment) = desc.leading_comments.last() {
|
|
|
|
if let Some(deno_types) = parse_deno_types(&comment.text).as_ref() {
|
2020-12-21 08:44:26 -05:00
|
|
|
Some(resolve_import(deno_types, specifier, maybe_import_map))
|
2020-12-07 05:46:39 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let dep = dependencies.entry(desc.specifier.to_string()).or_default();
|
|
|
|
dep.is_dynamic = desc.is_dynamic;
|
|
|
|
match desc.kind {
|
|
|
|
swc_ecmascript::dep_graph::DependencyKind::ExportType
|
|
|
|
| swc_ecmascript::dep_graph::DependencyKind::ImportType => {
|
|
|
|
dep.maybe_type = Some(resolved_import)
|
|
|
|
}
|
2020-12-24 05:53:03 -05:00
|
|
|
_ => {
|
|
|
|
dep.maybe_code_specifier_range = Some(Range {
|
|
|
|
start: Position {
|
|
|
|
line: (desc.specifier_line - 1) as u32,
|
|
|
|
character: desc.specifier_col as u32,
|
|
|
|
},
|
|
|
|
end: Position {
|
|
|
|
line: (desc.specifier_line - 1) as u32,
|
|
|
|
character: (desc.specifier_col
|
|
|
|
+ desc.specifier.chars().count()
|
|
|
|
+ 2) as u32,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
dep.maybe_code = Some(resolved_import);
|
|
|
|
}
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
2021-02-09 17:46:12 -05:00
|
|
|
if maybe_resolved_type_dependency.is_some() && dep.maybe_type.is_none() {
|
|
|
|
dep.maybe_type = maybe_resolved_type_dependency;
|
2020-12-07 05:46:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some((dependencies, maybe_type))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:30:41 -05:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
pub enum CodeLensSource {
|
2021-02-08 05:45:10 -05:00
|
|
|
#[serde(rename = "implementations")]
|
|
|
|
Implementations,
|
2021-01-31 22:30:41 -05:00
|
|
|
#[serde(rename = "references")]
|
|
|
|
References,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct CodeLensData {
|
|
|
|
pub source: CodeLensSource,
|
|
|
|
pub specifier: ModuleSpecifier,
|
|
|
|
}
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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),
|
|
|
|
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,
|
|
|
|
diagnostic: &lsp::Diagnostic,
|
|
|
|
) -> Result<(), AnyError> {
|
|
|
|
if let Some(data) = diagnostic.data.clone() {
|
|
|
|
let fix_data: DenoFixData = serde_json::from_value(data)?;
|
2021-02-17 23:37:05 -05:00
|
|
|
let title = if matches!(&diagnostic.code, Some(lsp::NumberOrString::String(code)) if code == "no-cache-data")
|
|
|
|
{
|
|
|
|
"Cache the data URL and its dependencies.".to_string()
|
|
|
|
} else {
|
|
|
|
format!("Cache \"{}\" and its dependencies.", fix_data.specifier)
|
|
|
|
};
|
2021-02-11 23:17:48 -05:00
|
|
|
let code_action = lsp::CodeAction {
|
2021-02-17 23:37:05 -05:00
|
|
|
title,
|
2021-02-11 23:17:48 -05:00
|
|
|
kind: Some(lsp::CodeActionKind::QUICKFIX),
|
|
|
|
diagnostics: Some(vec![diagnostic.clone()]),
|
|
|
|
edit: None,
|
|
|
|
command: Some(lsp::Command {
|
|
|
|
title: "".to_string(),
|
|
|
|
command: "deno.cache".to_string(),
|
|
|
|
arguments: Some(vec![json!([fix_data.specifier])]),
|
|
|
|
}),
|
|
|
|
is_preferred: None,
|
|
|
|
disabled: None,
|
|
|
|
data: None,
|
|
|
|
};
|
|
|
|
self.actions.push(CodeActionKind::Deno(code_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,
|
|
|
|
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-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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-02-17 13:47:18 -05:00
|
|
|
use deno_core::resolve_url;
|
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 {
|
|
|
|
line: 1,
|
|
|
|
col: 2,
|
|
|
|
byte_pos: 23,
|
|
|
|
},
|
|
|
|
end: deno_lint::diagnostic::Position {
|
|
|
|
line: 2,
|
|
|
|
col: 0,
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_analyze_dependencies() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = resolve_url("file:///a.ts").expect("bad specifier");
|
2020-12-07 05:46:39 -05:00
|
|
|
let source = r#"import {
|
|
|
|
Application,
|
|
|
|
Context,
|
|
|
|
Router,
|
|
|
|
Status,
|
|
|
|
} from "https://deno.land/x/oak@v6.3.2/mod.ts";
|
2021-01-29 14:34:33 -05:00
|
|
|
|
2020-12-07 05:46:39 -05:00
|
|
|
// @deno-types="https://deno.land/x/types/react/index.d.ts";
|
|
|
|
import * as React from "https://cdn.skypack.dev/react";
|
|
|
|
"#;
|
|
|
|
let actual =
|
2020-12-21 08:44:26 -05:00
|
|
|
analyze_dependencies(&specifier, source, &MediaType::TypeScript, &None);
|
2020-12-07 05:46:39 -05:00
|
|
|
assert!(actual.is_some());
|
|
|
|
let (actual, maybe_type) = actual.unwrap();
|
|
|
|
assert!(maybe_type.is_none());
|
|
|
|
assert_eq!(actual.len(), 2);
|
|
|
|
assert_eq!(
|
|
|
|
actual.get("https://cdn.skypack.dev/react").cloned(),
|
|
|
|
Some(Dependency {
|
|
|
|
is_dynamic: false,
|
2020-12-24 05:53:03 -05:00
|
|
|
maybe_code: Some(ResolvedDependency::Resolved(
|
2021-02-17 13:47:18 -05:00
|
|
|
resolve_url("https://cdn.skypack.dev/react").unwrap()
|
2020-12-07 05:46:39 -05:00
|
|
|
)),
|
2020-12-24 05:53:03 -05:00
|
|
|
maybe_type: Some(ResolvedDependency::Resolved(
|
2021-02-17 13:47:18 -05:00
|
|
|
resolve_url("https://deno.land/x/types/react/index.d.ts").unwrap()
|
2020-12-07 05:46:39 -05:00
|
|
|
)),
|
2020-12-24 05:53:03 -05:00
|
|
|
maybe_code_specifier_range: Some(Range {
|
|
|
|
start: Position {
|
|
|
|
line: 8,
|
|
|
|
character: 27,
|
|
|
|
},
|
|
|
|
end: Position {
|
|
|
|
line: 8,
|
|
|
|
character: 58,
|
|
|
|
}
|
|
|
|
}),
|
2020-12-07 05:46:39 -05:00
|
|
|
})
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
actual.get("https://deno.land/x/oak@v6.3.2/mod.ts").cloned(),
|
|
|
|
Some(Dependency {
|
|
|
|
is_dynamic: false,
|
2020-12-24 05:53:03 -05:00
|
|
|
maybe_code: Some(ResolvedDependency::Resolved(
|
2021-02-17 13:47:18 -05:00
|
|
|
resolve_url("https://deno.land/x/oak@v6.3.2/mod.ts").unwrap()
|
2020-12-07 05:46:39 -05:00
|
|
|
)),
|
|
|
|
maybe_type: None,
|
2020-12-24 05:53:03 -05:00
|
|
|
maybe_code_specifier_range: Some(Range {
|
|
|
|
start: Position {
|
|
|
|
line: 5,
|
|
|
|
character: 11,
|
|
|
|
},
|
|
|
|
end: Position {
|
|
|
|
line: 5,
|
|
|
|
character: 50,
|
|
|
|
}
|
|
|
|
}),
|
2020-12-07 05:46:39 -05:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|