This repository has been archived on 2020-11-02. You can view files and clone it, but cannot push or open issues or pull requests.
2020-11-01 22:46:04 +00:00

77 lines
2.2 KiB
JavaScript

/**
* @author Toru Nagashima
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforce valid `v-if` directives',
categories: ['vue3-essential', 'essential'],
url: 'https://eslint.vuejs.org/rules/valid-v-if.html'
},
fixable: null,
schema: []
},
/** @param {RuleContext} context */
create(context) {
return utils.defineTemplateBodyVisitor(context, {
/** @param {VDirective} node */
"VAttribute[directive=true][key.name.name='if']"(node) {
const element = node.parent.parent
if (utils.hasDirective(element, 'else')) {
context.report({
node,
loc: node.loc,
message:
"'v-if' and 'v-else' directives can't exist on the same element. You may want 'v-else-if' directives."
})
}
if (utils.hasDirective(element, 'else-if')) {
context.report({
node,
loc: node.loc,
message:
"'v-if' and 'v-else-if' directives can't exist on the same element."
})
}
if (node.key.argument) {
context.report({
node,
loc: node.loc,
message: "'v-if' directives require no argument."
})
}
if (node.key.modifiers.length > 0) {
context.report({
node,
loc: node.loc,
message: "'v-if' directives require no modifier."
})
}
if (!node.value || utils.isEmptyValueDirective(node, context)) {
context.report({
node,
loc: node.loc,
message: "'v-if' directives require that attribute value."
})
}
}
})
}
}