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,40 @@
/* @flow */

export const input = `
function foo<X>() {
let bar: X;
}
`;

export const expected = `
import t from "flow-runtime";
function foo() {
const X = t.typeParameter("X");
let _barType = X,
bar;
}
`;

export const annotated = `
import t from "flow-runtime";
function foo() {
let bar;
}
t.annotate(foo, t.function(_fn => {
const X = _fn.typeParameter("X");
return [];
}));
`;

export const combined = `
import t from "flow-runtime";
function foo() {
const X = t.typeParameter("X");
let _barType = X,
bar;
}
t.annotate(foo, t.function(_fn => {
const X = _fn.typeParameter("X");
return [];
}));
`;
8 changes: 6 additions & 2 deletions packages/babel-plugin-flow-runtime/src/transformVisitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ export default function transformVisitors (context: ConversionContext): Object {
}
const {name} = id.node;

if (!shouldCheck) {
return;
}

if (!path.has('init') || path.parentPath.node.kind !== 'const') {
const valueUid = path.scope.generateUidIdentifier(`${name}Type`);
path.scope.setData(`valueUid:${name}`, valueUid);
Expand Down Expand Up @@ -263,7 +267,7 @@ export default function transformVisitors (context: ConversionContext): Object {
));
}

if (shouldCheck && path.has('init')) {
if (path.has('init')) {
const wrapped = context.assert(
valueUid,
path.get('init').node
Expand All @@ -276,7 +280,7 @@ export default function transformVisitors (context: ConversionContext): Object {
));
}
}
else if (shouldCheck) {
else {
const wrapped = context.assert(
convert(context, id.get('typeAnnotation')),
path.get('init').node
Expand Down