| 1 |
import { declare } from "@babel/helper-plugin-utils"; |
| 2 |
import syntaxFlow from "@babel/plugin-syntax-flow"; |
| 3 |
import { types as t } from "@babel/core"; |
| 4 |
import type { NodePath } from "@babel/traverse"; |
| 5 |
|
| 6 |
export interface Options { |
| 7 |
requireDirective?: boolean; |
| 8 |
allowDeclareFields?: boolean; |
| 9 |
} |
| 10 |
|
| 11 |
export default declare((api, opts: Options) => { |
| 12 |
api.assertVersion(7); |
| 13 |
|
| 14 |
const FLOW_DIRECTIVE = /(@flow(\s+(strict(-local)?|weak))?|@noflow)/; |
| 15 |
|
| 16 |
let skipStrip = false; |
| 17 |
|
| 18 |
const { requireDirective = false } = opts; |
| 19 |
|
| 20 |
if (!process.env.BABEL_8_BREAKING) { |
| 21 |
// eslint-disable-next-line no-var |
| 22 |
var { allowDeclareFields = false } = opts; |
| 23 |
} |
| 24 |
|
| 25 |
return { |
| 26 |
name: "transform-flow-strip-types", |
| 27 |
inherits: syntaxFlow, |
| 28 |
|
| 29 |
visitor: { |
| 30 |
Program( |
| 31 |
path, |
| 32 |
{ |
| 33 |
file: { |
| 34 |
ast: { comments }, |
| 35 |
}, |
| 36 |
}, |
| 37 |
) { |
| 38 |
skipStrip = false; |
| 39 |
let directiveFound = false; |
| 40 |
|
| 41 |
if (comments) { |
| 42 |
for (const comment of comments) { |
| 43 |
if (FLOW_DIRECTIVE.test(comment.value)) { |
| 44 |
directiveFound = true; |
| 45 |
|
| 46 |
// remove flow directive |
| 47 |
comment.value = comment.value.replace(FLOW_DIRECTIVE, ""); |
| 48 |
|
| 49 |
// remove the comment completely if it only consists of whitespace and/or stars |
| 50 |
if (!comment.value.replace(/\*/g, "").trim()) { |
| 51 |
comment.ignore = true; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if (!directiveFound && requireDirective) { |
| 58 |
skipStrip = true; |
| 59 |
} |
| 60 |
}, |
| 61 |
ImportDeclaration(path) { |
| 62 |
if (skipStrip) return; |
| 63 |
if (!path.node.specifiers.length) return; |
| 64 |
|
| 65 |
let typeCount = 0; |
| 66 |
|
| 67 |
// @ts-expect-error importKind is only in importSpecifier |
| 68 |
path.node.specifiers.forEach(({ importKind }) => { |
| 69 |
if (importKind === "type" || importKind === "typeof") { |
| 70 |
typeCount++; |
| 71 |
} |
| 72 |
}); |
| 73 |
|
| 74 |
if (typeCount === path.node.specifiers.length) { |
| 75 |
path.remove(); |
| 76 |
} |
| 77 |
}, |
| 78 |
|
| 79 |
Flow( |
| 80 |
path: NodePath< |
| 81 |
t.Flow | t.ImportDeclaration | t.ExportDeclaration | t.ImportSpecifier |
| 82 |
>, |
| 83 |
) { |
| 84 |
if (skipStrip) { |
| 85 |
throw path.buildCodeFrameError( |
| 86 |
"A @flow directive is required when using Flow annotations with " + |
| 87 |
"the `requireDirective` option.", |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
path.remove(); |
| 92 |
}, |
| 93 |
|
| 94 |
ClassPrivateProperty(path) { |
| 95 |
if (skipStrip) return; |
| 96 |
path.node.typeAnnotation = null; |
| 97 |
}, |
| 98 |
|
| 99 |
Class(path) { |
| 100 |
if (skipStrip) return; |
| 101 |
path.node.implements = null; |
| 102 |
|
| 103 |
// We do this here instead of in a `ClassProperty` visitor because the class transform |
| 104 |
// would transform the class before we reached the class property. |
| 105 |
path.get("body.body").forEach(child => { |
| 106 |
if (child.isClassProperty()) { |
| 107 |
const { node } = child; |
| 108 |
|
| 109 |
if (!process.env.BABEL_8_BREAKING) { |
| 110 |
if (!allowDeclareFields && node.declare) { |
| 111 |
throw child.buildCodeFrameError( |
| 112 |
`The 'declare' modifier is only allowed when the ` + |
| 113 |
`'allowDeclareFields' option of ` + |
| 114 |
`@babel/plugin-transform-flow-strip-types or ` + |
| 115 |
`@babel/preset-flow is enabled.`, |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if (node.declare) { |
| 121 |
child.remove(); |
| 122 |
} else { |
| 123 |
if (!process.env.BABEL_8_BREAKING) { |
| 124 |
if (!allowDeclareFields && !node.value && !node.decorators) { |
| 125 |
child.remove(); |
| 126 |
return; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
node.variance = null; |
| 131 |
node.typeAnnotation = null; |
| 132 |
} |
| 133 |
} |
| 134 |
}); |
| 135 |
}, |
| 136 |
|
| 137 |
AssignmentPattern({ node }) { |
| 138 |
if (skipStrip) return; |
| 139 |
// @ts-expect-error optional is not in ObjectPattern |
| 140 |
if (node.left.optional) { |
| 141 |
// @ts-expect-error optional is not in ObjectPattern |
| 142 |
node.left.optional = false; |
| 143 |
} |
| 144 |
}, |
| 145 |
|
| 146 |
Function({ node }) { |
| 147 |
if (skipStrip) return; |
| 148 |
if ( |
| 149 |
node.params.length > 0 && |
| 150 |
node.params[0].type === "Identifier" && |
| 151 |
node.params[0].name === "this" |
| 152 |
) { |
| 153 |
node.params.shift(); |
| 154 |
} |
| 155 |
for (let i = 0; i < node.params.length; i++) { |
| 156 |
let param = node.params[i]; |
| 157 |
if (param.type === "AssignmentPattern") { |
| 158 |
// @ts-expect-error: refine AST types, the left of an assignment pattern as a binding |
| 159 |
// must not be a MemberExpression |
| 160 |
param = param.left; |
| 161 |
} |
| 162 |
// @ts-expect-error optional is not in ObjectPattern |
| 163 |
if (param.optional) { |
| 164 |
// @ts-expect-error optional is not in ObjectPattern |
| 165 |
param.optional = false; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
if (!t.isMethod(node)) { |
| 170 |
node.predicate = null; |
| 171 |
} |
| 172 |
}, |
| 173 |
|
| 174 |
TypeCastExpression(path) { |
| 175 |
if (skipStrip) return; |
| 176 |
let { node } = path; |
| 177 |
do { |
| 178 |
// @ts-expect-error node is a search pointer |
| 179 |
node = node.expression; |
| 180 |
} while (t.isTypeCastExpression(node)); |
| 181 |
path.replaceWith(node); |
| 182 |
}, |
| 183 |
|
| 184 |
CallExpression({ node }) { |
| 185 |
if (skipStrip) return; |
| 186 |
node.typeArguments = null; |
| 187 |
}, |
| 188 |
|
| 189 |
OptionalCallExpression({ node }) { |
| 190 |
if (skipStrip) return; |
| 191 |
node.typeArguments = null; |
| 192 |
}, |
| 193 |
|
| 194 |
NewExpression({ node }) { |
| 195 |
if (skipStrip) return; |
| 196 |
node.typeArguments = null; |
| 197 |
}, |
| 198 |
}, |
| 199 |
}; |
| 200 |
}); |