Skip to content

Commit

Permalink
fix: issues with import type conversion on recent versions of @babel/…
Browse files Browse the repository at this point in the history
…traverse

fix #254
  • Loading branch information
jedwards1211 committed Dec 29, 2020
1 parent a554625 commit afec3c2
Show file tree
Hide file tree
Showing 7 changed files with 942 additions and 31 deletions.
32 changes: 16 additions & 16 deletions packages/babel-plugin-flow-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
"author": "",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.2.0",
"@babel/core": "^7.2.0",
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.2.1",
"@babel/plugin-proposal-decorators": "^7.2.0",
"@babel/plugin-proposal-object-rest-spread": "^7.2.0",
"@babel/plugin-transform-regenerator": "^7.0.0",
"@babel/polyfill": "^7.0.0",
"@babel/preset-env": "^7.2.0",
"@babel/preset-flow": "^7.0.0",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"@babel/cli": "^7.12.0",
"@babel/core": "^7.12.0",
"@babel/helper-plugin-utils": "^7.10.0",
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/plugin-proposal-decorators": "^7.12.0",
"@babel/plugin-proposal-object-rest-spread": "^7.12.0",
"@babel/plugin-transform-regenerator": "^7.12.0",
"@babel/polyfill": "^7.12.0",
"@babel/preset-env": "^7.12.0",
"@babel/preset-flow": "^7.12.0",
"@babel/preset-react": "^7.12.0",
"@babel/register": "^7.12.0",
"babel-eslint": "^10.0.0",
"dedent": "^0.6.0",
"eslint": "^5.0.0",
Expand All @@ -44,10 +44,10 @@
"rimraf": "^2.6.3"
},
"dependencies": {
"@babel/generator": "^7.0.0",
"@babel/parser": "^7.0.0",
"@babel/traverse": "^7.0.0",
"@babel/types": "^7.0.0",
"@babel/generator": "^7.12.0",
"@babel/parser": "^7.12.0",
"@babel/traverse": "^7.12.0",
"@babel/types": "^7.12.0",
"camelcase": "^3.0.0",
"flow-config-parser": "^0.3.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default class ConversionContext {
isAnnotating: boolean = false;
suppressCommentPatterns: RegExp[] = [/\$FlowFixMe/];
suppressTypeNames: string[] = ['$FlowFixMe'];
lastImportDeclaration: ?NodePath = null;

/**
* A set of nodes that have already been visited.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// @flow

export const integration = {
presets: [
["@babel/preset-env", {"targets": {"node": "current"}}],
'@babel/preset-flow',
],
plugins: [
'babel-plugin-flow-runtime',
],
};

export const input = `
// @flow
// @flow-runtime
import { type Foo } from './Foo'
`;

export const expected = `
"use strict";
var _Foo2 = require("./Foo");
var _flowRuntime = _interopRequireDefault(require("flow-runtime"));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
// -runtime
const Foo = _flowRuntime.default.tdz(() => _Foo2.Foo);
`;
2 changes: 2 additions & 0 deletions packages/babel-plugin-flow-runtime/src/firstPassVisitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export default function firstPassVisitors (context: ConversionContext): Object {
}
}
}

context.lastImportDeclaration = path;
},
VariableDeclarator (path: NodePath) {
for (const id of findIdentifiers(path.get('id'))) {
Expand Down
1 change: 0 additions & 1 deletion packages/babel-plugin-flow-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import getTypeParameters from './getTypeParameters';
export default function babelPluginFlowRuntime () {
return {
visitor: {

Program (path: NodePath, state: Object) {
const {opts} = state;

Expand Down
13 changes: 2 additions & 11 deletions packages/babel-plugin-flow-runtime/src/transformVisitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,8 @@ export default function transformVisitors (context: ConversionContext): Object {
]));
}
if (declarations.length !== 0) {
let target = path;

const container = path.parentPath.get(path.listKey);
for (let i = path.key; i < container.length; i++) {
const item = container[i];
if (item.isImportDeclaration()) {
target = item;
continue;
}
break;
}
let target = context.lastImportDeclaration || path;

for (let i = declarations.length - 1; i >= 0; i--) {
target.insertAfter(declarations[i]);
}
Expand Down

0 comments on commit afec3c2

Please sign in to comment.