|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +/** |
| 8 | + * @typedef {import('vue-eslint-parser').AST.ESLintLiteral} Literal |
| 9 | + * @typedef {import('vue-eslint-parser').AST.ESLintCallExpression} CallExpression |
| 10 | + */ |
| 11 | + |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | +// Requirements |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | + |
| 16 | +const { findVariable } = require('eslint-utils') |
| 17 | +const utils = require('../utils') |
| 18 | +const { isKebabCase } = require('../utils/casing') |
| 19 | + |
| 20 | +// ------------------------------------------------------------------------------ |
| 21 | +// Helpers |
| 22 | +// ------------------------------------------------------------------------------ |
| 23 | + |
| 24 | +/** |
| 25 | + * Check whether the given event name is valid. |
| 26 | + * @param {string} name The name to check. |
| 27 | + * @returns {boolean} `true` if the given event name is valid. |
| 28 | + */ |
| 29 | +function isValidEventName(name) { |
| 30 | + return isKebabCase(name) || name.startsWith('update:') |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Get the name param node from the given CallExpression |
| 35 | + * @param {CallExpression} node CallExpression |
| 36 | + * @returns { Literal & { value: string } } |
| 37 | + */ |
| 38 | +function getNameParamNode(node) { |
| 39 | + const nameLiteralNode = node.arguments[0] |
| 40 | + if ( |
| 41 | + !nameLiteralNode || |
| 42 | + nameLiteralNode.type !== 'Literal' || |
| 43 | + typeof nameLiteralNode.value !== 'string' |
| 44 | + ) { |
| 45 | + // cannot check |
| 46 | + return null |
| 47 | + } |
| 48 | + |
| 49 | + return nameLiteralNode |
| 50 | +} |
| 51 | +/** |
| 52 | + * Get the callee member node from the given CallExpression |
| 53 | + * @param {CallExpression} node CallExpression |
| 54 | + */ |
| 55 | +function getCalleeMemberNode(node) { |
| 56 | + const callee = node.callee |
| 57 | + |
| 58 | + if (callee.type === 'MemberExpression') { |
| 59 | + const name = utils.getStaticPropertyName(callee) |
| 60 | + if (name) { |
| 61 | + return { name, member: callee } |
| 62 | + } |
| 63 | + } |
| 64 | + return null |
| 65 | +} |
| 66 | + |
| 67 | +// ------------------------------------------------------------------------------ |
| 68 | +// Rule Definition |
| 69 | +// ------------------------------------------------------------------------------ |
| 70 | + |
| 71 | +module.exports = { |
| 72 | + meta: { |
| 73 | + type: 'suggestion', |
| 74 | + docs: { |
| 75 | + description: 'enforce custom event names always use "kebab-case"', |
| 76 | + categories: ['vue3-essential', 'essential'], |
| 77 | + url: 'https://eslint.vuejs.org/rules/custom-event-name-casing.html' |
| 78 | + }, |
| 79 | + fixable: null, |
| 80 | + schema: [], |
| 81 | + messages: { |
| 82 | + unexpected: "Custom event name '{{name}}' must be kebab-case." |
| 83 | + } |
| 84 | + }, |
| 85 | + |
| 86 | + create(context) { |
| 87 | + const setupContexts = new Map() |
| 88 | + |
| 89 | + /** |
| 90 | + * @param { Literal & { value: string } } nameLiteralNode |
| 91 | + */ |
| 92 | + function verify(nameLiteralNode) { |
| 93 | + const name = nameLiteralNode.value |
| 94 | + if (isValidEventName(name)) { |
| 95 | + return |
| 96 | + } |
| 97 | + context.report({ |
| 98 | + node: nameLiteralNode, |
| 99 | + messageId: 'unexpected', |
| 100 | + data: { |
| 101 | + name |
| 102 | + } |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + return utils.defineTemplateBodyVisitor( |
| 107 | + context, |
| 108 | + { |
| 109 | + CallExpression(node) { |
| 110 | + const callee = node.callee |
| 111 | + const nameLiteralNode = getNameParamNode(node) |
| 112 | + if (!nameLiteralNode) { |
| 113 | + // cannot check |
| 114 | + return |
| 115 | + } |
| 116 | + if (callee.type === 'Identifier' && callee.name === '$emit') { |
| 117 | + verify(nameLiteralNode) |
| 118 | + } |
| 119 | + } |
| 120 | + }, |
| 121 | + utils.compositingVisitors( |
| 122 | + utils.defineVueVisitor(context, { |
| 123 | + onSetupFunctionEnter(node, { node: vueNode }) { |
| 124 | + const contextParam = node.params[1] |
| 125 | + if (!contextParam) { |
| 126 | + // no arguments |
| 127 | + return |
| 128 | + } |
| 129 | + if (contextParam.type === 'RestElement') { |
| 130 | + // cannot check |
| 131 | + return |
| 132 | + } |
| 133 | + if (contextParam.type === 'ArrayPattern') { |
| 134 | + // cannot check |
| 135 | + return |
| 136 | + } |
| 137 | + const contextReferenceIds = new Set() |
| 138 | + const emitReferenceIds = new Set() |
| 139 | + if (contextParam.type === 'ObjectPattern') { |
| 140 | + const emitProperty = contextParam.properties.find( |
| 141 | + (p) => |
| 142 | + p.type === 'Property' && |
| 143 | + utils.getStaticPropertyName(p) === 'emit' |
| 144 | + ) |
| 145 | + if (!emitProperty) { |
| 146 | + return |
| 147 | + } |
| 148 | + const emitParam = emitProperty.value |
| 149 | + // `setup(props, {emit})` |
| 150 | + const variable = findVariable(context.getScope(), emitParam) |
| 151 | + if (!variable) { |
| 152 | + return |
| 153 | + } |
| 154 | + for (const reference of variable.references) { |
| 155 | + emitReferenceIds.add(reference.identifier) |
| 156 | + } |
| 157 | + } else { |
| 158 | + // `setup(props, context)` |
| 159 | + const variable = findVariable(context.getScope(), contextParam) |
| 160 | + if (!variable) { |
| 161 | + return |
| 162 | + } |
| 163 | + for (const reference of variable.references) { |
| 164 | + contextReferenceIds.add(reference.identifier) |
| 165 | + } |
| 166 | + } |
| 167 | + setupContexts.set(vueNode, { |
| 168 | + contextReferenceIds, |
| 169 | + emitReferenceIds |
| 170 | + }) |
| 171 | + }, |
| 172 | + CallExpression(node, { node: vueNode }) { |
| 173 | + const nameLiteralNode = getNameParamNode(node) |
| 174 | + if (!nameLiteralNode) { |
| 175 | + // cannot check |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + // verify setup context |
| 180 | + const setupContext = setupContexts.get(vueNode) |
| 181 | + if (setupContext) { |
| 182 | + const { contextReferenceIds, emitReferenceIds } = setupContext |
| 183 | + if (emitReferenceIds.has(node.callee)) { |
| 184 | + // verify setup(props,{emit}) {emit()} |
| 185 | + verify(nameLiteralNode) |
| 186 | + } else { |
| 187 | + const emit = getCalleeMemberNode(node) |
| 188 | + if ( |
| 189 | + emit && |
| 190 | + emit.name === 'emit' && |
| 191 | + contextReferenceIds.has(emit.member.object) |
| 192 | + ) { |
| 193 | + // verify setup(props,context) {context.emit()} |
| 194 | + verify(nameLiteralNode) |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + }, |
| 199 | + onVueObjectExit(node) { |
| 200 | + setupContexts.delete(node) |
| 201 | + } |
| 202 | + }), |
| 203 | + { |
| 204 | + CallExpression(node) { |
| 205 | + const nameLiteralNode = getNameParamNode(node) |
| 206 | + if (!nameLiteralNode) { |
| 207 | + // cannot check |
| 208 | + return |
| 209 | + } |
| 210 | + const emit = getCalleeMemberNode(node) |
| 211 | + // verify $emit |
| 212 | + if (emit && emit.name === '$emit') { |
| 213 | + // verify this.$emit() |
| 214 | + verify(nameLiteralNode) |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + ) |
| 219 | + ) |
| 220 | + } |
| 221 | +} |
0 commit comments