Skip to content

Commit

Permalink
feat: Support dynamic imports (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Feb 15, 2023
1 parent 45fb433 commit 86891e5
Show file tree
Hide file tree
Showing 42 changed files with 109 additions and 57 deletions.
19 changes: 18 additions & 1 deletion crates/stc_ts_file_analyzer/src/analyzer/expr/call_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,28 @@ impl Analyzer<'_, '_> {
}
RCallee::Expr(callee) => callee,
RCallee::Import(..) => {
let base = self.storage.path(self.ctx.module_id);

let src = args.iter().next().and_then(|arg| match arg {
RExprOrSpread { spread: None, expr } => match &**expr {
RExpr::Lit(RLit::Str(RStr { span, value, .. })) => Some(value.clone()),
_ => None,
},
_ => None,
});
let dep_id = src.and_then(|src| self.loader.module_id(&base, &src));

if let Some(dep_id) = dep_id {
if let Some(dep) = self.data.imports.get(&(self.ctx.module_id, dep_id)) {
return Ok(dep.clone());
}
}

return Err(ErrorKind::Unimplemented {
span: e.span,
msg: "validation of dynamic import".to_string(),
}
.into())
.into());
}
};

Expand Down
48 changes: 34 additions & 14 deletions crates/stc_ts_file_analyzer/src/analyzer/import.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(not(feature = "no-threading"))]
use rayon::prelude::*;
use rnode::{Visit, VisitWith};
use stc_ts_ast_rnode::{
Expand Down Expand Up @@ -102,20 +101,31 @@ impl Analyzer<'_, '_> {
normal_imports.push((ctxt, base.clone(), dep_id, import.src.clone(), import));
}

#[cfg(feature = "no-threading")]
let iter = normal_imports.into_iter();
#[cfg(not(feature = "no-threading"))]
let iter = normal_imports.into_par_iter();
let import_results = if cfg!(feature = "no-threading") {
let iter = normal_imports.into_iter();

let import_results = GLOBALS.with(|globals| {
iter.map(|(ctxt, base, dep_id, module_specifier, import)| {
GLOBALS.set(globals, || {
let res = loader.load_non_circular_dep(&base, &module_specifier);
(ctxt, dep_id, import, res)
GLOBALS.with(|globals| {
iter.map(|(ctxt, base, dep_id, module_specifier, import)| {
GLOBALS.set(globals, || {
let res = loader.load_non_circular_dep(&base, &module_specifier);
(ctxt, dep_id, import, res)
})
})
.collect::<Vec<_>>()
})
} else {
let iter = normal_imports.into_par_iter();

GLOBALS.with(|globals| {
iter.map(|(ctxt, base, dep_id, module_specifier, import)| {
GLOBALS.set(globals, || {
let res = loader.load_non_circular_dep(&base, &module_specifier);
(ctxt, dep_id, import, res)
})
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>()
});
};

for (ctxt, dep_id, import, res) in import_results {
let span = import.span;
Expand Down Expand Up @@ -321,8 +331,8 @@ where
fn visit(&mut self, expr: &RCallExpr) {
let span = expr.span();

match expr.callee {
RCallee::Expr(box RExpr::Ident(ref i)) if i.sym == js_word!("require") => {
match &expr.callee {
RCallee::Expr(box RExpr::Ident(i)) if i.sym == js_word!("require") => {
let src = expr
.args
.iter()
Expand All @@ -334,6 +344,16 @@ where
.unwrap();
self.to.push((self.cur_ctxt, DepInfo { span, src }));
}
RCallee::Import(import) => {
let src = expr.args.first().and_then(|v| match *v.expr {
RExpr::Lit(RLit::Str(RStr { ref value, .. })) => Some(value.clone()),
_ => None,
});

if let Some(src) = src {
self.to.push((self.cur_ctxt, DepInfo { span, src }));
}
}
_ => {}
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/stc_ts_type_checker/tests/conformance.pass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ classes/classExpressions/classWithStaticFieldInParameterBindingPattern.3.ts
classes/classExpressions/classWithStaticFieldInParameterBindingPattern.ts
classes/classExpressions/classWithStaticFieldInParameterInitializer.3.ts
classes/classExpressions/classWithStaticFieldInParameterInitializer.ts
classes/classExpressions/extendClassExpressionFromModule.ts
classes/classExpressions/genericClassExpressionInFunction.ts
classes/classExpressions/modifierOnClassExpressionMemberInFunction.ts
classes/classStaticBlock/classStaticBlock1.ts
Expand Down Expand Up @@ -369,6 +370,16 @@ declarationEmit/typePredicates/declarationEmitIdentifierPredicatesWithPrivateNam
declarationEmit/typeofImportTypeOnlyExport.ts
decorators/1.0lib-noErrors.ts
directives/ts-expect-error-nocheck.ts
dynamicImport/importCallExpression3ES2020.ts
dynamicImport/importCallExpressionDeclarationEmit2.ts
dynamicImport/importCallExpressionInAMD3.ts
dynamicImport/importCallExpressionInCJS4.ts
dynamicImport/importCallExpressionInExportEqualsAMD.ts
dynamicImport/importCallExpressionInExportEqualsCJS.ts
dynamicImport/importCallExpressionInExportEqualsUMD.ts
dynamicImport/importCallExpressionInScriptContext1.ts
dynamicImport/importCallExpressionInSystem3.ts
dynamicImport/importCallExpressionInUMD3.ts
emitter/es2015/asyncGenerators/emitter.asyncGenerators.classMethods.es2015.ts
emitter/es2015/asyncGenerators/emitter.asyncGenerators.functionDeclarations.es2015.ts
emitter/es2015/asyncGenerators/emitter.asyncGenerators.functionExpressions.es2015.ts
Expand Down Expand Up @@ -1592,6 +1603,7 @@ externalModules/exportDefaultClassNameWithObject.ts
externalModules/exportNonLocalDeclarations.ts
externalModules/exportNonVisibleType.ts
externalModules/globalAugmentationModuleResolution.ts
externalModules/importImportOnlyModule.ts
externalModules/multipleExportDefault1.ts
externalModules/multipleExportDefault2.ts
externalModules/multipleExportDefault3.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ Stats {
required_error: 0,
matched_error: 0,
extra_error: 0,
panic: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 4,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 2,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 2,
matched_error: 0,
extra_error: 3,
extra_error: 2,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 4,
extra_error: 3,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 6,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 6,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 6,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 6,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 6,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 6,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 6,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 6,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 4,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 2,
extra_error: 4,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 4,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 2,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 2,
extra_error: 4,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 4,
extra_error: 1,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 2,
extra_error: 4,
panic: 0,
}
Loading

0 comments on commit 86891e5

Please sign in to comment.