-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathdefine-emits-declaration.js
106 lines (98 loc) · 2.77 KB
/
define-emits-declaration.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* @author Amorites
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
/**
* @typedef {import('@typescript-eslint/types').TSESTree.TypeNode} TypeNode
*
*/
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce declaration style of `defineEmits`',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/define-emits-declaration.html'
},
fixable: null,
schema: [
{
enum: ['type-based', 'type-literal', 'runtime']
}
],
messages: {
hasArg: 'Use type based declaration instead of runtime declaration.',
hasTypeArg: 'Use runtime declaration instead of type based declaration.',
hasTypeCallArg:
'Use new type literal declaration instead of the old call signature declaration.'
}
},
/** @param {RuleContext} context */
create(context) {
const scriptSetup = utils.getScriptSetupElement(context)
if (!scriptSetup || !utils.hasAttribute(scriptSetup, 'lang', 'ts')) {
return {}
}
const defineType = context.options[0] || 'type-based'
return utils.defineScriptSetupVisitor(context, {
onDefineEmitsEnter(node) {
switch (defineType) {
case 'type-based': {
if (node.arguments.length > 0) {
context.report({
node,
messageId: 'hasArg'
})
}
break
}
case 'type-literal': {
verifyTypeLiteral(node)
break
}
case 'runtime': {
const typeArguments =
'typeArguments' in node ? node.typeArguments : node.typeParameters
if (typeArguments && typeArguments.params.length > 0) {
context.report({
node,
messageId: 'hasTypeArg'
})
}
break
}
}
}
})
/** @param {CallExpression} node */
function verifyTypeLiteral(node) {
if (node.arguments.length > 0) {
context.report({
node,
messageId: 'hasArg'
})
return
}
const typeArguments = node.typeArguments || node.typeParameters
const param = /** @type {TypeNode|undefined} */ (typeArguments?.params[0])
if (!param) return
if (param.type === 'TSTypeLiteral') {
for (const memberNode of param.members) {
if (memberNode.type !== 'TSPropertySignature') {
context.report({
node: memberNode,
messageId: 'hasTypeCallArg'
})
}
}
} else if (param.type === 'TSFunctionType') {
context.report({
node: param,
messageId: 'hasTypeCallArg'
})
}
}
}
}