forked from vuejs/eslint-plugin-vue
/
padding-line-between-tags.js
232 lines (213 loc) · 6.73 KB
/
padding-line-between-tags.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
/**
* @author dev1437
* See LICENSE file in root directory for full license.
*/
'use strict'
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
const utils = require('../utils')
/**
* Split the source code into multiple lines based on the line delimiters.
* Copied from padding-line-between-blocks
* @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 {RuleContext} context
* @param {VElement} tag
* @param {VElement} sibling
* @param {number} lineDifference
*/
function insertNewLine(context, tag, sibling, lineDifference) {
const endTag = tag.endTag || tag.startTag
if (lineDifference === 1) {
context.report({
messageId: 'always',
loc: sibling.loc,
// @ts-ignore
fix(fixer) {
return fixer.insertTextAfter(tag, '\n')
}
})
} else if (lineDifference === 0) {
context.report({
messageId: 'always',
loc: sibling.loc,
// @ts-ignore
fix(fixer) {
const lastSpaces = /** @type {RegExpExecArray} */ (
/^\s*/.exec(context.getSourceCode().lines[endTag.loc.start.line - 1])
)[0]
return fixer.insertTextAfter(endTag, `\n\n${lastSpaces}`)
}
})
}
}
/**
* @param {RuleContext} context
* @param {VEndTag | VStartTag} endTag
* @param {VElement} sibling
* @param {number} lineDifference
*/
function removeExcessLines(context, endTag, sibling, lineDifference) {
if (lineDifference > 1) {
let hasOnlyTextBetween = true
for (
let i = endTag.loc.start.line;
i < sibling.loc.start.line - 1 && hasOnlyTextBetween;
i++
) {
hasOnlyTextBetween = !/^\s*$/.test(context.getSourceCode().lines[i])
}
if (!hasOnlyTextBetween) {
context.report({
messageId: 'never',
loc: sibling.loc,
// @ts-ignore
fix(fixer) {
const start = endTag.range[1]
const end = sibling.range[0]
const paddingText = context.getSourceCode().text.slice(start, end)
const textBetween = splitLines(paddingText)
let newTextBetween = `\n${textBetween.pop()}`
for (let i = textBetween.length - 1; i >= 0; i--) {
if (!/^\s*$/.test(textBetween[i])) {
newTextBetween = `${i === 0 ? '' : '\n'}${
textBetween[i]
}${newTextBetween}`
}
}
return fixer.replaceTextRange([start, end], `${newTextBetween}`)
}
})
}
}
}
/**
* @param {VElement} block
* @param {Array<{blankLine: "always" | "never" | "consistent", prev: string, next: string}>} configureList
*/
function hasConsistentConfiguration(block, configureList) {
for (let i = configureList.length - 1; i >= 0; --i) {
const configure = configureList[i];
if (configure.blankLine !== 'consistent' && configure.prev === "*" || configure.prev === block.name) {
return false;
}
}
return true
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/**
* @param {RuleContext} context
*/
function checkNewline(context) {
/** @type {Array<{blankLine: "always" | "never" | "consistent", prev: string, next: string}>} */
const configureList = context.options[0] || [
{ blankLine: 'always', prev: '*', next: '*' }
]
/**
* @param {VElement} block
*/
return (block) => {
if (!block.parent.parent) {
return
}
const endTag = block.endTag || block.startTag
const lowerSiblings = block.parent.children
.filter(
(element) =>
element.type === 'VElement' && element.range !== block.range
)
.filter((sibling) => sibling.range[0] - endTag.range[1] >= 0)
if (lowerSiblings.length === 0) {
return
}
const closestSibling = /** @type {VElement} */ (lowerSiblings[0])
for (let i = configureList.length - 1; i >= 0; --i) {
const configure = configureList[i]
const matched =
(configure.prev === '*' || block.name === configure.prev) &&
(configure.next === '*' || closestSibling.name === configure.next)
if (matched) {
const lineDifference =
closestSibling.loc.start.line - endTag.loc.end.line
if (configure.blankLine === 'always') {
insertNewLine(context, block, closestSibling, lineDifference)
} else if (configure.blankLine === 'consistent') {
let newlineCount = 0
let siblingLineDifference = 0
/** @type {VElement | null} */
let prevChild = null
const siblingElements = block.parent.children.filter(
(element) => element.type === 'VElement' && hasConsistentConfiguration(element, configureList)
)
// get parent
for (const child of siblingElements) {
if (!prevChild) {
prevChild = /** @type {VElement} */ (child)
continue
}
siblingLineDifference =
child.loc.start.line - prevChild.loc.end.line
if (siblingLineDifference > 1) {
newlineCount++
}
prevChild = /** @type {VElement} */ (child)
}
const ratio = newlineCount / (siblingElements.length - 1)
if (0.5 - ratio < 0.005) {
insertNewLine(context, block, closestSibling, lineDifference)
} else {
removeExcessLines(context, endTag, closestSibling, lineDifference)
}
} else {
removeExcessLines(context, endTag, closestSibling, lineDifference)
}
break
}
}
}
}
module.exports = {
meta: {
type: 'layout',
docs: {
description:
'require or disallow newlines between sibling tags in template',
categories: undefined,
url: 'https://eslint.vuejs.org/rules/padding-line-between-tags.html'
},
fixable: 'whitespace',
schema: [
{
type: 'array',
items: {
type: 'object',
properties: {
blankLine: { enum: ['always', 'never', 'consistent'] },
prev: { type: 'string' },
next: { type: 'string' }
},
additionalProperties: false,
required: ['blankLine', 'prev', 'next']
}
}
],
messages: {
never: 'Unexpected blank line before this tag.',
always: 'Expected blank line before this tag.'
}
},
/** @param {RuleContext} context */
create(context) {
return utils.defineTemplateBodyVisitor(context, {
VElement: checkNewline(context)
})
}
}