eslint-codemod-utils
Version:
A collection of AST helper functions for more complex ESLint rule fixes.
36 lines (35 loc) • 1.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.insertJSXAttribute = void 0;
const __1 = require("..");
/**
* Adds a prop to a JSXElement.
*
* @author Sam Scheding
* @example
* ```
* const boxNode = jsxElement({ ...node })
* console.log(boxNode.toString()) // --> "<Box></Box>"
*
* const boxNodeWithProp = insertJSXAttribute(node, 'display', 'block')
* console.log(boxNodeWithProp.toString()) // --> "<Box display='block'></Box>"
* ```
*/
function insertJSXAttribute(node, propName, propValue) {
const { openingElement } = node;
const { attributes = [] } = openingElement;
return (0, __1.jsxElement)({
...node,
openingElement: (0, __1.jsxOpeningElement)({
...openingElement,
attributes: [
...attributes,
(0, __1.jsxAttribute)({
name: (0, __1.jsxIdentifier)(propName),
value: propValue,
}),
],
}),
});
}
exports.insertJSXAttribute = insertJSXAttribute;