Skip to content

Commit

Permalink
remove ClassMember::Constructor() since it isn't being used
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbarabash committed Oct 10, 2023
1 parent bda8fb6 commit e25d381
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 133 deletions.
1 change: 0 additions & 1 deletion crates/escalier_ast/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,5 @@ pub enum ClassMember {
Method(Method),
Getter(Getter),
Setter(Setter),
Constructor(Constructor),
Field(Field), // TODO: rename to property?
}
1 change: 0 additions & 1 deletion crates/escalier_ast/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ pub fn walk_expr<V: Visitor>(visitor: &mut V, expr: &Expr) {
ClassMember::Method(_) => {}
ClassMember::Getter(_) => {}
ClassMember::Setter(_) => {}
ClassMember::Constructor(_) => {}
ClassMember::Field(_) => {}
}
}
Expand Down
53 changes: 1 addition & 52 deletions crates/escalier_codegen/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stmt>, ctx: &mut Context) -> Class {
let body: Vec<ClassMember> = 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<ParamOrTsParamProp> = iter
.map(|param| {
let pat = build_pattern(&param.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`

Check warning on line 1183 in crates/escalier_codegen/src/js.rs

View check run for this annotation

Codecov / codecov/patch

crates/escalier_codegen/src/js.rs#L1183

Added line #L1183 was not covered by tests
let body = build_body_block_stmt(&method.body, &BlockFinalizer::ExprStmt, ctx);

// In Escalier, `self` is always the first param in non-static
Expand Down
56 changes: 0 additions & 56 deletions crates/escalier_hm/src/infer_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Vec<types::FuncParam>, TypeError> {
let func_params = params
.iter_mut()
.map(|func_param| self.infer_func_param(func_param, sig_ctx))
.collect::<Result<Vec<_>, _>>()?;

Ok(func_params)
}

fn infer_func_param(
&mut self,
param: &mut syntax::FuncParam,
Expand Down
36 changes: 13 additions & 23 deletions crates/escalier_parser/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit e25d381

Please sign in to comment.