jsx-no-duplicate-props
Ast Rule: html element
jsx-no-duplicate-props
function getProp(attributes = [], prop = "") {
if (!prop) return;
return attributes.find((attribute) => {
if (attribute && attribute.name && attribute.name.value) {
return attribute.name.value === prop;
}
});
}
function getName(attribute = {}) {
if (attribute.name) {
return attribute.name.value;
}
}
function visit(node, filename, code) {
if (node && node.attributes && node.attributes.length) {
const props = [];
node.attributes.forEach((attribute) => {
const name = getName(attribute);
if (props.includes(name)) {
const prop = getProp(node.attributes, name);
const error = buildError(
prop.start.line,
prop.start.col,
prop.end.line,
prop.end.col,
"No duplicate props allowed",
"INFO",
"BEST_PRACTICES"
);
const editRemove = buildEditRemove(
prop.start.line,
prop.start.col,
prop.end.line,
prop.end.col,
);
const fix = buildFix(`remove ${name} prop`, [
editRemove,
]);
addError(error.addFix(fix));
} else {
props.push(name);
}
});
}
}
test.js
Expected test result: has error
duplicate props