summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorCasper Beyer <caspervonb@pm.me>2020-10-02 19:13:23 +0800
committerGitHub <noreply@github.com>2020-10-02 13:13:23 +0200
commit6825d7f13d51006967c2638b4433fb582cd6ddbc (patch)
tree89a2bc7860a64b52098797698e3c159d1522088d /cli
parent454de99680db7fdf93bcf0f41beb81d594e17271 (diff)
fix(cli/repl): use a default referrer when empty (#7794)
This makes use of a default referrer when its empty in repl mode so that dynamic imports work in the global evaluation context. Co-authored-by: Bartek Iwanczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli')
-rw-r--r--cli/state.rs14
-rw-r--r--cli/tests/integration_tests.rs12
2 files changed, 26 insertions, 0 deletions
diff --git a/cli/state.rs b/cli/state.rs
index 7a3541af7..ef63dad97 100644
--- a/cli/state.rs
+++ b/cli/state.rs
@@ -46,10 +46,23 @@ impl CliModuleLoader {
impl ModuleLoader for CliModuleLoader {
fn resolve(
&self,
+ op_state: Rc<RefCell<OpState>>,
specifier: &str,
referrer: &str,
is_main: bool,
) -> Result<ModuleSpecifier, AnyError> {
+ let global_state = {
+ let state = op_state.borrow();
+ state.borrow::<Arc<GlobalState>>().clone()
+ };
+
+ // FIXME(bartlomieju): hacky way to provide compatibility with repl
+ let referrer = if referrer.is_empty() && global_state.flags.repl {
+ "<unknown>"
+ } else {
+ referrer
+ };
+
if !is_main {
if let Some(import_map) = &self.import_map {
let result = import_map.resolve(specifier, referrer)?;
@@ -58,6 +71,7 @@ impl ModuleLoader for CliModuleLoader {
}
}
}
+
let module_specifier =
ModuleSpecifier::resolve_import(specifier, referrer)?;
diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs
index 20e43ca29..2e2f91d0a 100644
--- a/cli/tests/integration_tests.rs
+++ b/cli/tests/integration_tests.rs
@@ -1253,6 +1253,18 @@ fn repl_test_multiline() {
}
#[test]
+fn repl_test_import() {
+ let (out, _) = util::run_and_collect_output(
+ true,
+ "repl",
+ Some(vec!["import('./subdir/auto_print_hello.ts')"]),
+ Some(vec![("NO_COLOR".to_owned(), "1".to_owned())]),
+ false,
+ );
+ assert!(out.contains("hello!\n"));
+}
+
+#[test]
fn repl_test_eval_unterminated() {
let (out, err) = util::run_and_collect_output(
true,