|
9 | 9 |
|
10 | 10 | const utils = require('../utils')
|
11 | 11 |
|
| 12 | +/** |
| 13 | + * @typedef { import('../utils').ComponentPropertyData } ComponentPropertyData |
| 14 | + */ |
| 15 | + |
12 | 16 | // ------------------------------------------------------------------------------
|
13 | 17 | // Helpers
|
14 | 18 | // ------------------------------------------------------------------------------
|
@@ -96,77 +100,110 @@ module.exports = {
|
96 | 100 | return expression
|
97 | 101 | }
|
98 | 102 |
|
99 |
| - return utils.defineTemplateBodyVisitor(context, { |
100 |
| - ...(always |
101 |
| - ? { |
102 |
| - /** @param {Identifier} node */ |
103 |
| - "VAttribute[directive=true][key.name.name='on'][key.argument!=null] > VExpressionContainer > Identifier"( |
104 |
| - node |
105 |
| - ) { |
106 |
| - context.report({ |
107 |
| - node, |
108 |
| - message: |
109 |
| - "Method calls inside of 'v-on' directives must have parentheses." |
110 |
| - }) |
| 103 | + if (always) { |
| 104 | + return utils.defineTemplateBodyVisitor(context, { |
| 105 | + /** @param {Identifier} node */ |
| 106 | + "VAttribute[directive=true][key.name.name='on'][key.argument!=null] > VExpressionContainer > Identifier"( |
| 107 | + node |
| 108 | + ) { |
| 109 | + context.report({ |
| 110 | + node, |
| 111 | + message: |
| 112 | + "Method calls inside of 'v-on' directives must have parentheses." |
| 113 | + }) |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + const option = context.options[1] || {} |
| 119 | + const ignoreIncludesComment = !!option.ignoreIncludesComment |
| 120 | + /** @type {Set<string>} */ |
| 121 | + const useArgsMethods = new Set() |
| 122 | + |
| 123 | + return utils.defineTemplateBodyVisitor( |
| 124 | + context, |
| 125 | + { |
| 126 | + /** @param {VOnExpression} node */ |
| 127 | + "VAttribute[directive=true][key.name.name='on'][key.argument!=null] VOnExpression"( |
| 128 | + node |
| 129 | + ) { |
| 130 | + const expression = getInvalidNeverCallExpression(node) |
| 131 | + if (!expression) { |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + const tokenStore = context.parserServices.getTemplateBodyTokenStore() |
| 136 | + const tokens = tokenStore.getTokens(node.parent, { |
| 137 | + includeComments: true |
| 138 | + }) |
| 139 | + /** @type {Token | undefined} */ |
| 140 | + let leftQuote |
| 141 | + /** @type {Token | undefined} */ |
| 142 | + let rightQuote |
| 143 | + if (isQuote(tokens[0])) { |
| 144 | + leftQuote = tokens.shift() |
| 145 | + rightQuote = tokens.pop() |
| 146 | + } |
| 147 | + |
| 148 | + const hasComment = tokens.some( |
| 149 | + (token) => token.type === 'Block' || token.type === 'Line' |
| 150 | + ) |
| 151 | + |
| 152 | + if (ignoreIncludesComment && hasComment) { |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + if (expression.callee.type === 'Identifier') { |
| 157 | + if (useArgsMethods.has(expression.callee.name)) { |
| 158 | + // The behavior of target method can change given the arguments. |
| 159 | + return |
111 | 160 | }
|
112 | 161 | }
|
113 |
| - : { |
114 |
| - /** @param {VOnExpression} node */ |
115 |
| - "VAttribute[directive=true][key.name.name='on'][key.argument!=null] VOnExpression"( |
116 |
| - node |
| 162 | + |
| 163 | + context.report({ |
| 164 | + node: expression, |
| 165 | + message: |
| 166 | + "Method calls without arguments inside of 'v-on' directives must not have parentheses.", |
| 167 | + fix: hasComment |
| 168 | + ? null /* The comment is included and cannot be fixed. */ |
| 169 | + : (fixer) => { |
| 170 | + /** @type {Range} */ |
| 171 | + const range = |
| 172 | + leftQuote && rightQuote |
| 173 | + ? [leftQuote.range[1], rightQuote.range[0]] |
| 174 | + : [tokens[0].range[0], tokens[tokens.length - 1].range[1]] |
| 175 | + |
| 176 | + return fixer.replaceTextRange( |
| 177 | + range, |
| 178 | + context.getSourceCode().getText(expression.callee) |
| 179 | + ) |
| 180 | + } |
| 181 | + }) |
| 182 | + } |
| 183 | + }, |
| 184 | + utils.defineVueVisitor(context, { |
| 185 | + onVueObjectEnter(node) { |
| 186 | + for (const method of utils.iterateProperties( |
| 187 | + node, |
| 188 | + new Set(['methods']) |
| 189 | + )) { |
| 190 | + if (useArgsMethods.has(method.name)) { |
| 191 | + continue |
| 192 | + } |
| 193 | + if (method.type !== 'object') { |
| 194 | + continue |
| 195 | + } |
| 196 | + const value = method.property.value |
| 197 | + if ( |
| 198 | + (value.type === 'FunctionExpression' || |
| 199 | + value.type === 'ArrowFunctionExpression') && |
| 200 | + value.params.length > 0 |
117 | 201 | ) {
|
118 |
| - const expression = getInvalidNeverCallExpression(node) |
119 |
| - if (!expression) { |
120 |
| - return |
121 |
| - } |
122 |
| - const option = context.options[1] || {} |
123 |
| - const ignoreIncludesComment = !!option.ignoreIncludesComment |
124 |
| - |
125 |
| - const tokenStore = context.parserServices.getTemplateBodyTokenStore() |
126 |
| - const tokens = tokenStore.getTokens(node.parent, { |
127 |
| - includeComments: true |
128 |
| - }) |
129 |
| - /** @type {Token | undefined} */ |
130 |
| - let leftQuote |
131 |
| - /** @type {Token | undefined} */ |
132 |
| - let rightQuote |
133 |
| - if (isQuote(tokens[0])) { |
134 |
| - leftQuote = tokens.shift() |
135 |
| - rightQuote = tokens.pop() |
136 |
| - } |
137 |
| - |
138 |
| - const hasComment = tokens.some( |
139 |
| - (token) => token.type === 'Block' || token.type === 'Line' |
140 |
| - ) |
141 |
| - |
142 |
| - if (ignoreIncludesComment && hasComment) { |
143 |
| - return |
144 |
| - } |
145 |
| - |
146 |
| - context.report({ |
147 |
| - node: expression, |
148 |
| - message: |
149 |
| - "Method calls without arguments inside of 'v-on' directives must not have parentheses.", |
150 |
| - fix: hasComment |
151 |
| - ? null /* The comment is included and cannot be fixed. */ |
152 |
| - : (fixer) => { |
153 |
| - /** @type {Range} */ |
154 |
| - const range = |
155 |
| - leftQuote && rightQuote |
156 |
| - ? [leftQuote.range[1], rightQuote.range[0]] |
157 |
| - : [ |
158 |
| - tokens[0].range[0], |
159 |
| - tokens[tokens.length - 1].range[1] |
160 |
| - ] |
161 |
| - |
162 |
| - return fixer.replaceTextRange( |
163 |
| - range, |
164 |
| - context.getSourceCode().getText(expression.callee) |
165 |
| - ) |
166 |
| - } |
167 |
| - }) |
| 202 | + useArgsMethods.add(method.name) |
168 | 203 | }
|
169 |
| - }) |
170 |
| - }) |
| 204 | + } |
| 205 | + } |
| 206 | + }) |
| 207 | + ) |
171 | 208 | }
|
172 | 209 | }
|
0 commit comments