From 2a57eebc63eba59450798bb80240dde85881f589 Mon Sep 17 00:00:00 2001 From: Tianyu Yao Date: Wed, 6 Mar 2024 17:53:37 -0800 Subject: [PATCH] Add support for function return type Summary: Now we can get all of the type nodes of a function decalaration, we will use them for relay schema generation. Reviewed By: captbaritone Differential Revision: D54521748 fbshipit-source-id: 63b6d13fd998580913c2039015b4da292d7096de --- .../hermes_estree_codegen/src/ecmascript.json | 10 ++++++++-- .../hermes_parser/src/generated_extension.rs | 19 +++++++++++++++++++ .../tests/fixtures/function.flow.js | 2 -- .../tests/fixtures/type-annotations.flow.js | 2 ++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/unsupported/hermes/crates/hermes_estree_codegen/src/ecmascript.json b/unsupported/hermes/crates/hermes_estree_codegen/src/ecmascript.json index 6c94c79f9e7..cd9a21805b4 100644 --- a/unsupported/hermes/crates/hermes_estree_codegen/src/ecmascript.json +++ b/unsupported/hermes/crates/hermes_estree_codegen/src/ecmascript.json @@ -53,6 +53,11 @@ "body": { "type": "Option" }, + "return_type": { + "type": "Option", + "rename": "returnType", + "optional": true + }, "is_generator": { "type": "bool", "optional": true, @@ -1160,8 +1165,9 @@ "type": "Option", "optional": true }, - "returnType": { - "type": "FlowTypeAnnotation" + "return_type": { + "type": "FlowTypeAnnotation", + "rename": "returnType" }, "rest": { "type": "Option", diff --git a/unsupported/hermes/crates/hermes_parser/src/generated_extension.rs b/unsupported/hermes/crates/hermes_parser/src/generated_extension.rs index 604d371c48a..122b776c919 100644 --- a/unsupported/hermes/crates/hermes_parser/src/generated_extension.rs +++ b/unsupported/hermes/crates/hermes_parser/src/generated_extension.rs @@ -12,6 +12,7 @@ use hermes::parser::hermes_get_ArrowFunctionExpression_body; use hermes::parser::hermes_get_ArrowFunctionExpression_expression; use hermes::parser::hermes_get_ArrowFunctionExpression_id; use hermes::parser::hermes_get_ArrowFunctionExpression_params; +use hermes::parser::hermes_get_ArrowFunctionExpression_returnType; use hermes::parser::hermes_get_ClassDeclaration_body; use hermes::parser::hermes_get_ClassDeclaration_id; use hermes::parser::hermes_get_ClassDeclaration_superClass; @@ -23,11 +24,13 @@ use hermes::parser::hermes_get_FunctionDeclaration_body; use hermes::parser::hermes_get_FunctionDeclaration_generator; use hermes::parser::hermes_get_FunctionDeclaration_id; use hermes::parser::hermes_get_FunctionDeclaration_params; +use hermes::parser::hermes_get_FunctionDeclaration_returnType; use hermes::parser::hermes_get_FunctionExpression_async; use hermes::parser::hermes_get_FunctionExpression_body; use hermes::parser::hermes_get_FunctionExpression_generator; use hermes::parser::hermes_get_FunctionExpression_id; use hermes::parser::hermes_get_FunctionExpression_params; +use hermes::parser::hermes_get_FunctionExpression_returnType; use hermes::parser::hermes_get_Property_computed; use hermes::parser::hermes_get_Property_key; use hermes::parser::hermes_get_Property_kind; @@ -63,6 +66,7 @@ use hermes_estree::Pattern; use hermes_estree::SourceRange; use hermes_estree::TemplateElement; use hermes_estree::TemplateElementValue; +use hermes_estree::TypeAnnotation; use juno_support::NullTerminatedBuf; use serde::Serialize; @@ -220,6 +224,10 @@ impl FromHermes for FunctionDeclaration { |node| Pattern::convert(cx, node), ); let body = FunctionBody::convert(cx, unsafe { hermes_get_FunctionDeclaration_body(node) }); + let return_type = convert_option( + unsafe { hermes_get_FunctionDeclaration_returnType(node) }, + |node| TypeAnnotation::convert(cx, node), + ); let is_generator = unsafe { hermes_get_FunctionDeclaration_generator(node) }; let is_async = unsafe { hermes_get_FunctionDeclaration_async(node) }; let loc = None; @@ -229,6 +237,7 @@ impl FromHermes for FunctionDeclaration { id, params, body: Some(body), + return_type, is_generator, is_async, loc: loc.clone(), @@ -250,6 +259,10 @@ impl FromHermes for FunctionExpression { |node| Pattern::convert(cx, node), ); let body = FunctionBody::convert(cx, unsafe { hermes_get_FunctionExpression_body(node) }); + let return_type = convert_option( + unsafe { hermes_get_FunctionExpression_returnType(node) }, + |node| TypeAnnotation::convert(cx, node), + ); let is_generator = unsafe { hermes_get_FunctionExpression_generator(node) }; let is_async = unsafe { hermes_get_FunctionExpression_async(node) }; let loc = None; @@ -259,6 +272,7 @@ impl FromHermes for FunctionExpression { id, params, body: Some(body), + return_type, is_generator, is_async, loc: loc.clone(), @@ -282,6 +296,10 @@ impl FromHermes for ArrowFunctionExpression { ); let body = FunctionBody::convert(cx, unsafe { hermes_get_ArrowFunctionExpression_body(node) }); + let return_type = convert_option( + unsafe { hermes_get_ArrowFunctionExpression_returnType(node) }, + |node| TypeAnnotation::convert(cx, node), + ); let is_generator = unsafe { hermes_get_FunctionExpression_generator(node) }; let is_async = unsafe { hermes_get_ArrowFunctionExpression_async(node) }; let is_expression = unsafe { hermes_get_ArrowFunctionExpression_expression(node) }; @@ -292,6 +310,7 @@ impl FromHermes for ArrowFunctionExpression { id, params, body: Some(body), + return_type, is_generator, is_async, loc: loc.clone(), diff --git a/unsupported/hermes/crates/hermes_parser/tests/fixtures/function.flow.js b/unsupported/hermes/crates/hermes_parser/tests/fixtures/function.flow.js index 3b1809fe980..de30301a43b 100644 --- a/unsupported/hermes/crates/hermes_parser/tests/fixtures/function.flow.js +++ b/unsupported/hermes/crates/hermes_parser/tests/fixtures/function.flow.js @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -/* TODO: function field(user: User): string { return "Hello"; } -*/ diff --git a/unsupported/hermes/crates/hermes_parser/tests/fixtures/type-annotations.flow.js b/unsupported/hermes/crates/hermes_parser/tests/fixtures/type-annotations.flow.js index 0b4016efb32..d731b21431d 100644 --- a/unsupported/hermes/crates/hermes_parser/tests/fixtures/type-annotations.flow.js +++ b/unsupported/hermes/crates/hermes_parser/tests/fixtures/type-annotations.flow.js @@ -3,6 +3,8 @@ * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. + * + * @flow */ let BooleanTypeAnnotation: boolean;