Skip to content

Commit

Permalink
refactor(cli): migrate runtime compile/bundle to new infrastructure (d…
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored and jannes committed Dec 1, 2020
1 parent cf74fb2 commit 9a83c25
Show file tree
Hide file tree
Showing 23 changed files with 852 additions and 2,770 deletions.
36 changes: 31 additions & 5 deletions cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

use crate::colors;

use deno_core::serde::Deserialize;
use deno_core::serde::Deserializer;
use deno_core::serde::Serialize;
use deno_core::serde::Serializer;
use regex::Regex;
use serde::Deserialize;
use serde::Deserializer;
use std::error::Error;
use std::fmt;

Expand Down Expand Up @@ -157,6 +159,21 @@ impl<'de> Deserialize<'de> for DiagnosticCategory {
}
}

impl Serialize for DiagnosticCategory {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let value = match self {
DiagnosticCategory::Warning => 0 as i32,
DiagnosticCategory::Error => 1 as i32,
DiagnosticCategory::Suggestion => 2 as i32,
DiagnosticCategory::Message => 3 as i32,
};
Serialize::serialize(&value, serializer)
}
}

impl From<i64> for DiagnosticCategory {
fn from(value: i64) -> Self {
match value {
Expand All @@ -169,7 +186,7 @@ impl From<i64> for DiagnosticCategory {
}
}

#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DiagnosticMessageChain {
message_text: String,
Expand All @@ -196,14 +213,14 @@ impl DiagnosticMessageChain {
}
}

#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Position {
pub line: u64,
pub character: u64,
}

#[derive(Debug, Deserialize, Clone, Eq, PartialEq)]
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Diagnostic {
pub category: DiagnosticCategory,
Expand Down Expand Up @@ -367,6 +384,15 @@ impl<'de> Deserialize<'de> for Diagnostics {
}
}

impl Serialize for Diagnostics {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Serialize::serialize(&self.0, serializer)
}
}

