Skip to content

Commit

Permalink
refactor: new module graph used for no check (#7621)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Sep 24, 2020
1 parent 7726cfb commit c489589
Show file tree
Hide file tree
Showing 24 changed files with 1,765 additions and 216 deletions.
32 changes: 22 additions & 10 deletions cli/ast.rs
@@ -1,6 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use crate::file_fetcher::TextDocument;
use crate::media_type::MediaType;

use deno_core::error::AnyError;
use deno_core::ModuleSpecifier;
use std::error::Error;
Expand Down Expand Up @@ -245,7 +247,7 @@ impl ParsedModule {
pub fn transpile(
self,
options: &TranspileOptions,
) -> Result<(String, Option<String>)> {
) -> Result<(TextDocument, Option<TextDocument>)> {
let program = Program::Module(self.module);

let jsx_pass = react::react(
Expand Down Expand Up @@ -295,7 +297,7 @@ impl ParsedModule {
program.emit_with(&mut emitter)?;
}
let mut src = String::from_utf8(buf)?;
let mut map: Option<String> = None;
let mut map: Option<TextDocument> = None;
{
let mut buf = Vec::new();
self
Expand All @@ -308,10 +310,10 @@ impl ParsedModule {
let encoded_map = base64::encode(buf);
src.push_str(&encoded_map);
} else {
map = Some(String::from_utf8(buf)?);
map = Some(TextDocument::from(buf));
}
}
Ok((src, map))
Ok((src.into(), map))
}
}

Expand Down Expand Up @@ -437,10 +439,14 @@ mod tests {
let (code, maybe_map) = module
.transpile(&TranspileOptions::default())
.expect("could not strip types");
assert!(code.starts_with("var D;\n(function(D) {\n"));
assert!(
code.contains("\n//# sourceMappingURL=data:application/json;base64,")
);
assert!(code
.to_string()
.unwrap()
.starts_with("var D;\n(function(D) {\n"));
assert!(code
.to_string()
.unwrap()
.contains("\n//# sourceMappingURL=data:application/json;base64,"));
assert!(maybe_map.is_none());
}

Expand All @@ -461,7 +467,10 @@ mod tests {
let (code, _) = module
.transpile(&TranspileOptions::default())
.expect("could not strip types");
assert!(code.contains("React.createElement(\"div\", null"));
assert!(code
.to_string()
.unwrap()
.contains("React.createElement(\"div\", null"));
}

#[test]
Expand Down Expand Up @@ -492,6 +501,9 @@ mod tests {
let (code, _) = module
.transpile(&TranspileOptions::default())
.expect("could not strip types");
assert!(code.contains("_applyDecoratedDescriptor("));
assert!(code
.to_string()
.unwrap()
.contains("_applyDecoratedDescriptor("));
}
}
14 changes: 13 additions & 1 deletion cli/file_fetcher.rs
Expand Up @@ -33,7 +33,7 @@ use std::sync::Arc;
use std::sync::Mutex;

/// Structure representing a text document.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TextDocument {
bytes: Vec<u8>,
charset: Cow<'static, str>,
Expand Down Expand Up @@ -73,6 +73,18 @@ impl From<Vec<u8>> for TextDocument {
}
}

impl From<String> for TextDocument {
fn from(s: String) -> Self {
TextDocument::new(s.as_bytes().to_vec(), Option::<&str>::None)
}
}

impl From<&str> for TextDocument {
fn from(s: &str) -> Self {
TextDocument::new(s.as_bytes().to_vec(), Option::<&str>::None)
}
}

/// Structure representing local or remote file.
///
/// In case of remote file `url` might be different than originally requested URL, if so
Expand Down
132 changes: 88 additions & 44 deletions cli/global_state.rs
Expand Up @@ -3,19 +3,26 @@
use crate::deno_dir;
use crate::file_fetcher::SourceFileFetcher;
use crate::flags;
use crate::graph::GraphBuilder;
use crate::graph::TranspileOptions;
use crate::http_cache;
use crate::import_map::ImportMap;
use crate::lockfile::Lockfile;
use crate::media_type::MediaType;
use crate::module_graph::ModuleGraphFile;
use crate::module_graph::ModuleGraphLoader;
use crate::permissions::Permissions;
use crate::specifier_handler::FetchHandler;
use crate::tsc::CompiledModule;
use crate::tsc::TargetLib;
use crate::tsc::TsCompiler;
use deno_core::error::AnyError;
use deno_core::ModuleSpecifier;
use std::cell::RefCell;
use std::env;
use std::fs;
use std::io;
use std::rc::Rc;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use std::sync::Mutex;
Expand Down Expand Up @@ -68,7 +75,7 @@ impl GlobalState {
)?;

