From 10d4945b7cabbeb0c2382e17d8c8ccceb93a2467 Mon Sep 17 00:00:00 2001 From: Aakash Patel Date: Wed, 19 Jan 2022 18:35:21 -0800 Subject: [PATCH] Handle type params in methods Reviewed By: tmikov Differential Revision: D33107197 fbshipit-source-id: 1bd6414875c102b3075c03a40453b6d610545f96 --- unsupported/juno/crates/juno/src/gen_js.rs | 80 +++++++++++++------ .../juno/crates/juno/tests/gen_js/mod.rs | 6 ++ 2 files changed, 61 insertions(+), 25 deletions(-) diff --git a/unsupported/juno/crates/juno/src/gen_js.rs b/unsupported/juno/crates/juno/src/gen_js.rs index ce52c84327c..05a34736dea 100644 --- a/unsupported/juno/crates/juno/src/gen_js.rs +++ b/unsupported/juno/crates/juno/src/gen_js.rs @@ -322,14 +322,15 @@ impl GenJS { if let Some(id) = id { id.visit(ctx, self, Some(Path::new(node, NodeField::id))); } - if let Some(type_parameters) = type_parameters { - type_parameters.visit( - ctx, - self, - Some(Path::new(node, NodeField::type_parameters)), - ); - } - self.visit_func_params_body(ctx, params, *return_type, *predicate, *body, node); + self.visit_func_params_body( + ctx, + params, + *type_parameters, + *return_type, + *predicate, + *body, + node, + ); } Node::ArrowFunctionExpression(ArrowFunctionExpression { @@ -1379,15 +1380,21 @@ impl GenJS { match value { Node::FunctionExpression(FunctionExpression { metadata: _, + // Name is handled by the property key. + id: _, params, body, return_type, predicate, - .. + type_parameters, + // Handled above. + generator: _, + is_async: _, }) => { self.visit_func_params_body( ctx, params, + *type_parameters, *return_type, *predicate, *body, @@ -1603,21 +1610,31 @@ impl GenJS { computed, is_static, }) => { - let (is_async, generator, params, body, return_type, predicate) = match value { - Node::FunctionExpression(FunctionExpression { - metadata: _, - generator, - is_async, - params, - body, - return_type, - predicate, - .. - }) => (*is_async, *generator, params, body, return_type, predicate), - _ => { - unreachable!("Invalid method value"); - } - }; + let (is_async, generator, params, body, return_type, predicate, type_parameters) = + match value { + Node::FunctionExpression(FunctionExpression { + metadata: _, + id: _, + generator, + is_async, + params, + body, + return_type, + predicate, + type_parameters, + }) => ( + *is_async, + *generator, + params, + body, + return_type, + predicate, + type_parameters, + ), + _ => { + unreachable!("Invalid method value"); + } + }; if *is_static { out!(self, "static "); } @@ -1646,7 +1663,15 @@ impl GenJS { if *computed { out!(self, "]"); } - self.visit_func_params_body(ctx, params, *return_type, *predicate, *body, node); + self.visit_func_params_body( + ctx, + params, + *type_parameters, + *return_type, + *predicate, + *body, + node, + ); } Node::ImportDeclaration(ImportDeclaration { @@ -3167,15 +3192,20 @@ impl GenJS { out!(self, "}}"); } + #[allow(clippy::too_many_arguments)] fn visit_func_params_body<'gc>( &mut self, ctx: &'gc GCLock, params: &[&'gc Node<'gc>], + type_parameters: Option<&'gc Node<'gc>>, return_type: Option<&'gc Node<'gc>>, predicate: Option<&'gc Node<'gc>>, body: &'gc Node<'gc>, node: &'gc Node<'gc>, ) { + if let Some(type_parameters) = type_parameters { + type_parameters.visit(ctx, self, Some(Path::new(node, NodeField::type_parameters))); + } out!(self, "("); for (i, param) in params.iter().enumerate() { if i > 0 { diff --git a/unsupported/juno/crates/juno/tests/gen_js/mod.rs b/unsupported/juno/crates/juno/tests/gen_js/mod.rs index d49da011ea6..1f41fff5140 100644 --- a/unsupported/juno/crates/juno/tests/gen_js/mod.rs +++ b/unsupported/juno/crates/juno/tests/gen_js/mod.rs @@ -317,6 +317,11 @@ fn test_objects() { ...from, })", ); + test_roundtrip_flow( + "({ + foo() {}, + })", + ); } #[test] @@ -422,6 +427,7 @@ fn test_classes() { declare prop4; #prop5; #prop5: ?number = null; + foo() {} }", ); test_roundtrip(