| 1 |
import { |
| 2 |
booleanLiteral, |
| 3 |
callExpression, |
| 4 |
identifier, |
| 5 |
inherits, |
| 6 |
isIdentifier, |
| 7 |
isJSXExpressionContainer, |
| 8 |
isJSXIdentifier, |
| 9 |
isJSXMemberExpression, |
| 10 |
isJSXNamespacedName, |
| 11 |
isJSXSpreadAttribute, |
| 12 |
isObjectExpression, |
| 13 |
isReferenced, |
| 14 |
isStringLiteral, |
| 15 |
isValidIdentifier, |
| 16 |
memberExpression, |
| 17 |
nullLiteral, |
| 18 |
objectExpression, |
| 19 |
objectProperty, |
| 20 |
react, |
| 21 |
spreadElement, |
| 22 |
stringLiteral, |
| 23 |
thisExpression, |
| 24 |
} from "@babel/types"; |
| 25 |
import annotateAsPure from "@babel/helper-annotate-as-pure"; |
| 26 |
import type { NodePath, Visitor } from "@babel/traverse"; |
| 27 |
import type { PluginPass } from "@babel/core"; |
| 28 |
import type * as t from "@babel/types"; |
| 29 |
|
| 30 |
type ElementState = { |
| 31 |
tagExpr: t.Expression; // tag node, |
| 32 |
tagName: string | undefined | null; // raw string tag name, |
| 33 |
args: Array<any>; // array of call arguments, |
| 34 |
call?: any; // optional call property that can be set to override the call expression returned, |
| 35 |
pure: boolean; // true if the element can be marked with a #__PURE__ annotation |
| 36 |
callee?: any; |
| 37 |
}; |
| 38 |
|
| 39 |
export interface Options { |
| 40 |
filter?: (node: t.Node, pass: PluginPass) => boolean; |
| 41 |
pre?: (state: ElementState, pass: PluginPass) => void; |
| 42 |
post?: (state: ElementState, pass: PluginPass) => void; |
| 43 |
compat?: boolean; |
| 44 |
pure?: string; |
| 45 |
throwIfNamespace?: boolean; |
| 46 |
} |
| 47 |
|
| 48 |
export default function (opts: Options) { |
| 49 |
const visitor: Visitor<PluginPass> = {}; |
| 50 |
|
| 51 |
visitor.JSXNamespacedName = function (path) { |
| 52 |
if (opts.throwIfNamespace) { |
| 53 |
throw path.buildCodeFrameError( |
| 54 |
`Namespace tags are not supported by default. React's JSX doesn't support namespace tags. \ |
| 55 |
You can set \`throwIfNamespace: false\` to bypass this warning.`, |
| 56 |
); |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
visitor.JSXSpreadChild = function (path) { |
| 61 |
throw path.buildCodeFrameError( |
| 62 |
"Spread children are not supported in React.", |
| 63 |
); |
| 64 |
}; |
| 65 |
|
| 66 |
visitor.JSXElement = { |
| 67 |
exit(path, state) { |
| 68 |
const callExpr = buildElementCall(path, state); |
| 69 |
if (callExpr) { |
| 70 |
path.replaceWith(inherits(callExpr, path.node)); |
| 71 |
} |
| 72 |
}, |
| 73 |
}; |
| 74 |
|
| 75 |
visitor.JSXFragment = { |
| 76 |
exit(path, state) { |
| 77 |
if (opts.compat) { |
| 78 |
throw path.buildCodeFrameError( |
| 79 |
"Fragment tags are only supported in React 16 and up.", |
| 80 |
); |
| 81 |
} |
| 82 |
const callExpr = buildFragmentCall(path, state); |
| 83 |
if (callExpr) { |
| 84 |
path.replaceWith(inherits(callExpr, path.node)); |
| 85 |
} |
| 86 |
}, |
| 87 |
}; |
| 88 |
|
| 89 |
return visitor; |
| 90 |
|
| 91 |
function convertJSXIdentifier( |
| 92 |
node: t.JSXIdentifier | t.JSXMemberExpression | t.JSXNamespacedName, |
| 93 |
parent: t.JSXOpeningElement | t.JSXMemberExpression, |
| 94 |
): t.ThisExpression | t.StringLiteral | t.MemberExpression | t.Identifier { |
| 95 |
if (isJSXIdentifier(node)) { |
| 96 |
if (node.name === "this" && isReferenced(node, parent)) { |
| 97 |
return thisExpression(); |
| 98 |
} else if (isValidIdentifier(node.name, false)) { |
| 99 |
// @ts-expect-error casting JSXIdentifier to Identifier |
| 100 |
node.type = "Identifier"; |
| 101 |
return node as unknown as t.Identifier; |
| 102 |
} else { |
| 103 |
return stringLiteral(node.name); |
| 104 |
} |
| 105 |
} else if (isJSXMemberExpression(node)) { |
| 106 |
return memberExpression( |
| 107 |
convertJSXIdentifier(node.object, node), |
| 108 |
convertJSXIdentifier(node.property, node), |
| 109 |
); |
| 110 |
} else if (isJSXNamespacedName(node)) { |
| 111 |
/** |
| 112 |
* If there is flag "throwIfNamespace" |
| 113 |
* print XMLNamespace like string literal |
| 114 |
*/ |
| 115 |
return stringLiteral(`${node.namespace.name}:${node.name.name}`); |
| 116 |
} |
| 117 |
|
| 118 |
return node; |
| 119 |
} |
| 120 |
|
| 121 |
function convertAttributeValue( |
| 122 |
node: t.JSXAttribute["value"] | t.BooleanLiteral, |
| 123 |
) { |
| 124 |
if (isJSXExpressionContainer(node)) { |
| 125 |
return node.expression; |
| 126 |
} else { |
| 127 |
return node; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
function convertAttribute(node: t.JSXAttribute | t.JSXSpreadAttribute) { |
| 132 |
if (isJSXSpreadAttribute(node)) { |
| 133 |
return spreadElement(node.argument); |
| 134 |
} |
| 135 |
const value = convertAttributeValue(node.value || booleanLiteral(true)); |
| 136 |
|
| 137 |
if (isStringLiteral(value) && !isJSXExpressionContainer(node.value)) { |
| 138 |
value.value = value.value.replace(/\n\s+/g, " "); |
| 139 |
|
| 140 |
// "raw" JSXText should not be used from a StringLiteral because it needs to be escaped. |
| 141 |
delete value.extra?.raw; |
| 142 |
} |
| 143 |
|
| 144 |
if (isJSXNamespacedName(node.name)) { |
| 145 |
// @ts-expect-error Mutating AST nodes |
| 146 |
node.name = stringLiteral( |
| 147 |
node.name.namespace.name + ":" + node.name.name.name, |
| 148 |
); |
| 149 |
} else if (isValidIdentifier(node.name.name, false)) { |
| 150 |
// @ts-expect-error Mutating AST nodes |
| 151 |
node.name.type = "Identifier"; |
| 152 |
} else { |
| 153 |
// @ts-expect-error Mutating AST nodes |
| 154 |
node.name = stringLiteral(node.name.name); |
| 155 |
} |
| 156 |
|
| 157 |
return inherits( |
| 158 |
objectProperty( |
| 159 |
// @ts-expect-error Mutating AST nodes |
| 160 |
node.name, |
| 161 |
value, |
| 162 |
), |
| 163 |
node, |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
function buildElementCall(path: NodePath<t.JSXElement>, pass: PluginPass) { |
| 168 |
if (opts.filter && !opts.filter(path.node, pass)) return; |
| 169 |
|
| 170 |
const openingPath = path.get("openingElement"); |
| 171 |
// @ts-expect-error mutating AST nodes |
| 172 |
path.node.children = react.buildChildren(path.node); |
| 173 |
|
| 174 |
const tagExpr = convertJSXIdentifier( |
| 175 |
openingPath.node.name, |
| 176 |
openingPath.node, |
| 177 |
); |
| 178 |
const args: (t.Expression | t.JSXElement | t.JSXFragment)[] = []; |
| 179 |
|
| 180 |
let tagName: string; |
| 181 |
if (isIdentifier(tagExpr)) { |
| 182 |
tagName = tagExpr.name; |
| 183 |
} else if (isStringLiteral(tagExpr)) { |
| 184 |
tagName = tagExpr.value; |
| 185 |
} |
| 186 |
|
| 187 |
const state: ElementState = { |
| 188 |
tagExpr: tagExpr, |
| 189 |
tagName: tagName, |
| 190 |
args: args, |
| 191 |
pure: false, |
| 192 |
}; |
| 193 |
|
| 194 |
if (opts.pre) { |
| 195 |
opts.pre(state, pass); |
| 196 |
} |
| 197 |
|
| 198 |
const attribs = openingPath.node.attributes; |
| 199 |
let convertedAttributes: t.Expression; |
| 200 |
if (attribs.length) { |
| 201 |
if (process.env.BABEL_8_BREAKING) { |
| 202 |
convertedAttributes = objectExpression(attribs.map(convertAttribute)); |
| 203 |
} else { |
| 204 |
convertedAttributes = buildOpeningElementAttributes(attribs, pass); |
| 205 |
} |
| 206 |
} else { |
| 207 |
convertedAttributes = nullLiteral(); |
| 208 |
} |
| 209 |
|
| 210 |
args.push( |
| 211 |
convertedAttributes, |
| 212 |
// @ts-expect-error JSXExpressionContainer has been transformed by convertAttributeValue |
| 213 |
...path.node.children, |
| 214 |
); |
| 215 |
|
| 216 |
if (opts.post) { |
| 217 |
opts.post(state, pass); |
| 218 |
} |
| 219 |
|
| 220 |
const call = state.call || callExpression(state.callee, args); |
| 221 |
if (state.pure) annotateAsPure(call); |
| 222 |
|
| 223 |
return call; |
| 224 |
} |
| 225 |
|
| 226 |
function pushProps( |
| 227 |
_props: (t.ObjectProperty | t.SpreadElement)[], |
| 228 |
objs: t.Expression[], |
| 229 |
) { |
| 230 |
if (!_props.length) return _props; |
| 231 |
|
| 232 |
objs.push(objectExpression(_props)); |
| 233 |
return []; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* The logic for this is quite terse. It's because we need to |
| 238 |
* support spread elements. We loop over all attributes, |
| 239 |
* breaking on spreads, we then push a new object containing |
| 240 |
* all prior attributes to an array for later processing. |
| 241 |
*/ |
| 242 |
|
| 243 |
function buildOpeningElementAttributes( |
| 244 |
attribs: (t.JSXAttribute | t.JSXSpreadAttribute)[], |
| 245 |
pass: PluginPass, |
| 246 |
): t.Expression { |
| 247 |
let _props: (t.ObjectProperty | t.SpreadElement)[] = []; |
| 248 |
const objs: t.Expression[] = []; |
| 249 |
|
| 250 |
const { useSpread = false } = pass.opts; |
| 251 |
if (typeof useSpread !== "boolean") { |
| 252 |
throw new Error( |
| 253 |
"transform-react-jsx currently only accepts a boolean option for " + |
| 254 |
"useSpread (defaults to false)", |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
const useBuiltIns = pass.opts.useBuiltIns || false; |
| 259 |
if (typeof useBuiltIns !== "boolean") { |
| 260 |
throw new Error( |
| 261 |
"transform-react-jsx currently only accepts a boolean option for " + |
| 262 |
"useBuiltIns (defaults to false)", |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
if (useSpread && useBuiltIns) { |
| 267 |
throw new Error( |
| 268 |
"transform-react-jsx currently only accepts useBuiltIns or useSpread " + |
| 269 |
"but not both", |
| 270 |
); |
| 271 |
} |
| 272 |
|
| 273 |
if (useSpread) { |
| 274 |
const props = attribs.map(convertAttribute); |
| 275 |
return objectExpression(props); |
| 276 |
} |
| 277 |
|
| 278 |
while (attribs.length) { |
| 279 |
const prop = attribs.shift(); |
| 280 |
if (isJSXSpreadAttribute(prop)) { |
| 281 |
_props = pushProps(_props, objs); |
| 282 |
objs.push(prop.argument); |
| 283 |
} else { |
| 284 |
_props.push(convertAttribute(prop)); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
pushProps(_props, objs); |
| 289 |
let convertedAttribs: t.Expression; |
| 290 |
|
| 291 |
if (objs.length === 1) { |
| 292 |
// only one object |
| 293 |
convertedAttribs = objs[0]; |
| 294 |
} else { |
| 295 |
// looks like we have multiple objects |
| 296 |
if (!isObjectExpression(objs[0])) { |
| 297 |
objs.unshift(objectExpression([])); |
| 298 |
} |
| 299 |
|
| 300 |
const helper = useBuiltIns |
| 301 |
? memberExpression(identifier("Object"), identifier("assign")) |
| 302 |
: pass.addHelper("extends"); |
| 303 |
|
| 304 |
// spread it |
| 305 |
convertedAttribs = callExpression(helper, objs); |
| 306 |
} |
| 307 |
|
| 308 |
return convertedAttribs; |
| 309 |
} |
| 310 |
|
| 311 |
function buildFragmentCall(path: NodePath<t.JSXFragment>, pass: PluginPass) { |
| 312 |
if (opts.filter && !opts.filter(path.node, pass)) return; |
| 313 |
|
| 314 |
// @ts-expect-error mutating AST nodes |
| 315 |
path.node.children = react.buildChildren(path.node); |
| 316 |
|
| 317 |
const args: t.Expression[] = []; |
| 318 |
const tagName: null = null; |
| 319 |
const tagExpr = pass.get("jsxFragIdentifier")(); |
| 320 |
|
| 321 |
const state: ElementState = { |
| 322 |
tagExpr: tagExpr, |
| 323 |
tagName: tagName, |
| 324 |
args: args, |
| 325 |
pure: false, |
| 326 |
}; |
| 327 |
|
| 328 |
if (opts.pre) { |
| 329 |
opts.pre(state, pass); |
| 330 |
} |
| 331 |
|
| 332 |
// no attributes are allowed with <> syntax |
| 333 |
args.push( |
| 334 |
nullLiteral(), |
| 335 |
// @ts-expect-error JSXExpressionContainer has been transformed by convertAttributeValue |
| 336 |
...path.node.children, |
| 337 |
); |
| 338 |
|
| 339 |
if (opts.post) { |
| 340 |
opts.post(state, pass); |
| 341 |
} |
| 342 |
|
| 343 |
pass.set("usedFragment", true); |
| 344 |
|
| 345 |
const call = state.call || callExpression(state.callee, args); |
| 346 |
if (state.pure) annotateAsPure(call); |
| 347 |
|
| 348 |
return call; |
| 349 |
} |
| 350 |
} |