-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathv-bind-style.js
171 lines (152 loc) · 4.83 KB
/
v-bind-style.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
/**
* @author Toru Nagashima
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const casing = require('../utils/casing')
/**
* @typedef { VDirectiveKey & { name: VIdentifier & { name: 'bind' }, argument: VExpressionContainer | VIdentifier } } VBindDirectiveKey
* @typedef { VDirective & { key: VBindDirectiveKey } } VBindDirective
*/
/**
* @param {string} name
* @returns {string}
*/
function kebabCaseToCamelCase(name) {
return casing.isKebabCase(name) ? casing.camelCase(name) : name
}
/**
* @param {VBindDirective} node
* @returns {boolean}
*/
function isSameName(node) {
const attrName =
node.key.argument.type === 'VIdentifier' ? node.key.argument.rawName : null
const valueName =
node.value?.expression?.type === 'Identifier'
? node.value.expression.name
: null
if (!attrName || !valueName) return false
return kebabCaseToCamelCase(attrName) === kebabCaseToCamelCase(valueName)
}
/**
* @param {VBindDirectiveKey} key
* @returns {number}
*/
function getCutStart(key) {
const modifiers = key.modifiers
return modifiers.length > 0
? modifiers[modifiers.length - 1].range[1]
: key.argument.range[1]
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'enforce `v-bind` directive style',
categories: ['vue3-strongly-recommended', 'vue2-strongly-recommended'],
url: 'https://eslint.vuejs.org/rules/v-bind-style.html'
},
fixable: 'code',
schema: [
{ enum: ['shorthand', 'longform'] },
{
type: 'object',
properties: {
sameNameShorthand: { enum: ['always', 'never', 'ignore'] }
},
additionalProperties: false
}
],
messages: {
expectedLonghand: "Expected 'v-bind' before ':'.",
unexpectedLonghand: "Unexpected 'v-bind' before ':'.",
expectedLonghandForProp: "Expected 'v-bind:' instead of '.'.",
expectedShorthand: 'Expected same-name shorthand.',
unexpectedShorthand: 'Unexpected same-name shorthand.'
}
},
/** @param {RuleContext} context */
create(context) {
const preferShorthand = context.options[0] !== 'longform'
/** @type {"always" | "never" | "ignore"} */
const sameNameShorthand = context.options[1]?.sameNameShorthand || 'ignore'
/** @param {VBindDirective} node */
function checkAttributeStyle(node) {
const shorthandProp = node.key.name.rawName === '.'
const shorthand = node.key.name.rawName === ':' || shorthandProp
if (shorthand === preferShorthand) {
return
}
let messageId = 'expectedLonghand'
if (preferShorthand) {
messageId = 'unexpectedLonghand'
} else if (shorthandProp) {
messageId = 'expectedLonghandForProp'
}
context.report({
node,
loc: node.loc,
messageId,
*fix(fixer) {
if (preferShorthand) {
yield fixer.remove(node.key.name)
} else {
yield fixer.insertTextBefore(node, 'v-bind')
if (shorthandProp) {
// Replace `.` by `:`.
yield fixer.replaceText(node.key.name, ':')
// Insert `.prop` modifier if it doesn't exist.
const modifier = node.key.modifiers[0]
const isAutoGeneratedPropModifier =
modifier.name === 'prop' && modifier.rawName === ''
if (isAutoGeneratedPropModifier) {
yield fixer.insertTextBefore(modifier, '.prop')
}
}
}
}
})
}
/** @param {VBindDirective} node */
function checkAttributeSameName(node) {
if (sameNameShorthand === 'ignore' || !isSameName(node)) return
const preferShorthand = sameNameShorthand === 'always'
const isShorthand = utils.isVBindSameNameShorthand(node)
if (isShorthand === preferShorthand) {
return
}
const messageId = preferShorthand
? 'expectedShorthand'
: 'unexpectedShorthand'
context.report({
node,
loc: node.loc,
messageId,
*fix(fixer) {
if (preferShorthand) {
/** @type {Range} */
const valueRange = [getCutStart(node.key), node.range[1]]
yield fixer.removeRange(valueRange)
} else if (node.key.argument.type === 'VIdentifier') {
yield fixer.insertTextAfter(
node,
`="${kebabCaseToCamelCase(node.key.argument.rawName)}"`
)
}
}
})
}
return utils.defineTemplateBodyVisitor(context, {
/** @param {VBindDirective} node */
"VAttribute[directive=true][key.name.name='bind'][key.argument!=null]"(
node
) {
checkAttributeSameName(node)
checkAttributeStyle(node)
}
})
}
}