eslint-plugin-react-form-fields
Version:
React Form Fields specific linting rules for ESLint
35 lines (34 loc) • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.jsxElementsParser = void 0;
const experimental_utils_1 = require("@typescript-eslint/experimental-utils");
const getAsPropValue_1 = require("./getAsPropValue");
const propertyToJSXAttribute_1 = require("./propertyToJSXAttribute");
const jsxElementsParser = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
context,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rule, collectedStyledComponents) => (node) => {
var _a, _b;
if (node.openingElement.name.type !== experimental_utils_1.AST_NODE_TYPES.JSXIdentifier) {
return;
}
const originalName = node.openingElement.name.name;
const styledComponent = collectedStyledComponents[originalName];
if (styledComponent) {
const { tag, attrs } = styledComponent;
const originalNodeAttr = node.openingElement.attributes;
try {
const allAttrs = originalNodeAttr.concat(attrs.map(propertyToJSXAttribute_1.propertyToJSXAttribute));
const asPropValue = (0, getAsPropValue_1.getAsPropValue)(allAttrs);
node.openingElement.attributes = allAttrs;
node.openingElement.name.name = asPropValue || tag;
(_b = (_a = rule.create(context)).JSXElement) === null || _b === void 0 ? void 0 : _b.call(_a, node);
}
finally {
node.openingElement.name.name = originalName;
node.openingElement.attributes = originalNodeAttr;
}
}
};
exports.jsxElementsParser = jsxElementsParser;