-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathprefer-prop-type-boolean-first.js
109 lines (102 loc) · 2.79 KB
/
prefer-prop-type-boolean-first.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
/**
* @author Pig Fang
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
/**
* @param {ArrayExpression} node
* @param {RuleContext} context
*/
function checkArrayExpression(node, context) {
const booleanType = node.elements.find(
(element) =>
element && element.type === 'Identifier' && element.name === 'Boolean'
)
if (!booleanType) {
return
}
const booleanTypeIndex = node.elements.indexOf(booleanType)
if (booleanTypeIndex > 0) {
context.report({
node: booleanType,
messageId: 'shouldBeFirst',
suggest: [
{
messageId: 'moveToFirst',
fix: (fixer) => {
const sourceCode = context.getSourceCode()
const elements = [...node.elements]
elements.splice(booleanTypeIndex, 1)
const code = elements
.filter(utils.isDef)
.map((element) => sourceCode.getText(element))
code.unshift('Boolean')
return fixer.replaceText(node, `[${code.join(', ')}]`)
}
}
]
})
}
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'enforce `Boolean` comes first in component prop types',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/prefer-prop-type-boolean-first.html'
},
fixable: null,
hasSuggestions: true,
schema: [],
messages: {
shouldBeFirst: 'Type `Boolean` should be at first in prop types.',
moveToFirst: 'Move `Boolean` to be first in prop types.'
}
},
/** @param {RuleContext} context */
create(context) {
/**
* @param {import('../utils').ComponentProp} prop
*/
function checkProperty(prop) {
if (prop.type !== 'object') {
return
}
const { value } = prop
if (!value) {
return
}
if (value.type === 'ArrayExpression') {
checkArrayExpression(value, context)
} else if (value.type === 'ObjectExpression') {
const type = value.properties.find(
/** @return {property is Property} */
(property) =>
property.type === 'Property' &&
utils.getStaticPropertyName(property) === 'type'
)
if (!type || type.value.type !== 'ArrayExpression') {
return
}
checkArrayExpression(type.value, context)
}
}
return utils.compositingVisitors(
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(_, props) {
for (const prop of props) {
checkProperty(prop)
}
}
}),
utils.executeOnVue(context, (obj) => {
const props = utils.getComponentPropsFromOptions(obj)
for (const prop of props) {
checkProperty(prop)
}
})
)
}
}