jscodeshaft
Version:
Collection of more or less primitive helpers and abstractions for JSCodeShift, build for design system migrations and upgrades.
57 lines (49 loc) • 1.51 kB
JavaScript
import {
isElement,
isTruthyString,
isValidAttrValue,
isPrimitive,
} from '../../typeChecking';
import { createLiteralOrLiteralExpression } from '../../createNode';
import { getAttributes } from '../getAttributes';
/**
* @typedef {function} setAttrValueParams
* @param {string} attrName Existing attribute name
* @param {string} newValue New attribute value
* @returns {array} Mutated collection of attribute nodes
*//**
* Sets given attribute's value on an element node
* @param {function} j JSCodeShift instance
* @param {object} node Element node to mutate
* @returns {function(setAttrValueParams): Node[]}
*/
export const setAttrValue = (j, node) => (attrName, newValue) => {
if (
j
&& isElement(node)
&& isTruthyString(attrName)
) {
const _isValueNode = isValidAttrValue(newValue);
const _isPrimitive = isPrimitive(newValue);
if (_isValueNode || _isPrimitive) {
let sanitisedValue;
const attr = getAttributes(node).find(
({name}) => name.name === attrName,
);
if (_isPrimitive) {
sanitisedValue = createLiteralOrLiteralExpression(j)(newValue);
}
if (_isValueNode) {
sanitisedValue = newValue;
}
if (sanitisedValue) {
if (sanitisedValue.expression && sanitisedValue.expression.value === true) {
delete attr.value;
} else {
attr.value = sanitisedValue;
}
}
}
}
return getAttributes(node);
};