UNPKG

1.72 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 _conditionalClassMerge = _interopRequireDefault(require("./conditionalClassMerge"));
11
12var _getClassName = _interopRequireDefault(require("./getClassName"));
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16/**
17 * Updates the className value of a JSX element using a provided styleName attribute.
18 */
19var _default = (path, styleModuleImportMap, sourceAttribute, destinationName, options) => {
20 const resolvedStyleName = (0, _getClassName.default)(sourceAttribute.value.value, styleModuleImportMap, options);
21 const destinationAttribute = path.node.openingElement.attributes.find(attribute => {
22 return typeof attribute.name !== 'undefined' && attribute.name.name === destinationName;
23 });
24
25 if (destinationAttribute) {
26 if ((0, _types.isStringLiteral)(destinationAttribute.value)) {
27 destinationAttribute.value.value += ' ' + resolvedStyleName;
28 } else if ((0, _types.isJSXExpressionContainer)(destinationAttribute.value)) {
29 destinationAttribute.value.expression = (0, _conditionalClassMerge.default)(destinationAttribute.value.expression, (0, _types.stringLiteral)(resolvedStyleName));
30 } else {
31 throw new Error('Unexpected attribute value:' + destinationAttribute.value);
32 }
33
34 path.node.openingElement.attributes.splice(path.node.openingElement.attributes.indexOf(sourceAttribute), 1);
35 } else {
36 sourceAttribute.name.name = destinationName;
37 sourceAttribute.value.value = resolvedStyleName;
38 }
39};
40
41exports.default = _default;
42//# sourceMappingURL=resolveStringLiteral.js.map
\No newline at end of file