-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathpadding-lines-in-component-definition.js
375 lines (341 loc) · 11.3 KB
/
padding-lines-in-component-definition.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* @author ItMaga <https://github.com/ItMaga>
* See LICENSE file in root directory for full license.
*/
'use strict'
/**
* @typedef {import('../utils').ComponentProp} ComponentProp
* @typedef {import('../utils').ComponentEmit} ComponentEmit
* @typedef {import('../utils').GroupName} GroupName
*/
const utils = require('../utils')
const { isCommentToken } = require('@eslint-community/eslint-utils')
const AvailablePaddingOptions = {
Never: 'never',
Always: 'always',
Ignore: 'ignore'
}
const OptionKeys = {
BetweenOptions: 'betweenOptions',
WithinOption: 'withinOption',
BetweenItems: 'betweenItems',
WithinEach: 'withinEach',
GroupSingleLineProperties: 'groupSingleLineProperties'
}
/**
* @param {Token} node
*/
function isComma(node) {
return node.type === 'Punctuator' && node.value === ','
}
/**
* @typedef {Exclude<ComponentProp | ComponentEmit, {type:'infer-type'}> & { node: {type: 'Property' | 'SpreadElement'} }} ValidComponentPropOrEmit
*/
/**
* @template {ComponentProp | ComponentEmit} T
* @param {T} propOrEmit
* @returns {propOrEmit is ValidComponentPropOrEmit & T}
*/
function isValidProperties(propOrEmit) {
return Boolean(
propOrEmit.type !== 'infer-type' &&
propOrEmit.node &&
['Property', 'SpreadElement'].includes(propOrEmit.node.type)
)
}
/**
* Split the source code into multiple lines based on the line delimiters.
* @param {string} text Source code as a string.
* @returns {string[]} Array of source code lines.
*/
function splitLines(text) {
return text.split(/\r\n|[\r\n\u2028\u2029]/gu)
}
/**
* @param {any} initialOption
* @param {string} optionKey
* @private
* */
function parseOption(initialOption, optionKey) {
return typeof initialOption === 'string'
? initialOption
: initialOption[optionKey]
}
/**
* @param {any} initialOption
* @param {string} optionKey
* @private
* */
function parseBooleanOption(initialOption, optionKey) {
if (typeof initialOption === 'string') {
if (initialOption === AvailablePaddingOptions.Always) return true
if (initialOption === AvailablePaddingOptions.Never) return false
}
return initialOption[optionKey]
}
/**
* @param {(Property | SpreadElement)} currentProperty
* @param {(Property | SpreadElement)} nextProperty
* @param {boolean} option
* @returns {boolean}
* @private
* */
function needGroupSingleLineProperties(currentProperty, nextProperty, option) {
const isSingleCurrentProperty =
currentProperty.loc.start.line === currentProperty.loc.end.line
const isSingleNextProperty =
nextProperty.loc.start.line === nextProperty.loc.end.line
return isSingleCurrentProperty && isSingleNextProperty && option
}
module.exports = {
meta: {
type: 'layout',
docs: {
description: 'require or disallow padding lines in component definition',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/padding-lines-in-component-definition.html'
},
fixable: 'whitespace',
schema: [
{
oneOf: [
{
enum: [
AvailablePaddingOptions.Always,
AvailablePaddingOptions.Never
]
},
{
type: 'object',
additionalProperties: false,
properties: {
[OptionKeys.BetweenOptions]: {
enum: Object.values(AvailablePaddingOptions)
},
[OptionKeys.WithinOption]: {
oneOf: [
{
enum: Object.values(AvailablePaddingOptions)
},
{
type: 'object',
patternProperties: {
'^[a-zA-Z]*$': {
oneOf: [
{
enum: Object.values(AvailablePaddingOptions)
},
{
type: 'object',
properties: {
[OptionKeys.BetweenItems]: {
enum: Object.values(AvailablePaddingOptions)
},
[OptionKeys.WithinEach]: {
enum: Object.values(AvailablePaddingOptions)
}
},
additionalProperties: false
}
]
}
},
minProperties: 1,
additionalProperties: false
}
]
},
[OptionKeys.GroupSingleLineProperties]: {
type: 'boolean'
}
}
}
]
}
],
messages: {
never: 'Unexpected blank line before this definition.',
always: 'Expected blank line before this definition.',
groupSingleLineProperties:
'Unexpected blank line between single line properties.'
}
},
/** @param {RuleContext} context */
create(context) {
const options = context.options[0] || AvailablePaddingOptions.Always
const sourceCode = context.getSourceCode()
/**
* @param {(Property | SpreadElement)} currentProperty
* @param {(Property | SpreadElement | Token)} nextProperty
* @param {RuleFixer} fixer
* */
function replaceLines(currentProperty, nextProperty, fixer) {
const commaToken = sourceCode.getTokenAfter(currentProperty, isComma)
const start = commaToken ? commaToken.range[1] : currentProperty.range[1]
const end = nextProperty.range[0]
const paddingText = sourceCode.text.slice(start, end)
const newText = `\n${splitLines(paddingText).pop()}`
return fixer.replaceTextRange([start, end], newText)
}
/**
* @param {(Property | SpreadElement)} currentProperty
* @param {(Property | SpreadElement | Token)} nextProperty
* @param {RuleFixer} fixer
* @param {number} betweenLinesRange
* */
function insertLines(
currentProperty,
nextProperty,
fixer,
betweenLinesRange
) {
const commaToken = sourceCode.getTokenAfter(currentProperty, isComma)
const lineBeforeNextProperty =
sourceCode.lines[nextProperty.loc.start.line - 1]
const lastSpaces = /** @type {RegExpExecArray} */ (
/^\s*/.exec(lineBeforeNextProperty)
)[0]
const newText = betweenLinesRange === 0 ? `\n\n${lastSpaces}` : '\n'
return fixer.insertTextAfter(commaToken || currentProperty, newText)
}
/**
* @param {(Property | SpreadElement)[]} properties
* @param {any} option
* @param {any} nextOption
* */
function verify(properties, option, nextOption) {
const groupSingleLineProperties = parseBooleanOption(
options,
OptionKeys.GroupSingleLineProperties
)
for (const [i, currentProperty] of properties.entries()) {
const nextProperty = properties[i + 1]
if (nextProperty && option !== AvailablePaddingOptions.Ignore) {
const tokenBeforeNext = sourceCode.getTokenBefore(nextProperty, {
includeComments: true
})
const isCommentBefore = isCommentToken(tokenBeforeNext)
const reportNode = isCommentBefore ? tokenBeforeNext : nextProperty
const betweenLinesRange =
reportNode.loc.start.line - currentProperty.loc.end.line
if (
needGroupSingleLineProperties(
currentProperty,
nextProperty,
groupSingleLineProperties
)
) {
if (betweenLinesRange > 1) {
context.report({
node: reportNode,
messageId: 'groupSingleLineProperties',
loc: reportNode.loc,
fix(fixer) {
return replaceLines(currentProperty, reportNode, fixer)
}
})
}
continue
}
if (
betweenLinesRange <= 1 &&
option === AvailablePaddingOptions.Always
) {
context.report({
node: reportNode,
messageId: 'always',
loc: reportNode.loc,
fix(fixer) {
return insertLines(
currentProperty,
reportNode,
fixer,
betweenLinesRange
)
}
})
} else if (
betweenLinesRange > 1 &&
option === AvailablePaddingOptions.Never
) {
context.report({
node: reportNode,
messageId: 'never',
loc: reportNode.loc,
fix(fixer) {
return replaceLines(currentProperty, reportNode, fixer)
}
})
}
}
if (!nextOption) return
const name = /** @type {GroupName | null} */ (
currentProperty.type === 'Property' &&
utils.getStaticPropertyName(currentProperty)
)
if (!name) continue
const propertyOption = parseOption(nextOption, name)
if (!propertyOption) continue
const nestedProperties =
currentProperty.type === 'Property' &&
currentProperty.value.type === 'ObjectExpression' &&
currentProperty.value.properties
if (!nestedProperties) continue
verify(
nestedProperties,
parseOption(propertyOption, OptionKeys.BetweenItems),
parseOption(propertyOption, OptionKeys.WithinEach)
)
}
}
return utils.compositingVisitors(
utils.defineVueVisitor(context, {
onVueObjectEnter(node) {
verify(
node.properties,
parseOption(options, OptionKeys.BetweenOptions),
parseOption(options, OptionKeys.WithinOption)
)
}
}),
utils.defineScriptSetupVisitor(context, {
onDefinePropsEnter(_, props) {
const propNodes = props
.filter(isValidProperties)
.map((prop) => prop.node)
const withinOption = parseOption(options, OptionKeys.WithinOption)
const propsOption = withinOption && parseOption(withinOption, 'props')
if (!propsOption) return
verify(
propNodes,
parseOption(propsOption, OptionKeys.BetweenItems),
parseOption(propsOption, OptionKeys.WithinEach)
)
},
onDefineEmitsEnter(_, emits) {
const emitNodes = emits
.filter(isValidProperties)
.map((emit) => emit.node)
const withinOption = parseOption(options, OptionKeys.WithinOption)
const emitsOption = withinOption && parseOption(withinOption, 'emits')
if (!emitsOption) return
verify(
emitNodes,
parseOption(emitsOption, OptionKeys.BetweenItems),
parseOption(emitsOption, OptionKeys.WithinEach)
)
},
onDefineOptionsEnter(node) {
if (node.arguments.length === 0) return
const define = node.arguments[0]
if (define.type !== 'ObjectExpression') return
verify(
define.properties,
parseOption(options, OptionKeys.BetweenOptions),
parseOption(options, OptionKeys.WithinOption)
)
}
})
)
}
}