impl fmt::Display for Diagnostics {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut i = 0;
Expand Down
27 changes: 13 additions & 14 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,6 @@ declare namespace Deno {
/** Provide full support for iterables in `for..of`, spread and
* destructuring when targeting ES5 or ES3. Defaults to `false`. */
downlevelIteration?: boolean;
/** Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files.
* Defaults to `false`. */
emitBOM?: boolean;
/** Only emit `.d.ts` declaration files. Defaults to `false`. */
emitDeclarationOnly?: boolean;
/** Emit design-type metadata for decorated declarations in source. See issue
Expand All @@ -316,29 +313,32 @@ declare namespace Deno {
* ecosystem compatibility and enable `allowSyntheticDefaultImports` for type
* system compatibility. Defaults to `true`. */
esModuleInterop?: boolean;
/** Enables experimental support for ES decorators. Defaults to `false`. */
/** Enables experimental support for ES decorators. Defaults to `true`. */
experimentalDecorators?: boolean;
/** Import emit helpers (e.g. `__extends`, `__rest`, etc..) from
* [tslib](https://www.npmjs.com/package/tslib). */
importHelpers?: boolean;
/** Emit a single file with source maps instead of having a separate file.
* Defaults to `false`. */
inlineSourceMap?: boolean;
/** Emit the source alongside the source maps within a single file; requires
* `inlineSourceMap` or `sourceMap` to be set. Defaults to `false`. */
inlineSources?: boolean;
/** Perform additional checks to ensure that transpile only would be safe.
* Defaults to `false`. */
* Defaults to `true`. */
isolatedModules?: boolean;
/** Support JSX in `.tsx` files: `"react"`, `"preserve"`, `"react-native"`.
* Defaults to `"react"`. */
jsx?: "react" | "preserve" | "react-native";
/** Specify the JSX factory function to use when targeting react JSX emit,
* e.g. `React.createElement` or `h`. Defaults to `React.createElement`. */
jsxFactory?: string;
/** Specify the JSX fragment factory function to use when targeting react
* JSX emit, e.g. `Fragment`. Defaults to `React.Fragment`. */
jsxFragmentFactory?: string;
/** Resolve keyof to string valued property names only (no numbers or
* symbols). Defaults to `false`. */
keyofStringsOnly?: string;
/** Emit class fields with ECMAScript-standard semantics. Defaults to `false`.
*/
useDefineForClassFields?: boolean;
/** List of library files to be included in the compilation. If omitted,
* then the Deno main runtime libs are used. */
lib?: string[];
Expand Down Expand Up @@ -389,10 +389,6 @@ declare namespace Deno {
noUnusedLocals?: boolean;
/** Report errors on unused parameters. Defaults to `false`. */
noUnusedParameters?: boolean;
/** Redirect output structure to the directory. This only impacts
* `Deno.compile` and only changes the emitted file names. Defaults to
* `undefined`. */
outDir?: string;
/** List of path mapping entries for module names to locations relative to the
* `baseUrl`. Defaults to `undefined`. */
paths?: Record<string, string[]>;
Expand All @@ -402,8 +398,6 @@ declare namespace Deno {
/** Remove all comments except copy-right header comments beginning with
* `/*!`. Defaults to `true`. */
removeComments?: boolean;
/** Include modules imported with `.json` extension. Defaults to `true`. */
resolveJsonModule?: boolean;
/** Specifies the root directory of input files. Only use to control the
* output directory structure with `outDir`. Defaults to `undefined`. */
rootDir?: string;
Expand All @@ -418,6 +412,8 @@ declare namespace Deno {
* specified will be embedded in the sourceMap to direct the debugger where
* the source files will be located. Defaults to `undefined`. */
sourceRoot?: string;
/** Skip type checking of all declaration files (`*.d.ts`). */
skipLibCheck?: boolean;
/** Enable all strict type checking options. Enabling `strict` enables
* `noImplicitAny`, `noImplicitThis`, `alwaysStrict`, `strictBindCallApply`,
* `strictNullChecks`, `strictFunctionTypes` and
Expand Down Expand Up @@ -472,6 +468,9 @@ declare namespace Deno {
* ```
*/
types?: string[];
/** Emit class fields with ECMAScript-standard semantics. Defaults to
* `false`. */
useDefineForClassFields?: boolean;
}

/** **UNSTABLE**: new API, yet to be vetted.
Expand Down
34 changes: 18 additions & 16 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ mod lint;
mod lockfile;
mod media_type;
mod metrics;
mod module_graph;
mod module_graph2;
mod module_loader;
mod op_fetch_asset;
Expand All @@ -50,7 +49,6 @@ mod specifier_handler;
mod test_runner;
mod text_encoding;
mod tokio_util;
mod tsc;
mod tsc2;
mod tsc_config;
mod upgrade;
Expand Down Expand Up @@ -242,14 +240,19 @@ async fn cache_command(
flags: Flags,
files: Vec<String>,
) -> Result<(), AnyError> {
let lib = if flags.unstable {
module_graph2::TypeLib::UnstableDenoWindow
} else {
module_graph2::TypeLib::DenoWindow
};
let program_state = ProgramState::new(flags)?;

for file in files {
let specifier = ModuleSpecifier::resolve_url_or_path(&file)?;
program_state
.prepare_module_load(
specifier,
tsc::TargetLib::Main,
lib.clone(),
Permissions::allow_all(),
false,
program_state.maybe_import_map.clone(),
Expand Down Expand Up @@ -343,21 +346,20 @@ async fn bundle_command(
module_graph2::TypeLib::DenoWindow
};
let graph = graph.clone();
let (stats, diagnostics, maybe_ignored_options) =
graph.check(module_graph2::CheckOptions {
debug,
emit: false,
lib,
maybe_config_path: flags.config_path.clone(),
reload: flags.reload,
})?;

debug!("{}", stats);
if let Some(ignored_options) = maybe_ignored_options {
let result_info = graph.check(module_graph2::CheckOptions {
debug,
emit: false,
lib,
maybe_config_path: flags.config_path.clone(),
reload: flags.reload,
})?;

debug!("{}", result_info.stats);
if let Some(ignored_options) = result_info.maybe_ignored_options {
eprintln!("{}", ignored_options);
}
if !diagnostics.is_empty() {
return Err(generic_error(diagnostics.to_string()));
if !result_info.diagnostics.is_empty() {
return Err(generic_error(result_info.diagnostics.to_string()));
}
}

Expand Down
17 changes: 17 additions & 0 deletions cli/media_type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use deno_core::ModuleSpecifier;
use serde::Serialize;
use serde::Serializer;
use std::fmt;
Expand Down Expand Up @@ -60,6 +61,22 @@ impl<'a> From<&'a String> for MediaType {
}
}

impl<'a> From<&'a ModuleSpecifier> for MediaType {
fn from(specifier: &'a ModuleSpecifier) -> Self {
let url = specifier.as_url();
let path = if url.scheme() == "file" {
if let Ok(path) = url.to_file_path() {
path
} else {
PathBuf::from(url.path())
}
} else {
PathBuf::from(url.path())
};
MediaType::from_path(&path)
}
}

impl Default for MediaType {
fn default() -> Self {
MediaType::Unknown
Expand Down
Loading

0 comments on commit 9a83c25

Please sign in to comment.