Skip to content

Commit

Permalink
insert import flow-runtime after other imports, fixes #26
Browse files Browse the repository at this point in the history
  • Loading branch information
phpnode committed Jan 16, 2017
1 parent 852104b commit a1e84b6
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Point extends R.Component {
`;

export const expected = `
import t from "flow-runtime";
import * as R from 'react';
import t from "flow-runtime";
const Props = t.type("Props", t.object(
t.property("x", t.number()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const input = `
`;

export const expected = `
import _t from "flow-runtime";
import t from "babel-types";
import _t from "flow-runtime";
const Demo = _t.type("Demo", _t.number());
`;
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const input = `
`;

export const expected = `
import t from "flow-runtime";
import { reify } from "flow-runtime";
import { Type } from "flow-runtime";
import t from "flow-runtime";
const Demo = t.type("Demo", t.number());
console.log(Demo);
`;
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const input = `
`;

export const expected = `
import t from "flow-runtime";
import { Demo } from './simplestExportType';
import t from "flow-runtime";
const Local = t.type("Local", t.number());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ export const input = `
`;

export const expected = `
import t from "flow-runtime";
import {
Demo
} from './simplestExport';
import t from "flow-runtime";
`;
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const input = `
`;

export const expected = `
import t from "flow-runtime";
import React from "react";
import t from "flow-runtime";
const Props = t.type("Props", t.object(
t.property("x", t.number()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const input = `
`;

export const expected = `
import t from "flow-runtime";
import { Component } from "react";
import t from "flow-runtime";
const Props = t.type("Props", t.object(
t.property("x", t.number()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const input = `
`;

export const expected = `
import t from "flow-runtime";
import { Component as C } from "react";
import t from "flow-runtime";
const Props = t.type("Props", t.object(
t.property("x", t.number()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const input = `
`;

export const expected = `
import t from "flow-runtime";
import { PureComponent } from "react";
import t from "flow-runtime";
const Props = t.type("Props", t.object(
t.property("x", t.number()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const input = `
`;

export const expected = `
import t from "flow-runtime";
import React from "react";
import t from "flow-runtime";
const Props = t.type("Props", t.object(
t.property("x", t.number()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const input = `
`;

export const expected = `
import t from "flow-runtime";
import React from "react";
import t from "flow-runtime";
const Props = t.type("Props", t.object(
t.property("x", t.number()),
Expand Down
13 changes: 11 additions & 2 deletions packages/babel-plugin-flow-runtime/src/attachImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ export default function attachImport (context: ConversionContext, program: NodeP
[t.importDefaultSpecifier(t.identifier(context.libraryId))],
t.stringLiteral(context.libraryName)
);
let last;
for (const item of program.get('body')) {
if (item.type === 'Directive') {
if (item.isDirective() || item.isImportDeclaration()) {
last = item;
continue;
}

item.insertBefore(importDeclaration);
return;
}
program.insertAfter(importDeclaration);

if (last) {
last.insertAfter(importDeclaration);
}
else {
program.get('body').unshiftContainer('body', importDeclaration);
}
}

0 comments on commit a1e84b6

Please sign in to comment.