-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathv-on-function-call.js
60 lines (53 loc) · 2.03 KB
/
v-on-function-call.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @author Niklas Higi
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce or forbid parentheses after method calls without arguments in `v-on` directives',
category: undefined,
url: 'https://eslint.vuejs.org/rules/v-on-function-call.html'
},
fixable: 'code',
schema: [
{ enum: ['always', 'never'] }
]
},
create (context) {
const always = context.options[0] === 'always'
return utils.defineTemplateBodyVisitor(context, {
"VAttribute[directive=true][key.name.name='on'][key.argument!=null] > VExpressionContainer > Identifier" (node) {
if (!always) return
context.report({
node,
loc: node.loc,
message: "Method calls inside of 'v-on' directives must have parentheses."
})
},
"VAttribute[directive=true][key.name.name='on'][key.argument!=null] VOnExpression > ExpressionStatement > *" (node) {
if (!always && node.type === 'CallExpression' && node.arguments.length === 0) {
context.report({
node,
loc: node.loc,
message: "Method calls without arguments inside of 'v-on' directives must not have parentheses.",
fix: fixer => {
const nodeString = context.getSourceCode().getText().substring(node.range[0], node.range[1])
// This ensures that parens are also removed if they contain whitespace
const parensLength = nodeString.match(/\(\s*\)\s*$/)[0].length
return fixer.removeRange([node.end - parensLength, node.end])
}
})
}
}
})
}
}