babel-plugin-flow-comments
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -11,4 +11,22 @@ "use strict"; | ||
function wrapInFlowComment(context, parent) { | ||
context.addComment("trailing", generateComment(context, parent)); | ||
context.replaceWith(t.noop()); | ||
} | ||
function generateComment(context, parent) { | ||
var comment = context.getSource().replace(/\*-\//g, "*-ESCAPED/").replace(/\*\//g, "*-/"); | ||
if (parent && parent.optional) comment = "?" + comment; | ||
if (comment[0] !== ":") comment = ":: " + comment; | ||
return comment; | ||
} | ||
return new Plugin("flow-comments", { | ||
visitor: { | ||
TypeCastExpression: function TypeCastExpression(node) { | ||
this.get("expression").addComment("trailing", generateComment(this.get("typeAnnotation"))); | ||
this.replaceWith(t.parenthesizedExpression(node.expression)); | ||
}, | ||
// support function a(b?) {} | ||
Identifier: function Identifier(node, parent, scope, file) { | ||
@@ -20,2 +38,4 @@ if (!node.optional || node.typeAnnotation) { | ||
}, | ||
// support `export type a = {}` - #8 Error: You passed path.replaceWith() a falsy node | ||
"ExportNamedDeclaration|Flow": function ExportNamedDeclarationFlow(node, parent, scope, file) { | ||
@@ -25,7 +45,11 @@ if (t.isExportNamedDeclaration(node) && !t.isFlow(node.declaration)) { | ||
} | ||
var comment = this.getSource().replace("*/", "//"); | ||
if (parent.optional) comment = "?" + comment; | ||
if (comment[0] !== ":") comment = ":: " + comment; | ||
this.addComment("trailing", comment); | ||
this.replaceWith(t.noop()); | ||
wrapInFlowComment(this, parent); | ||
}, | ||
// support `import type A` and `import typeof A` #10 | ||
ImportDeclaration: function ImportDeclaration(node, parent, scope, file) { | ||
if (t.isImportDeclaration(node) && node.importKind !== "type" && node.importKind !== "typeof") { | ||
return; | ||
} | ||
wrapInFlowComment(this, parent); | ||
} | ||
@@ -32,0 +56,0 @@ } |
{ | ||
"name": "babel-plugin-flow-comments", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Turn flow type annotations into comments", | ||
@@ -5,0 +5,0 @@ "repository": "babel-plugins/babel-plugin-flow-comments", |
6242
20
133