UNPKG

2.94 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _babelTypes = require('babel-types');
8
9var _babelTypes2 = _interopRequireDefault(_babelTypes);
10
11var _conditionalClassMerge = require('./conditionalClassMerge');
12
13var _conditionalClassMerge2 = _interopRequireDefault(_conditionalClassMerge);
14
15var _createObjectExpression = require('./createObjectExpression');
16
17var _createObjectExpression2 = _interopRequireDefault(_createObjectExpression);
18
19var _optionsDefaults = require('./schemas/optionsDefaults');
20
21var _optionsDefaults2 = _interopRequireDefault(_optionsDefaults);
22
23function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
24
25exports.default = (t, path, sourceAttribute, destinationName, importedHelperIndentifier, styleModuleImportMapIdentifier, options) => {
26 const expressionContainerValue = sourceAttribute.value;
27 const destinationAttribute = path.node.openingElement.attributes.find(attribute => {
28 return typeof attribute.name !== 'undefined' && attribute.name.name === destinationName;
29 });
30
31 if (destinationAttribute) {
32 path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(destinationAttribute), 1);
33 }
34
35 path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(sourceAttribute), 1);
36
37 const args = [expressionContainerValue.expression, styleModuleImportMapIdentifier];
38
39 // Only provide options argument if the options are something other than default
40 // This helps save a few bits in the generated user code
41 if (options.handleMissingStyleName !== _optionsDefaults2.default.handleMissingStyleName) {
42 args.push((0, _createObjectExpression2.default)(t, options));
43 }
44
45 const styleNameExpression = t.callExpression(t.clone(importedHelperIndentifier), args);
46
47 if (destinationAttribute) {
48 if ((0, _babelTypes.isStringLiteral)(destinationAttribute.value)) {
49 path.node.openingElement.attributes.push((0, _babelTypes.jSXAttribute)((0, _babelTypes.jSXIdentifier)(destinationName), (0, _babelTypes.jSXExpressionContainer)((0, _babelTypes.binaryExpression)('+', t.stringLiteral(destinationAttribute.value.value + ' '), styleNameExpression))));
50 } else if ((0, _babelTypes.isJSXExpressionContainer)(destinationAttribute.value)) {
51 path.node.openingElement.attributes.push((0, _babelTypes.jSXAttribute)((0, _babelTypes.jSXIdentifier)(destinationName), (0, _babelTypes.jSXExpressionContainer)((0, _conditionalClassMerge2.default)(destinationAttribute.value.expression, styleNameExpression))));
52 } else {
53 throw new Error('Unexpected attribute value: ' + destinationAttribute.value);
54 }
55 } else {
56 path.node.openingElement.attributes.push((0, _babelTypes.jSXAttribute)((0, _babelTypes.jSXIdentifier)(destinationName), (0, _babelTypes.jSXExpressionContainer)(styleNameExpression)));
57 }
58};
59//# sourceMappingURL=replaceJsxExpressionContainer.js.map
\No newline at end of file