|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 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 | +// https://github.com/vuejs/vue-next/blob/64e2f4643602c5980361e66674141e61ba60ef70/packages/compiler-core/src/parse.ts#L405 |
| 18 | +const SPECIAL_TEMPLATE_DIRECTIVES = new Set([ |
| 19 | + 'if', |
| 20 | + 'else', |
| 21 | + 'else-if', |
| 22 | + 'for', |
| 23 | + 'slot' |
| 24 | +]) |
| 25 | + |
| 26 | +// ------------------------------------------------------------------------------ |
| 27 | +// Rule Definition |
| 28 | +// ------------------------------------------------------------------------------ |
| 29 | + |
| 30 | +module.exports = { |
| 31 | + meta: { |
| 32 | + type: 'problem', |
| 33 | + docs: { |
| 34 | + description: 'disallow unnecessary `<template>`', |
| 35 | + categories: ['vue3-recommended', 'recommended'], |
| 36 | + url: 'https://eslint.vuejs.org/rules/no-lone-template.html' |
| 37 | + }, |
| 38 | + fixable: null, |
| 39 | + schema: [ |
| 40 | + { |
| 41 | + type: 'object', |
| 42 | + properties: { |
| 43 | + ignoreAccessible: { |
| 44 | + type: 'boolean' |
| 45 | + } |
| 46 | + }, |
| 47 | + additionalProperties: false |
| 48 | + } |
| 49 | + ], |
| 50 | + messages: { |
| 51 | + requireDirective: '`<template>` require directive.' |
| 52 | + } |
| 53 | + }, |
| 54 | + /** @param {RuleContext} context */ |
| 55 | + create(context) { |
| 56 | + const options = context.options[0] || {} |
| 57 | + const ignoreAccessible = options.ignoreAccessible === true |
| 58 | + |
| 59 | + /** |
| 60 | + * @param {VAttribute | VDirective} attr |
| 61 | + */ |
| 62 | + function getKeyName(attr) { |
| 63 | + if (attr.directive) { |
| 64 | + if (attr.key.name.name !== 'bind') { |
| 65 | + // no v-bind |
| 66 | + return null |
| 67 | + } |
| 68 | + if ( |
| 69 | + !attr.key.argument || |
| 70 | + attr.key.argument.type === 'VExpressionContainer' |
| 71 | + ) { |
| 72 | + // unknown |
| 73 | + return null |
| 74 | + } |
| 75 | + return attr.key.argument.name |
| 76 | + } |
| 77 | + return attr.key.name |
| 78 | + } |
| 79 | + |
| 80 | + return utils.defineTemplateBodyVisitor(context, { |
| 81 | + /** @param {VStartTag} node */ |
| 82 | + "VElement[name='template'][parent.type='VElement'] > VStartTag"(node) { |
| 83 | + if ( |
| 84 | + node.attributes.some((attr) => { |
| 85 | + if (attr.directive) { |
| 86 | + const directiveName = attr.key.name.name |
| 87 | + if (SPECIAL_TEMPLATE_DIRECTIVES.has(directiveName)) { |
| 88 | + return true |
| 89 | + } |
| 90 | + if (directiveName === 'slot-scope') { |
| 91 | + // `slot-scope` is deprecated in Vue.js 2.6 |
| 92 | + return true |
| 93 | + } |
| 94 | + if (directiveName === 'scope') { |
| 95 | + // `scope` is deprecated in Vue.js 2.5 |
| 96 | + return true |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + const keyName = getKeyName(attr) |
| 101 | + if (keyName === 'slot') { |
| 102 | + // `slot` is deprecated in Vue.js 2.6 |
| 103 | + return true |
| 104 | + } |
| 105 | + |
| 106 | + return false |
| 107 | + }) |
| 108 | + ) { |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + if ( |
| 113 | + ignoreAccessible && |
| 114 | + node.attributes.some((attr) => { |
| 115 | + const keyName = getKeyName(attr) |
| 116 | + return keyName === 'id' || keyName === 'ref' |
| 117 | + }) |
| 118 | + ) { |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + context.report({ |
| 123 | + node, |
| 124 | + messageId: 'requireDirective' |
| 125 | + }) |
| 126 | + } |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
0 commit comments