diff --git a/crates/escalier_ast/src/class.rs b/crates/escalier_ast/src/class.rs index 76c56ebd..50fabac4 100644 --- a/crates/escalier_ast/src/class.rs +++ b/crates/escalier_ast/src/class.rs @@ -84,6 +84,5 @@ pub enum ClassMember { Method(Method), Getter(Getter), Setter(Setter), - Constructor(Constructor), Field(Field), // TODO: rename to property? } diff --git a/crates/escalier_ast/src/visitor.rs b/crates/escalier_ast/src/visitor.rs index fe2e30b3..d07cac15 100644 --- a/crates/escalier_ast/src/visitor.rs +++ b/crates/escalier_ast/src/visitor.rs @@ -165,7 +165,6 @@ pub fn walk_expr(visitor: &mut V, expr: &Expr) { ClassMember::Method(_) => {} ClassMember::Getter(_) => {} ClassMember::Setter(_) => {} - ClassMember::Constructor(_) => {} ClassMember::Field(_) => {} } } diff --git a/crates/escalier_codegen/src/js.rs b/crates/escalier_codegen/src/js.rs index e33d8367..d7a4a2f2 100644 --- a/crates/escalier_codegen/src/js.rs +++ b/crates/escalier_codegen/src/js.rs @@ -1174,64 +1174,13 @@ fn build_jsx_element( elem } -// fn build_lit(lit: &values::Lit) -> Lit { -// match lit { -// values::Lit::Num(n) => Lit::Num(Number { -// span: DUMMY_SP, -// value: n.value.parse().unwrap(), -// raw: None, -// }), -// values::Lit::Bool(b) => Lit::Bool(Bool { -// span: DUMMY_SP, -// value: b.value, -// }), -// values::Lit::Str(s) => Lit::Str(Str { -// span: DUMMY_SP, -// value: JsWord::from(s.value.to_owned()), -// raw: None, -// // Some would include the quotes around the string -// // Some(JsWord::from(s.value.to_owned())), -// }), -// } -// } - fn build_class(class: &values::Class, stmts: &mut Vec, ctx: &mut Context) -> Class { let body: Vec = class .body .iter() .filter_map(|member| match member { - values::ClassMember::Constructor(constructor) => { - let body = build_body_block_stmt(&constructor.body, &BlockFinalizer::ExprStmt, ctx); - // In Crochet, `self` is always the first param in methods, but - // it represents `this` in JavaScript which is implicit so we - // ignore it here. - let mut iter = constructor.params.iter(); - iter.next(); - let params: Vec = iter - .map(|param| { - let pat = build_pattern(¶m.pattern, stmts, ctx).unwrap(); - ParamOrTsParamProp::Param(Param { - span: DUMMY_SP, - decorators: vec![], - pat, - }) - }) - .collect(); - - Some(ClassMember::Constructor(Constructor { - span: DUMMY_SP, // TODO - key: PropName::Ident(Ident { - span: DUMMY_SP, // TODO - sym: swc_atoms::JsWord::from(String::from("constructor")), - optional: false, - }), - params, - body: Some(body), - accessibility: None, - is_optional: false, - })) - } values::ClassMember::Method(method) => { + // TODO: check if `name` is `constructor` let body = build_body_block_stmt(&method.body, &BlockFinalizer::ExprStmt, ctx); // In Escalier, `self` is always the first param in non-static diff --git a/crates/escalier_hm/src/infer_class.rs b/crates/escalier_hm/src/infer_class.rs index 209ec514..d97dbdd5 100644 --- a/crates/escalier_hm/src/infer_class.rs +++ b/crates/escalier_hm/src/infer_class.rs @@ -178,14 +178,6 @@ impl Checker { } ClassMember::Getter(_) => todo!(), ClassMember::Setter(_) => todo!(), - ClassMember::Constructor(Constructor { - span: _, - is_public: _, - params: _, - body: _, - }) => { - eprintln!("inferring constuctor"); - } ClassMember::Field(_) => { // If there's an initializer, infer its type and then // unify with the type annotation of the field. @@ -221,43 +213,8 @@ impl Checker { }, ); - // TODO: - // - add `Self` type to `sig_ctx` so that it can be referenced in - // type annotations - // - `Self` should expand to whatever the class name is + type params - for member in &mut class.body { match member { - // Constructors are part of statics and thus not part of the interface - ClassMember::Constructor(Constructor { - span: _, - is_public: _, - params, - body: _, - }) => { - let mut sig_ctx = cls_ctx.clone(); - - let func_params = self.infer_func_params(params, &mut sig_ctx)?; - - let ret = self.new_type_ref( - "Self", - Some(Scheme { - t: self_type, - type_params: None, - is_type_param: false, - }), - &[], - ); - - let constructor = TObjElem::Constructor(types::Function { - params: func_params, - ret, - type_params: None, - throws: None, // TODO: constructors can throw - }); - - static_elems.push(constructor); - } // TODO: update Method {} to contain `name` and `function` fields // so that we can reuse some of the logic around function inference ClassMember::Method(Method { @@ -428,19 +385,6 @@ impl Checker { Ok((instance_scheme, static_type)) } - fn infer_func_params( - &mut self, - params: &mut [syntax::FuncParam], - sig_ctx: &mut Context, - ) -> Result, TypeError> { - let func_params = params - .iter_mut() - .map(|func_param| self.infer_func_param(func_param, sig_ctx)) - .collect::, _>>()?; - - Ok(func_params) - } - fn infer_func_param( &mut self, param: &mut syntax::FuncParam, diff --git a/crates/escalier_parser/src/class.rs b/crates/escalier_parser/src/class.rs index 48c94a3a..db95a60e 100644 --- a/crates/escalier_parser/src/class.rs +++ b/crates/escalier_parser/src/class.rs @@ -240,29 +240,19 @@ impl<'a> Parser<'a> { let end = self.scanner.cursor(); let span = Span { start, end }; - let method = match name { - PropName::Ident(ident) if ident.name == "new" => { - ClassMember::Constructor(Constructor { - span, - is_public, - params, - body, - }) - } - _ => ClassMember::Method(Method { - span, - name, - is_public, - is_async, - is_gen, - is_mutating, - is_static, - params, - body, - type_params, - type_ann, - }), - }; + let method = ClassMember::Method(Method { + span, + name, + is_public, + is_async, + is_gen, + is_mutating, + is_static, + params, + body, + type_params, + type_ann, + }); Ok(method) }