[79a0317] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = createPlugin;
|
---|
| 7 | var _pluginSyntaxJsx = require("@babel/plugin-syntax-jsx");
|
---|
| 8 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
---|
| 9 | var _core = require("@babel/core");
|
---|
| 10 | var _helperModuleImports = require("@babel/helper-module-imports");
|
---|
| 11 | var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
|
---|
| 12 | const DEFAULT = {
|
---|
| 13 | importSource: "react",
|
---|
| 14 | runtime: "automatic",
|
---|
| 15 | pragma: "React.createElement",
|
---|
| 16 | pragmaFrag: "React.Fragment"
|
---|
| 17 | };
|
---|
| 18 | const JSX_SOURCE_ANNOTATION_REGEX = /^\s*(?:\*\s*)?@jsxImportSource\s+(\S+)\s*$/m;
|
---|
| 19 | const JSX_RUNTIME_ANNOTATION_REGEX = /^\s*(?:\*\s*)?@jsxRuntime\s+(\S+)\s*$/m;
|
---|
| 20 | const JSX_ANNOTATION_REGEX = /^\s*(?:\*\s*)?@jsx\s+(\S+)\s*$/m;
|
---|
| 21 | const JSX_FRAG_ANNOTATION_REGEX = /^\s*(?:\*\s*)?@jsxFrag\s+(\S+)\s*$/m;
|
---|
| 22 | const get = (pass, name) => pass.get(`@babel/plugin-react-jsx/${name}`);
|
---|
| 23 | const set = (pass, name, v) => pass.set(`@babel/plugin-react-jsx/${name}`, v);
|
---|
| 24 | function hasProto(node) {
|
---|
| 25 | return node.properties.some(value => _core.types.isObjectProperty(value, {
|
---|
| 26 | computed: false,
|
---|
| 27 | shorthand: false
|
---|
| 28 | }) && (_core.types.isIdentifier(value.key, {
|
---|
| 29 | name: "__proto__"
|
---|
| 30 | }) || _core.types.isStringLiteral(value.key, {
|
---|
| 31 | value: "__proto__"
|
---|
| 32 | })));
|
---|
| 33 | }
|
---|
| 34 | function createPlugin({
|
---|
| 35 | name,
|
---|
| 36 | development
|
---|
| 37 | }) {
|
---|
| 38 | return (0, _helperPluginUtils.declare)((_, options) => {
|
---|
| 39 | const {
|
---|
| 40 | pure: PURE_ANNOTATION,
|
---|
| 41 | throwIfNamespace = true,
|
---|
| 42 | filter,
|
---|
| 43 | runtime: RUNTIME_DEFAULT = development ? "automatic" : "classic",
|
---|
| 44 | importSource: IMPORT_SOURCE_DEFAULT = DEFAULT.importSource,
|
---|
| 45 | pragma: PRAGMA_DEFAULT = DEFAULT.pragma,
|
---|
| 46 | pragmaFrag: PRAGMA_FRAG_DEFAULT = DEFAULT.pragmaFrag
|
---|
| 47 | } = options;
|
---|
| 48 | {
|
---|
| 49 | var {
|
---|
| 50 | useSpread = false,
|
---|
| 51 | useBuiltIns = false
|
---|
| 52 | } = options;
|
---|
| 53 | if (RUNTIME_DEFAULT === "classic") {
|
---|
| 54 | if (typeof useSpread !== "boolean") {
|
---|
| 55 | throw new Error("transform-react-jsx currently only accepts a boolean option for " + "useSpread (defaults to false)");
|
---|
| 56 | }
|
---|
| 57 | if (typeof useBuiltIns !== "boolean") {
|
---|
| 58 | throw new Error("transform-react-jsx currently only accepts a boolean option for " + "useBuiltIns (defaults to false)");
|
---|
| 59 | }
|
---|
| 60 | if (useSpread && useBuiltIns) {
|
---|
| 61 | throw new Error("transform-react-jsx currently only accepts useBuiltIns or useSpread " + "but not both");
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 | const injectMetaPropertiesVisitor = {
|
---|
| 66 | JSXOpeningElement(path, state) {
|
---|
| 67 | const attributes = [];
|
---|
| 68 | if (isThisAllowed(path.scope)) {
|
---|
| 69 | attributes.push(_core.types.jsxAttribute(_core.types.jsxIdentifier("__self"), _core.types.jsxExpressionContainer(_core.types.thisExpression())));
|
---|
| 70 | }
|
---|
| 71 | attributes.push(_core.types.jsxAttribute(_core.types.jsxIdentifier("__source"), _core.types.jsxExpressionContainer(makeSource(path, state))));
|
---|
| 72 | path.pushContainer("attributes", attributes);
|
---|
| 73 | }
|
---|
| 74 | };
|
---|
| 75 | return {
|
---|
| 76 | name,
|
---|
| 77 | inherits: _pluginSyntaxJsx.default,
|
---|
| 78 | visitor: {
|
---|
| 79 | JSXNamespacedName(path) {
|
---|
| 80 | if (throwIfNamespace) {
|
---|
| 81 | throw path.buildCodeFrameError(`Namespace tags are not supported by default. React's JSX doesn't support namespace tags. \
|
---|
| 82 | You can set \`throwIfNamespace: false\` to bypass this warning.`);
|
---|
| 83 | }
|
---|
| 84 | },
|
---|
| 85 | JSXSpreadChild(path) {
|
---|
| 86 | throw path.buildCodeFrameError("Spread children are not supported in React.");
|
---|
| 87 | },
|
---|
| 88 | Program: {
|
---|
| 89 | enter(path, state) {
|
---|
| 90 | const {
|
---|
| 91 | file
|
---|
| 92 | } = state;
|
---|
| 93 | let runtime = RUNTIME_DEFAULT;
|
---|
| 94 | let source = IMPORT_SOURCE_DEFAULT;
|
---|
| 95 | let pragma = PRAGMA_DEFAULT;
|
---|
| 96 | let pragmaFrag = PRAGMA_FRAG_DEFAULT;
|
---|
| 97 | let sourceSet = !!options.importSource;
|
---|
| 98 | let pragmaSet = !!options.pragma;
|
---|
| 99 | let pragmaFragSet = !!options.pragmaFrag;
|
---|
| 100 | if (file.ast.comments) {
|
---|
| 101 | for (const comment of file.ast.comments) {
|
---|
| 102 | const sourceMatches = JSX_SOURCE_ANNOTATION_REGEX.exec(comment.value);
|
---|
| 103 | if (sourceMatches) {
|
---|
| 104 | source = sourceMatches[1];
|
---|
| 105 | sourceSet = true;
|
---|
| 106 | }
|
---|
| 107 | const runtimeMatches = JSX_RUNTIME_ANNOTATION_REGEX.exec(comment.value);
|
---|
| 108 | if (runtimeMatches) {
|
---|
| 109 | runtime = runtimeMatches[1];
|
---|
| 110 | }
|
---|
| 111 | const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value);
|
---|
| 112 | if (jsxMatches) {
|
---|
| 113 | pragma = jsxMatches[1];
|
---|
| 114 | pragmaSet = true;
|
---|
| 115 | }
|
---|
| 116 | const jsxFragMatches = JSX_FRAG_ANNOTATION_REGEX.exec(comment.value);
|
---|
| 117 | if (jsxFragMatches) {
|
---|
| 118 | pragmaFrag = jsxFragMatches[1];
|
---|
| 119 | pragmaFragSet = true;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | set(state, "runtime", runtime);
|
---|
| 124 | if (runtime === "classic") {
|
---|
| 125 | if (sourceSet) {
|
---|
| 126 | throw path.buildCodeFrameError(`importSource cannot be set when runtime is classic.`);
|
---|
| 127 | }
|
---|
| 128 | const createElement = toMemberExpression(pragma);
|
---|
| 129 | const fragment = toMemberExpression(pragmaFrag);
|
---|
| 130 | set(state, "id/createElement", () => _core.types.cloneNode(createElement));
|
---|
| 131 | set(state, "id/fragment", () => _core.types.cloneNode(fragment));
|
---|
| 132 | set(state, "defaultPure", pragma === DEFAULT.pragma);
|
---|
| 133 | } else if (runtime === "automatic") {
|
---|
| 134 | if (pragmaSet || pragmaFragSet) {
|
---|
| 135 | throw path.buildCodeFrameError(`pragma and pragmaFrag cannot be set when runtime is automatic.`);
|
---|
| 136 | }
|
---|
| 137 | const define = (name, id) => set(state, name, createImportLazily(state, path, id, source));
|
---|
| 138 | define("id/jsx", development ? "jsxDEV" : "jsx");
|
---|
| 139 | define("id/jsxs", development ? "jsxDEV" : "jsxs");
|
---|
| 140 | define("id/createElement", "createElement");
|
---|
| 141 | define("id/fragment", "Fragment");
|
---|
| 142 | set(state, "defaultPure", source === DEFAULT.importSource);
|
---|
| 143 | } else {
|
---|
| 144 | throw path.buildCodeFrameError(`Runtime must be either "classic" or "automatic".`);
|
---|
| 145 | }
|
---|
| 146 | if (development) {
|
---|
| 147 | path.traverse(injectMetaPropertiesVisitor, state);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | },
|
---|
| 151 | JSXFragment: {
|
---|
| 152 | exit(path, file) {
|
---|
| 153 | let callExpr;
|
---|
| 154 | if (get(file, "runtime") === "classic") {
|
---|
| 155 | callExpr = buildCreateElementFragmentCall(path, file);
|
---|
| 156 | } else {
|
---|
| 157 | callExpr = buildJSXFragmentCall(path, file);
|
---|
| 158 | }
|
---|
| 159 | path.replaceWith(_core.types.inherits(callExpr, path.node));
|
---|
| 160 | }
|
---|
| 161 | },
|
---|
| 162 | JSXElement: {
|
---|
| 163 | exit(path, file) {
|
---|
| 164 | let callExpr;
|
---|
| 165 | if (get(file, "runtime") === "classic" || shouldUseCreateElement(path)) {
|
---|
| 166 | callExpr = buildCreateElementCall(path, file);
|
---|
| 167 | } else {
|
---|
| 168 | callExpr = buildJSXElementCall(path, file);
|
---|
| 169 | }
|
---|
| 170 | path.replaceWith(_core.types.inherits(callExpr, path.node));
|
---|
| 171 | }
|
---|
| 172 | },
|
---|
| 173 | JSXAttribute(path) {
|
---|
| 174 | if (_core.types.isJSXElement(path.node.value)) {
|
---|
| 175 | path.node.value = _core.types.jsxExpressionContainer(path.node.value);
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | };
|
---|
| 180 | function isDerivedClass(classPath) {
|
---|
| 181 | return classPath.node.superClass !== null;
|
---|
| 182 | }
|
---|
| 183 | function isThisAllowed(scope) {
|
---|
| 184 | do {
|
---|
| 185 | const {
|
---|
| 186 | path
|
---|
| 187 | } = scope;
|
---|
| 188 | if (path.isFunctionParent() && !path.isArrowFunctionExpression()) {
|
---|
| 189 | if (!path.isMethod()) {
|
---|
| 190 | return true;
|
---|
| 191 | }
|
---|
| 192 | if (path.node.kind !== "constructor") {
|
---|
| 193 | return true;
|
---|
| 194 | }
|
---|
| 195 | return !isDerivedClass(path.parentPath.parentPath);
|
---|
| 196 | }
|
---|
| 197 | if (path.isTSModuleBlock()) {
|
---|
| 198 | return false;
|
---|
| 199 | }
|
---|
| 200 | } while (scope = scope.parent);
|
---|
| 201 | return true;
|
---|
| 202 | }
|
---|
| 203 | function call(pass, name, args) {
|
---|
| 204 | const node = _core.types.callExpression(get(pass, `id/${name}`)(), args);
|
---|
| 205 | if (PURE_ANNOTATION != null ? PURE_ANNOTATION : get(pass, "defaultPure")) (0, _helperAnnotateAsPure.default)(node);
|
---|
| 206 | return node;
|
---|
| 207 | }
|
---|
| 208 | function shouldUseCreateElement(path) {
|
---|
| 209 | const openingPath = path.get("openingElement");
|
---|
| 210 | const attributes = openingPath.node.attributes;
|
---|
| 211 | let seenPropsSpread = false;
|
---|
| 212 | for (let i = 0; i < attributes.length; i++) {
|
---|
| 213 | const attr = attributes[i];
|
---|
| 214 | if (seenPropsSpread && _core.types.isJSXAttribute(attr) && attr.name.name === "key") {
|
---|
| 215 | return true;
|
---|
| 216 | } else if (_core.types.isJSXSpreadAttribute(attr)) {
|
---|
| 217 | seenPropsSpread = true;
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 | return false;
|
---|
| 221 | }
|
---|
| 222 | function convertJSXIdentifier(node, parent) {
|
---|
| 223 | if (_core.types.isJSXIdentifier(node)) {
|
---|
| 224 | if (node.name === "this" && _core.types.isReferenced(node, parent)) {
|
---|
| 225 | return _core.types.thisExpression();
|
---|
| 226 | } else if (_core.types.isValidIdentifier(node.name, false)) {
|
---|
| 227 | node.type = "Identifier";
|
---|
| 228 | return node;
|
---|
| 229 | } else {
|
---|
| 230 | return _core.types.stringLiteral(node.name);
|
---|
| 231 | }
|
---|
| 232 | } else if (_core.types.isJSXMemberExpression(node)) {
|
---|
| 233 | return _core.types.memberExpression(convertJSXIdentifier(node.object, node), convertJSXIdentifier(node.property, node));
|
---|
| 234 | } else if (_core.types.isJSXNamespacedName(node)) {
|
---|
| 235 | return _core.types.stringLiteral(`${node.namespace.name}:${node.name.name}`);
|
---|
| 236 | }
|
---|
| 237 | return node;
|
---|
| 238 | }
|
---|
| 239 | function convertAttributeValue(node) {
|
---|
| 240 | if (_core.types.isJSXExpressionContainer(node)) {
|
---|
| 241 | return node.expression;
|
---|
| 242 | } else {
|
---|
| 243 | return node;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | function accumulateAttribute(array, attribute) {
|
---|
| 247 | if (_core.types.isJSXSpreadAttribute(attribute.node)) {
|
---|
| 248 | const arg = attribute.node.argument;
|
---|
| 249 | if (_core.types.isObjectExpression(arg) && !hasProto(arg)) {
|
---|
| 250 | array.push(...arg.properties);
|
---|
| 251 | } else {
|
---|
| 252 | array.push(_core.types.spreadElement(arg));
|
---|
| 253 | }
|
---|
| 254 | return array;
|
---|
| 255 | }
|
---|
| 256 | const value = convertAttributeValue(attribute.node.name.name !== "key" ? attribute.node.value || _core.types.booleanLiteral(true) : attribute.node.value);
|
---|
| 257 | if (attribute.node.name.name === "key" && value === null) {
|
---|
| 258 | throw attribute.buildCodeFrameError('Please provide an explicit key value. Using "key" as a shorthand for "key={true}" is not allowed.');
|
---|
| 259 | }
|
---|
| 260 | if (_core.types.isStringLiteral(value) && !_core.types.isJSXExpressionContainer(attribute.node.value)) {
|
---|
| 261 | var _value$extra;
|
---|
| 262 | value.value = value.value.replace(/\n\s+/g, " ");
|
---|
| 263 | (_value$extra = value.extra) == null || delete _value$extra.raw;
|
---|
| 264 | }
|
---|
| 265 | if (_core.types.isJSXNamespacedName(attribute.node.name)) {
|
---|
| 266 | attribute.node.name = _core.types.stringLiteral(attribute.node.name.namespace.name + ":" + attribute.node.name.name.name);
|
---|
| 267 | } else if (_core.types.isValidIdentifier(attribute.node.name.name, false)) {
|
---|
| 268 | attribute.node.name.type = "Identifier";
|
---|
| 269 | } else {
|
---|
| 270 | attribute.node.name = _core.types.stringLiteral(attribute.node.name.name);
|
---|
| 271 | }
|
---|
| 272 | array.push(_core.types.inherits(_core.types.objectProperty(attribute.node.name, value), attribute.node));
|
---|
| 273 | return array;
|
---|
| 274 | }
|
---|
| 275 | function buildChildrenProperty(children) {
|
---|
| 276 | let childrenNode;
|
---|
| 277 | if (children.length === 1) {
|
---|
| 278 | childrenNode = children[0];
|
---|
| 279 | } else if (children.length > 1) {
|
---|
| 280 | childrenNode = _core.types.arrayExpression(children);
|
---|
| 281 | } else {
|
---|
| 282 | return undefined;
|
---|
| 283 | }
|
---|
| 284 | return _core.types.objectProperty(_core.types.identifier("children"), childrenNode);
|
---|
| 285 | }
|
---|
| 286 | function buildJSXElementCall(path, file) {
|
---|
| 287 | const openingPath = path.get("openingElement");
|
---|
| 288 | const args = [getTag(openingPath)];
|
---|
| 289 | const attribsArray = [];
|
---|
| 290 | const extracted = Object.create(null);
|
---|
| 291 | for (const attr of openingPath.get("attributes")) {
|
---|
| 292 | if (attr.isJSXAttribute() && _core.types.isJSXIdentifier(attr.node.name)) {
|
---|
| 293 | const {
|
---|
| 294 | name
|
---|
| 295 | } = attr.node.name;
|
---|
| 296 | switch (name) {
|
---|
| 297 | case "__source":
|
---|
| 298 | case "__self":
|
---|
| 299 | if (extracted[name]) throw sourceSelfError(path, name);
|
---|
| 300 | case "key":
|
---|
| 301 | {
|
---|
| 302 | const keyValue = convertAttributeValue(attr.node.value);
|
---|
| 303 | if (keyValue === null) {
|
---|
| 304 | throw attr.buildCodeFrameError('Please provide an explicit key value. Using "key" as a shorthand for "key={true}" is not allowed.');
|
---|
| 305 | }
|
---|
| 306 | extracted[name] = keyValue;
|
---|
| 307 | break;
|
---|
| 308 | }
|
---|
| 309 | default:
|
---|
| 310 | attribsArray.push(attr);
|
---|
| 311 | }
|
---|
| 312 | } else {
|
---|
| 313 | attribsArray.push(attr);
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
| 316 | const children = _core.types.react.buildChildren(path.node);
|
---|
| 317 | let attribs;
|
---|
| 318 | if (attribsArray.length || children.length) {
|
---|
| 319 | attribs = buildJSXOpeningElementAttributes(attribsArray, children);
|
---|
| 320 | } else {
|
---|
| 321 | attribs = _core.types.objectExpression([]);
|
---|
| 322 | }
|
---|
| 323 | args.push(attribs);
|
---|
| 324 | if (development) {
|
---|
| 325 | var _extracted$key;
|
---|
| 326 | args.push((_extracted$key = extracted.key) != null ? _extracted$key : path.scope.buildUndefinedNode(), _core.types.booleanLiteral(children.length > 1));
|
---|
| 327 | if (extracted.__source) {
|
---|
| 328 | args.push(extracted.__source);
|
---|
| 329 | if (extracted.__self) args.push(extracted.__self);
|
---|
| 330 | } else if (extracted.__self) {
|
---|
| 331 | args.push(path.scope.buildUndefinedNode(), extracted.__self);
|
---|
| 332 | }
|
---|
| 333 | } else if (extracted.key !== undefined) {
|
---|
| 334 | args.push(extracted.key);
|
---|
| 335 | }
|
---|
| 336 | return call(file, children.length > 1 ? "jsxs" : "jsx", args);
|
---|
| 337 | }
|
---|
| 338 | function buildJSXOpeningElementAttributes(attribs, children) {
|
---|
| 339 | const props = attribs.reduce(accumulateAttribute, []);
|
---|
| 340 | if ((children == null ? void 0 : children.length) > 0) {
|
---|
| 341 | props.push(buildChildrenProperty(children));
|
---|
| 342 | }
|
---|
| 343 | return _core.types.objectExpression(props);
|
---|
| 344 | }
|
---|
| 345 | function buildJSXFragmentCall(path, file) {
|
---|
| 346 | const args = [get(file, "id/fragment")()];
|
---|
| 347 | const children = _core.types.react.buildChildren(path.node);
|
---|
| 348 | args.push(_core.types.objectExpression(children.length > 0 ? [buildChildrenProperty(children)] : []));
|
---|
| 349 | if (development) {
|
---|
| 350 | args.push(path.scope.buildUndefinedNode(), _core.types.booleanLiteral(children.length > 1));
|
---|
| 351 | }
|
---|
| 352 | return call(file, children.length > 1 ? "jsxs" : "jsx", args);
|
---|
| 353 | }
|
---|
| 354 | function buildCreateElementFragmentCall(path, file) {
|
---|
| 355 | if (filter && !filter(path.node, file)) return;
|
---|
| 356 | return call(file, "createElement", [get(file, "id/fragment")(), _core.types.nullLiteral(), ..._core.types.react.buildChildren(path.node)]);
|
---|
| 357 | }
|
---|
| 358 | function buildCreateElementCall(path, file) {
|
---|
| 359 | const openingPath = path.get("openingElement");
|
---|
| 360 | return call(file, "createElement", [getTag(openingPath), buildCreateElementOpeningElementAttributes(file, path, openingPath.get("attributes")), ..._core.types.react.buildChildren(path.node)]);
|
---|
| 361 | }
|
---|
| 362 | function getTag(openingPath) {
|
---|
| 363 | const tagExpr = convertJSXIdentifier(openingPath.node.name, openingPath.node);
|
---|
| 364 | let tagName;
|
---|
| 365 | if (_core.types.isIdentifier(tagExpr)) {
|
---|
| 366 | tagName = tagExpr.name;
|
---|
| 367 | } else if (_core.types.isStringLiteral(tagExpr)) {
|
---|
| 368 | tagName = tagExpr.value;
|
---|
| 369 | }
|
---|
| 370 | if (_core.types.react.isCompatTag(tagName)) {
|
---|
| 371 | return _core.types.stringLiteral(tagName);
|
---|
| 372 | } else {
|
---|
| 373 | return tagExpr;
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
| 376 | function buildCreateElementOpeningElementAttributes(file, path, attribs) {
|
---|
| 377 | const runtime = get(file, "runtime");
|
---|
| 378 | {
|
---|
| 379 | if (runtime !== "automatic") {
|
---|
| 380 | const objs = [];
|
---|
| 381 | const props = attribs.reduce(accumulateAttribute, []);
|
---|
| 382 | if (!useSpread) {
|
---|
| 383 | let start = 0;
|
---|
| 384 | props.forEach((prop, i) => {
|
---|
| 385 | if (_core.types.isSpreadElement(prop)) {
|
---|
| 386 | if (i > start) {
|
---|
| 387 | objs.push(_core.types.objectExpression(props.slice(start, i)));
|
---|
| 388 | }
|
---|
| 389 | objs.push(prop.argument);
|
---|
| 390 | start = i + 1;
|
---|
| 391 | }
|
---|
| 392 | });
|
---|
| 393 | if (props.length > start) {
|
---|
| 394 | objs.push(_core.types.objectExpression(props.slice(start)));
|
---|
| 395 | }
|
---|
| 396 | } else if (props.length) {
|
---|
| 397 | objs.push(_core.types.objectExpression(props));
|
---|
| 398 | }
|
---|
| 399 | if (!objs.length) {
|
---|
| 400 | return _core.types.nullLiteral();
|
---|
| 401 | }
|
---|
| 402 | if (objs.length === 1) {
|
---|
| 403 | if (!(_core.types.isSpreadElement(props[0]) && _core.types.isObjectExpression(props[0].argument))) {
|
---|
| 404 | return objs[0];
|
---|
| 405 | }
|
---|
| 406 | }
|
---|
| 407 | if (!_core.types.isObjectExpression(objs[0])) {
|
---|
| 408 | objs.unshift(_core.types.objectExpression([]));
|
---|
| 409 | }
|
---|
| 410 | const helper = useBuiltIns ? _core.types.memberExpression(_core.types.identifier("Object"), _core.types.identifier("assign")) : file.addHelper("extends");
|
---|
| 411 | return _core.types.callExpression(helper, objs);
|
---|
| 412 | }
|
---|
| 413 | }
|
---|
| 414 | const props = [];
|
---|
| 415 | const found = Object.create(null);
|
---|
| 416 | for (const attr of attribs) {
|
---|
| 417 | const {
|
---|
| 418 | node
|
---|
| 419 | } = attr;
|
---|
| 420 | const name = _core.types.isJSXAttribute(node) && _core.types.isJSXIdentifier(node.name) && node.name.name;
|
---|
| 421 | if (runtime === "automatic" && (name === "__source" || name === "__self")) {
|
---|
| 422 | if (found[name]) throw sourceSelfError(path, name);
|
---|
| 423 | found[name] = true;
|
---|
| 424 | }
|
---|
| 425 | accumulateAttribute(props, attr);
|
---|
| 426 | }
|
---|
| 427 | return props.length === 1 && _core.types.isSpreadElement(props[0]) && !_core.types.isObjectExpression(props[0].argument) ? props[0].argument : props.length > 0 ? _core.types.objectExpression(props) : _core.types.nullLiteral();
|
---|
| 428 | }
|
---|
| 429 | });
|
---|
| 430 | function getSource(source, importName) {
|
---|
| 431 | switch (importName) {
|
---|
| 432 | case "Fragment":
|
---|
| 433 | return `${source}/${development ? "jsx-dev-runtime" : "jsx-runtime"}`;
|
---|
| 434 | case "jsxDEV":
|
---|
| 435 | return `${source}/jsx-dev-runtime`;
|
---|
| 436 | case "jsx":
|
---|
| 437 | case "jsxs":
|
---|
| 438 | return `${source}/jsx-runtime`;
|
---|
| 439 | case "createElement":
|
---|
| 440 | return source;
|
---|
| 441 | }
|
---|
| 442 | }
|
---|
| 443 | function createImportLazily(pass, path, importName, source) {
|
---|
| 444 | return () => {
|
---|
| 445 | const actualSource = getSource(source, importName);
|
---|
| 446 | if ((0, _helperModuleImports.isModule)(path)) {
|
---|
| 447 | let reference = get(pass, `imports/${importName}`);
|
---|
| 448 | if (reference) return _core.types.cloneNode(reference);
|
---|
| 449 | reference = (0, _helperModuleImports.addNamed)(path, importName, actualSource, {
|
---|
| 450 | importedInterop: "uncompiled",
|
---|
| 451 | importPosition: "after"
|
---|
| 452 | });
|
---|
| 453 | set(pass, `imports/${importName}`, reference);
|
---|
| 454 | return reference;
|
---|
| 455 | } else {
|
---|
| 456 | let reference = get(pass, `requires/${actualSource}`);
|
---|
| 457 | if (reference) {
|
---|
| 458 | reference = _core.types.cloneNode(reference);
|
---|
| 459 | } else {
|
---|
| 460 | reference = (0, _helperModuleImports.addNamespace)(path, actualSource, {
|
---|
| 461 | importedInterop: "uncompiled"
|
---|
| 462 | });
|
---|
| 463 | set(pass, `requires/${actualSource}`, reference);
|
---|
| 464 | }
|
---|
| 465 | return _core.types.memberExpression(reference, _core.types.identifier(importName));
|
---|
| 466 | }
|
---|
| 467 | };
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
| 470 | function toMemberExpression(id) {
|
---|
| 471 | return id.split(".").map(name => _core.types.identifier(name)).reduce((object, property) => _core.types.memberExpression(object, property));
|
---|
| 472 | }
|
---|
| 473 | function makeSource(path, state) {
|
---|
| 474 | const location = path.node.loc;
|
---|
| 475 | if (!location) {
|
---|
| 476 | return path.scope.buildUndefinedNode();
|
---|
| 477 | }
|
---|
| 478 | if (!state.fileNameIdentifier) {
|
---|
| 479 | const {
|
---|
| 480 | filename = ""
|
---|
| 481 | } = state;
|
---|
| 482 | const fileNameIdentifier = path.scope.generateUidIdentifier("_jsxFileName");
|
---|
| 483 | path.scope.getProgramParent().push({
|
---|
| 484 | id: fileNameIdentifier,
|
---|
| 485 | init: _core.types.stringLiteral(filename)
|
---|
| 486 | });
|
---|
| 487 | state.fileNameIdentifier = fileNameIdentifier;
|
---|
| 488 | }
|
---|
| 489 | return makeTrace(_core.types.cloneNode(state.fileNameIdentifier), location.start.line, location.start.column);
|
---|
| 490 | }
|
---|
| 491 | function makeTrace(fileNameIdentifier, lineNumber, column0Based) {
|
---|
| 492 | const fileLineLiteral = lineNumber != null ? _core.types.numericLiteral(lineNumber) : _core.types.nullLiteral();
|
---|
| 493 | const fileColumnLiteral = column0Based != null ? _core.types.numericLiteral(column0Based + 1) : _core.types.nullLiteral();
|
---|
| 494 | return _core.template.expression.ast`{
|
---|
| 495 | fileName: ${fileNameIdentifier},
|
---|
| 496 | lineNumber: ${fileLineLiteral},
|
---|
| 497 | columnNumber: ${fileColumnLiteral},
|
---|
| 498 | }`;
|
---|
| 499 | }
|
---|
| 500 | function sourceSelfError(path, name) {
|
---|
| 501 | const pluginName = `transform-react-jsx-${name.slice(2)}`;
|
---|
| 502 | return path.buildCodeFrameError(`Duplicate ${name} prop found. You are most likely using the deprecated ${pluginName} Babel plugin. Both __source and __self are automatically set when using the automatic runtime. Please remove transform-react-jsx-source and transform-react-jsx-self from your Babel config.`);
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 | //# sourceMappingURL=create-plugin.js.map
|
---|