Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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");
}
`;
Original file line number Diff line number Diff line change
@@ -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();
}
`;
Original file line number Diff line number Diff line change
@@ -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();
}
`;
21 changes: 12 additions & 9 deletions packages/babel-plugin-flow-runtime/src/transformVisitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}
Expand Down