1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | var _helperPluginUtils = require("@babel/helper-plugin-utils");
|
9 |
|
10 | var _helperBuilderReactJsx = _interopRequireDefault(require("@babel/helper-builder-react-jsx"));
|
11 |
|
12 | var _core = require("@babel/core");
|
13 |
|
14 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
15 |
|
16 | var _default = (0, _helperPluginUtils.declare)(api => {
|
17 | api.assertVersion(7);
|
18 |
|
19 | function hasRefOrSpread(attrs) {
|
20 | for (let i = 0; i < attrs.length; i++) {
|
21 | const attr = attrs[i];
|
22 | if (_core.types.isJSXSpreadAttribute(attr)) return true;
|
23 | if (isJSXAttributeOfName(attr, "ref")) return true;
|
24 | }
|
25 |
|
26 | return false;
|
27 | }
|
28 |
|
29 | function isJSXAttributeOfName(attr, name) {
|
30 | return _core.types.isJSXAttribute(attr) && _core.types.isJSXIdentifier(attr.name, {
|
31 | name: name
|
32 | });
|
33 | }
|
34 |
|
35 | const visitor = (0, _helperBuilderReactJsx.default)({
|
36 | filter(node) {
|
37 | return node.openingElement && !hasRefOrSpread(node.openingElement.attributes);
|
38 | },
|
39 |
|
40 | pre(state) {
|
41 | const tagName = state.tagName;
|
42 | const args = state.args;
|
43 |
|
44 | if (_core.types.react.isCompatTag(tagName)) {
|
45 | args.push(_core.types.stringLiteral(tagName));
|
46 | } else {
|
47 | args.push(state.tagExpr);
|
48 | }
|
49 | },
|
50 |
|
51 | post(state, pass) {
|
52 | state.callee = pass.addHelper("jsx");
|
53 | const props = state.args[1];
|
54 | let hasKey = false;
|
55 |
|
56 | if (_core.types.isObjectExpression(props)) {
|
57 | const keyIndex = props.properties.findIndex(prop => _core.types.isIdentifier(prop.key, {
|
58 | name: "key"
|
59 | }));
|
60 |
|
61 | if (keyIndex > -1) {
|
62 | state.args.splice(2, 0, props.properties[keyIndex].value);
|
63 | props.properties.splice(keyIndex, 1);
|
64 | hasKey = true;
|
65 | }
|
66 | } else if (_core.types.isNullLiteral(props)) {
|
67 | state.args.splice(1, 1, _core.types.objectExpression([]));
|
68 | }
|
69 |
|
70 | if (!hasKey && state.args.length > 2) {
|
71 | state.args.splice(2, 0, _core.types.unaryExpression("void", _core.types.numericLiteral(0)));
|
72 | }
|
73 |
|
74 | state.pure = true;
|
75 | }
|
76 |
|
77 | });
|
78 | return {
|
79 | name: "transform-react-inline-elements",
|
80 | visitor
|
81 | };
|
82 | });
|
83 |
|
84 | exports.default = _default; |
\ | No newline at end of file |