fix external imports bug

This commit is contained in:
Dmitry Vasilev
2022-10-26 07:47:23 +08:00
parent 7a0e783e12
commit 0fa7c03e87
2 changed files with 37 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
import {map_object, pick_keys, collect_nodes_with_parents, uniq}
import {map_object, filter_object, pick_keys, collect_nodes_with_parents, uniq}
from './utils.js'
import {
is_eq, is_child, ancestry, ancestry_inc, map_tree,
@@ -105,6 +105,7 @@ const run_code = (s, index, dirty_files) => {
external_imports.some(i => state.external_imports_cache[i] == null)
)
) {
// Trigger loading of external modules
return {...state,
loading_external_imports_state: {
index,
@@ -112,10 +113,16 @@ const run_code = (s, index, dirty_files) => {
}
}
} else {
// Modules were loaded and cached, proceed
return external_imports_loaded(
state,
state,
state.external_imports_cache == null
? null
: filter_object(
state.external_imports_cache,
(module_name, module) => external_imports.includes(module_name)
),
index
)
}

View File

@@ -951,6 +951,34 @@ export const tests = [
)
}),
test('module external cache invalidation bug', () => {
const code = `
// external
import {foo_var} from 'foo.js'
`
const initial = test_initial_state(code)
// simulate module load error
const next = COMMANDS.external_imports_loaded(initial, initial, {
'foo.js': {
ok: false,
error: new Error('Failed to resolve module'),
}
})
const edited = ``
// edit code
const {state, effects} = COMMANDS.input(
next,
edited,
0,
)
assert_equal(state.parse_result.ok, true)
}),
// Static analysis
test('undeclared', () => {