|
| 1 | +/** |
| 2 | + * @author Doug Wade <[email protected]> |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const utils = require('../utils') |
| 12 | + |
| 13 | +// ------------------------------------------------------------------------------ |
| 14 | +// Helpers |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +// ... |
| 18 | + |
| 19 | +// ------------------------------------------------------------------------------ |
| 20 | +// Rule Definition |
| 21 | +// ------------------------------------------------------------------------------ |
| 22 | + |
| 23 | +module.exports = { |
| 24 | + meta: { |
| 25 | + type: 'problem', |
| 26 | + docs: { |
| 27 | + description: 'require `key` with `v-if/v-else-if/v-else` directives', |
| 28 | + categories: undefined, |
| 29 | + url: 'https://eslint.vuejs.org/rules/require-v-if-else-key.html' |
| 30 | + }, |
| 31 | + fixable: null, |
| 32 | + schema: [], |
| 33 | + messages: { |
| 34 | + unexpected: |
| 35 | + 'Elements in v-if/v-else-if/v-else expect to have distinct keys if they are of the same type.' |
| 36 | + } |
| 37 | + }, |
| 38 | + /** @param {RuleContext} context */ |
| 39 | + create(context) { |
| 40 | + const NO_MATCH = Symbol('no match') |
| 41 | + |
| 42 | + /** |
| 43 | + * Gets the key from an element. |
| 44 | + * @param {VElement} element The element node to get the key from. |
| 45 | + * @return {String | null} |
| 46 | + */ |
| 47 | + function getKey(element) { |
| 48 | + const keyAttribute = utils.getAttribute(element, 'key') |
| 49 | + if (keyAttribute) { |
| 50 | + return keyAttribute?.value?.value || '' |
| 51 | + } |
| 52 | + |
| 53 | + const keyDirective = utils.getDirective(element, 'bind', 'key') |
| 54 | + if (!keyDirective) { |
| 55 | + return '' |
| 56 | + } |
| 57 | + |
| 58 | + if (keyDirective.value?.expression?.type !== 'Identifier') { |
| 59 | + return null |
| 60 | + } |
| 61 | + |
| 62 | + return `v-bind:${keyDirective.value?.expression?.name}` |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Check the given element about `v-bind:key` attributes. |
| 67 | + * @param {VElement} element The element node to check. |
| 68 | + */ |
| 69 | + function checkKey(element) { |
| 70 | + const prevSibling = utils.prevSibling(element) |
| 71 | + |
| 72 | + if (!prevSibling) { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + if (prevSibling.name !== element.name) { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + const key = getKey(element) |
| 81 | + |
| 82 | + if (key === null) { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + if (!key || key === getKey(prevSibling)) { |
| 87 | + context.report({ node: element, messageId: 'unexpected' }) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return utils.defineTemplateBodyVisitor(context, { |
| 92 | + /** @param {VDirective} node */ |
| 93 | + "VAttribute[directive=true][key.name.name='else']"(node) { |
| 94 | + checkKey(node.parent.parent) |
| 95 | + }, |
| 96 | + /** @param {VDirective} node */ |
| 97 | + "VAttribute[directive=true][key.name.name='else-if']"(node) { |
| 98 | + checkKey(node.parent.parent) |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | +} |
0 commit comments