UNPKG

eslint-codemod-utils

Version:

A collection of AST helper functions for more complex ESLint rule fixes.

41 lines (40 loc) 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.insertJSXAttribute = void 0; const __1 = require(".."); /** * Adds a prop to a JSXElement. * * Returns a `StringableASTNode<JSXElement>` so that callers can immediately * `.toString()` the result inside an ESLint fixer, and so the result can be * composed with other node helpers without forcing callers to supply a * synthetic `loc`/`range`. * * @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;