Skip to content

Commit a6f34d4

Browse files
authored
refactor: use ParsedModule and improve MediaTypes enum (#7456)
1 parent e688a70 commit a6f34d4

12 files changed

+654
-578
lines changed

cli/ast.rs

Lines changed: 487 additions & 0 deletions
Large diffs are not rendered by default.

cli/errors.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//! But Diagnostics are compile-time type errors, whereas JsErrors are runtime
1111
//! exceptions.
1212
13+
use crate::ast::DiagnosticBuffer;
1314
use crate::import_map::ImportMapError;
14-
use crate::swc_util::SwcDiagnosticBuffer;
1515
use deno_core::ErrBox;
1616
use deno_core::ModuleResolutionError;
1717
use rustyline::error::ReadlineError;
@@ -144,7 +144,7 @@ fn get_serde_json_error_class(
144144
}
145145
}
146146

147-
fn get_swc_diagnostic_class(_: &SwcDiagnosticBuffer) -> &'static str {
147+
fn get_diagnostic_class(_: &DiagnosticBuffer) -> &'static str {
148148
"SyntaxError"
149149
}
150150

@@ -211,8 +211,8 @@ pub(crate) fn get_error_class_name(e: &ErrBox) -> &'static str {
211211
.map(get_serde_json_error_class)
212212
})
213213
.or_else(|| {
214-
e.downcast_ref::<SwcDiagnosticBuffer>()
215-
.map(get_swc_diagnostic_class)
214+
e.downcast_ref::<DiagnosticBuffer>()
215+
.map(get_diagnostic_class)
216216
})
217217
.or_else(|| {
218218
e.downcast_ref::<url::ParseError>()

cli/file_fetcher.rs

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -552,23 +552,6 @@ impl SourceFileFetcher {
552552
}
553553
}
554554

555-
pub fn map_file_extension(path: &Path) -> msg::MediaType {
556-
match path.extension() {
557-
None => msg::MediaType::Unknown,
558-
Some(os_str) => match os_str.to_str() {
559-
Some("ts") => msg::MediaType::TypeScript,
560-
Some("tsx") => msg::MediaType::TSX,
561-
Some("js") => msg::MediaType::JavaScript,
562-
Some("jsx") => msg::MediaType::JSX,
563-
Some("mjs") => msg::MediaType::JavaScript,
564-
Some("cjs") => msg::MediaType::JavaScript,
565-
Some("json") => msg::MediaType::Json,
566-
Some("wasm") => msg::MediaType::Wasm,
567-
_ => msg::MediaType::Unknown,
568-
},
569-
}
570-
}
571-
572555
// convert a ContentType string into a enumerated MediaType + optional charset
573556
fn map_content_type(
574557
path: &Path,
@@ -600,7 +583,7 @@ fn map_content_type(
600583
"application/json" | "text/json" => msg::MediaType::Json,
601584
"application/wasm" => msg::MediaType::Wasm,
602585
// Handle plain and possibly webassembly
603-
"text/plain" | "application/octet-stream" => map_file_extension(path),
586+
"text/plain" | "application/octet-stream" => msg::MediaType::from(path),
604587
_ => {
605588
debug!("unknown content type: {}", content_type);
606589
msg::MediaType::Unknown
@@ -614,7 +597,7 @@ fn map_content_type(
614597

615598
(media_type, charset)
616599
}
617-
None => (map_file_extension(path), None),
600+
None => (msg::MediaType::from(path), None),
618601
}
619602
}
620603

@@ -1603,50 +1586,6 @@ mod tests {
16031586
.await;
16041587
}
16051588

1606-
#[test]
1607-
fn test_map_file_extension() {
1608-
assert_eq!(
1609-
map_file_extension(Path::new("foo/bar.ts")),
1610-
msg::MediaType::TypeScript
1611-
);
1612-
assert_eq!(
1613-
map_file_extension(Path::new("foo/bar.tsx")),
1614-
msg::MediaType::TSX
1615-
);
1616-
assert_eq!(
1617-
map_file_extension(Path::new("foo/bar.d.ts")),
1618-
msg::MediaType::TypeScript
1619-
);
1620-
assert_eq!(
1621-
map_file_extension(Path::new("foo/bar.js")),
1622-
msg::MediaType::JavaScript
1623-
);
1624-
assert_eq!(
1625-
map_file_extension(Path::new("foo/bar.jsx")),
1626-
msg::MediaType::JSX
1627-
);
1628-
assert_eq!(
1629-
map_file_extension(Path::new("foo/bar.json")),
1630-
msg::MediaType::Json
1631-
);
1632-
assert_eq!(
1633-
map_file_extension(Path::new("foo/bar.wasm")),
1634-
msg::MediaType::Wasm
1635-
);
1636-
assert_eq!(
1637-
map_file_extension(Path::new("foo/bar.cjs")),
1638-
msg::MediaType::JavaScript
1639-
);
1640-
assert_eq!(
1641-
map_file_extension(Path::new("foo/bar.txt")),
1642-
msg::MediaType::Unknown
1643-
);
1644-
assert_eq!(
1645-
map_file_extension(Path::new("foo/bar")),
1646-
msg::MediaType::Unknown
1647-
);
1648-
}
1649-
16501589
#[test]
16511590
fn test_map_content_type_extension_only() {
16521591
// Extension only

cli/global_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ fn thread_safe() {
314314

315315
#[test]
316316
fn test_should_allow_js() {
317+
use crate::ast::Location;
317318
use crate::module_graph::ImportDescriptor;
318-
use crate::swc_util::Location;
319319

320320
assert!(should_allow_js(&[
321321
&ModuleGraphFile {

cli/info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ pub fn human_size(bytse: f64) -> String {
349349
#[cfg(test)]
350350
mod test {
351351
use super::*;
352+
use crate::ast::Location;
352353
use crate::module_graph::ImportDescriptor;
353-
use crate::swc_util::Location;
354354
use crate::MediaType;
355355

356356
#[test]

cli/lint.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
//! At the moment it is only consumed using CLI but in
77
//! the future it can be easily extended to provide
88
//! the same functions as ops available in JS runtime.
9+
use crate::ast;
910
use crate::colors;
10-
use crate::file_fetcher::map_file_extension;
1111
use crate::fmt::collect_files;
1212
use crate::fmt::run_parallelized;
1313
use crate::fmt_errors;
1414
use crate::msg;
15-
use crate::swc_util;
1615
use deno_core::ErrBox;
1716
use deno_lint::diagnostic::LintDiagnostic;
1817
use deno_lint::linter::Linter;
@@ -131,8 +130,8 @@ fn lint_file(
131130
) -> Result<(Vec<LintDiagnostic>, String), ErrBox> {
132131
let file_name = file_path.to_string_lossy().to_string();
133132
let source_code = fs::read_to_string(&file_path)?;
134-
let media_type = map_file_extension(&file_path);
135-
let syntax = swc_util::get_syntax_for_media_type(media_type);
133+
let media_type = msg::MediaType::from(&file_path);
134+
let syntax = ast::get_syntax(&media_type);
136135

137136
let lint_rules = rules::get_recommended_rules();
138137
let mut linter = create_linter(syntax, lint_rules);
@@ -158,7 +157,7 @@ fn lint_stdin(json: bool) -> Result<(), ErrBox> {
158157
};
159158
let mut reporter = create_reporter(reporter_kind);
160159
let lint_rules = rules::get_recommended_rules();
161-
let syntax = swc_util::get_syntax_for_media_type(msg::MediaType::TypeScript);
160+
let syntax = ast::get_syntax(&msg::MediaType::TypeScript);
162161
let mut linter = create_linter(syntax, lint_rules);
163162
let mut has_error = false;
164163
let pseudo_file_name = "_stdin.ts";

cli/main.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern crate serde_derive;
2222
extern crate tokio;
2323
extern crate url;
2424

25+
mod ast;
2526
mod checksum;
2627
pub mod colors;
2728
mod coverage;
@@ -59,7 +60,6 @@ pub mod resolve_addr;
5960
pub mod signal;
6061
pub mod source_maps;
6162
pub mod state;
62-
mod swc_util;
6363
mod test_runner;
6464
mod text_encoding;
6565
mod tokio_util;
@@ -72,7 +72,6 @@ pub mod worker;
7272

7373
use crate::coverage::CoverageCollector;
7474
use crate::coverage::PrettyCoverageReporter;
75-
use crate::file_fetcher::map_file_extension;
7675
use crate::file_fetcher::SourceFile;
7776
use crate::file_fetcher::SourceFileFetcher;
7877
use crate::file_fetcher::TextDocument;
@@ -376,20 +375,16 @@ async fn doc_command(
376375
let doc_parser = doc::DocParser::new(loader, private);
377376

378377
let parse_result = if source_file == "--builtin" {
379-
let syntax = swc_util::get_syntax_for_dts();
378+
let syntax = ast::get_syntax(&msg::MediaType::Dts);
380379
doc_parser.parse_source(
381380
"lib.deno.d.ts",
382381
syntax,
383382
get_types(flags.unstable).as_str(),
384383
)
385384
} else {
386385
let path = PathBuf::from(&source_file);
387-
let syntax = if path.ends_with("d.ts") {
388-
swc_util::get_syntax_for_dts()
389-
} else {
390-
let media_type = map_file_extension(&path);
391-
swc_util::get_syntax_for_media_type(media_type)
392-
};
386+
let media_type = MediaType::from(&path);
387+
let syntax = ast::get_syntax(&media_type);
393388
let module_specifier =
394389
ModuleSpecifier::resolve_url_or_path(&source_file).unwrap();
395390
doc_parser

cli/module_graph.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
22

3+
use crate::ast::Location;
34
use crate::checksum;
4-
use crate::file_fetcher::map_file_extension;
55
use crate::file_fetcher::SourceFile;
66
use crate::file_fetcher::SourceFileFetcher;
77
use crate::import_map::ImportMap;
88
use crate::msg::MediaType;
99
use crate::permissions::Permissions;
10-
use crate::swc_util::Location;
1110
use crate::tsc::pre_process_file;
1211
use crate::tsc::ImportDesc;
1312
use crate::tsc::TsReferenceDesc;
@@ -24,7 +23,6 @@ use serde::Serialize;
2423
use serde::Serializer;
2524
use std::collections::HashMap;
2625
use std::collections::HashSet;
27-
use std::path::PathBuf;
2826
use std::pin::Pin;
2927

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

349347
let (raw_imports, raw_references) = pre_process_file(
350348
&module_specifier.to_string(),
351-
map_file_extension(&PathBuf::from(&specifier)),
349+
MediaType::from(&specifier),
352350
&source_code,
353351
self.analyze_dynamic_imports,
354352
)?;
@@ -380,7 +378,7 @@ impl ModuleGraphLoader {
380378
url: specifier.to_string(),
381379
redirect: None,
382380
version_hash: "".to_string(),
383-
media_type: map_file_extension(&PathBuf::from(specifier.clone())),
381+
media_type: MediaType::from(&specifier),
384382
filename: specifier,
385383
source_code,
386384
imports,
@@ -931,14 +929,16 @@ console.log(qat.qat);
931929
// According to TS docs (https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html)
932930
// directives that are not at the top of the file are ignored, so only
933931
// 3 references should be captured instead of 4.
932+
let file_specifier =
933+
ModuleSpecifier::resolve_url_or_path("some/file.ts").unwrap();
934934
assert_eq!(
935935
references,
936936
vec![
937937
TsReferenceDesc {
938938
specifier: "dom".to_string(),
939939
kind: TsReferenceKind::Lib,
940940
location: Location {
941-
filename: "some/file.ts".to_string(),
941+
filename: file_specifier.to_string(),
942942
line: 5,
943943
col: 0,
944944
},
@@ -947,7 +947,7 @@ console.log(qat.qat);
947947
specifier: "./type_reference.d.ts".to_string(),
948948
kind: TsReferenceKind::Types,
949949
location: Location {
950-
filename: "some/file.ts".to_string(),
950+
filename: file_specifier.to_string(),
951951
line: 6,
952952
col: 0,
953953
},
@@ -956,7 +956,7 @@ console.log(qat.qat);
956956
specifier: "./type_reference/dep.ts".to_string(),
957957
kind: TsReferenceKind::Path,
958958
location: Location {
959-
filename: "some/file.ts".to_string(),
959+
filename: file_specifier.to_string(),
960960
line: 7,
961961
col: 0,
962962
},

0 commit comments

Comments
 (0)