ember-template-lint
Version:
Linter for Ember or Handlebars templates.
50 lines (42 loc) • 1.58 kB
JavaScript
const Rule = require('./base');
function logDuplicateAttributes(node, attributes, identifier, type) {
let currentAttribute;
let currentIndex = 0;
let length = attributes.length;
for (const [index, attribute] of attributes.entries()) {
for (currentIndex = index + 1; currentIndex < length; currentIndex++) {
currentAttribute = attributes[currentIndex];
if (attribute[identifier] === currentAttribute[identifier]) {
this.log({
message: `Duplicate attribute '${currentAttribute[identifier]}' found in the ${type}.`,
line: currentAttribute.loc && currentAttribute.loc.start.line,
column: currentAttribute.loc && currentAttribute.loc.start.column,
source: this.sourceForNode(node),
});
break;
}
}
}
}
module.exports = class NoDuplicateAttributes extends Rule {
visitor() {
return {
ElementNode(node) {
logDuplicateAttributes.call(this, node, node.attributes, 'name', 'Element');
},
BlockStatement(node) {
let attributes = (node.hash || {}).pairs || [];
logDuplicateAttributes.call(this, node, attributes, 'key', 'BlockStatement');
},
MustacheStatement(node) {
let attributes = (node.hash || {}).pairs || [];
logDuplicateAttributes.call(this, node, attributes, 'key', 'MustacheStatement');
},
SubExpression(node) {
let attributes = (node.hash || {}).pairs || [];
logDuplicateAttributes.call(this, node, attributes, 'key', 'SubExpression');
},
};
}
};
;