Skip to content

Commit 73b138e

Browse files
committed
fix #201
1 parent 7da07eb commit 73b138e

File tree

9 files changed

+123
-16
lines changed

9 files changed

+123
-16
lines changed

packages/babel-plugin-flow-runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "babel-plugin-flow-runtime",
33
"homepage": "https://codemix.github.io/flow-runtime",
44
"repository": "https://github.com/codemix/flow-runtime.git",
5-
"version": "0.18.0",
5+
"version": "0.19.0",
66
"description": "Transforms flow type annotations into flow-runtime types, optionally adds runtime type validation to annotated code.",
77
"main": "babel-plugin-flow-runtime.js",
88
"scripts": {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export const integration = {
2+
presets: [
3+
["@babel/preset-env", {"targets": {"node": "current"}}],
4+
'@babel/preset-flow',
5+
],
6+
plugins: [
7+
'babel-plugin-flow-runtime',
8+
],
9+
};
10+
11+
export const input = `
12+
class Model {
13+
static init({foo}: {foo: string}) {
14+
15+
}
16+
}
17+
`;
18+
19+
export const expected = `
20+
"use strict";
21+
var _flowRuntime = _interopRequireDefault(require("flow-runtime"));
22+
function _interopRequireDefault(obj) {
23+
return obj && obj.__esModule ? obj : {
24+
default: obj
25+
};
26+
27+
} @_flowRuntime.default.annotate(_flowRuntime.default.class("Model", _flowRuntime.default.staticMethod("init", _flowRuntime.default.param("_arg", _flowRuntime.default.object(_flowRuntime.default.property("foo", _flowRuntime.default.string())))))) class Model {
28+
static init(_arg) {
29+
let {
30+
foo
31+
} = _arg;
32+
33+
}
34+
}
35+
`;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export const integration = {
2+
presets: [
3+
["@babel/preset-env", {"targets": {"node": "current"}}],
4+
'@babel/preset-flow',
5+
],
6+
plugins: [
7+
'babel-plugin-flow-runtime',
8+
],
9+
};
10+
11+
export const input = `
12+
class Model {
13+
static init({a: [b, {d, e}], f: {g: [h, ...k], i}, ...j}: {a: [number, {d: string, e: number}], f: {g: [string], i: number}}) {
14+
15+
}
16+
}
17+
`;
18+
19+
export const expected = `
20+
"use strict";
21+
var _flowRuntime = _interopRequireDefault(require("flow-runtime"));
22+
function _interopRequireDefault(obj) {
23+
return obj && obj.__esModule ? obj : {
24+
default: obj
25+
};
26+
27+
} @_flowRuntime.default.annotate(_flowRuntime.default.class("Model", _flowRuntime.default.staticMethod("init", _flowRuntime.default.param("_arg", _flowRuntime.default.object(_flowRuntime.default.property("a", _flowRuntime.default.tuple(_flowRuntime.default.number(), _flowRuntime.default.object(_flowRuntime.default.property("d", _flowRuntime.default.string()), _flowRuntime.default.property("e", _flowRuntime.default.number())))), _flowRuntime.default.property("f", _flowRuntime.default.object(_flowRuntime.default.property("g", _flowRuntime.default.tuple(_flowRuntime.default.string())), _flowRuntime.default.property("i", _flowRuntime.default.number())))))))) class Model {
28+
static init(_arg) {
29+
let {
30+
a: [b, {
31+
d, e
32+
}], f: {
33+
g: [h, ...k], i
34+
}, ...j
35+
} = _arg;
36+
}
37+
}
38+
`;

packages/babel-plugin-flow-runtime/src/__tests__/fixtures.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type Fixture = {
4343
annotated?: string;
4444
combined?: string;
4545
customRuntime?: string;
46+
integration?: boolean;
4647
};
4748

4849
const fixtures: Map<string, Fixture> = new Map(files.filter(filterIncluded).map(filename => {

packages/babel-plugin-flow-runtime/src/__tests__/testTransform.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import {equal} from 'assert';
44

55
import transform from '../transform';
6+
import * as babel from '@babel/core';
67

78
import * as babylon from '@babel/parser';
89
import generate from '@babel/generator';
@@ -67,9 +68,14 @@ function normalize (input: string): string {
6768
;
6869
}
6970

70-
export default function testTransform(input: string, options: Options, expected: string) {
71+
export default function testTransform(input: string, options: Options, expected: string, integration?: Object) {
7172
const parsed = parse(input);
72-
const transformed = stripFlowTypes(transform(parsed, options));
73-
const generated = generate(transformed).code;
73+
let generated;
74+
if (integration) {
75+
generated = babel.transform(input, integration).code;
76+
} else {
77+
const transformed = stripFlowTypes(transform(parsed, options));
78+
generated = generate(transformed).code;
79+
}
7480
equal(normalize(generated), normalize(expected));
7581
}

packages/babel-plugin-flow-runtime/src/__tests__/transform.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ import fixtures from './fixtures';
55
import testTransform from './testTransform';
66

77
describe('transform', () => {
8-
for (const [name, {input, expected, annotated, combined, customRuntime}] of fixtures) {
8+
for (const [name, {input, expected, annotated, combined, customRuntime, integration}] of fixtures) {
99
it(`should transform ${name}`, () => {
10-
testTransform(input, {assert: true, annotate: false}, expected);
10+
testTransform(input, {assert: true, annotate: false}, expected, integration);
1111
});
1212
if (annotated) {
1313
it(`should transform ${name} with decorations`, () => {
14-
testTransform(input, {assert: false, annotate: true}, annotated);
14+
testTransform(input, {assert: false, annotate: true, integration}, annotated, integration);
1515
});
1616
}
1717
if (combined) {
1818
it(`should transform ${name} with decorations and assertions`, () => {
19-
testTransform(input, {assert: true, annotate: true}, combined);
19+
testTransform(input, {assert: true, annotate: true, integration}, combined, integration);
2020
});
2121
}
2222
if (customRuntime) {
2323
it(`should transform ${name} with custom runtime path`, () => {
24-
testTransform(input, {libraryName: './custom-flow-runtime'}, customRuntime);
24+
testTransform(input, {libraryName: './custom-flow-runtime'}, customRuntime, integration);
2525
});
2626
}
2727
}

packages/babel-plugin-flow-runtime/src/preTransformVisitors.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@ export default function preTransformVisitors (context: ConversionContext): Objec
1515
};
1616
}
1717

18+
function removePatternBindings(path: NodePath) {
19+
if (path.isIdentifier()) {
20+
path.scope.removeBinding(path.node.name);
21+
} else if (path.isRestElement()) {
22+
removePatternBindings(path.get('argument'));
23+
} else if (path.isObjectProperty()) {
24+
removePatternBindings(path.get('value'));
25+
} else if (path.isObjectPattern()) {
26+
const properties = path.get('properties');
27+
for (let i = 0; i < properties.length; i++) {
28+
removePatternBindings(properties[i]);
29+
}
30+
} else if (path.isArrayPattern()) {
31+
const elements = path.get('elements');
32+
for (let i = 0; i < elements.length; i++) {
33+
removePatternBindings(elements[i]);
34+
}
35+
}
36+
}
37+
1838
function foldComplexParamsIntoBody (path: NodePath) {
1939
let body = path.get('body');
2040
const params = path.get('params');
@@ -39,6 +59,7 @@ function foldComplexParamsIntoBody (path: NodePath) {
3959
body = path.get('body');
4060
path.node.expression = false;
4161
}
62+
removePatternBindings(param);
4263
const cloned = t.cloneDeep(param.node);
4364
const uid = body.scope.generateUidIdentifier(`arg${params[i].key}`);
4465
uid.__flowRuntime__wasParam = true;

packages/babel-plugin-flow-runtime/src/transformVisitors.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,31 +264,25 @@ export default function transformVisitors (context: ConversionContext): Object {
264264
path.get('init').node
265265
);
266266

267+
path.scope.removeOwnBinding(name);
267268
context.replacePath(path, t.variableDeclarator(
268269
t.identifier(name),
269270
wrapped
270271
));
271272
}
272-
else {
273-
context.replacePath(id, t.identifier(name));
274-
}
275273
}
276274
else if (shouldCheck) {
277275
const wrapped = context.assert(
278276
convert(context, id.get('typeAnnotation')),
279277
path.get('init').node
280278
);
281279

282-
// vjpr: We remove the scope.
283280
path.scope.removeOwnBinding(name);
284281
context.replacePath(path, t.variableDeclarator(
285282
t.identifier(name),
286283
wrapped
287284
));
288285
}
289-
else {
290-
context.replacePath(id, t.identifier(name));
291-
}
292286
},
293287
AssignmentExpression (path: NodePath) {
294288
if (context.shouldSuppressPath(path)) {

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,18 @@ babel-plugin-external-helpers@^6.18.0:
16111611
dependencies:
16121612
babel-runtime "^6.22.0"
16131613

1614+
babel-plugin-flow-runtime@^0.17.0:
1615+
version "0.17.0"
1616+
resolved "https://registry.yarnpkg.com/babel-plugin-flow-runtime/-/babel-plugin-flow-runtime-0.17.0.tgz#51d7b2e6a526edc48fad8d8eed45bf7cc32ffc08"
1617+
integrity sha512-6u9abuyr26AFzJJ7tENieAHvK37RvO5atKbZ/oIdXHABE2dHkurgdgYTDQRznmNUx+ppx2ORkwy5GBKXTake3g==
1618+
dependencies:
1619+
babel-generator "^6.21.0"
1620+
babel-traverse "^6.20.0"
1621+
babel-types "^6.16.0"
1622+
babylon "^6.16.1"
1623+
camelcase "^3.0.0"
1624+
flow-config-parser "^0.3.0"
1625+
16141626
babel-plugin-istanbul@^2.0.0:
16151627
version "2.0.3"
16161628
resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-2.0.3.tgz#266b304b9109607d60748474394676982f660df4"

0 commit comments

Comments
 (0)