Skip to content

Commit 19c6e86

Browse files
authored
Enable vue/html-closing-bracket-* for top-level tags (#1883)
* Enable `vue/html-closing-bracket-*` for top-level tags * Switch to much simpler `defineDocumentVisitor` * Add type annotation again
1 parent 4daf4c8 commit 19c6e86

5 files changed

+263
-11
lines changed

lib/rules/block-tag-newline.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,6 @@ module.exports = {
347347

348348
const verify = normalizeOptionValue(context.options[0])
349349

350-
/**
351-
* @returns {VElement[]}
352-
*/
353-
function getTopLevelHTMLElements() {
354-
return documentFragment.children.filter(utils.isVElement)
355-
}
356-
357350
return utils.defineTemplateBodyVisitor(
358351
context,
359352
{},
@@ -364,8 +357,10 @@ module.exports = {
364357
return
365358
}
366359

367-
for (const element of getTopLevelHTMLElements()) {
368-
verify(element)
360+
for (const element of documentFragment.children) {
361+
if (utils.isVElement(element)) {
362+
verify(element)
363+
}
369364
}
370365
}
371366
}

lib/rules/html-closing-bracket-newline.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ module.exports = {
6868
context.parserServices.getTemplateBodyTokenStore &&
6969
context.parserServices.getTemplateBodyTokenStore()
7070

71-
return utils.defineTemplateBodyVisitor(context, {
71+
return utils.defineDocumentVisitor(context, {
7272
/** @param {VStartTag | VEndTag} node */
7373
'VStartTag, VEndTag'(node) {
7474
const closingBracketToken = template.getLastToken(node)

lib/rules/html-closing-bracket-spacing.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = {
9292
context.parserServices.getTemplateBodyTokenStore()
9393
const options = parseOptions(context.options[0], tokens)
9494

95-
return utils.defineTemplateBodyVisitor(context, {
95+
return utils.defineDocumentVisitor(context, {
9696
/** @param {VStartTag | VEndTag} node */
9797
'VStartTag, VEndTag'(node) {
9898
const type = options.detectType(node)

tests/lib/rules/html-closing-bracket-newline.js

+150
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,156 @@ tester.run('html-closing-bracket-newline', rule, {
337337
'Expected 1 line break before closing bracket, but no line breaks found.',
338338
'Expected 1 line break before closing bracket, but no line breaks found.'
339339
]
340+
},
341+
{
342+
code: `
343+
<template
344+
>
345+
</template
346+
>
347+
<script
348+
>
349+
</script
350+
>
351+
<style
352+
></style
353+
>
354+
`,
355+
output: `
356+
<template>
357+
</template>
358+
<script>
359+
</script>
360+
<style></style>
361+
`,
362+
options: [
363+
{
364+
singleline: 'never',
365+
multiline: 'never'
366+
}
367+
],
368+
errors: [
369+
{
370+
message:
371+
'Expected no line breaks before closing bracket, but 1 line break found.',
372+
line: 2,
373+
column: 18,
374+
endLine: 3,
375+
endColumn: 9
376+
},
377+
{
378+
message:
379+
'Expected no line breaks before closing bracket, but 1 line break found.',
380+
line: 4,
381+
column: 19,
382+
endLine: 5,
383+
endColumn: 9
384+
},
385+
{
386+
message:
387+
'Expected no line breaks before closing bracket, but 1 line break found.',
388+
line: 6,
389+
column: 16,
390+
endLine: 7,
391+
endColumn: 9
392+
},
393+
{
394+
message:
395+
'Expected no line breaks before closing bracket, but 1 line break found.',
396+
line: 8,
397+
column: 17,
398+
endLine: 9,
399+
endColumn: 9
400+
},
401+
{
402+
message:
403+
'Expected no line breaks before closing bracket, but 1 line break found.',
404+
line: 10,
405+
column: 15,
406+
endLine: 11,
407+
endColumn: 9
408+
},
409+
{
410+
message:
411+
'Expected no line breaks before closing bracket, but 1 line break found.',
412+
line: 11,
413+
column: 17,
414+
endLine: 12,
415+
endColumn: 9
416+
}
417+
]
418+
},
419+
{
420+
code: `
421+
<template>
422+
</template>
423+
<script>
424+
</script>
425+
<style></style>
426+
`,
427+
output: `
428+
<template
429+
>
430+
</template
431+
>
432+
<script
433+
>
434+
</script
435+
>
436+
<style
437+
></style
438+
>
439+
`,
440+
options: [
441+
{
442+
singleline: 'always',
443+
multiline: 'always'
444+
}
445+
],
446+
errors: [
447+
{
448+
message:
449+
'Expected 1 line break before closing bracket, but no line breaks found.',
450+
line: 2,
451+
column: 18,
452+
endColumn: 18
453+
},
454+
{
455+
message:
456+
'Expected 1 line break before closing bracket, but no line breaks found.',
457+
line: 3,
458+
column: 19,
459+
endColumn: 19
460+
},
461+
{
462+
message:
463+
'Expected 1 line break before closing bracket, but no line breaks found.',
464+
line: 4,
465+
column: 16,
466+
endColumn: 16
467+
},
468+
{
469+
message:
470+
'Expected 1 line break before closing bracket, but no line breaks found.',
471+
line: 5,
472+
column: 17,
473+
endColumn: 17
474+
},
475+
{
476+
message:
477+
'Expected 1 line break before closing bracket, but no line breaks found.',
478+
line: 6,
479+
column: 15,
480+
endColumn: 15
481+
},
482+
{
483+
message:
484+
'Expected 1 line break before closing bracket, but no line breaks found.',
485+
line: 6,
486+
column: 23,
487+
endColumn: 23
488+
}
489+
]
340490
}
341491
]
342492
})

tests/lib/rules/html-closing-bracket-spacing.js

+107
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,56 @@ ruleTester.run('html-closing-bracket-spacing', rule, {
114114
}
115115
]
116116
},
117+
{
118+
code: `
119+
<template ></template >
120+
<script ></script >
121+
<style ></style >
122+
`,
123+
output: `
124+
<template></template>
125+
<script></script>
126+
<style></style>
127+
`,
128+
errors: [
129+
{
130+
message: "Expected no space before '>', but found.",
131+
line: 2,
132+
column: 18,
133+
endColumn: 20
134+
},
135+
{
136+
message: "Expected no space before '>', but found.",
137+
line: 2,
138+
column: 30,
139+
endColumn: 32
140+
},
141+
{
142+
message: "Expected no space before '>', but found.",
143+
line: 3,
144+
column: 16,
145+
endColumn: 18
146+
},
147+
{
148+
message: "Expected no space before '>', but found.",
149+
line: 3,
150+
column: 26,
151+
endColumn: 28
152+
},
153+
{
154+
message: "Expected no space before '>', but found.",
155+
line: 4,
156+
column: 15,
157+
endColumn: 17
158+
},
159+
{
160+
message: "Expected no space before '>', but found.",
161+
line: 4,
162+
column: 24,
163+
endColumn: 26
164+
}
165+
]
166+
},
117167
{
118168
code: '<template >\n <div>\n </div>\n <div />\n</template >',
119169
output: '<template >\n <div >\n </div >\n <div/>\n</template >',
@@ -144,6 +194,63 @@ ruleTester.run('html-closing-bracket-spacing', rule, {
144194
endColumn: 10
145195
}
146196
]
197+
},
198+
{
199+
code: `
200+
<template></template>
201+
<script></script>
202+
<style></style>
203+
`,
204+
output: `
205+
<template ></template >
206+
<script ></script >
207+
<style ></style >
208+
`,
209+
options: [
210+
{
211+
startTag: 'always',
212+
endTag: 'always',
213+
selfClosingTag: 'never'
214+
}
215+
],
216+
errors: [
217+
{
218+
message: "Expected a space before '>', but not found.",
219+
line: 2,
220+
column: 18,
221+
endColumn: 19
222+
},
223+
{
224+
message: "Expected a space before '>', but not found.",
225+
line: 2,
226+
column: 29,
227+
endColumn: 30
228+
},
229+
{
230+
message: "Expected a space before '>', but not found.",
231+
line: 3,
232+
column: 16,
233+
endColumn: 17
234+
},
235+
{
236+
message: "Expected a space before '>', but not found.",
237+
line: 3,
238+
column: 25,
239+
endColumn: 26
240+
},
241+
{
242+
message: "Expected a space before '>', but not found.",
243+
line: 4,
244+
column: 15,
245+
endColumn: 16
246+
},
247+
{
248+
message: "Expected a space before '>', but not found.",
249+
line: 4,
250+
column: 23,
251+
endColumn: 24
252+
}
253+
]
147254
}
148255
]
149256
})

0 commit comments

Comments
 (0)