UNPKG

babel-plugin-flow-comments

Version:

Turn flow type annotations into comments

77 lines (61 loc) 2.23 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)); }, // support function a(b?) {} Identifier: function Identifier(path) { var node = path.node; if (!node.optional || node.typeAnnotation) { return; } path.addComment("trailing", ":: ?"); }, // strip optional property from function params - facebook/fbjs#17 Function: { exit: function exit(_ref2) { var node = _ref2.node; node.params.forEach(function (param) { return param.optional = false; }); } }, // support `export type a = {}` - #8 Error: You passed path.replaceWith() a falsy node "ExportNamedDeclaration|Flow": function ExportNamedDeclarationFlow(path) { var node = path.node; var parent = path.parent; if (t.isExportNamedDeclaration(node) && !t.isFlow(node.declaration)) { return; } wrapInFlowComment(path, parent); }, // support `import type A` and `import typeof A` #10 ImportDeclaration: function ImportDeclaration(path) { var node = path.node; var parent = path.parent; if (t.isImportDeclaration(node) && node.importKind !== "type" && node.importKind !== "typeof") { return; } wrapInFlowComment(path, parent); } } }; }; module.exports = exports["default"];