-
-
Notifications
You must be signed in to change notification settings - Fork 681
/
Copy pathsingleline-html-element-content-newline.js
212 lines (197 loc) · 5.93 KB
/
singleline-html-element-content-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
/**
* @author Yosuke Ota
* See LICENSE file in root directory for full license.
*/
'use strict'
const utils = require('../utils')
const casing = require('../utils/casing')
const INLINE_ELEMENTS = require('../utils/inline-non-void-elements.json')
/**
* @param {VElement & { endTag: VEndTag } } element
*/
function isSinglelineElement(element) {
return element.loc.start.line === element.endTag.loc.start.line
}
/**
* @param {any} options
*/
function parseOptions(options) {
return Object.assign(
{
ignores: ['pre', 'textarea', ...INLINE_ELEMENTS],
externalIgnores: [],
ignoreWhenNoAttributes: true,
ignoreWhenEmpty: true,
ignoreComments: false
},
options
)
}
/**
* Check whether the given element is empty or not.
* This ignores whitespaces, doesn't ignore comments.
* @param {VElement & { endTag: VEndTag } } node The element node to check.
* @param {SourceCode} sourceCode The source code object of the current context.
* @returns {boolean} `true` if the element is empty.
*/
function isEmpty(node, sourceCode) {
const start = node.startTag.range[1]
const end = node.endTag.range[0]
return sourceCode.text.slice(start, end).trim() === ''
}
module.exports = {
meta: {
type: 'layout',
docs: {
description:
'require a line break before and after the contents of a singleline element',
categories: ['vue3-strongly-recommended', 'vue2-strongly-recommended'],
url: 'https://eslint.vuejs.org/rules/singleline-html-element-content-newline.html'
},
fixable: 'whitespace',
schema: [
{
type: 'object',
properties: {
ignoreWhenNoAttributes: {
type: 'boolean'
},
ignoreWhenEmpty: {
type: 'boolean'
},
ignoreComments: {
type: 'boolean'
},
ignores: {
type: 'array',
items: { type: 'string' },
uniqueItems: true,
additionalItems: false
},
externalIgnores: {
type: 'array',
items: { type: 'string' },
uniqueItems: true,
additionalItems: false
}
},
additionalProperties: false
}
],
messages: {
unexpectedAfterClosingBracket:
'Expected 1 line break after opening tag (`<{{name}}>`), but no line breaks found.',
unexpectedBeforeOpeningBracket:
'Expected 1 line break before closing tag (`</{{name}}>`), but no line breaks found.'
}
},
/** @param {RuleContext} context */
create(context) {
const options = parseOptions(context.options[0])
const ignores = new Set([...options.ignores, ...options.externalIgnores])
const ignoreWhenNoAttributes = options.ignoreWhenNoAttributes
const ignoreWhenEmpty = options.ignoreWhenEmpty
const ignoreComments = options.ignoreComments
const sourceCode = context.getSourceCode()
const template =
sourceCode.parserServices.getTemplateBodyTokenStore &&
sourceCode.parserServices.getTemplateBodyTokenStore()
/** @type {VElement | null} */
let inIgnoreElement = null
/** @param {VElement} node */
function isIgnoredElement(node) {
return (
ignores.has(node.name) ||
ignores.has(casing.pascalCase(node.rawName)) ||
ignores.has(casing.kebabCase(node.rawName))
)
}
return utils.defineTemplateBodyVisitor(context, {
/** @param {VElement} node */
VElement(node) {
if (inIgnoreElement) {
return
}
if (isIgnoredElement(node)) {
// ignore element name
inIgnoreElement = node
return
}
if (node.startTag.selfClosing || !node.endTag) {
// self closing
return
}
const elem = /** @type {VElement & { endTag: VEndTag } } */ (node)
if (!isSinglelineElement(elem)) {
return
}
if (ignoreWhenNoAttributes && elem.startTag.attributes.length === 0) {
return
}
/** @type {SourceCode.CursorWithCountOptions} */
const getTokenOption = {
includeComments: !ignoreComments,
filter: (token) => token.type !== 'HTMLWhitespace'
}
if (
ignoreWhenEmpty &&
elem.children.length === 0 &&
template.getFirstTokensBetween(
elem.startTag,
elem.endTag,
getTokenOption
).length === 0
) {
return
}
const contentFirst = /** @type {Token} */ (
template.getTokenAfter(elem.startTag, getTokenOption)
)
const contentLast = /** @type {Token} */ (
template.getTokenBefore(elem.endTag, getTokenOption)
)
context.report({
node: template.getLastToken(elem.startTag),
loc: {
start: elem.startTag.loc.end,
end: contentFirst.loc.start
},
messageId: 'unexpectedAfterClosingBracket',
data: {
name: elem.rawName
},
fix(fixer) {
/** @type {Range} */
const range = [elem.startTag.range[1], contentFirst.range[0]]
return fixer.replaceTextRange(range, '\n')
}
})
if (isEmpty(elem, sourceCode)) {
return
}
context.report({
node: template.getFirstToken(elem.endTag),
loc: {
start: contentLast.loc.end,
end: elem.endTag.loc.start
},
messageId: 'unexpectedBeforeOpeningBracket',
data: {
name: elem.rawName
},
fix(fixer) {
/** @type {Range} */
const range = [contentLast.range[1], elem.endTag.range[0]]
return fixer.replaceTextRange(range, '\n')
}
})
},
/** @param {VElement} node */
'VElement:exit'(node) {
if (inIgnoreElement === node) {
inIgnoreElement = null
}
}
})
}
}