From 9a327ababeb433da26d4bf9c30f73197dc6c4c0b Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Mon, 24 Jul 2023 17:57:51 +1000 Subject: [PATCH] Implement static method props --- inputs/passing/staticMethodProp.ts | 11 +++++++ valuescript_compiler/src/module_compiler.rs | 34 +++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 inputs/passing/staticMethodProp.ts diff --git a/inputs/passing/staticMethodProp.ts b/inputs/passing/staticMethodProp.ts new file mode 100644 index 0000000..4087310 --- /dev/null +++ b/inputs/passing/staticMethodProp.ts @@ -0,0 +1,11 @@ +//! test_output("foo") + +export default () => { + return stuff.foo(); +}; + +const stuff = { + foo() { + return 'foo'; + } +}; diff --git a/valuescript_compiler/src/module_compiler.rs b/valuescript_compiler/src/module_compiler.rs index 0691b53..b13affe 100644 --- a/valuescript_compiler/src/module_compiler.rs +++ b/valuescript_compiler/src/module_compiler.rs @@ -1007,11 +1007,41 @@ impl ModuleCompiler { } swc_ecma_ast::Prop::Assign(_) | swc_ecma_ast::Prop::Getter(_) - | swc_ecma_ast::Prop::Setter(_) - | swc_ecma_ast::Prop::Method(_) => { + | swc_ecma_ast::Prop::Setter(_) => { self.todo(prop.span(), "This type of static prop"); return Value::String("(error)".to_string()); } + swc_ecma_ast::Prop::Method(method) => { + let key = match &method.key { + swc_ecma_ast::PropName::Ident(ident) => Value::String(ident.sym.to_string()), + _ => { + self.todo(method.key.span(), "Static non-ident prop names"); + Value::String("(error)".to_string()) + } + }; + + let fn_ident = match &method.key { + swc_ecma_ast::PropName::Ident(ident) => Some(ident.clone()), + _ => None, + }; + + let fn_name = fn_ident.clone().map(|ident| ident.sym.to_string()); + + let p = match &fn_name { + Some(name) => self.allocate_defn(name), + None => self.allocate_defn_numbered("_anon"), + }; + + let mut nested_defns = self.compile_fn( + p.clone(), + fn_name.clone(), + Functionish::Fn(fn_ident, method.function.clone()), + ); + + self.module.definitions.append(&mut nested_defns); + + (key, Value::Pointer(p)) + } }, };