UNPKG

babel-plugin-transform-flow-comments

Version:

Turn flow type annotations into comments

86 lines (68 loc) 2.3 kB
"use strict"; exports.__esModule = true; exports.default = function (_ref) { var t = _ref.types; function wrapInFlowComment(path, parent) { path.addComment("trailing", generateComment(path, parent)); path.replaceWith(t.noop()); } function generateComment(path, parent) { var comment = path.getSource().replace(/\*-\//g, "*-ESCAPED/").replace(/\*\//g, "*-/"); if (parent && parent.optional) comment = "?" + comment; if (comment[0] !== ":") comment = ":: " + comment; return comment; } return { inherits: require("babel-plugin-syntax-flow"), visitor: { TypeCastExpression: function TypeCastExpression(path) { var node = path.node; path.get("expression").addComment("trailing", generateComment(path.get("typeAnnotation"))); path.replaceWith(t.parenthesizedExpression(node.expression)); }, Identifier: function Identifier(path) { var node = path.node; if (!node.optional || node.typeAnnotation) { return; } path.addComment("trailing", ":: ?"); }, AssignmentPattern: { exit: function exit(_ref2) { var node = _ref2.node; node.left.optional = false; } }, Function: { exit: function exit(_ref3) { var node = _ref3.node; node.params.forEach(function (param) { return param.optional = false; }); } }, ClassProperty: function ClassProperty(path) { var node = path.node, parent = path.parent; if (!node.value) wrapInFlowComment(path, parent); }, "ExportNamedDeclaration|Flow": function ExportNamedDeclarationFlow(path) { var node = path.node, parent = path.parent; if (t.isExportNamedDeclaration(node) && !t.isFlow(node.declaration)) { return; } wrapInFlowComment(path, parent); }, ImportDeclaration: function ImportDeclaration(path) { var node = path.node, parent = path.parent; if (t.isImportDeclaration(node) && node.importKind !== "type" && node.importKind !== "typeof") { return; } wrapInFlowComment(path, parent); } } }; }; module.exports = exports["default"];