jscodeshaft
Version:
Collection of more or less primitive helpers and abstractions for JSCodeShift, build for design system migrations and upgrades.
31 lines (25 loc) • 1.06 kB
JavaScript
import {isElement, isTruthyString} from '../../typeChecking';
import {toTruthyStringArray} from '../../general';
import { getAttributes } from '../getAttributes';
/**
* @typedef {function} removeAttrsParams
* @param {(string|string[])} attrNames Attribute name
* @returns {Node[]} Mutated collection of attribute nodes
*//**
* Removes 1 or more traversal by name
* @param {function} j JSCodeShift instance
* @param {object} node Element node to manipulate
* @returns {function(removeAttrsParams): Node[]}
*/
export const removeAttrs = (j, node) => attrNames => {
let attrs = getAttributes(node);
if (j && isElement(node) && (Array.isArray(attrNames) || isTruthyString(attrNames))) {
const toRemove = toTruthyStringArray(attrNames);
const filteredAttrs = attrs.filter(({name, type}) => (
type === 'JSXSpreadAttribute'
|| (name && name.name && !toRemove.includes(name.name))
));
node.openingElement.attributes = filteredAttrs;
}
return getAttributes(node);
};