Skip to content

Commit

Permalink
feat(cli): support "types" when type checking (denoland#10999)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jun 21, 2021
1 parent cda15f2 commit 281c4cd
Show file tree
Hide file tree
Showing 31 changed files with 636 additions and 92 deletions.
4 changes: 4 additions & 0 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ fn create_compiler_snapshot(
}))
}),
);
js_runtime.register_op(
"op_cwd",
op_sync(move |_state, _args: Value, _: ()| Ok(json!("cache:https:///"))),
);
// using the same op that is used in `tsc.rs` for loading modules and reading
// files, but a slightly different implementation at build time.
js_runtime.register_op(
Expand Down
9 changes: 8 additions & 1 deletion cli/config_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ pub struct EmitConfigOptions {
pub jsx_fragment_factory: String,
}

/// There are certain compiler options that can impact what modules are part of
/// a module graph, which need to be deserialized into a structure for analysis.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CompilerOptions {
pub types: Option<Vec<String>>,
}

/// A structure that represents a set of options that were ignored and the
/// path those options came from.
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -90,7 +98,6 @@ pub const IGNORED_COMPILER_OPTIONS: &[&str] = &[
"sourceMap",
"sourceRoot",
"target",
"types",
"useDefineForClassFields",
];

Expand Down
20 changes: 17 additions & 3 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,25 @@ declare namespace Deno {
| "es2019"
| "es2020"
| "esnext";
/** List of names of type definitions to include. Defaults to `undefined`.
/** List of names of type definitions to include when type checking.
* Defaults to `undefined`.
*
* The type definitions are resolved according to the normal Deno resolution
* irrespective of if sources are provided on the call. Like other Deno
* modules, there is no "magical" resolution. For example:
* irrespective of if sources are provided on the call. In addition, unlike
* passing the `--config` option on startup, there is no base to resolve
* relative specifiers, so the specifiers here have to be fully qualified
* URLs or paths. For example:
*
* ```ts
* Deno.emit("./a.ts", {
* compilerOptions: {
* types: [
* "https://deno.land/x/pkg/types.d.ts",
* "/Users/me/pkg/types.d.ts",
* ]
* }
* });
* ```
*/
types?: string[];
/** Emit class fields with ECMAScript-standard semantics. Defaults to
Expand Down
5 changes: 5 additions & 0 deletions cli/lsp/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use deno_core::serde_json::Value;
use deno_core::url::Url;
use deno_core::ModuleSpecifier;
use log::error;
use lsp::WorkspaceFolder;
use lspower::lsp;
use std::collections::BTreeMap;
use std::collections::HashMap;
Expand Down Expand Up @@ -188,6 +189,7 @@ pub struct ConfigSnapshot {
pub client_capabilities: ClientCapabilities,
pub root_uri: Option<Url>,
pub settings: Settings,
pub workspace_folders: Option<Vec<lsp::WorkspaceFolder>>,
}

impl ConfigSnapshot {
Expand Down Expand Up @@ -218,6 +220,7 @@ pub struct Config {
pub root_uri: Option<Url>,
settings: Arc<RwLock<Settings>>,
tx: mpsc::Sender<ConfigRequest>,
pub workspace_folders: Option<Vec<WorkspaceFolder>>,
}

impl Config {
Expand Down Expand Up @@ -319,6 +322,7 @@ impl Config {
root_uri: None,
settings,
tx,
workspace_folders: None,
}
}

Expand All @@ -343,6 +347,7 @@ impl Config {
.try_read()
.map_err(|_| anyhow!("Error reading settings."))?
.clone(),
workspace_folders: self.workspace_folders.clone(),
})
}

Expand Down
33 changes: 24 additions & 9 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct StateSnapshot {
pub assets: Assets,
pub config: ConfigSnapshot,
pub documents: DocumentCache,
pub maybe_config_uri: Option<ModuleSpecifier>,
pub module_registries: registries::ModuleRegistry,
pub performance: Performance,
pub sources: Sources,
Expand All @@ -92,6 +93,9 @@ pub(crate) struct Inner {
module_registries: registries::ModuleRegistry,
/// The path to the module registries cache
module_registries_location: PathBuf,
/// An optional configuration file which has been specified in the client
/// options.
maybe_config_file: Option<ConfigFile>,
/// An optional URL which provides the location of a TypeScript configuration
/// file which will be used by the Deno LSP.
maybe_config_uri: Option<Url>,
Expand Down Expand Up @@ -138,6 +142,7 @@ impl Inner {
config,
diagnostics_server,
documents: Default::default(),
maybe_config_file: Default::default(),
maybe_config_uri: Default::default(),
maybe_import_map: Default::default(),
maybe_import_map_uri: Default::default(),
Expand Down Expand Up @@ -326,6 +331,7 @@ impl Inner {
LspError::internal_error()
})?,
documents: self.documents.clone(),
maybe_config_uri: self.maybe_config_uri.clone(),
module_registries: self.module_registries.clone(),
performance: self.performance.clone(),
sources: self.sources.clone(),
Expand Down Expand Up @@ -477,6 +483,7 @@ impl Inner {
};
let (value, maybe_ignored_options) = config_file.as_compiler_options()?;
tsconfig.merge(&value);
self.maybe_config_file = Some(config_file);
self.maybe_config_uri = Some(config_url);
if let Some(ignored_options) = maybe_ignored_options {
// TODO(@kitsonk) turn these into diagnostics that can be sent to the
Expand Down Expand Up @@ -2281,20 +2288,28 @@ impl Inner {
if !params.uris.is_empty() {
for identifier in &params.uris {
let specifier = self.url_map.normalize_url(&identifier.uri);
sources::cache(&specifier, &self.maybe_import_map)
.await
.map_err(|err| {
error!("{}", err);
LspError::internal_error()
})?;
}
} else {
sources::cache(&referrer, &self.maybe_import_map)
sources::cache(
&specifier,
&self.maybe_import_map,
&self.maybe_config_file,
)
.await
.map_err(|err| {
error!("{}", err);
LspError::internal_error()
})?;
}
} else {
sources::cache(
&referrer,
&self.maybe_import_map,
&self.maybe_config_file,
)
.await
.map_err(|err| {
error!("{}", err);
LspError::internal_error()
})?;
}
// now that we have dependencies loaded, we need to re-analyze them and
// invalidate some diagnostics
Expand Down
3 changes: 3 additions & 0 deletions cli/lsp/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::analysis;
use super::text::LineIndex;
use super::tsc;

use crate::config_file::ConfigFile;
use crate::file_fetcher::get_source_from_bytes;
use crate::file_fetcher::map_content_type;
use crate::file_fetcher::SUPPORTED_SCHEMES;
Expand Down Expand Up @@ -33,6 +34,7 @@ use tsc::NavigationTree;
pub async fn cache(
specifier: &ModuleSpecifier,
maybe_import_map: &Option<ImportMap>,
maybe_config_file: &Option<ConfigFile>,
) -> Result<(), AnyError> {
let program_state = Arc::new(ProgramState::build(Default::default()).await?);
let handler = Arc::new(Mutex::new(FetchHandler::new(
Expand All @@ -41,6 +43,7 @@ pub async fn cache(
Permissions::allow_all(),
)?));
let mut builder = GraphBuilder::new(handler, maybe_import_map.clone(), None);
builder.analyze_config_file(maybe_config_file).await?;
builder.add(specifier, false).await
}

Expand Down
Loading

0 comments on commit 281c4cd

Please sign in to comment.