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