summaryrefslogtreecommitdiff
path: root/cli/tsc.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tsc.rs')
-rw-r--r--cli/tsc.rs128
1 files changed, 62 insertions, 66 deletions
diff --git a/cli/tsc.rs b/cli/tsc.rs
index 7bc70c786..fb25df8d5 100644
--- a/cli/tsc.rs
+++ b/cli/tsc.rs
@@ -19,12 +19,6 @@ use crate::permissions::Permissions;
use crate::source_maps::SourceMapGetter;
use crate::startup_data;
use crate::state::State;
-use crate::swc_common::comments::CommentKind;
-use crate::swc_common::Span;
-use crate::swc_ecma_ast;
-use crate::swc_ecma_visit;
-use crate::swc_ecma_visit::Node;
-use crate::swc_ecma_visit::Visit;
use crate::swc_util::AstParser;
use crate::swc_util::SwcDiagnosticBuffer;
use crate::version;
@@ -57,6 +51,10 @@ use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::Mutex;
use std::task::Poll;
+use swc_common::comments::CommentKind;
+use swc_common::Span;
+use swc_ecmascript::visit::Node;
+use swc_ecmascript::visit::Visit;
use url::Url;
pub const AVAILABLE_LIBS: &[&str] = &[
@@ -1262,7 +1260,7 @@ struct DependencyVisitor {
impl Visit for DependencyVisitor {
fn visit_import_decl(
&mut self,
- import_decl: &swc_ecma_ast::ImportDecl,
+ import_decl: &swc_ecmascript::ast::ImportDecl,
_parent: &dyn Node,
) {
let src_str = import_decl.src.value.to_string();
@@ -1275,7 +1273,7 @@ impl Visit for DependencyVisitor {
fn visit_named_export(
&mut self,
- named_export: &swc_ecma_ast::NamedExport,
+ named_export: &swc_ecmascript::ast::NamedExport,
_parent: &dyn Node,
) {
if let Some(src) = &named_export.src {
@@ -1290,7 +1288,7 @@ impl Visit for DependencyVisitor {
fn visit_export_all(
&mut self,
- export_all: &swc_ecma_ast::ExportAll,
+ export_all: &swc_ecmascript::ast::ExportAll,
_parent: &dyn Node,
) {
let src_str = export_all.src.value.to_string();
@@ -1303,7 +1301,7 @@ impl Visit for DependencyVisitor {
fn visit_ts_import_type(
&mut self,
- ts_import_type: &swc_ecma_ast::TsImportType,
+ ts_import_type: &swc_ecmascript::ast::TsImportType,
_parent: &dyn Node,
) {
// TODO(bartlomieju): possibly add separate DependencyKind
@@ -1317,13 +1315,13 @@ impl Visit for DependencyVisitor {
fn visit_call_expr(
&mut self,
- call_expr: &swc_ecma_ast::CallExpr,
+ call_expr: &swc_ecmascript::ast::CallExpr,
parent: &dyn Node,
) {
- use swc_ecma_ast::Expr::*;
- use swc_ecma_ast::ExprOrSuper::*;
+ use swc_ecmascript::ast::Expr::*;
+ use swc_ecmascript::ast::ExprOrSuper::*;
- swc_ecma_visit::visit_call_expr(self, call_expr, parent);
+ swc_ecmascript::visit::visit_call_expr(self, call_expr, parent);
let boxed_expr = match call_expr.callee.clone() {
Super(_) => return,
Expr(boxed) => boxed,
@@ -1341,7 +1339,7 @@ impl Visit for DependencyVisitor {
if let Some(arg) = call_expr.args.get(0) {
match &*arg.expr {
Lit(lit) => {
- if let swc_ecma_ast::Lit::Str(str_) = lit {
+ if let swc_ecmascript::ast::Lit::Str(str_) = lit {
let src_str = str_.value.to_string();
self.dependencies.push(DependencyDescriptor {
specifier: src_str,
@@ -1389,61 +1387,59 @@ pub fn pre_process_file(
analyze_dynamic_imports: bool,
) -> Result<(Vec<ImportDesc>, Vec<TsReferenceDesc>), SwcDiagnosticBuffer> {
let parser = AstParser::default();
- parser.parse_module(file_name, media_type, source_code, |parse_result| {
- let module = parse_result?;
- let mut collector = DependencyVisitor {
- dependencies: vec![],
- };
- let module_span = module.span;
- collector.visit_module(&module, &module);
-
- let dependency_descriptors = collector.dependencies;
-
- // for each import check if there's relevant @deno-types directive
- let imports = dependency_descriptors
- .iter()
- .filter(|desc| {
- if analyze_dynamic_imports {
- return true;
- }
-
- desc.kind != DependencyKind::DynamicImport
- })
- .map(|desc| {
- let location = parser.get_span_location(desc.span);
- let deno_types = get_deno_types(&parser, desc.span);
- ImportDesc {
- specifier: desc.specifier.to_string(),
- deno_types,
- location: location.into(),
- }
- })
- .collect();
-
- // analyze comment from beginning of the file and find TS directives
- let comments = parser
- .comments
- .take_leading_comments(module_span.lo())
- .unwrap_or_else(Vec::new);
-
- let mut references = vec![];
- for comment in comments {
- if comment.kind != CommentKind::Line {
- continue;
+ let parse_result = parser.parse_module(file_name, media_type, source_code);
+ let module = parse_result?;
+ let mut collector = DependencyVisitor {
+ dependencies: vec![],
+ };
+ let module_span = module.span;
+ collector.visit_module(&module, &module);
+
+ let dependency_descriptors = collector.dependencies;
+
+ // for each import check if there's relevant @deno-types directive
+ let imports = dependency_descriptors
+ .iter()
+ .filter(|desc| {
+ if analyze_dynamic_imports {
+ return true;
}
- let text = comment.text.to_string();
- if let Some((kind, specifier)) = parse_ts_reference(text.trim()) {
- let location = parser.get_span_location(comment.span);
- references.push(TsReferenceDesc {
- kind,
- specifier,
- location: location.into(),
- });
+ desc.kind != DependencyKind::DynamicImport
+ })
+ .map(|desc| {
+ let location = parser.get_span_location(desc.span);
+ let deno_types = get_deno_types(&parser, desc.span);
+ ImportDesc {
+ specifier: desc.specifier.to_string(),
+ deno_types,
+ location: location.into(),
}
+ })
+ .collect();
+
+ // analyze comment from beginning of the file and find TS directives
+ let comments = parser
+ .comments
+ .with_leading(module_span.lo(), |cmts| cmts.to_vec());
+
+ let mut references = vec![];
+ for comment in comments {
+ if comment.kind != CommentKind::Line {
+ continue;
}
- Ok((imports, references))
- })
+
+ let text = comment.text.to_string();
+ if let Some((kind, specifier)) = parse_ts_reference(text.trim()) {
+ let location = parser.get_span_location(comment.span);
+ references.push(TsReferenceDesc {
+ kind,
+ specifier,
+ location: location.into(),
+ });
+ }
+ }
+ Ok((imports, references))
}
fn get_deno_types(parser: &AstParser, span: Span) -> Option<String> {