Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use ParsedModule and improve MediaTypes enum #7456

Merged
merged 2 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
487 changes: 487 additions & 0 deletions cli/ast.rs

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions cli/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//! But Diagnostics are compile-time type errors, whereas JsErrors are runtime
//! exceptions.

use crate::ast::DiagnosticBuffer;
use crate::import_map::ImportMapError;
use crate::swc_util::SwcDiagnosticBuffer;
use deno_core::ErrBox;
use deno_core::ModuleResolutionError;
use rustyline::error::ReadlineError;
Expand Down Expand Up @@ -144,7 +144,7 @@ fn get_serde_json_error_class(
}
}

fn get_swc_diagnostic_class(_: &SwcDiagnosticBuffer) -> &'static str {
fn get_diagnostic_class(_: &DiagnosticBuffer) -> &'static str {
"SyntaxError"
}

Expand Down Expand Up @@ -211,8 +211,8 @@ pub(crate) fn get_error_class_name(e: &ErrBox) -> &'static str {
.map(get_serde_json_error_class)
})
.or_else(|| {
e.downcast_ref::<SwcDiagnosticBuffer>()
.map(get_swc_diagnostic_class)
e.downcast_ref::<DiagnosticBuffer>()
.map(get_diagnostic_class)
})
.or_else(|| {
e.downcast_ref::<url::ParseError>()
Expand Down
65 changes: 2 additions & 63 deletions cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,23 +552,6 @@ impl SourceFileFetcher {
}
}

pub fn map_file_extension(path: &Path) -> msg::MediaType {
match path.extension() {
None => msg::MediaType::Unknown,
Some(os_str) => match os_str.to_str() {
Some("ts") => msg::MediaType::TypeScript,
Some("tsx") => msg::MediaType::TSX,
Some("js") => msg::MediaType::JavaScript,
Some("jsx") => msg::MediaType::JSX,
Some("mjs") => msg::MediaType::JavaScript,
Some("cjs") => msg::MediaType::JavaScript,
Some("json") => msg::MediaType::Json,
Some("wasm") => msg::MediaType::Wasm,
_ => msg::MediaType::Unknown,
},
}
}

// convert a ContentType string into a enumerated MediaType + optional charset
fn map_content_type(
path: &Path,
Expand Down Expand Up @@ -600,7 +583,7 @@ fn map_content_type(
"application/json" | "text/json" => msg::MediaType::Json,
"application/wasm" => msg::MediaType::Wasm,
// Handle plain and possibly webassembly
"text/plain" | "application/octet-stream" => map_file_extension(path),
"text/plain" | "application/octet-stream" => msg::MediaType::from(path),
_ => {
debug!("unknown content type: {}", content_type);
msg::MediaType::Unknown
Expand All @@ -614,7 +597,7 @@ fn map_content_type(

(media_type, charset)
}
None => (map_file_extension(path), None),
None => (msg::MediaType::from(path), None),
}
}

Expand Down Expand Up @@ -1603,50 +1586,6 @@ mod tests {
.await;
}

#[test]
fn test_map_file_extension() {
assert_eq!(
map_file_extension(Path::new("foo/bar.ts")),
msg::MediaType::TypeScript
);
assert_eq!(
map_file_extension(Path::new("foo/bar.tsx")),
msg::MediaType::TSX
);
assert_eq!(
map_file_extension(Path::new("foo/bar.d.ts")),
msg::MediaType::TypeScript
);
assert_eq!(
map_file_extension(Path::new("foo/bar.js")),
msg::MediaType::JavaScript
);
assert_eq!(
map_file_extension(Path::new("foo/bar.jsx")),
msg::MediaType::JSX
);
assert_eq!(
map_file_extension(Path::new("foo/bar.json")),
msg::MediaType::Json
);
assert_eq!(
map_file_extension(Path::new("foo/bar.wasm")),
msg::MediaType::Wasm
);
assert_eq!(
map_file_extension(Path::new("foo/bar.cjs")),
msg::MediaType::JavaScript
);
assert_eq!(
map_file_extension(Path::new("foo/bar.txt")),
msg::MediaType::Unknown
);
assert_eq!(
map_file_extension(Path::new("foo/bar")),
msg::MediaType::Unknown
);
}

#[test]
fn test_map_content_type_extension_only() {
// Extension only
Expand Down
2 changes: 1 addition & 1 deletion cli/global_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ fn thread_safe() {

#[test]
fn test_should_allow_js() {
use crate::ast::Location;
use crate::module_graph::ImportDescriptor;
use crate::swc_util::Location;

assert!(should_allow_js(&[
&ModuleGraphFile {
Expand Down
2 changes: 1 addition & 1 deletion cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ pub fn human_size(bytse: f64) -> String {
#[cfg(test)]
mod test {
use super::*;
use crate::ast::Location;
use crate::module_graph::ImportDescriptor;
use crate::swc_util::Location;
use crate::MediaType;

#[test]
Expand Down
9 changes: 4 additions & 5 deletions cli/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
//! At the moment it is only consumed using CLI but in
//! the future it can be easily extended to provide
//! the same functions as ops available in JS runtime.
use crate::ast;
use crate::colors;
use crate::file_fetcher::map_file_extension;
use crate::fmt::collect_files;
use crate::fmt::run_parallelized;
use crate::fmt_errors;
use crate::msg;
use crate::swc_util;
use deno_core::ErrBox;
use deno_lint::diagnostic::LintDiagnostic;
use deno_lint::linter::Linter;
Expand Down Expand Up @@ -131,8 +130,8 @@ fn lint_file(
) -> Result<(Vec<LintDiagnostic>, String), ErrBox> {
let file_name = file_path.to_string_lossy().to_string();
let source_code = fs::read_to_string(&file_path)?;
let media_type = map_file_extension(&file_path);
let syntax = swc_util::get_syntax_for_media_type(media_type);
let media_type = msg::MediaType::from(&file_path);
let syntax = ast::get_syntax(&media_type);

let lint_rules = rules::get_recommended_rules();
let mut linter = create_linter(syntax, lint_rules);
Expand All @@ -158,7 +157,7 @@ fn lint_stdin(json: bool) -> Result<(), ErrBox> {
};
let mut reporter = create_reporter(reporter_kind);
let lint_rules = rules::get_recommended_rules();
let syntax = swc_util::get_syntax_for_media_type(msg::MediaType::TypeScript);
let syntax = ast::get_syntax(&msg::MediaType::TypeScript);
let mut linter = create_linter(syntax, lint_rules);
let mut has_error = false;
let pseudo_file_name = "_stdin.ts";
Expand Down
13 changes: 4 additions & 9 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern crate serde_derive;
extern crate tokio;
extern crate url;

mod ast;
mod checksum;
pub mod colors;
mod coverage;
Expand Down Expand Up @@ -59,7 +60,6 @@ pub mod resolve_addr;
pub mod signal;
pub mod source_maps;
pub mod state;
mod swc_util;
mod test_runner;
mod text_encoding;
mod tokio_util;
Expand All @@ -72,7 +72,6 @@ pub mod worker;

use crate::coverage::CoverageCollector;
use crate::coverage::PrettyCoverageReporter;
use crate::file_fetcher::map_file_extension;
use crate::file_fetcher::SourceFile;
use crate::file_fetcher::SourceFileFetcher;
use crate::file_fetcher::TextDocument;
Expand Down Expand Up @@ -376,20 +375,16 @@ async fn doc_command(
let doc_parser = doc::DocParser::new(loader, private);

let parse_result = if source_file == "--builtin" {
let syntax = swc_util::get_syntax_for_dts();
let syntax = ast::get_syntax(&msg::MediaType::Dts);
doc_parser.parse_source(
"lib.deno.d.ts",
syntax,
get_types(flags.unstable).as_str(),
)
} else {
let path = PathBuf::from(&source_file);
let syntax = if path.ends_with("d.ts") {
swc_util::get_syntax_for_dts()
} else {
let media_type = map_file_extension(&path);
swc_util::get_syntax_for_media_type(media_type)
};
let media_type = MediaType::from(&path);
let syntax = ast::get_syntax(&media_type);
let module_specifier =
ModuleSpecifier::resolve_url_or_path(&source_file).unwrap();
doc_parser
Expand Down
16 changes: 8 additions & 8 deletions cli/module_graph.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use crate::ast::Location;
use crate::checksum;
use crate::file_fetcher::map_file_extension;
use crate::file_fetcher::SourceFile;
use crate::file_fetcher::SourceFileFetcher;
use crate::import_map::ImportMap;
use crate::msg::MediaType;
use crate::permissions::Permissions;
use crate::swc_util::Location;
use crate::tsc::pre_process_file;
use crate::tsc::ImportDesc;
use crate::tsc::TsReferenceDesc;
Expand All @@ -24,7 +23,6 @@ use serde::Serialize;
use serde::Serializer;
use std::collections::HashMap;
use std::collections::HashSet;
use std::path::PathBuf;
use std::pin::Pin;

// TODO(bartlomieju): it'd be great if this function returned
Expand Down Expand Up @@ -348,7 +346,7 @@ impl ModuleGraphLoader {

let (raw_imports, raw_references) = pre_process_file(
&module_specifier.to_string(),
map_file_extension(&PathBuf::from(&specifier)),
MediaType::from(&specifier),
&source_code,
self.analyze_dynamic_imports,
)?;
Expand Down Expand Up @@ -380,7 +378,7 @@ impl ModuleGraphLoader {
url: specifier.to_string(),
redirect: None,
version_hash: "".to_string(),
media_type: map_file_extension(&PathBuf::from(specifier.clone())),
media_type: MediaType::from(&specifier),
filename: specifier,
source_code,
imports,
Expand Down Expand Up @@ -931,14 +929,16 @@ console.log(qat.qat);
// According to TS docs (https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html)
// directives that are not at the top of the file are ignored, so only
// 3 references should be captured instead of 4.
let file_specifier =
ModuleSpecifier::resolve_url_or_path("some/file.ts").unwrap();
assert_eq!(
references,
vec![
TsReferenceDesc {
specifier: "dom".to_string(),
kind: TsReferenceKind::Lib,
location: Location {
filename: "some/file.ts".to_string(),
filename: file_specifier.to_string(),
line: 5,
col: 0,
},
Expand All @@ -947,7 +947,7 @@ console.log(qat.qat);
specifier: "./type_reference.d.ts".to_string(),
kind: TsReferenceKind::Types,
location: Location {
filename: "some/file.ts".to_string(),
filename: file_specifier.to_string(),
line: 6,
col: 0,
},
Expand All @@ -956,7 +956,7 @@ console.log(qat.qat);
specifier: "./type_reference/dep.ts".to_string(),
kind: TsReferenceKind::Path,
location: Location {
filename: "some/file.ts".to_string(),
filename: file_specifier.to_string(),
line: 7,
col: 0,
},
Expand Down
Loading