forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-typed-ref.js
120 lines (110 loc) · 3.05 KB
/
require-typed-ref.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* @author Ivan Demchuk <https://github.com/Demivan>
* See LICENSE file in root directory for full license.
*/
'use strict'
const { iterateDefineRefs } = require('../utils/ref-object-references')
const utils = require('../utils')
/**
* @param {Expression|SpreadElement} node
*/
function isNullOrUndefined(node) {
return (
(node.type === 'Literal' && node.value === null) ||
(node.type === 'Identifier' && node.name === 'undefined')
)
}
/**
* @typedef {import('../utils/ref-object-references').RefObjectReferences} RefObjectReferences
*/
module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'require `ref` and `shallowRef` functions to be strongly typed',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/require-typed-ref.html'
},
fixable: null,
schema: [],
messages: {
noType:
'Specify type parameter for `{{name}}` function, otherwise created variable will not be typechecked.'
}
},
/** @param {RuleContext} context */
create(context) {
const filename = context.getFilename()
if (!utils.isVueFile(filename) && !utils.isTypeScriptFile(filename)) {
return {}
}
if (utils.isVueFile(filename)) {
const sourceCode = context.getSourceCode()
const documentFragment =
sourceCode.parserServices.getDocumentFragment &&
sourceCode.parserServices.getDocumentFragment()
if (!documentFragment) {
return {}
}
const scripts = documentFragment.children.filter(
/** @returns {element is VElement} */
(element) => utils.isVElement(element) && element.name === 'script'
)
if (
scripts.every((script) => !utils.hasAttribute(script, 'lang', 'ts'))
) {
return {}
}
}
const defines = iterateDefineRefs(
/** @type {import('eslint').Scope.Scope} */ (
context.getSourceCode().scopeManager.globalScope
)
)
/**
* @param {string} name
* @param {CallExpression} node
*/
function report(name, node) {
context.report({
node,
messageId: 'noType',
data: {
name
}
})
}
return {
Program() {
for (const ref of defines) {
if (ref.name !== 'ref' && ref.name !== 'shallowRef') {
continue
}
if (
ref.node.arguments.length > 0 &&
!isNullOrUndefined(ref.node.arguments[0])
) {
continue
}
const typeArguments =
'typeArguments' in ref.node
? ref.node.typeArguments
: ref.node.typeParameters
if (typeArguments == null) {
if (
ref.node.parent.type === 'VariableDeclarator' &&
ref.node.parent.id.type === 'Identifier'
) {
if (ref.node.parent.id.typeAnnotation == null) {
report(ref.name, ref.node)
}
} else {
report(ref.name, ref.node)
}
}
}
}
}
}
}