|
| 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 | + |
| 10 | +/** |
| 11 | + * @param {string} str |
| 12 | + * @returns {string} |
| 13 | + */ |
| 14 | +function camelize(str) { |
| 15 | + return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : '')) |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {ObjectExpression} componentObject |
| 20 | + * @returns { { node: Property, name: string }[] } Array of ASTNodes |
| 21 | + */ |
| 22 | +function getRegisteredDirectives(componentObject) { |
| 23 | + const directivesNode = componentObject.properties.find( |
| 24 | + (p) => |
| 25 | + p.type === 'Property' && |
| 26 | + utils.getStaticPropertyName(p) === 'directives' && |
| 27 | + p.value.type === 'ObjectExpression' |
| 28 | + ) |
| 29 | + |
| 30 | + if ( |
| 31 | + !directivesNode || |
| 32 | + directivesNode.type !== 'Property' || |
| 33 | + directivesNode.value.type !== 'ObjectExpression' |
| 34 | + ) { |
| 35 | + return [] |
| 36 | + } |
| 37 | + |
| 38 | + return directivesNode.value.properties |
| 39 | + .filter( |
| 40 | + /** |
| 41 | + * @param {Property | SpreadElement} node |
| 42 | + * @returns {node is Property} |
| 43 | + */ |
| 44 | + (node) => node.type === 'Property' |
| 45 | + ) |
| 46 | + .map((node) => { |
| 47 | + const name = utils.getStaticPropertyName(node) |
| 48 | + return name ? { node, name } : null |
| 49 | + }) |
| 50 | + .filter( |
| 51 | + /** |
| 52 | + * @param {{node: Property, name: string} | null} res |
| 53 | + * @returns {res is {node: Property, name: string}} |
| 54 | + */ |
| 55 | + (res) => res !== null |
| 56 | + ) |
| 57 | +} |
| 58 | + |
| 59 | +class DefinedInSetupDirectives { |
| 60 | + constructor() { |
| 61 | + /** |
| 62 | + * Directive names |
| 63 | + * @type {Set<string>} |
| 64 | + */ |
| 65 | + this.names = new Set() |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param {string[]} names |
| 70 | + */ |
| 71 | + addName(...names) { |
| 72 | + for (const name of names) { |
| 73 | + this.names.add(name) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param {string} rawName |
| 79 | + */ |
| 80 | + isDefinedDirective(rawName) { |
| 81 | + const camelName = camelize(rawName) |
| 82 | + const variableName = `v${casing.capitalize(camelName)}` |
| 83 | + if (this.names.has(variableName)) { |
| 84 | + return true |
| 85 | + } |
| 86 | + return false |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +class DefinedInOptionDirectives { |
| 91 | + constructor() { |
| 92 | + /** |
| 93 | + * Directive names |
| 94 | + * @type {Set<string>} |
| 95 | + */ |
| 96 | + this.names = new Set() |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @param {string[]} names |
| 101 | + */ |
| 102 | + addName(...names) { |
| 103 | + for (const name of names) { |
| 104 | + this.names.add(name) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param {string} rawName |
| 110 | + */ |
| 111 | + isDefinedDirective(rawName) { |
| 112 | + const camelName = camelize(rawName) |
| 113 | + if (this.names.has(rawName) || this.names.has(camelName)) { |
| 114 | + return true |
| 115 | + } |
| 116 | + |
| 117 | + // allow case-insensitive ONLY when the directive name itself contains capitalized letters |
| 118 | + for (const name of this.names) { |
| 119 | + if ( |
| 120 | + name.toLowerCase() === camelName.toLowerCase() && |
| 121 | + name !== name.toLowerCase() |
| 122 | + ) { |
| 123 | + return true |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return false |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +module.exports = { |
| 132 | + meta: { |
| 133 | + type: 'problem', |
| 134 | + docs: { |
| 135 | + description: 'disallow use of undefined custom directives', |
| 136 | + categories: undefined, |
| 137 | + url: 'https://eslint.vuejs.org/rules/no-undef-directives.html' |
| 138 | + }, |
| 139 | + fixable: null, |
| 140 | + schema: [ |
| 141 | + { |
| 142 | + type: 'object', |
| 143 | + properties: { |
| 144 | + ignorePatterns: { |
| 145 | + type: 'array', |
| 146 | + items: { |
| 147 | + type: 'string' |
| 148 | + }, |
| 149 | + uniqueItems: true |
| 150 | + } |
| 151 | + }, |
| 152 | + additionalProperties: true |
| 153 | + } |
| 154 | + ], |
| 155 | + messages: { |
| 156 | + undef: "The 'v-{{name}}' directive has been used, but not defined." |
| 157 | + } |
| 158 | + }, |
| 159 | + /** @param {RuleContext} context */ |
| 160 | + create(context) { |
| 161 | + const options = context.options[0] || {} |
| 162 | + /** @type {string[]} */ |
| 163 | + const ignorePatterns = options.ignorePatterns || [] |
| 164 | + |
| 165 | + /** |
| 166 | + * Check whether the given directive name is a verify target or not. |
| 167 | + * |
| 168 | + * @param {string} rawName The directive name. |
| 169 | + * @returns {boolean} |
| 170 | + */ |
| 171 | + function isVerifyTargetDirective(rawName) { |
| 172 | + if (utils.isBuiltInDirectiveName(rawName)) { |
| 173 | + return false |
| 174 | + } |
| 175 | + |
| 176 | + const ignored = ignorePatterns.some((pattern) => |
| 177 | + new RegExp(pattern).test(rawName) |
| 178 | + ) |
| 179 | + return !ignored |
| 180 | + } |
| 181 | + |
| 182 | + /** @type { (rawName:string, reportNode: ASTNode) => void } */ |
| 183 | + let verifyName |
| 184 | + /** @type {RuleListener} */ |
| 185 | + let scriptVisitor = {} |
| 186 | + /** @type {TemplateListener} */ |
| 187 | + const templateBodyVisitor = { |
| 188 | + /** @param {VDirective} node */ |
| 189 | + 'VAttribute[directive=true]'(node) { |
| 190 | + const name = node.key.name.name |
| 191 | + if (utils.isBuiltInDirectiveName(name)) { |
| 192 | + return |
| 193 | + } |
| 194 | + verifyName(node.key.name.rawName || name, node.key) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + if (utils.isScriptSetup(context)) { |
| 199 | + // For <script setup> |
| 200 | + const definedInSetupDirectives = new DefinedInSetupDirectives() |
| 201 | + const definedInOptionDirectives = new DefinedInOptionDirectives() |
| 202 | + |
| 203 | + const globalScope = context.sourceCode.scopeManager.globalScope |
| 204 | + if (globalScope) { |
| 205 | + for (const variable of globalScope.variables) { |
| 206 | + definedInSetupDirectives.addName(variable.name) |
| 207 | + } |
| 208 | + const moduleScope = globalScope.childScopes.find( |
| 209 | + (scope) => scope.type === 'module' |
| 210 | + ) |
| 211 | + for (const variable of (moduleScope && moduleScope.variables) || []) { |
| 212 | + definedInSetupDirectives.addName(variable.name) |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + scriptVisitor = utils.defineVueVisitor(context, { |
| 217 | + onVueObjectEnter(node) { |
| 218 | + for (const directive of getRegisteredDirectives(node)) { |
| 219 | + definedInOptionDirectives.addName(directive.name) |
| 220 | + } |
| 221 | + } |
| 222 | + }) |
| 223 | + |
| 224 | + verifyName = (rawName, reportNode) => { |
| 225 | + if (!isVerifyTargetDirective(rawName)) { |
| 226 | + return |
| 227 | + } |
| 228 | + if (definedInSetupDirectives.isDefinedDirective(rawName)) { |
| 229 | + return |
| 230 | + } |
| 231 | + if (definedInOptionDirectives.isDefinedDirective(rawName)) { |
| 232 | + return |
| 233 | + } |
| 234 | + |
| 235 | + context.report({ |
| 236 | + node: reportNode, |
| 237 | + messageId: 'undef', |
| 238 | + data: { |
| 239 | + name: rawName |
| 240 | + } |
| 241 | + }) |
| 242 | + } |
| 243 | + } else { |
| 244 | + // For Options API |
| 245 | + const definedInOptionDirectives = new DefinedInOptionDirectives() |
| 246 | + |
| 247 | + scriptVisitor = utils.executeOnVue(context, (obj) => { |
| 248 | + for (const directive of getRegisteredDirectives(obj)) { |
| 249 | + definedInOptionDirectives.addName(directive.name) |
| 250 | + } |
| 251 | + }) |
| 252 | + |
| 253 | + verifyName = (rawName, reportNode) => { |
| 254 | + if (!isVerifyTargetDirective(rawName)) { |
| 255 | + return |
| 256 | + } |
| 257 | + if (definedInOptionDirectives.isDefinedDirective(rawName)) { |
| 258 | + return |
| 259 | + } |
| 260 | + |
| 261 | + context.report({ |
| 262 | + node: reportNode, |
| 263 | + messageId: 'undef', |
| 264 | + data: { |
| 265 | + name: rawName |
| 266 | + } |
| 267 | + }) |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + return utils.defineTemplateBodyVisitor( |
| 272 | + context, |
| 273 | + templateBodyVisitor, |
| 274 | + scriptVisitor |
| 275 | + ) |
| 276 | + } |
| 277 | +} |
0 commit comments