-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathprefer-true-attribute-shorthand.js
180 lines (166 loc) · 4.62 KB
/
prefer-true-attribute-shorthand.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* @author Pig Fang
* See LICENSE file in root directory for full license.
*/
'use strict'
const { toRegExp } = require('../utils/regexp')
const utils = require('../utils')
/**
* @typedef { 'always' | 'never' } PreferOption
*/
/**
* @param {VDirective | VAttribute} node
* @returns {string | null}
*/
function getAttributeName(node) {
if (!node.directive) {
return node.key.rawName
}
if (
(node.key.name.name === 'bind' || node.key.name.name === 'model') &&
node.key.argument &&
node.key.argument.type === 'VIdentifier'
) {
return node.key.argument.rawName
}
return null
}
/**
* @param {VAttribute | VDirective} node
* @param {boolean} isExcepted
* @param {PreferOption} option
*/
function shouldConvertToLongForm(node, isExcepted, option) {
return (
!node.directive &&
!node.value &&
(option === 'always' ? isExcepted : !isExcepted)
)
}
/**
* @param {VAttribute | VDirective} node
* @param {boolean} isExcepted
* @param {PreferOption} option
*/
function shouldConvertToShortForm(node, isExcepted, option) {
const isLiteralTrue =
node.directive &&
node.value?.expression?.type === 'Literal' &&
node.value.expression.value === true &&
Boolean(node.key.argument)
return isLiteralTrue && (option === 'always' ? !isExcepted : isExcepted)
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description:
'require shorthand form attribute when `v-bind` value is `true`',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/prefer-true-attribute-shorthand.html'
},
fixable: null,
hasSuggestions: true,
schema: [
{ enum: ['always', 'never'] },
{
type: 'object',
properties: {
except: {
type: 'array',
items: { type: 'string' },
uniqueItems: true
}
},
additionalProperties: false
}
],
messages: {
expectShort:
"Boolean prop with 'true' value should be written in shorthand form.",
expectLong:
"Boolean prop with 'true' value should be written in long form.",
rewriteIntoShort: 'Rewrite this prop into shorthand form.',
rewriteIntoLongVueProp:
'Rewrite this prop into long-form Vue component prop.',
rewriteIntoLongHtmlAttr:
'Rewrite this prop into long-form HTML attribute.'
}
},
/** @param {RuleContext} context */
create(context) {
/** @type {'always' | 'never'} */
const option = context.options[0] || 'always'
/** @type {RegExp[]} */
const exceptReg = (context.options[1]?.except || []).map(toRegExp)
/**
* @param {VAttribute | VDirective} node
* @param {string} messageId
* @param {string} longVuePropText
* @param {string} longHtmlAttrText
*/
function reportLongForm(
node,
messageId,
longVuePropText,
longHtmlAttrText
) {
context.report({
node,
messageId,
suggest: [
{
messageId: 'rewriteIntoLongVueProp',
fix: (fixer) => fixer.replaceText(node, longVuePropText)
},
{
messageId: 'rewriteIntoLongHtmlAttr',
fix: (fixer) => fixer.replaceText(node, longHtmlAttrText)
}
]
})
}
/**
* @param {VAttribute | VDirective} node
* @param {string} messageId
* @param {string} shortFormText
*/
function reportShortForm(node, messageId, shortFormText) {
context.report({
node,
messageId,
suggest: [
{
messageId: 'rewriteIntoShort',
fix: (fixer) => fixer.replaceText(node, shortFormText)
}
]
})
}
return utils.defineTemplateBodyVisitor(context, {
VAttribute(node) {
if (!utils.isCustomComponent(node.parent.parent)) return
const name = getAttributeName(node)
if (name === null) return
const isExcepted = exceptReg.some((re) => re.test(name))
if (shouldConvertToLongForm(node, isExcepted, option)) {
const key = /** @type {VIdentifier} */ (node.key)
reportLongForm(
node,
'expectLong',
`:${key.rawName}="true"`,
`${key.rawName}="${key.rawName}"`
)
} else if (shouldConvertToShortForm(node, isExcepted, option)) {
const directiveKey = /** @type {VDirectiveKey} */ (node.key)
if (
directiveKey.argument &&
directiveKey.argument.type === 'VIdentifier'
) {
reportShortForm(node, 'expectShort', directiveKey.argument.rawName)
}
}
}
})
}
}