forked from vuejs/eslint-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire-prop-type-constructor.js
93 lines (81 loc) · 2.53 KB
/
require-prop-type-constructor.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
/**
* @fileoverview require prop type to be a constructor
* @author Michał Sajnóg
*/
'use strict'
const utils = require('../utils')
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
const message = 'The "{{name}}" property should be a constructor.'
const forbiddenTypes = [
'Literal',
'TemplateLiteral',
'BinaryExpression',
'UpdateExpression'
]
const isForbiddenType = node => forbiddenTypes.indexOf(node.type) > -1 && node.raw !== 'null'
module.exports = {
meta: {
docs: {
description: 'require prop type to be a constructor',
category: 'essential',
url: 'https://github.com/vuejs/eslint-plugin-vue/blob/v5.0.0-beta.3/docs/rules/require-prop-type-constructor.md'
},
fixable: 'code', // or "code" or "whitespace"
schema: []
},
create (context) {
const fix = node => fixer => {
if (node.type === 'Literal') {
return fixer.replaceText(node, node.value)
} else if (
node.type === 'TemplateLiteral' &&
node.expressions.length === 0 &&
node.quasis.length === 1
) {
return fixer.replaceText(node, node.quasis[0].value.cooked)
}
}
const checkPropertyNode = (key, node) => {
if (isForbiddenType(node)) {
context.report({
node: node,
message,
data: {
name: utils.getStaticPropertyName(key)
},
fix: fix(node)
})
} else if (node.type === 'ArrayExpression') {
node.elements
.filter(prop => isForbiddenType(prop))
.forEach(prop => context.report({
node: prop,
message,
data: {
name: utils.getStaticPropertyName(key)
},
fix: fix(prop)
}))
}
}
return utils.executeOnVueComponent(context, (obj) => {
const properties = utils.getPropsProperties(obj)
.props
.filter(cp => cp.value)
for (const p of properties) {
if (isForbiddenType(p.value) || p.value.type === 'ArrayExpression') {
checkPropertyNode(p.key, p.value)
} else if (p.value.type === 'ObjectExpression') {
const typeProperty = p.value.properties.find(prop =>
prop.type === 'Property' &&
prop.key.name === 'type'
)
if (!typeProperty) continue
checkPropertyNode(p.key, utils.unwrapTypes(typeProperty.value))
}
}
})
}
}