|
9 | 9 |
|
10 | 10 | const utils = require('../utils')
|
11 | 11 |
|
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | +// Helpers |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | + |
| 16 | +/** |
| 17 | + * Check whether the given token is a left parenthesis. |
| 18 | + * @param {Token} token The token to check. |
| 19 | + * @returns {boolean} `true` if the token is a left parenthesis. |
| 20 | + */ |
| 21 | +function isLeftParen (token) { |
| 22 | + return token != null && token.type === 'Punctuator' && token.value === '(' |
| 23 | +} |
| 24 | + |
12 | 25 | // ------------------------------------------------------------------------------
|
13 | 26 | // Rule Definition
|
14 | 27 | // ------------------------------------------------------------------------------
|
@@ -40,17 +53,24 @@ module.exports = {
|
40 | 53 | })
|
41 | 54 | },
|
42 | 55 |
|
43 |
| - "VAttribute[directive=true][key.name.name='on'][key.argument!=null] VOnExpression > ExpressionStatement > *" (node) { |
44 |
| - if (!always && node.type === 'CallExpression' && node.arguments.length === 0) { |
| 56 | + "VAttribute[directive=true][key.name.name='on'][key.argument!=null] VOnExpression > ExpressionStatement > CallExpression" (node) { |
| 57 | + if (!always && node.arguments.length === 0 && node.callee.type === 'Identifier') { |
45 | 58 | context.report({
|
46 | 59 | node,
|
47 | 60 | loc: node.loc,
|
48 | 61 | message: "Method calls without arguments inside of 'v-on' directives must not have parentheses.",
|
49 | 62 | fix: fixer => {
|
50 |
| - const nodeString = context.getSourceCode().getText().substring(node.range[0], node.range[1]) |
51 |
| - // This ensures that parens are also removed if they contain whitespace |
52 |
| - const parensLength = nodeString.match(/\(\s*\)\s*$/)[0].length |
53 |
| - return fixer.removeRange([node.end - parensLength, node.end]) |
| 63 | + const tokenStore = context.parserServices.getTemplateBodyTokenStore() |
| 64 | + const rightToken = tokenStore.getLastToken(node) |
| 65 | + const leftToken = tokenStore.getTokenAfter(node.callee, isLeftParen) |
| 66 | + const tokens = tokenStore.getTokensBetween(leftToken, rightToken, { includeComments: true }) |
| 67 | + |
| 68 | + if (tokens.length) { |
| 69 | + // The comment is included and cannot be fixed. |
| 70 | + return null |
| 71 | + } |
| 72 | + |
| 73 | + return fixer.removeRange([leftToken.range[0], rightToken.range[1]]) |
54 | 74 | }
|
55 | 75 | })
|
56 | 76 | }
|
|
0 commit comments