index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _esutils = _interopRequireDefault(require("esutils"));
  5. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6. /**
  7. * Converts JSX Spread arguments into Object Spread, avoiding Babel's helper or Object.assign injection.
  8. * Input:
  9. * <div a="1" {...b} />
  10. * Output:
  11. * <div {...{ a: "1", ...b }} />
  12. * ...which Babel converts to:
  13. * h("div", { a: "1", ...b })
  14. */
  15. var _default = ({
  16. types: t
  17. }) => {
  18. // converts a set of JSXAttributes to an Object.assign() call
  19. function convertAttributesAssign(attributes) {
  20. const args = [];
  21. for (let i = 0, current; i < attributes.length; i++) {
  22. const node = attributes[i];
  23. if (t.isJSXSpreadAttribute(node)) {
  24. // the first attribute is a spread, avoid copying all other attributes onto it
  25. if (i === 0) {
  26. args.push(t.objectExpression([]));
  27. }
  28. current = null;
  29. args.push(node.argument);
  30. } else {
  31. const name = getAttributeName(node);
  32. const value = getAttributeValue(node);
  33. if (!current) {
  34. current = t.objectExpression([]);
  35. args.push(current);
  36. }
  37. current.properties.push(t.objectProperty(name, value));
  38. }
  39. }
  40. return t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("assign")), args);
  41. } // Converts a JSXAttribute to the equivalent ObjectExpression property
  42. function convertAttributeSpread(node) {
  43. if (t.isJSXSpreadAttribute(node)) {
  44. return t.spreadElement(node.argument);
  45. }
  46. const name = getAttributeName(node);
  47. const value = getAttributeValue(node);
  48. return t.inherits(t.objectProperty(name, value), node);
  49. } // Convert a JSX attribute name to an Object expression property name
  50. function getAttributeName(node) {
  51. if (t.isJSXNamespacedName(node.name)) {
  52. return t.stringLiteral(node.name.namespace.name + ":" + node.name.name.name);
  53. }
  54. if (_esutils.default.keyword.isIdentifierNameES6(node.name.name)) {
  55. return t.identifier(node.name.name);
  56. }
  57. return t.stringLiteral(node.name.name);
  58. } // Convert a JSX attribute value to a JavaScript expression value
  59. function getAttributeValue(node) {
  60. let value = node.value || t.booleanLiteral(true);
  61. if (t.isJSXExpressionContainer(value)) {
  62. value = value.expression;
  63. } else if (t.isStringLiteral(value)) {
  64. value.value = value.value.replace(/\n\s+/g, " "); // "raw" JSXText should not be used from a StringLiteral because it needs to be escaped.
  65. if (value.extra && value.extra.raw) {
  66. delete value.extra.raw;
  67. }
  68. }
  69. return value;
  70. }
  71. return {
  72. name: "transform-jsx-spread",
  73. visitor: {
  74. JSXOpeningElement(path, state) {
  75. const useSpread = state.opts.useSpread === true;
  76. const hasSpread = path.node.attributes.some(attr => t.isJSXSpreadAttribute(attr)); // ignore JSX Elements without spread or with lone spread:
  77. if (!hasSpread || path.node.attributes.length === 1) return;
  78. if (useSpread) {
  79. path.node.attributes = [t.jsxSpreadAttribute(t.objectExpression(path.node.attributes.map(convertAttributeSpread)))];
  80. } else {
  81. path.node.attributes = [t.jsxSpreadAttribute(convertAttributesAssign(path.node.attributes))];
  82. }
  83. }
  84. }
  85. };
  86. };
  87. exports.default = _default;
  88. module.exports = exports.default;