Skip to content

Commit d121473

Browse files
authored
fix(no-multiple-template-root): skip comments outside template (#2964)
1 parent 8f85dd4 commit d121473

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

lib/rules/no-multiple-template-root.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@
66

77
const utils = require('../utils')
88

9-
/**
10-
* Get all comments that need to be reported
11-
* @param {(HTMLComment | HTMLBogusComment | Comment)[]} comments
12-
* @param {Range[]} elementRanges
13-
* @returns {(HTMLComment | HTMLBogusComment | Comment)[]}
14-
*/
15-
function getReportComments(comments, elementRanges) {
16-
return comments.filter(
17-
(comment) =>
18-
!elementRanges.some(
19-
(range) => range[0] <= comment.range[0] && comment.range[1] <= range[1]
20-
)
21-
)
22-
}
23-
249
module.exports = {
2510
meta: {
2611
type: 'problem',
@@ -65,10 +50,13 @@ module.exports = {
6550
return
6651
}
6752

68-
const comments = element.comments
69-
const elementRanges = element.children.map((child) => child.range)
70-
if (disallowComments && comments.length > 0) {
71-
for (const comment of getReportComments(comments, elementRanges)) {
53+
const reportComments = element.comments.filter(
54+
(comment) =>
55+
utils.inRange(element, comment) &&
56+
!element.children.some((child) => utils.inRange(child, comment))
57+
)
58+
if (disallowComments) {
59+
for (const comment of reportComments) {
7260
context.report({
7361
node: comment,
7462
loc: comment.loc,

lib/utils/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1969,10 +1969,11 @@ module.exports = {
19691969
},
19701970
/**
19711971
* Checks whether the target node is within the given range.
1972-
* @param { [number, number] } range
1972+
* @param { [number, number] | ASTNode | Token } rangeOrNode
19731973
* @param {ASTNode | Token} target
19741974
*/
1975-
inRange(range, target) {
1975+
inRange(rangeOrNode, target) {
1976+
const range = Array.isArray(rangeOrNode) ? rangeOrNode : rangeOrNode.range
19761977
return range[0] <= target.range[0] && target.range[1] <= range[1]
19771978
},
19781979
/**

tests/lib/rules/no-multiple-template-root.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ ruleTester.run('no-multiple-template-root', rule, {
122122
</template>
123123
`,
124124
options: [{ disallowComments: true }]
125+
},
126+
127+
// https://github.com/vuejs/eslint-plugin-vue/issues/2948
128+
{
129+
code: `
130+
<!-- comment -->
131+
<template>
132+
<div>abc</div>
133+
</template>
134+
`,
135+
options: [{ disallowComments: true }]
125136
}
126137
],
127138
invalid: [
@@ -371,7 +382,7 @@ ruleTester.run('no-multiple-template-root', rule, {
371382
{
372383
code: `
373384
<template>
374-
<!-- When you have a comment in the root of your template in vue 3,
385+
<!-- When you have a comment in the root of your template in vue 3,
375386
using $el will point to the first text comment instead of the actual DOM element. -->
376387
<div>
377388
12333

0 commit comments

Comments
 (0)