@dr.pogodin/babel-plugin-react-css-modules
Version:
Transforms styleName to className using compile time CSS module resolution.
18 lines • 1.21 kB
JavaScript
import { NodePath } from '@babel/traverse';
import { cloneNode,
// type Expression,
isStringLiteral, isJSXExpressionContainer, jsxExpressionContainer, binaryExpression, stringLiteral } from '@babel/types';
const handleSpreadClassName = (path, destinationName, classNamesFromSpread // TODO: It should be Expression type from '@babel/types', but I am not sure now, how to express it for flow.
) => {
const destinationAttribute = path.node.openingElement.attributes.find(attribute => typeof attribute.name !== 'undefined' && attribute.name.name === destinationName);
if (!destinationAttribute) {
return;
}
if (isStringLiteral(destinationAttribute.value)) {
destinationAttribute.value = jsxExpressionContainer(binaryExpression('+', cloneNode(destinationAttribute.value), binaryExpression('+', stringLiteral(' '), classNamesFromSpread)));
} else if (isJSXExpressionContainer(destinationAttribute.value)) {
destinationAttribute.value = jsxExpressionContainer(binaryExpression('+', cloneNode(destinationAttribute.value.expression), binaryExpression('+', stringLiteral(' '), classNamesFromSpread)));
}
};
export default handleSpreadClassName;
//# sourceMappingURL=handleSpreadClassName.js.map