From b89c606c6268caa6528aa82365b4174e65ec92a3 Mon Sep 17 00:00:00 2001 From: szw9999 <133481926+szw9999@users.noreply.github.com> Date: Tue, 26 May 2026 11:56:27 +0800 Subject: [PATCH] Fix implicit return assertions after statements --- ...implicit-function-return-after-statement.js | 18 ++++++++++++++++++ .../src/transformVisitors.js | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-implicit-function-return-after-statement.js diff --git a/packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-implicit-function-return-after-statement.js b/packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-implicit-function-return-after-statement.js new file mode 100644 index 0000000..f85830d --- /dev/null +++ b/packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-implicit-function-return-after-statement.js @@ -0,0 +1,18 @@ +/* @flow */ + +export const input = ` +function testFunction() : string { + const value = "hello"; +} +`; + +export const expected = ` +import t from "flow-runtime"; + +function testFunction() { + const _returnType = t.return(t.string()); + + const value = "hello"; + return _returnType.assert(); +} +`; diff --git a/packages/babel-plugin-flow-runtime/src/transformVisitors.js b/packages/babel-plugin-flow-runtime/src/transformVisitors.js index e88d0f7..f1e062e 100644 --- a/packages/babel-plugin-flow-runtime/src/transformVisitors.js +++ b/packages/babel-plugin-flow-runtime/src/transformVisitors.js @@ -504,7 +504,7 @@ export default function transformVisitors (context: ConversionContext): Object { if (body.node.body // do not add if last statement is return one && (body.node.body.length === 0 - || !body.node.body[ body.node.body.length - 1].type === "ReturnStatement") + || body.node.body[body.node.body.length - 1].type !== 'ReturnStatement') ) { // we do not add arguments here // only "return;"