summaryrefslogtreecommitdiff
path: root/cli/module_graph2.rs
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2020-10-07 22:43:44 +1100
committerGitHub <noreply@github.com>2020-10-07 22:43:44 +1100
commit99aa23b8ddd73ab6332430c2ba7c44792fad3886 (patch)
tree4f71b92a09e04602738165dbc941f1ef1c3d5b02 /cli/module_graph2.rs
parentcb3a3a1e951d0eef9e2d095f87eaaa2f5ea160ad (diff)
refactor(cli): remove TextDocument (#7850)
Diffstat (limited to 'cli/module_graph2.rs')
-rw-r--r--cli/module_graph2.rs79
1 files changed, 25 insertions, 54 deletions
diff --git a/cli/module_graph2.rs b/cli/module_graph2.rs
index b04f21200..738fe188d 100644
--- a/cli/module_graph2.rs
+++ b/cli/module_graph2.rs
@@ -4,7 +4,6 @@ use crate::ast;
use crate::ast::parse;
use crate::ast::Location;
use crate::ast::ParsedModule;
-use crate::file_fetcher::TextDocument;
use crate::import_map::ImportMap;
use crate::lockfile::Lockfile;
use crate::media_type::MediaType;
@@ -37,7 +36,7 @@ use std::sync::Mutex;
use std::time::Instant;
use swc_ecmascript::dep_graph::DependencyKind;
-pub type BuildInfoMap = HashMap<EmitType, TextDocument>;
+pub type BuildInfoMap = HashMap<EmitType, String>;
lazy_static! {
/// Matched the `@deno-types` pragma.
@@ -151,12 +150,8 @@ fn parse_deno_types(comment: &str) -> Option<String> {
/// A hashing function that takes the source code, version and optionally a
/// user provided config and generates a string hash which can be stored to
/// determine if the cached emit is valid or not.
-fn get_version(source: &TextDocument, version: &str, config: &[u8]) -> String {
- crate::checksum::gen(&[
- source.to_str().unwrap().as_bytes(),
- version.as_bytes(),
- config,
- ])
+fn get_version(source: &str, version: &str, config: &[u8]) -> String {
+ crate::checksum::gen(&[source.as_bytes(), version.as_bytes(), config])
}
/// A logical representation of a module within a graph.
@@ -173,7 +168,7 @@ struct Module {
maybe_version: Option<String>,
media_type: MediaType,
specifier: ModuleSpecifier,
- source: TextDocument,
+ source: String,
}
impl Default for Module {
@@ -190,7 +185,7 @@ impl Default for Module {
maybe_version: None,
media_type: MediaType::Unknown,
specifier: ModuleSpecifier::resolve_url("https://deno.land/x/").unwrap(),
- source: TextDocument::new(Vec::new(), Option::<&str>::None),
+ source: "".to_string(),
}
}
}
@@ -243,8 +238,7 @@ impl Module {
}
pub fn parse(&mut self) -> Result<(), AnyError> {
- let parsed_module =
- parse(&self.specifier, &self.source.to_str()?, &self.media_type)?;
+ let parsed_module = parse(&self.specifier, &self.source, &self.media_type)?;
// parse out any triple slash references
for comment in parsed_module.get_leading_comments().iter() {
@@ -470,8 +464,7 @@ impl Graph2 {
let mut lockfile = lf.lock().unwrap();
for (ms, module) in self.modules.iter() {
let specifier = module.specifier.to_string();
- let code = module.source.to_string()?;
- let valid = lockfile.check_or_insert(&specifier, &code);
+ let valid = lockfile.check_or_insert(&specifier, &module.source);
if !valid {
return Err(
InvalidSource(ms.clone(), lockfile.filename.clone()).into(),
@@ -702,14 +695,9 @@ mod tests {
#[derive(Debug, Default)]
pub struct MockSpecifierHandler {
pub fixtures: PathBuf,
- pub build_info: HashMap<ModuleSpecifier, TextDocument>,
- pub build_info_calls: Vec<(ModuleSpecifier, EmitType, TextDocument)>,
- pub cache_calls: Vec<(
- ModuleSpecifier,
- EmitType,
- TextDocument,
- Option<TextDocument>,
- )>,
+ pub build_info: HashMap<ModuleSpecifier, String>,
+ pub build_info_calls: Vec<(ModuleSpecifier, EmitType, String)>,
+ pub cache_calls: Vec<(ModuleSpecifier, EmitType, String, Option<String>)>,
pub deps_calls: Vec<(ModuleSpecifier, DependencyMap)>,
pub types_calls: Vec<(ModuleSpecifier, String)>,
pub version_calls: Vec<(ModuleSpecifier, String)>,
@@ -740,8 +728,7 @@ mod tests {
"jsx" => MediaType::JSX,
_ => MediaType::Unknown,
};
- let source =
- TextDocument::new(fs::read(specifier_path)?, Option::<&str>::None);
+ let source = fs::read_to_string(specifier_path)?;
Ok(CachedModule {
source,
@@ -760,15 +747,15 @@ mod tests {
&self,
specifier: &ModuleSpecifier,
_cache_type: &EmitType,
- ) -> Result<Option<TextDocument>, AnyError> {
+ ) -> Result<Option<String>, AnyError> {
Ok(self.build_info.get(specifier).cloned())
}
fn set_cache(
&mut self,
specifier: &ModuleSpecifier,
cache_type: &EmitType,
- code: TextDocument,
- maybe_map: Option<TextDocument>,
+ code: String,
+ maybe_map: Option<String>,
) -> Result<(), AnyError> {
self.cache_calls.push((
specifier.clone(),
@@ -790,7 +777,7 @@ mod tests {
&mut self,
specifier: &ModuleSpecifier,
cache_type: &EmitType,
- build_info: TextDocument,
+ build_info: String,
) -> Result<(), AnyError> {
self
.build_info
@@ -822,11 +809,9 @@ mod tests {
#[test]
fn test_get_version() {
- let doc_a =
- TextDocument::new(b"console.log(42);".to_vec(), Option::<&str>::None);
+ let doc_a = "console.log(42);";
let version_a = get_version(&doc_a, "1.2.3", b"");
- let doc_b =
- TextDocument::new(b"console.log(42);".to_vec(), Option::<&str>::None);
+ let doc_b = "console.log(42);";
let version_b = get_version(&doc_b, "1.2.3", b"");
assert_eq!(version_a, version_b);
@@ -845,8 +830,7 @@ mod tests {
#[test]
fn test_module_emit_valid() {
- let source =
- TextDocument::new(b"console.log(42);".to_vec(), Option::<&str>::None);
+ let source = "console.log(42);".to_string();
let maybe_version = Some(get_version(&source, version::DENO, b""));
let module = Module {
source,
@@ -855,11 +839,9 @@ mod tests {
};
assert!(module.emit_valid(b""));
- let source =
- TextDocument::new(b"console.log(42);".to_vec(), Option::<&str>::None);
- let old_source =
- TextDocument::new(b"console.log(43);".to_vec(), Option::<&str>::None);
- let maybe_version = Some(get_version(&old_source, version::DENO, b""));
+ let source = "console.log(42);".to_string();
+ let old_source = "console.log(43);";
+ let maybe_version = Some(get_version(old_source, version::DENO, b""));
let module = Module {
source,
maybe_version,
@@ -867,8 +849,7 @@ mod tests {
};
assert!(!module.emit_valid(b""));
- let source =
- TextDocument::new(b"console.log(42);".to_vec(), Option::<&str>::None);
+ let source = "console.log(42);".to_string();
let maybe_version = Some(get_version(&source, "0.0.0", b""));
let module = Module {
source,
@@ -877,8 +858,7 @@ mod tests {
};
assert!(!module.emit_valid(b""));
- let source =
- TextDocument::new(b"console.log(42);".to_vec(), Option::<&str>::None);
+ let source = "console.log(42);".to_string();
let module = Module {
source,
..Module::default()
@@ -888,8 +868,7 @@ mod tests {
#[test]
fn test_module_set_version() {
- let source =
- TextDocument::new(b"console.log(42);".to_vec(), Option::<&str>::None);
+ let source = "console.log(42);".to_string();
let expected = Some(get_version(&source, version::DENO, b""));
let mut module = Module {
source,
@@ -933,15 +912,11 @@ mod tests {
assert_eq!(h.cache_calls[0].1, EmitType::Cli);
assert!(h.cache_calls[0]
.2
- .to_string()
- .unwrap()
.contains("# sourceMappingURL=data:application/json;base64,"));
assert_eq!(h.cache_calls[0].3, None);
assert_eq!(h.cache_calls[1].1, EmitType::Cli);
assert!(h.cache_calls[1]
.2
- .to_string()
- .unwrap()
.contains("# sourceMappingURL=data:application/json;base64,"));
assert_eq!(h.cache_calls[0].3, None);
assert_eq!(h.deps_calls.len(), 7);
@@ -1002,11 +977,7 @@ mod tests {
assert_eq!(h.cache_calls.len(), 1, "only one file should be emitted");
// FIXME(bartlomieju): had to add space in `<div>`, probably a quirk in swc_ecma_codegen
assert!(
- h.cache_calls[0]
- .2
- .to_string()
- .unwrap()
- .contains("<div >Hello world!</div>"),
+ h.cache_calls[0].2.contains("<div >Hello world!</div>"),
"jsx should have been preserved"
);
}