Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix transform-flow-comments for import types #9901

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-plugin-transform-flow-comments/package.json
Expand Up @@ -12,6 +12,7 @@
"babel-plugin"
],
"dependencies": {
"@babel/generator": "^7.0.0",
"@babel/helper-plugin-utils": "^7.0.0",
"@babel/plugin-syntax-flow": "^7.2.0"
},
Expand Down
40 changes: 36 additions & 4 deletions packages/babel-plugin-transform-flow-comments/src/index.js
@@ -1,21 +1,26 @@
import { declare } from "@babel/helper-plugin-utils";
import syntaxFlow from "@babel/plugin-syntax-flow";
import { types as t } from "@babel/core";
import generateCode from "@babel/generator";

export default declare(api => {
api.assertVersion(7);

function wrapInFlowComment(path, parent) {
function attachComment(path, comment) {
let attach = path.getPrevSibling();
let where = "trailing";
if (!attach.node) {
attach = path.parentPath;
where = "inner";
}
attach.addComment(where, generateComment(path, parent));
attach.addComment(where, comment);
path.remove();
}

function wrapInFlowComment(path, parent) {
attachComment(path, generateComment(path, parent));
}

function generateComment(path, parent) {
let comment = path
.getSource()
Expand All @@ -26,6 +31,10 @@ export default declare(api => {
return comment;
}

function isTypeImport(importKind) {
return importKind === "type" || importKind === "typeof";
}

return {
name: "transform-flow-comments",
inherits: syntaxFlow,
Expand Down Expand Up @@ -122,10 +131,33 @@ export default declare(api => {
// support `import type A` and `import typeof A` #10
ImportDeclaration(path) {
const { node, parent } = path;
if (node.importKind !== "type" && node.importKind !== "typeof") {
if (isTypeImport(node.importKind)) {
wrapInFlowComment(path, parent);
return;
}
wrapInFlowComment(path, parent);

const typeSpecifiers = node.specifiers.filter(specifier =>
isTypeImport(specifier.importKind),
);

const nonTypeSpecifiers = node.specifiers.filter(
specifier => !isTypeImport(specifier.importKind),
);
node.specifiers = nonTypeSpecifiers;

if (typeSpecifiers.length > 0) {
const typeImportNode = t.cloneNode(node);
typeImportNode.specifiers = typeSpecifiers;

if (nonTypeSpecifiers.length > 0) {
path.addComment(
"trailing",
`:: ${generateCode(typeImportNode).code}`,
);
} else {
attachComment(path, `:: ${generateCode(typeImportNode).code}`);
}
}
},
ObjectPattern(path) {
const { node } = path;
Expand Down
@@ -0,0 +1,5 @@
import A, { B, type C, type D, E } from './types';
import F, { typeof G, H, type I } from './types';
import { type J } from './types';
import K, { L, M } from './types';
import './types';
@@ -0,0 +1,10 @@
import A, { B, E } from './types';
/*:: import { type C, type D } from './types';*/

import F, { H } from './types';
/*:: import { typeof G, type I } from './types';*/

/*:: import { type J } from './types';*/

import K, { L, M } from './types';
import './types';