From 1b76dcafa5e8eabf1eef0f3ea6f789992636d92d Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Sun, 12 Mar 2023 15:17:14 +1100 Subject: [PATCH] Partial support for named exports --- inputs/passing/helpers/bar.ts | 5 ++ inputs/passing/importBar.ts | 7 +++ valuescript_compiler/src/module_compiler.rs | 68 ++++++++++++++++++++- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 inputs/passing/helpers/bar.ts create mode 100644 inputs/passing/importBar.ts diff --git a/inputs/passing/helpers/bar.ts b/inputs/passing/helpers/bar.ts new file mode 100644 index 0000000..6c095d5 --- /dev/null +++ b/inputs/passing/helpers/bar.ts @@ -0,0 +1,5 @@ +function bar() { + return "this is the bar function"; +} + +export { bar, bar as barExported }; diff --git a/inputs/passing/importBar.ts b/inputs/passing/importBar.ts new file mode 100644 index 0000000..2abfcb2 --- /dev/null +++ b/inputs/passing/importBar.ts @@ -0,0 +1,7 @@ +// test_output! ["this is the bar function","this is the bar function"] + +import { bar, barExported } from "./helpers/bar.ts"; + +export default function () { + return [bar(), barExported()]; +} diff --git a/valuescript_compiler/src/module_compiler.rs b/valuescript_compiler/src/module_compiler.rs index e310435..2be188d 100644 --- a/valuescript_compiler/src/module_compiler.rs +++ b/valuescript_compiler/src/module_compiler.rs @@ -316,7 +316,7 @@ impl ModuleCompiler { match module_decl { Import(import) => self.compile_import(import, scope), ExportDecl(ed) => self.compile_export_decl(ed, scope), - ExportNamed(_) => self.todo(module_decl.span(), "ExportNamed declaration"), + ExportNamed(en) => self.compile_named_export(en, scope), ExportDefaultDecl(edd) => self.compile_export_default_decl(edd, scope), ExportDefaultExpr(_) => self.todo(module_decl.span(), "ExportDefaultExpr declaration"), ExportAll(_) => self.todo(module_decl.span(), "ExportAll declaration"), @@ -487,6 +487,72 @@ impl ModuleCompiler { }; } + fn compile_named_export(&mut self, en: &swc_ecma_ast::NamedExport, scope: &Scope) { + use swc_ecma_ast::ExportSpecifier::*; + use swc_ecma_ast::ModuleExportName; + + for specifier in &en.specifiers { + match specifier { + Named(named) => { + let orig_name = match &named.orig { + ModuleExportName::Ident(ident) => ident.sym.to_string(), + ModuleExportName::Str(_) => { + self.diagnostics.push(Diagnostic { + level: DiagnosticLevel::InternalError, + message: "exporting a non-identifier".to_string(), + span: named.span, + }); + + "_todo_export_non_ident".to_string() + } + }; + + let export_name = match &named.exported { + Some(ModuleExportName::Ident(ident)) => ident.sym.to_string(), + Some(ModuleExportName::Str(str_)) => { + self.todo(str_.span, "exporting a non-identifier"); + "_todo_export_non_ident".to_string() + } + None => orig_name.clone(), + }; + + let defn = match &en.src { + Some(src) => { + self.todo(src.span, "exporting a module export from another module"); + None + } + None => match scope.get_defn(&orig_name) { + Some(found_defn) => Some(found_defn), + None => { + self.diagnostics.push(Diagnostic { + level: DiagnosticLevel::InternalError, + message: format!("Definition for {} should have been in scope", orig_name), + span: named.orig.span(), + }); + + None + } + }, + }; + + if let Some(defn) = defn { + self + .module + .export_star + .properties + .push((Value::String(export_name), Value::Pointer(defn))); + } + } + Default(_) => { + self.todo(specifier.span(), "exporting a default module export"); + } + Namespace(_) => { + self.todo(specifier.span(), "exporting a namespace module export"); + } + } + } + } + fn compile_import(&mut self, import: &swc_ecma_ast::ImportDecl, scope: &Scope) { let import_path = import.src.value.to_string();