diff --git a/packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-explicit-function-return-value.js b/packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-explicit-function-return-value.js new file mode 100644 index 0000000..d7267e2 --- /dev/null +++ b/packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-explicit-function-return-value.js @@ -0,0 +1,16 @@ +/* @flow */ + +export const input = ` +function testFunction() : string { + return "hello"; +} +`; + +export const expected = ` +import t from "flow-runtime"; + +function testFunction() { + const _returnType = t.return(t.string()); + return _returnType.assert("hello"); +} +`; diff --git a/packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-implicit-function-return-after-branch.js b/packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-implicit-function-return-after-branch.js new file mode 100644 index 0000000..ab797e7 --- /dev/null +++ b/packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-implicit-function-return-after-branch.js @@ -0,0 +1,23 @@ +/* @flow */ + +export const input = ` +function testFunction(flag) : string { + if (flag) { + return "hello"; + } +} +`; + +export const expected = ` +import t from "flow-runtime"; + +function testFunction(flag) { + const _returnType = t.return(t.string()); + + if (flag) { + return _returnType.assert("hello"); + } + + return _returnType.assert(); +} +`; 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..a328846 --- /dev/null +++ b/packages/babel-plugin-flow-runtime/src/__tests__/__fixtures__/bugs/2-implicit-function-return-after-statement.js @@ -0,0 +1,20 @@ +/* @flow */ + +export const input = ` +function testFunction() : string { + const value = "hello"; + value.toUpperCase(); +} +`; + +export const expected = ` +import t from "flow-runtime"; + +function testFunction() { + const _returnType = t.return(t.string()); + + const value = "hello"; + value.toUpperCase(); + 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..1d8b0ad 100644 --- a/packages/babel-plugin-flow-runtime/src/transformVisitors.js +++ b/packages/babel-plugin-flow-runtime/src/transformVisitors.js @@ -501,15 +501,18 @@ export default function transformVisitors (context: ConversionContext): Object { // explicit check as last statement for implicit function returns // like in function test() : string { /*NOOP*/ } - 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") - ) { - // we do not add arguments here - // only "return;" - // assertion will be added later by code below - body.node.body.push( t.ReturnStatement() ); + if (!path.node.generator) { + const statements = body.node.body; + if (statements + // do not add if last statement is return one + && (statements.length === 0 + || statements[statements.length - 1].type !== 'ReturnStatement') + ) { + // we do not add arguments here + // only "return;" + // assertion will be added later by code below + statements.push(t.ReturnStatement()); + } } } }