-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathblock-tag-newline.js
372 lines (355 loc) · 10.5 KB
/
block-tag-newline.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
/**
* @fileoverview Enforce line breaks style after opening and before closing block-level tags.
* @author Yosuke Ota
*/
'use strict'
const utils = require('../utils')
/**
* @typedef { 'always' | 'never' | 'consistent' | 'ignore' } OptionType
* @typedef { { singleline?: OptionType, multiline?: OptionType, maxEmptyLines?: number } } ContentsOptions
* @typedef { ContentsOptions & { blocks?: { [element: string]: ContentsOptions } } } Options
* @typedef { Required<ContentsOptions> } ArgsOptions
*/
/**
* @param {string} text Source code as a string.
* @returns {number}
*/
function getLinebreakCount(text) {
return text.split(/\r\n|[\r\n\u2028\u2029]/gu).length - 1
}
/**
* @param {number} lineBreaks
*/
function getPhrase(lineBreaks) {
switch (lineBreaks) {
case 1: {
return '1 line break'
}
default: {
return `${lineBreaks} line breaks`
}
}
}
const ENUM_OPTIONS = { enum: ['always', 'never', 'consistent', 'ignore'] }
module.exports = {
meta: {
type: 'layout',
docs: {
description:
'enforce line breaks after opening and before closing block-level tags',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/block-tag-newline.html'
},
fixable: 'whitespace',
schema: [
{
type: 'object',
properties: {
singleline: ENUM_OPTIONS,
multiline: ENUM_OPTIONS,
maxEmptyLines: { type: 'number', minimum: 0 },
blocks: {
type: 'object',
patternProperties: {
'^(?:\\S+)$': {
type: 'object',
properties: {
singleline: ENUM_OPTIONS,
multiline: ENUM_OPTIONS,
maxEmptyLines: { type: 'number', minimum: 0 }
},
additionalProperties: false
}
},
additionalProperties: false
}
},
additionalProperties: false
}
],
defaultOptions: [
{
singleline: 'consistent',
multiline: 'always',
maxEmptyLines: 0,
blocks: {}
}
],
messages: {
unexpectedOpeningLinebreak:
"There should be no line break after '<{{tag}}>'.",
expectedOpeningLinebreak:
"Expected {{expected}} after '<{{tag}}>', but {{actual}} found.",
expectedClosingLinebreak:
"Expected {{expected}} before '</{{tag}}>', but {{actual}} found.",
missingOpeningLinebreak: "A line break is required after '<{{tag}}>'.",
missingClosingLinebreak: "A line break is required before '</{{tag}}>'."
}
},
/** @param {RuleContext} context */
create(context) {
const sourceCode = context.getSourceCode()
const df =
sourceCode.parserServices.getDocumentFragment &&
sourceCode.parserServices.getDocumentFragment()
if (!df) {
return {}
}
/**
* @param {VStartTag} startTag
* @param {string} beforeText
* @param {number} beforeLinebreakCount
* @param {'always' | 'never'} beforeOption
* @param {number} maxEmptyLines
* @returns {void}
*/
function verifyBeforeSpaces(
startTag,
beforeText,
beforeLinebreakCount,
beforeOption,
maxEmptyLines
) {
if (beforeOption === 'always') {
if (beforeLinebreakCount === 0) {
context.report({
loc: {
start: startTag.loc.end,
end: startTag.loc.end
},
messageId: 'missingOpeningLinebreak',
data: { tag: startTag.parent.name },
fix(fixer) {
return fixer.insertTextAfter(startTag, '\n')
}
})
} else if (maxEmptyLines < beforeLinebreakCount - 1) {
context.report({
loc: {
start: startTag.loc.end,
end: sourceCode.getLocFromIndex(
startTag.range[1] + beforeText.length
)
},
messageId: 'expectedOpeningLinebreak',
data: {
tag: startTag.parent.name,
expected: getPhrase(maxEmptyLines + 1),
actual: getPhrase(beforeLinebreakCount)
},
fix(fixer) {
return fixer.replaceTextRange(
[startTag.range[1], startTag.range[1] + beforeText.length],
'\n'.repeat(maxEmptyLines + 1)
)
}
})
}
} else {
if (beforeLinebreakCount > 0) {
context.report({
loc: {
start: startTag.loc.end,
end: sourceCode.getLocFromIndex(
startTag.range[1] + beforeText.length
)
},
messageId: 'unexpectedOpeningLinebreak',
data: { tag: startTag.parent.name },
fix(fixer) {
return fixer.removeRange([
startTag.range[1],
startTag.range[1] + beforeText.length
])
}
})
}
}
}
/**
* @param {VEndTag} endTag
* @param {string} afterText
* @param {number} afterLinebreakCount
* @param {'always' | 'never'} afterOption
* @param {number} maxEmptyLines
* @returns {void}
*/
function verifyAfterSpaces(
endTag,
afterText,
afterLinebreakCount,
afterOption,
maxEmptyLines
) {
if (afterOption === 'always') {
if (afterLinebreakCount === 0) {
context.report({
loc: {
start: endTag.loc.start,
end: endTag.loc.start
},
messageId: 'missingClosingLinebreak',
data: { tag: endTag.parent.name },
fix(fixer) {
return fixer.insertTextBefore(endTag, '\n')
}
})
} else if (maxEmptyLines < afterLinebreakCount - 1) {
context.report({
loc: {
start: sourceCode.getLocFromIndex(
endTag.range[0] - afterText.length
),
end: endTag.loc.start
},
messageId: 'expectedClosingLinebreak',
data: {
tag: endTag.parent.name,
expected: getPhrase(maxEmptyLines + 1),
actual: getPhrase(afterLinebreakCount)
},
fix(fixer) {
return fixer.replaceTextRange(
[endTag.range[0] - afterText.length, endTag.range[0]],
'\n'.repeat(maxEmptyLines + 1)
)
}
})
}
} else {
if (afterLinebreakCount > 0) {
context.report({
loc: {
start: sourceCode.getLocFromIndex(
endTag.range[0] - afterText.length
),
end: endTag.loc.start
},
messageId: 'unexpectedOpeningLinebreak',
data: { tag: endTag.parent.name },
fix(fixer) {
return fixer.removeRange([
endTag.range[0] - afterText.length,
endTag.range[0]
])
}
})
}
}
}
/**
* @param {VElement} element
* @param {ArgsOptions} options
* @returns {void}
*/
function verifyElement(element, options) {
const { startTag, endTag } = element
if (startTag.selfClosing || endTag == null) {
return
}
const text = sourceCode.text.slice(startTag.range[1], endTag.range[0])
const trimText = text.trim()
if (!trimText) {
return
}
const option =
options.multiline !== options.singleline &&
/[\n\r\u2028\u2029]/u.test(text.trim())
? options.multiline
: options.singleline
if (option === 'ignore') {
return
}
const beforeText = /** @type {RegExpExecArray} */ (/^\s*/u.exec(text))[0]
const afterText = /** @type {RegExpExecArray} */ (/\s*$/u.exec(text))[0]
const beforeLinebreakCount = getLinebreakCount(beforeText)
const afterLinebreakCount = getLinebreakCount(afterText)
/** @type {'always' | 'never'} */
let beforeOption
/** @type {'always' | 'never'} */
let afterOption
if (option === 'always' || option === 'never') {
beforeOption = option
afterOption = option
} else {
// consistent
if (beforeLinebreakCount > 0 === afterLinebreakCount > 0) {
return
}
beforeOption = 'always'
afterOption = 'always'
}
verifyBeforeSpaces(
startTag,
beforeText,
beforeLinebreakCount,
beforeOption,
options.maxEmptyLines
)
verifyAfterSpaces(
endTag,
afterText,
afterLinebreakCount,
afterOption,
options.maxEmptyLines
)
}
/**
* Normalizes a given option value.
* @param { Options | undefined } option An option value to parse.
* @returns { (element: VElement) => void } Verify function.
*/
function normalizeOptionValue(option) {
if (!option) {
return normalizeOptionValue({})
}
/** @type {ContentsOptions} */
const contentsOptions = option
/** @type {ArgsOptions} */
const options = {
singleline: contentsOptions.singleline || 'consistent',
multiline: contentsOptions.multiline || 'always',
maxEmptyLines: contentsOptions.maxEmptyLines || 0
}
const { blocks } = option
if (!blocks) {
return (element) => verifyElement(element, options)
}
return (element) => {
const { name } = element
const elementsOptions = blocks[name]
if (elementsOptions) {
normalizeOptionValue({
singleline: elementsOptions.singleline || options.singleline,
multiline: elementsOptions.multiline || options.multiline,
maxEmptyLines:
elementsOptions.maxEmptyLines == null
? options.maxEmptyLines
: elementsOptions.maxEmptyLines
})(element)
} else {
verifyElement(element, options)
}
}
}
const documentFragment = df
const verify = normalizeOptionValue(context.options[0])
return utils.defineTemplateBodyVisitor(
context,
{},
{
/** @param {Program} node */
Program(node) {
if (utils.hasInvalidEOF(node)) {
return
}
for (const element of documentFragment.children) {
if (utils.isVElement(element)) {
verify(element)
}
}
}
}
)
}
}