|
| 1 | +/** |
| 2 | + * @author Flo Edelmann |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +import { camelCase } from '../utils/casing.ts' |
| 6 | +import utils from '../utils/index.js' |
| 7 | + |
| 8 | +/** |
| 9 | + * Get the static argument name of a directive, or `null` for dynamic arguments. |
| 10 | + */ |
| 11 | +const getStaticArgName = (directive: VDirective): string | null => |
| 12 | + directive.key.argument?.type === 'VIdentifier' |
| 13 | + ? directive.key.argument.rawName |
| 14 | + : null |
| 15 | + |
| 16 | +/** |
| 17 | + * Extract the prop name from an `update:propName` event directive argument. |
| 18 | + */ |
| 19 | +function getUpdateEventPropName(onDirective: VDirective): string | null { |
| 20 | + const argName = getStaticArgName(onDirective) |
| 21 | + if (!argName?.startsWith('update:')) { |
| 22 | + return null |
| 23 | + } |
| 24 | + return argName.slice('update:'.length) |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Check if the event handler is a simple mirror assignment of the bind expression. |
| 29 | + * Matches: `bar = $event` or `(param) => bar = param` |
| 30 | + */ |
| 31 | +function isMirrorAssignment( |
| 32 | + bindExpr: Expression, |
| 33 | + onExpr: VOnExpression | Expression, |
| 34 | + sourceCode: SourceCode |
| 35 | +): boolean { |
| 36 | + const bindText = sourceCode.getText(bindExpr as ASTNode) |
| 37 | + |
| 38 | + // Form A: VOnExpression with "bar = $event" |
| 39 | + if (onExpr.type === 'VOnExpression') { |
| 40 | + const statements = onExpr.body |
| 41 | + if (statements.length !== 1) { |
| 42 | + return false |
| 43 | + } |
| 44 | + const stmt = statements[0] |
| 45 | + if (stmt.type !== 'ExpressionStatement') { |
| 46 | + return false |
| 47 | + } |
| 48 | + const expr = stmt.expression |
| 49 | + if (expr.type !== 'AssignmentExpression' || expr.operator !== '=') { |
| 50 | + return false |
| 51 | + } |
| 52 | + const lhsText = sourceCode.getText(expr.left as ASTNode) |
| 53 | + return ( |
| 54 | + lhsText === bindText && |
| 55 | + expr.right.type === 'Identifier' && |
| 56 | + expr.right.name === '$event' |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + // Form B: ArrowFunctionExpression "(param) => bar = param" |
| 61 | + if (onExpr.type === 'ArrowFunctionExpression') { |
| 62 | + if (onExpr.params.length !== 1) { |
| 63 | + return false |
| 64 | + } |
| 65 | + const param = onExpr.params[0] |
| 66 | + if (param.type !== 'Identifier') { |
| 67 | + return false |
| 68 | + } |
| 69 | + const body = onExpr.body |
| 70 | + if (body.type !== 'AssignmentExpression' || body.operator !== '=') { |
| 71 | + return false |
| 72 | + } |
| 73 | + const lhsText = sourceCode.getText(body.left as ASTNode) |
| 74 | + return ( |
| 75 | + lhsText === bindText && |
| 76 | + body.right.type === 'Identifier' && |
| 77 | + body.right.name === param.name |
| 78 | + ) |
| 79 | + } |
| 80 | + |
| 81 | + return false |
| 82 | +} |
| 83 | + |
| 84 | +export default { |
| 85 | + meta: { |
| 86 | + type: 'suggestion', |
| 87 | + docs: { |
| 88 | + description: |
| 89 | + 'enforce using `v-model` instead of `:prop`/`@update:prop` pair', |
| 90 | + categories: undefined, |
| 91 | + url: 'https://eslint.vuejs.org/rules/prefer-v-model.html' |
| 92 | + }, |
| 93 | + fixable: null, |
| 94 | + hasSuggestions: true, |
| 95 | + schema: [], |
| 96 | + messages: { |
| 97 | + preferVModel: |
| 98 | + 'Prefer `{{ vModelName }}` over the `:{{ propName }}`/`@update:{{ eventName }}` pair.', |
| 99 | + replaceWithVModel: 'Replace with `{{ vModelName }}`.' |
| 100 | + } |
| 101 | + }, |
| 102 | + create(context: RuleContext) { |
| 103 | + const sourceCode = context.sourceCode |
| 104 | + |
| 105 | + return utils.defineTemplateBodyVisitor(context, { |
| 106 | + VStartTag(node) { |
| 107 | + const element = node.parent |
| 108 | + if (!utils.isCustomComponent(element)) { |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + const bindDirectives: VDirective[] = [] |
| 113 | + const onDirectives: VDirective[] = [] |
| 114 | + |
| 115 | + for (const attr of node.attributes) { |
| 116 | + if (!attr.directive) continue |
| 117 | + if ( |
| 118 | + attr.key.name.name === 'bind' && |
| 119 | + getStaticArgName(attr) != null && |
| 120 | + attr.key.modifiers.length === 0 |
| 121 | + ) { |
| 122 | + bindDirectives.push(attr) |
| 123 | + } |
| 124 | + if ( |
| 125 | + attr.key.name.name === 'on' && |
| 126 | + getUpdateEventPropName(attr) != null && |
| 127 | + attr.key.modifiers.length === 0 |
| 128 | + ) { |
| 129 | + onDirectives.push(attr) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + for (const bindDir of bindDirectives) { |
| 134 | + const propName = getStaticArgName(bindDir) |
| 135 | + if (!propName) { |
| 136 | + continue |
| 137 | + } |
| 138 | + |
| 139 | + const normalizedBindName = camelCase(propName) |
| 140 | + |
| 141 | + const matchingOnDir = onDirectives.find( |
| 142 | + (onDir) => |
| 143 | + camelCase(getUpdateEventPropName(onDir)!) === normalizedBindName |
| 144 | + ) |
| 145 | + |
| 146 | + if (!matchingOnDir) { |
| 147 | + continue |
| 148 | + } |
| 149 | + |
| 150 | + const bindExpr = bindDir.value?.expression |
| 151 | + const onExpr = matchingOnDir.value?.expression |
| 152 | + if ( |
| 153 | + !bindExpr || |
| 154 | + bindExpr.type === 'VFilterSequenceExpression' || |
| 155 | + bindExpr.type === 'VForExpression' || |
| 156 | + bindExpr.type === 'VOnExpression' || |
| 157 | + bindExpr.type === 'VSlotScopeExpression' || |
| 158 | + !onExpr || |
| 159 | + !isMirrorAssignment( |
| 160 | + bindExpr, |
| 161 | + onExpr as VOnExpression | Expression, |
| 162 | + sourceCode |
| 163 | + ) |
| 164 | + ) { |
| 165 | + continue |
| 166 | + } |
| 167 | + |
| 168 | + const isModelValue = normalizedBindName === 'modelValue' |
| 169 | + const vModelName = isModelValue ? 'v-model' : `v-model:${propName}` |
| 170 | + const eventName = getUpdateEventPropName(matchingOnDir) ?? propName |
| 171 | + |
| 172 | + const bindValueText = sourceCode.getText(bindDir.value as ASTNode) |
| 173 | + const vModelText = `${vModelName}=${bindValueText}` |
| 174 | + |
| 175 | + context.report({ |
| 176 | + node: bindDir, |
| 177 | + messageId: 'preferVModel', |
| 178 | + data: { |
| 179 | + vModelName, |
| 180 | + propName, |
| 181 | + eventName |
| 182 | + }, |
| 183 | + suggest: [ |
| 184 | + { |
| 185 | + messageId: 'replaceWithVModel', |
| 186 | + data: { vModelName }, |
| 187 | + *fix(fixer) { |
| 188 | + yield fixer.replaceText(bindDir, vModelText) |
| 189 | + |
| 190 | + // Remove the `v-on` directive including preceding whitespace |
| 191 | + const textBefore = sourceCode |
| 192 | + .getText() |
| 193 | + .slice(0, matchingOnDir.range[0]) |
| 194 | + const removeStart = |
| 195 | + matchingOnDir.range[0] - |
| 196 | + (textBefore.length - textBefore.trimEnd().length) |
| 197 | + yield fixer.removeRange([removeStart, matchingOnDir.range[1]]) |
| 198 | + } |
| 199 | + } |
| 200 | + ] |
| 201 | + }) |
| 202 | + } |
| 203 | + } |
| 204 | + }) |
| 205 | + } |
| 206 | +} |
0 commit comments