eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
64 lines (63 loc) • 2.16 kB
JavaScript
const require_index = require("../utils/index.js");
// lib/rules/no-duplicate-attributes.ts
/**
* @author Toru Nagashima
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
/**
* Get the name of the given attribute node.
*/
function getName(attribute) {
return attribute.directive ? attribute.key.name.name === "bind" && attribute.key.argument?.type === "VIdentifier" && attribute.key.argument.name || null : attribute.key.name;
}
var no_duplicate_attributes_default = {
meta: {
type: "problem",
docs: {
description: "disallow duplication of attributes",
categories: ["vue3-essential", "vue2-essential"],
url: "https://eslint.vuejs.org/rules/no-duplicate-attributes.html"
},
fixable: null,
schema: [{
type: "object",
properties: {
allowCoexistClass: { type: "boolean" },
allowCoexistStyle: { type: "boolean" }
},
additionalProperties: false
}],
messages: { duplicateAttribute: "Duplicate attribute '{{name}}'." }
},
create(context) {
const options = context.options[0] || {};
const shouldAllowCoexistStyle = options.allowCoexistStyle !== false;
const shouldAllowCoexistClass = options.allowCoexistClass !== false;
const directiveNames = /* @__PURE__ */ new Set();
const attributeNames = /* @__PURE__ */ new Set();
function isDuplicate(name, isDirective) {
if (shouldAllowCoexistStyle && name === "style" || shouldAllowCoexistClass && name === "class") return isDirective ? directiveNames.has(name) : attributeNames.has(name);
return directiveNames.has(name) || attributeNames.has(name);
}
return require_index.default.defineTemplateBodyVisitor(context, {
VStartTag() {
directiveNames.clear();
attributeNames.clear();
},
VAttribute(node) {
const name = getName(node);
if (name == null) return;
if (isDuplicate(name, node.directive)) context.report({
node,
loc: node.loc,
messageId: "duplicateAttribute",
data: { name }
});
if (node.directive) directiveNames.add(name);
else attributeNames.add(name);
}
});
}
};
exports.default = no_duplicate_attributes_default;