UNPKG

1.93 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var _types = require("@babel/types");
9
10var _optionsDefaults = _interopRequireDefault(require("./schemas/optionsDefaults"));
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14const createSpreadMapper = (path, stats) => {
15 const result = {};
16 let {
17 attributeNames
18 } = _optionsDefaults.default;
19
20 if (stats.opts && stats.opts.attributeNames) {
21 attributeNames = Object.assign({}, attributeNames, stats.opts.attributeNames);
22 }
23
24 const attributes = Object.entries(attributeNames).filter(pair => {
25 return pair[1];
26 });
27 const attributeKeys = attributes.map(pair => {
28 return pair[0];
29 });
30 const spreadAttributes = path.node.openingElement.attributes.filter(attr => {
31 return (0, _types.isJSXSpreadAttribute)(attr);
32 });
33
34 for (const spread of spreadAttributes) {
35 for (const attributeKey of attributeKeys) {
36 const destinationName = attributeNames[attributeKey];
37
38 if (result[destinationName]) {
39 result[destinationName] = (0, _types.binaryExpression)('+', result[destinationName], (0, _types.conditionalExpression)(spread.argument, (0, _types.binaryExpression)('+', (0, _types.stringLiteral)(' '), (0, _types.logicalExpression)('||', (0, _types.memberExpression)(spread.argument, (0, _types.identifier)(destinationName)), (0, _types.stringLiteral)(''))), (0, _types.stringLiteral)('')));
40 } else {
41 result[destinationName] = (0, _types.conditionalExpression)(spread.argument, (0, _types.logicalExpression)('||', (0, _types.memberExpression)(spread.argument, (0, _types.identifier)(destinationName)), (0, _types.stringLiteral)('')), (0, _types.stringLiteral)(''));
42 }
43 }
44 }
45
46 return result;
47};
48
49var _default = createSpreadMapper;
50exports.default = _default;
51//# sourceMappingURL=createSpreadMapper.js.map
\No newline at end of file