|
| 1 | +/** |
| 2 | + * @author rzzf |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const utils = require('../utils') |
| 8 | +const casing = require('../utils/casing') |
| 9 | +const regexp = require('../utils/regexp') |
| 10 | + |
| 11 | +/** |
| 12 | + * @param {ObjectExpression} componentObject |
| 13 | + * @returns { { node: Property, name: string }[] } Array of ASTNodes |
| 14 | + */ |
| 15 | +function getRegisteredDirectives(componentObject) { |
| 16 | + const directivesNode = componentObject.properties.find( |
| 17 | + (p) => |
| 18 | + p.type === 'Property' && |
| 19 | + utils.getStaticPropertyName(p) === 'directives' && |
| 20 | + p.value.type === 'ObjectExpression' |
| 21 | + ) |
| 22 | + |
| 23 | + if ( |
| 24 | + !directivesNode || |
| 25 | + directivesNode.type !== 'Property' || |
| 26 | + directivesNode.value.type !== 'ObjectExpression' |
| 27 | + ) { |
| 28 | + return [] |
| 29 | + } |
| 30 | + |
| 31 | + return directivesNode.value.properties.flatMap((node) => { |
| 32 | + const name = |
| 33 | + node.type === 'Property' ? utils.getStaticPropertyName(node) : null |
| 34 | + return name ? [{ node: /** @type {Property} */ (node), name }] : [] |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * @param {string} rawName |
| 40 | + * @param {Set<string>} definedNames |
| 41 | + */ |
| 42 | +function isDefinedInSetup(rawName, definedNames) { |
| 43 | + const camelName = casing.camelCase(rawName) |
| 44 | + const variableName = `v${casing.capitalize(camelName)}` |
| 45 | + return definedNames.has(variableName) |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * @param {string} rawName |
| 50 | + * @param {Set<string>} definedNames |
| 51 | + */ |
| 52 | +function isDefinedInOptions(rawName, definedNames) { |
| 53 | + const camelName = casing.camelCase(rawName) |
| 54 | + |
| 55 | + if (definedNames.has(rawName)) { |
| 56 | + return true |
| 57 | + } |
| 58 | + |
| 59 | + // allow case-insensitive only when the directive name itself contains capitalized letters |
| 60 | + for (const name of definedNames) { |
| 61 | + const lowercaseName = name.toLowerCase() |
| 62 | + if (name !== lowercaseName && lowercaseName === camelName.toLowerCase()) { |
| 63 | + return true |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return false |
| 68 | +} |
| 69 | + |
| 70 | +module.exports = { |
| 71 | + meta: { |
| 72 | + type: 'suggestion', |
| 73 | + docs: { |
| 74 | + description: 'disallow use of undefined custom directives', |
| 75 | + categories: undefined, |
| 76 | + url: 'https://eslint.vuejs.org/rules/no-undef-directives.html' |
| 77 | + }, |
| 78 | + fixable: null, |
| 79 | + schema: [ |
| 80 | + { |
| 81 | + type: 'object', |
| 82 | + properties: { |
| 83 | + ignore: { |
| 84 | + type: 'array', |
| 85 | + items: { type: 'string' }, |
| 86 | + uniqueItems: true |
| 87 | + } |
| 88 | + }, |
| 89 | + additionalProperties: false |
| 90 | + } |
| 91 | + ], |
| 92 | + messages: { |
| 93 | + undef: "The 'v-{{name}}' directive has been used, but not defined." |
| 94 | + } |
| 95 | + }, |
| 96 | + /** @param {RuleContext} context */ |
| 97 | + create(context) { |
| 98 | + const options = context.options[0] || {} |
| 99 | + const { ignore = [] } = options |
| 100 | + const isAnyIgnored = regexp.toRegExpGroupMatcher(ignore) |
| 101 | + |
| 102 | + /** |
| 103 | + * Check whether the given directive name is a verify target or not. |
| 104 | + * |
| 105 | + * @param {string} rawName The directive name. |
| 106 | + * @returns {boolean} |
| 107 | + */ |
| 108 | + function isVerifyTargetDirective(rawName) { |
| 109 | + const kebabName = casing.kebabCase(rawName) |
| 110 | + if ( |
| 111 | + utils.isBuiltInDirectiveName(rawName) || |
| 112 | + isAnyIgnored(rawName, kebabName) |
| 113 | + ) { |
| 114 | + return false |
| 115 | + } |
| 116 | + return true |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param {(rawName: string) => boolean} isDefined |
| 121 | + * @returns {TemplateListener} |
| 122 | + */ |
| 123 | + function createTemplateBodyVisitor(isDefined) { |
| 124 | + return { |
| 125 | + /** @param {VDirective} node */ |
| 126 | + 'VAttribute[directive=true]'(node) { |
| 127 | + const name = node.key.name.name |
| 128 | + if (utils.isBuiltInDirectiveName(name)) { |
| 129 | + return |
| 130 | + } |
| 131 | + const rawName = node.key.name.rawName || name |
| 132 | + if (isVerifyTargetDirective(rawName) && !isDefined(rawName)) { |
| 133 | + context.report({ |
| 134 | + node: node.key, |
| 135 | + messageId: 'undef', |
| 136 | + data: { |
| 137 | + name: rawName |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /** @type {Set<string>} */ |
| 146 | + const definedInOptionDirectives = new Set() |
| 147 | + |
| 148 | + if (utils.isScriptSetup(context)) { |
| 149 | + // For <script setup> |
| 150 | + /** @type {Set<string>} */ |
| 151 | + const definedInSetupDirectives = new Set() |
| 152 | + |
| 153 | + const globalScope = context.sourceCode.scopeManager.globalScope |
| 154 | + if (globalScope) { |
| 155 | + for (const variable of globalScope.variables) { |
| 156 | + definedInSetupDirectives.add(variable.name) |
| 157 | + } |
| 158 | + const moduleScope = globalScope.childScopes.find( |
| 159 | + (scope) => scope.type === 'module' |
| 160 | + ) |
| 161 | + for (const variable of moduleScope?.variables ?? []) { |
| 162 | + definedInSetupDirectives.add(variable.name) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + const scriptVisitor = utils.defineVueVisitor(context, { |
| 167 | + onVueObjectEnter(node) { |
| 168 | + for (const directive of getRegisteredDirectives(node)) { |
| 169 | + definedInOptionDirectives.add(directive.name) |
| 170 | + } |
| 171 | + } |
| 172 | + }) |
| 173 | + |
| 174 | + const templateBodyVisitor = createTemplateBodyVisitor( |
| 175 | + (rawName) => |
| 176 | + isDefinedInSetup(rawName, definedInSetupDirectives) || |
| 177 | + isDefinedInOptions(rawName, definedInOptionDirectives) |
| 178 | + ) |
| 179 | + |
| 180 | + return utils.defineTemplateBodyVisitor( |
| 181 | + context, |
| 182 | + templateBodyVisitor, |
| 183 | + scriptVisitor |
| 184 | + ) |
| 185 | + } |
| 186 | + |
| 187 | + // For Options API |
| 188 | + const scriptVisitor = utils.executeOnVue(context, (obj) => { |
| 189 | + for (const directive of getRegisteredDirectives(obj)) { |
| 190 | + definedInOptionDirectives.add(directive.name) |
| 191 | + } |
| 192 | + }) |
| 193 | + |
| 194 | + const templateBodyVisitor = createTemplateBodyVisitor((rawName) => |
| 195 | + isDefinedInOptions(rawName, definedInOptionDirectives) |
| 196 | + ) |
| 197 | + |
| 198 | + return utils.defineTemplateBodyVisitor( |
| 199 | + context, |
| 200 | + templateBodyVisitor, |
| 201 | + scriptVisitor |
| 202 | + ) |
| 203 | + } |
| 204 | +} |
0 commit comments