1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
8 | var _pluginSyntaxFlow = require("@babel/plugin-syntax-flow");
|
9 | var _core = require("@babel/core");
|
10 | var _default = exports.default = (0, _helperPluginUtils.declare)((api, opts) => {
|
11 | api.assertVersion(7);
|
12 | const FLOW_DIRECTIVE = /(@flow(\s+(strict(-local)?|weak))?|@noflow)/;
|
13 | let skipStrip = false;
|
14 | const {
|
15 | requireDirective = false
|
16 | } = opts;
|
17 | {
|
18 | var {
|
19 | allowDeclareFields = false
|
20 | } = opts;
|
21 | }
|
22 | return {
|
23 | name: "transform-flow-strip-types",
|
24 | inherits: _pluginSyntaxFlow.default,
|
25 | visitor: {
|
26 | Program(path, {
|
27 | file: {
|
28 | ast: {
|
29 | comments
|
30 | }
|
31 | }
|
32 | }) {
|
33 | skipStrip = false;
|
34 | let directiveFound = false;
|
35 | if (comments) {
|
36 | for (const comment of comments) {
|
37 | if (FLOW_DIRECTIVE.test(comment.value)) {
|
38 | directiveFound = true;
|
39 | comment.value = comment.value.replace(FLOW_DIRECTIVE, "");
|
40 | if (!comment.value.replace(/\*/g, "").trim()) {
|
41 | comment.ignore = true;
|
42 | }
|
43 | }
|
44 | }
|
45 | }
|
46 | if (!directiveFound && requireDirective) {
|
47 | skipStrip = true;
|
48 | }
|
49 | },
|
50 | ImportDeclaration(path) {
|
51 | if (skipStrip) return;
|
52 | if (!path.node.specifiers.length) return;
|
53 | let typeCount = 0;
|
54 | path.node.specifiers.forEach(({
|
55 | importKind
|
56 | }) => {
|
57 | if (importKind === "type" || importKind === "typeof") {
|
58 | typeCount++;
|
59 | }
|
60 | });
|
61 | if (typeCount === path.node.specifiers.length) {
|
62 | path.remove();
|
63 | }
|
64 | },
|
65 | Flow(path) {
|
66 | if (skipStrip) {
|
67 | throw path.buildCodeFrameError("A @flow directive is required when using Flow annotations with " + "the `requireDirective` option.");
|
68 | }
|
69 | path.remove();
|
70 | },
|
71 | ClassPrivateProperty(path) {
|
72 | if (skipStrip) return;
|
73 | path.node.typeAnnotation = null;
|
74 | },
|
75 | Class(path) {
|
76 | if (skipStrip) return;
|
77 | path.node.implements = null;
|
78 | path.get("body.body").forEach(child => {
|
79 | if (child.isClassProperty()) {
|
80 | const {
|
81 | node
|
82 | } = child;
|
83 | {
|
84 | if (!allowDeclareFields && node.declare) {
|
85 | throw child.buildCodeFrameError(`The 'declare' modifier is only allowed when the ` + `'allowDeclareFields' option of ` + `@babel/plugin-transform-flow-strip-types or ` + `@babel/preset-flow is enabled.`);
|
86 | }
|
87 | }
|
88 | if (node.declare) {
|
89 | child.remove();
|
90 | } else {
|
91 | {
|
92 | if (!allowDeclareFields && !node.value && !node.decorators) {
|
93 | child.remove();
|
94 | return;
|
95 | }
|
96 | }
|
97 | node.variance = null;
|
98 | node.typeAnnotation = null;
|
99 | }
|
100 | }
|
101 | });
|
102 | },
|
103 | AssignmentPattern({
|
104 | node
|
105 | }) {
|
106 | if (skipStrip) return;
|
107 | if (node.left.optional) {
|
108 | node.left.optional = false;
|
109 | }
|
110 | },
|
111 | Function({
|
112 | node
|
113 | }) {
|
114 | if (skipStrip) return;
|
115 | if (node.params.length > 0 && node.params[0].type === "Identifier" && node.params[0].name === "this") {
|
116 | node.params.shift();
|
117 | }
|
118 | for (let i = 0; i < node.params.length; i++) {
|
119 | let param = node.params[i];
|
120 | if (param.type === "AssignmentPattern") {
|
121 | param = param.left;
|
122 | }
|
123 | if (param.optional) {
|
124 | param.optional = false;
|
125 | }
|
126 | }
|
127 | if (!_core.types.isMethod(node)) {
|
128 | node.predicate = null;
|
129 | }
|
130 | },
|
131 | TypeCastExpression(path) {
|
132 | if (skipStrip) return;
|
133 | let {
|
134 | node
|
135 | } = path;
|
136 | do {
|
137 | node = node.expression;
|
138 | } while (_core.types.isTypeCastExpression(node));
|
139 | path.replaceWith(node);
|
140 | },
|
141 | CallExpression({
|
142 | node
|
143 | }) {
|
144 | if (skipStrip) return;
|
145 | node.typeArguments = null;
|
146 | },
|
147 | OptionalCallExpression({
|
148 | node
|
149 | }) {
|
150 | if (skipStrip) return;
|
151 | node.typeArguments = null;
|
152 | },
|
153 | NewExpression({
|
154 | node
|
155 | }) {
|
156 | if (skipStrip) return;
|
157 | node.typeArguments = null;
|
158 | }
|
159 | }
|
160 | };
|
161 | });
|
162 |
|
163 |
|