-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathno-direct-composable-in-event-handler.js
66 lines (61 loc) · 1.74 KB
/
no-direct-composable-in-event-handler.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
61
62
63
64
65
66
/**
* @author Nils Haberkamp
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
/**
* Check if the given function name follows the composable naming convention (starts with 'use')
* @param {string | null | undefined} name The function name
* @returns {boolean} `true` if the function name starts with 'use'
*/
function isComposable(name) {
return Boolean(name && name.startsWith('use'))
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'disallow direct composable usage in event handler',
categories: ['vue3-essential'],
url: 'https://eslint.vuejs.org/rules/no-direct-composable-in-event-handler.html'
},
fixable: null,
schema: [],
messages: {
forbiddenComposableUsage:
'Direct composable usage in event handler is not allowed.'
}
},
/** @param {RuleContext} context */
create(context) {
return utils.defineTemplateBodyVisitor(context, {
/** @param {VDirective} node */
'VAttribute[directive=true][key.name.name="on"]'(node) {
const eventHandler = node.value
if (!eventHandler || !eventHandler.expression) {
return
}
if (
eventHandler.expression.type === 'Identifier' &&
isComposable(eventHandler.expression.name)
) {
context.report({
node,
messageId: 'forbiddenComposableUsage',
loc: {
start: {
line: node.loc.start.line,
column: node.loc.start.column
},
end: {
line: node.loc.end.line,
column: node.loc.end.column
}
}
})
}
}
})
}
}