UNPKG

2.3 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4
5exports.default = function (_ref) {
6 var t = _ref.types;
7
8 function wrapInFlowComment(path, parent) {
9 path.addComment("trailing", generateComment(path, parent));
10 path.replaceWith(t.noop());
11 }
12
13 function generateComment(path, parent) {
14 var comment = path.getSource().replace(/\*-\//g, "*-ESCAPED/").replace(/\*\//g, "*-/");
15 if (parent && parent.optional) comment = "?" + comment;
16 if (comment[0] !== ":") comment = ":: " + comment;
17 return comment;
18 }
19
20 return {
21 inherits: require("babel-plugin-syntax-flow"),
22
23 visitor: {
24 TypeCastExpression: function TypeCastExpression(path) {
25 var node = path.node;
26
27 path.get("expression").addComment("trailing", generateComment(path.get("typeAnnotation")));
28 path.replaceWith(t.parenthesizedExpression(node.expression));
29 },
30 Identifier: function Identifier(path) {
31 var node = path.node;
32
33 if (!node.optional || node.typeAnnotation) {
34 return;
35 }
36 path.addComment("trailing", ":: ?");
37 },
38
39
40 AssignmentPattern: {
41 exit: function exit(_ref2) {
42 var node = _ref2.node;
43
44 node.left.optional = false;
45 }
46 },
47
48 Function: {
49 exit: function exit(_ref3) {
50 var node = _ref3.node;
51
52 node.params.forEach(function (param) {
53 return param.optional = false;
54 });
55 }
56 },
57
58 ClassProperty: function ClassProperty(path) {
59 var node = path.node,
60 parent = path.parent;
61
62 if (!node.value) wrapInFlowComment(path, parent);
63 },
64 "ExportNamedDeclaration|Flow": function ExportNamedDeclarationFlow(path) {
65 var node = path.node,
66 parent = path.parent;
67
68 if (t.isExportNamedDeclaration(node) && !t.isFlow(node.declaration)) {
69 return;
70 }
71 wrapInFlowComment(path, parent);
72 },
73 ImportDeclaration: function ImportDeclaration(path) {
74 var node = path.node,
75 parent = path.parent;
76
77 if (t.isImportDeclaration(node) && node.importKind !== "type" && node.importKind !== "typeof") {
78 return;
79 }
80 wrapInFlowComment(path, parent);
81 }
82 }
83 };
84};
85
86module.exports = exports["default"];
\No newline at end of file