mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
bare imports
This commit is contained in:
@@ -41,9 +41,17 @@ export const map_destructuring_identifiers = (node, mapper) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const collect_imports = module => {
|
export const collect_imports = module => {
|
||||||
const imports = module.stmts
|
const import_statements =
|
||||||
|
module.stmts
|
||||||
.filter(n => n.type == 'import')
|
.filter(n => n.type == 'import')
|
||||||
.filter(n => !n.is_external)
|
.filter(n => !n.is_external)
|
||||||
|
|
||||||
|
const imported_modules = uniq(
|
||||||
|
import_statements.map(n => n.full_import_path)
|
||||||
|
)
|
||||||
|
|
||||||
|
const imports =
|
||||||
|
import_statements
|
||||||
.map(n =>
|
.map(n =>
|
||||||
n.imports.map(i =>
|
n.imports.map(i =>
|
||||||
({name: i.value, module: n.full_import_path})
|
({name: i.value, module: n.full_import_path})
|
||||||
@@ -51,8 +59,7 @@ export const collect_imports = module => {
|
|||||||
)
|
)
|
||||||
.flat()
|
.flat()
|
||||||
|
|
||||||
const modules = uniq(imports.map(i => i.module))
|
const by_module = Object.fromEntries(imported_modules.map(m =>
|
||||||
const by_module = Object.fromEntries(modules.map(m =>
|
|
||||||
[
|
[
|
||||||
m,
|
m,
|
||||||
imports
|
imports
|
||||||
@@ -60,6 +67,7 @@ export const collect_imports = module => {
|
|||||||
.map(i => i.name)
|
.map(i => i.name)
|
||||||
]
|
]
|
||||||
))
|
))
|
||||||
|
|
||||||
return by_module
|
return by_module
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -240,7 +240,11 @@ const codegen = (node, cxt, parent) => {
|
|||||||
return do_codegen(node.key) + ' : ' + do_codegen(node.value);
|
return do_codegen(node.key) + ' : ' + do_codegen(node.value);
|
||||||
} else if(node.type == 'import') {
|
} else if(node.type == 'import') {
|
||||||
const names = node.imports.map(n => n.value)
|
const names = node.imports.map(n => n.value)
|
||||||
|
if(names.length == 0) {
|
||||||
|
return ''
|
||||||
|
} else {
|
||||||
return `const {${names.join(',')}} = __modules['${node.full_import_path}'];`;
|
return `const {${names.join(',')}} = __modules['${node.full_import_path}'];`;
|
||||||
|
}
|
||||||
} else if(node.type == 'export') {
|
} else if(node.type == 'export') {
|
||||||
const identifiers = collect_destructuring_identifiers(node.binding.name_node)
|
const identifiers = collect_destructuring_identifiers(node.binding.name_node)
|
||||||
.map(i => i.value)
|
.map(i => i.value)
|
||||||
|
|||||||
@@ -1178,16 +1178,20 @@ const import_statement =
|
|||||||
optional(by_type('pragma_external')),
|
optional(by_type('pragma_external')),
|
||||||
seq([
|
seq([
|
||||||
literal('import'),
|
literal('import'),
|
||||||
|
optional(
|
||||||
|
seq([
|
||||||
list(
|
list(
|
||||||
['{', '}'],
|
['{', '}'],
|
||||||
identifier,
|
identifier,
|
||||||
),
|
),
|
||||||
literal('from'),
|
literal('from'),
|
||||||
|
])
|
||||||
|
),
|
||||||
string_literal
|
string_literal
|
||||||
])
|
])
|
||||||
]),
|
]),
|
||||||
({value: [external, imp]}) => {
|
({value: [external, imp]}) => {
|
||||||
const {value: [_import, identifiers, _from, module], ...node} = imp
|
const {value: [_import, identifiers, module], ...node} = imp
|
||||||
return {
|
return {
|
||||||
...node,
|
...node,
|
||||||
not_evaluatable: true,
|
not_evaluatable: true,
|
||||||
@@ -1196,7 +1200,7 @@ const import_statement =
|
|||||||
// TODO refactor hanlding of string literals. Drop quotes from value and
|
// TODO refactor hanlding of string literals. Drop quotes from value and
|
||||||
// fix codegen for string_literal
|
// fix codegen for string_literal
|
||||||
module: module.value.slice(1, module.value.length - 1),
|
module: module.value.slice(1, module.value.length - 1),
|
||||||
children: identifiers.value,
|
children: identifiers == null ? [] : identifiers.value[0].value,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
18
test/test.js
18
test/test.js
@@ -786,6 +786,24 @@ export const tests = [
|
|||||||
assert_equal(mods.root.is_eq, true)
|
assert_equal(mods.root.is_eq, true)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
test('modules empty import', () => {
|
||||||
|
const i = test_initial_state({
|
||||||
|
'': 'import {} from "a"',
|
||||||
|
'a': 'Object.assign(globalThis, {test_import: true})',
|
||||||
|
})
|
||||||
|
assert_equal(i.active_calltree_node.ok, true)
|
||||||
|
assert_equal(globalThis.test_import, true)
|
||||||
|
}),
|
||||||
|
|
||||||
|
test('modules bare import', () => {
|
||||||
|
const i = test_initial_state({
|
||||||
|
'': 'import "a"',
|
||||||
|
'a': 'Object.assign(globalThis, {test_import: true})',
|
||||||
|
})
|
||||||
|
assert_equal(i.active_calltree_node.ok, true)
|
||||||
|
assert_equal(globalThis.test_import, true)
|
||||||
|
}),
|
||||||
|
|
||||||
test('bug parser pragma external', () => {
|
test('bug parser pragma external', () => {
|
||||||
const result = parse(`
|
const result = parse(`
|
||||||
// external
|
// external
|
||||||
|
|||||||
Reference in New Issue
Block a user