UNPKG

1.78 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _babelTypes = require('babel-types');
8
9var _conditionalClassMerge = require('./conditionalClassMerge');
10
11var _conditionalClassMerge2 = _interopRequireDefault(_conditionalClassMerge);
12
13var _getClassName = require('./getClassName');
14
15var _getClassName2 = _interopRequireDefault(_getClassName);
16
17function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
18
19/**
20 * Updates the className value of a JSX element using a provided styleName attribute.
21 */
22exports.default = (path, styleModuleImportMap, sourceAttribute, destinationName, options) => {
23 const resolvedStyleName = (0, _getClassName2.default)(sourceAttribute.value.value, styleModuleImportMap, options);
24
25 const destinationAttribute = path.node.openingElement.attributes.find(attribute => {
26 return typeof attribute.name !== 'undefined' && attribute.name.name === destinationName;
27 });
28
29 if (destinationAttribute) {
30 if ((0, _babelTypes.isStringLiteral)(destinationAttribute.value)) {
31 destinationAttribute.value.value += ' ' + resolvedStyleName;
32 } else if ((0, _babelTypes.isJSXExpressionContainer)(destinationAttribute.value)) {
33 destinationAttribute.value.expression = (0, _conditionalClassMerge2.default)(destinationAttribute.value.expression, (0, _babelTypes.stringLiteral)(resolvedStyleName));
34 } else {
35 throw new Error('Unexpected attribute value:' + destinationAttribute.value);
36 }
37
38 path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(sourceAttribute), 1);
39 } else {
40 sourceAttribute.name.name = destinationName;
41 sourceAttribute.value.value = resolvedStyleName;
42 }
43};
44//# sourceMappingURL=resolveStringLiteral.js.map
\No newline at end of file