forked from vuejs/eslint-plugin-vue
/
padding-line-between-tags.js
193 lines (176 loc) · 5.15 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
/**
* @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
*/
function insertNewLine(context, tag, sibling) {
context.report({
messageId: 'always',
loc: sibling.loc,
// @ts-ignore
fix(fixer) {
return fixer.insertTextAfter(tag, '\n')
}
})
}
/**
* @param {RuleContext} context
* @param {VEndTag | VStartTag} endTag
* @param {VElement} sibling
*/
function removeExcessLines(context, endTag, sibling) {
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 lastSpaces = splitLines(paddingText).pop()
return fixer.replaceTextRange([start, end], `\n${lastSpaces}`)
}
})
}
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
/**
* @param {RuleContext} context
*/
function checkNewline(context) {
/** @type {Array<{blankLine: "always" | "never", prev: string, next: string}>} */
const configureList = context.options[0] || [
{ blankLine: 'always', prev: '*', next: '*' }
]
// Copied from padding-line-between-tags
if (!context.parserServices.getDocumentFragment) {
return
}
const df = context.parserServices.getDocumentFragment()
if (!df) {
return
}
const documentFragment = df
/**
* @returns {VElement[]}
*/
function getTopLevelHTMLElements() {
return documentFragment.children.filter(utils.isVElement)
}
/**
* @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') {
if (lineDifference === 1) {
insertNewLine(context, block, closestSibling)
} else if (lineDifference === 0) {
const templateTag = getTopLevelHTMLElements().find(
(element) => element.name === 'template'
)
context.report({
messageId: 'always',
loc: closestSibling.loc,
// @ts-ignore
fix(fixer) {
if (!templateTag) {
return
}
const start = templateTag.range[0]
const end = endTag.range[0]
const paddingText = context
.getSourceCode()
.text.slice(start, end)
const lastSpaces = splitLines(paddingText).pop()
return fixer.insertTextAfter(endTag, `\n\n${lastSpaces}`)
}
})
}
} else {
if (lineDifference > 1) {
removeExcessLines(context, endTag, closestSibling)
}
}
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'] },
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)
})
}
}