Skip to content

Commit 74e9d06

Browse files
committed
refactor(no-multiple-template-root): optimize code
1 parent ca99c1e commit 74e9d06

File tree

2 files changed

+35
-54
lines changed

2 files changed

+35
-54
lines changed

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

+15-54
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,16 @@ const utils = require('../utils')
99
/**
1010
* Get all comments that need to be reported
1111
* @param {(HTMLComment | HTMLBogusComment | Comment)[]} comments
12-
* @param {VRootElement} element
12+
* @param {Range[]} elementRanges
1313
* @returns {(HTMLComment | HTMLBogusComment | Comment)[]}
1414
*/
15-
function getReportComments(comments, element) {
16-
const commentRanges = comments.map((comment) => comment.range)
17-
const elementRanges = element.children
18-
.filter((child) => child.type === 'VElement')
19-
.map((child) => child.range)
20-
21-
// should return comment directly when no any elements
22-
if (elementRanges.length === 0) {
23-
return comments
24-
}
25-
26-
let commentIndex = 0
27-
let elementIndex = 0
28-
29-
const needReportComments = []
30-
while (commentIndex < commentRanges.length) {
31-
const [commentStart, commentEnd] = commentRanges[commentIndex]
32-
const [elementStart, elementEnd] = elementRanges[elementIndex]
33-
// if the comment is in the range of element, should skip
34-
if (commentStart > elementStart && commentEnd < elementEnd) {
35-
commentIndex += 1
36-
continue
37-
}
38-
39-
if (commentEnd < elementStart) {
40-
needReportComments.push(comments[commentIndex])
41-
commentIndex += 1
42-
}
43-
44-
// the element array has no any element, but comment still has some elements
45-
if (
46-
elementIndex === elementRanges.length - 1 &&
47-
commentStart > elementEnd
48-
) {
49-
needReportComments.push(comments[commentIndex])
50-
commentIndex += 1
51-
}
52-
53-
if (elementIndex < elementRanges.length - 1 && commentStart > elementEnd) {
54-
elementIndex += 1
55-
}
56-
}
57-
58-
return needReportComments
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+
)
5922
}
6023

6124
module.exports = {
@@ -103,16 +66,14 @@ module.exports = {
10366
}
10467

10568
const comments = element.comments
69+
const elementRanges = element.children.map((child) => child.range)
10670
if (disallowComments && comments.length > 0) {
107-
const needReportComments = getReportComments(comments, element)
108-
if (needReportComments.length > 0) {
109-
for (const comment of needReportComments) {
110-
context.report({
111-
node: comment,
112-
loc: comment.loc,
113-
messageId: 'commentRoot'
114-
})
115-
}
71+
for (const comment of getReportComments(comments, elementRanges)) {
72+
context.report({
73+
node: comment,
74+
loc: comment.loc,
75+
messageId: 'commentRoot'
76+
})
11677
}
11778
}
11879

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

+20
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,26 @@ ruleTester.run('no-multiple-template-root', rule, {
257257
}
258258
]
259259
},
260+
{
261+
code: `
262+
<template>
263+
<div />
264+
<!-- comments -->
265+
<div />
266+
</template>
267+
`,
268+
options: [{ disallowComments: true }],
269+
errors: [
270+
{
271+
message: 'The template root disallows comments.',
272+
line: 4
273+
},
274+
{
275+
message: 'The template root requires exactly one element.',
276+
line: 5
277+
}
278+
]
279+
},
260280
{
261281
code: `
262282
<template>

0 commit comments

Comments
 (0)