jscodeshaft
Version:
Collection of more or less primitive helpers and abstractions for JSCodeShift, build for design system migrations and upgrades.
32 lines (27 loc) • 875 B
JavaScript
import { isElement, isTruthyString } from '../../typeChecking';
import { getAttributes } from '../getAttributes';
/**
* @typedef {function} renameAttrParams
* @param {string} oldName Existing attribute name
* @param {string} newName Wanted attribute name
* @returns {array} Mutated collection of attribute nodes
*//**
* Renames attribute on the given element node
* @param {function} j JSCodeShift instance
* @param {object} node Element node to mutate
* @returns {function(renameAttrParams): Node[]}
*/
export const renameAttr = (j, node) => (oldName, newName) => {
if (
j
&& isElement(node)
&& isTruthyString(oldName)
&& isTruthyString(newName)
) {
const attr = getAttributes(node).find(
({name}) => name.name === oldName,
);
attr.name.name = newName;
}
return getAttributes(node);
};