eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
83 lines (82 loc) • 2.47 kB
JavaScript
const require_index = require("../utils/index.js");
// lib/rules/html-button-has-type.ts
/**
* @fileoverview Disallow usage of button without an explicit type attribute
* @author Jonathan Santerre <jonathan@santerre.dev>
*/
function isButtonType(type) {
return [
"button",
"submit",
"reset"
].includes(type);
}
const optionDefaults = {
button: true,
submit: true,
reset: true
};
var html_button_has_type_default = {
meta: {
type: "suggestion",
docs: {
description: "disallow usage of button without an explicit type attribute",
categories: null,
url: "https://eslint.vuejs.org/rules/html-button-has-type.html"
},
fixable: null,
schema: [{
type: "object",
properties: {
button: { type: "boolean" },
submit: { type: "boolean" },
reset: { type: "boolean" }
},
additionalProperties: false
}],
messages: {
missingTypeAttribute: "Missing an explicit type attribute for button.",
invalidTypeAttribute: "{{value}} is an invalid value for button type attribute.",
forbiddenTypeAttribute: "{{value}} is a forbidden value for button type attribute.",
emptyTypeAttribute: "A value must be set for button type attribute."
}
},
create(context) {
const configuration = Object.assign({}, optionDefaults, context.options[0]);
function report(node, messageId, data) {
context.report({
node,
messageId,
data
});
}
function validateAttribute(attribute) {
const value = attribute.value;
if (!value || !value.value) {
report(value || attribute, "emptyTypeAttribute");
return;
}
const strValue = value.value;
if (!isButtonType(strValue)) report(value, "invalidTypeAttribute", { value: strValue });
else if (!configuration[strValue]) report(value, "forbiddenTypeAttribute", { value: strValue });
}
function validateDirective(directive) {
const value = directive.value;
if (!value || !value.expression) report(value || directive, "emptyTypeAttribute");
}
return require_index.default.defineTemplateBodyVisitor(context, { "VElement[rawName='button']"(node) {
const typeAttr = require_index.default.getAttribute(node, "type");
if (typeAttr) {
validateAttribute(typeAttr);
return;
}
const typeDir = require_index.default.getDirective(node, "bind", "type");
if (typeDir) {
validateDirective(typeDir);
return;
}
report(node.startTag, "missingTypeAttribute");
} });
}
};
exports.default = html_button_has_type_default;