let lockfile = if let Some(filename) = &flags.lock {
let lockfile = Lockfile::new(filename.to_string(), flags.lock_write)?;
let lockfile = Lockfile::new(filename.clone(), flags.lock_write)?;
Some(Mutex::new(lockfile))
} else {
None
Expand Down Expand Up @@ -113,54 +120,91 @@ impl GlobalState {
) -> Result<(), AnyError> {
let module_specifier = module_specifier.clone();

let mut module_graph_loader = ModuleGraphLoader::new(
self.file_fetcher.clone(),
maybe_import_map,
permissions.clone(),
is_dyn_import,
false,
);
module_graph_loader
.add_to_graph(&module_specifier, maybe_referrer)
.await?;
let module_graph = module_graph_loader.get_graph();

let out = self
.file_fetcher
.fetch_cached_source_file(&module_specifier, permissions.clone())
.expect("Source file not found");

let module_graph_files = module_graph.values().collect::<Vec<_>>();
// Check integrity of every file in module graph
if let Some(ref lockfile) = self.lockfile {
let mut g = lockfile.lock().unwrap();
if self.flags.no_check {
debug!("Transpiling root: {}", module_specifier);
let handler =
Rc::new(RefCell::new(FetchHandler::new(&self.flags, &permissions)?));
let mut builder = GraphBuilder::new(handler, maybe_import_map);
builder.insert(&module_specifier).await?;
let mut graph = builder.get_graph(&self.lockfile)?;

// TODO(kitsonk) this needs to move, but CompilerConfig is way too
// complicated to use here.
let maybe_config = if let Some(path) = self.flags.config_path.clone() {
let cwd = std::env::current_dir()?;
let config_file = cwd.join(path);
let config_path = config_file.canonicalize().map_err(|_| {
io::Error::new(
io::ErrorKind::InvalidInput,
format!(
"Could not find the config file: {}",
config_file.to_string_lossy()
),
)
})?;
let config_str = fs::read_to_string(config_path)?;

Some(config_str)
} else {
None
};

for graph_file in &module_graph_files {
let check_passed =
g.check_or_insert(&graph_file.url, &graph_file.source_code);
let (stats, maybe_ignored_options) =
graph.transpile(TranspileOptions {
debug: self.flags.log_level == Some(log::Level::Debug),
maybe_config,
})?;

if !check_passed {
eprintln!(
"Subresource integrity check failed --lock={}\n{}",
g.filename, graph_file.url
);
std::process::exit(10);
debug!("{}", stats);
if let Some(ignored_options) = maybe_ignored_options {
println!("Some compiler options were ignored:\n {}", ignored_options);
}
} else {
let mut module_graph_loader = ModuleGraphLoader::new(
self.file_fetcher.clone(),
maybe_import_map,
permissions.clone(),
is_dyn_import,
false,
);
module_graph_loader
.add_to_graph(&module_specifier, maybe_referrer)
.await?;
let module_graph = module_graph_loader.get_graph();

let out = self
.file_fetcher
.fetch_cached_source_file(&module_specifier, permissions.clone())
.expect("Source file not found");

let module_graph_files = module_graph.values().collect::<Vec<_>>();
// Check integrity of every file in module graph
if let Some(ref lockfile) = self.lockfile {
let mut g = lockfile.lock().unwrap();

for graph_file in &module_graph_files {
let check_passed =
g.check_or_insert(&graph_file.url, &graph_file.source_code);

if !check_passed {
eprintln!(
"Subresource integrity check failed --lock={}\n{}",
g.filename, graph_file.url
);
std::process::exit(10);
}
}
}
}

// Check if we need to compile files.
let should_compile = needs_compilation(
self.ts_compiler.compile_js,
out.media_type,
&module_graph_files,
);
let allow_js = should_allow_js(&module_graph_files);

if should_compile {
if self.flags.no_check {
self.ts_compiler.transpile(&module_graph).await?;
} else {
// Check if we need to compile files.
let should_compile = needs_compilation(
self.ts_compiler.compile_js,
out.media_type,
&module_graph_files,
);
let allow_js = should_allow_js(&module_graph_files);

if should_compile {
self
.ts_compiler
.compile(self, &out, target_lib, permissions, &module_graph, allow_js)
Expand Down

0 comments on commit c489589

Please sign in to comment.