Skip to content

Commit ea93477

Browse files
committed
refactor(no-multiple-template-root): optimize code
1 parent 2741924 commit ea93477

File tree

2 files changed

+78
-17
lines changed

2 files changed

+78
-17
lines changed

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

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,55 @@ module.exports = {
5050
return
5151
}
5252

53-
const commentRangesMap = new Map()
5453
const comments = element.comments
5554
if (disallowComments && comments.length > 0) {
56-
for (const comment of comments) {
57-
const [start, end] = comment.range
58-
commentRangesMap.set(`${start}-${end}`, comment)
59-
}
55+
const commentRanges = comments.map((comment) => comment.range)
56+
const elementRanges = element.children
57+
.filter((child) => child.type === 'VElement')
58+
.map((child) => child.range)
59+
60+
let commentIndex = 0
61+
let elementIndex = 0
62+
63+
const needReportComments = elementRanges.length === 0 ? comments : []
64+
while (
65+
commentIndex < commentRanges.length &&
66+
elementRanges.length > 0
67+
) {
68+
const [commentStart, commentEnd] = commentRanges[commentIndex]
69+
const [elementStart, elementEnd] = elementRanges[elementIndex]
70+
if (commentStart > elementStart && commentEnd < elementEnd) {
71+
commentIndex += 1
72+
continue
73+
}
74+
75+
if (commentEnd < elementStart) {
76+
needReportComments.push(comments[commentIndex])
77+
commentIndex += 1
78+
}
79+
80+
// the element array has no any element, but comment still has some elements
81+
if (
82+
elementIndex === elementRanges.length - 1 &&
83+
commentStart > elementEnd
84+
) {
85+
needReportComments.push(comments[commentIndex])
86+
commentIndex += 1
87+
}
6088

61-
for (const child of element.children) {
62-
if (child.type === 'VElement') {
63-
for (const range of commentRangesMap.keys()) {
64-
const ranges = range.split('-')
65-
if (ranges[0] > child.range[0] && ranges[1] < child.range[1]) {
66-
commentRangesMap.delete(range)
67-
}
68-
}
89+
if (
90+
elementIndex < elementRanges.length - 1 &&
91+
commentStart > elementEnd
92+
) {
93+
elementIndex += 1
6994
}
7095
}
7196

72-
if (commentRangesMap.size > 0) {
73-
for (const node of commentRangesMap.values()) {
97+
if (needReportComments.length > 0) {
98+
for (const comment of needReportComments) {
7499
context.report({
75-
node,
76-
loc: node.loc,
100+
node: comment,
101+
loc: comment.loc,
77102
messageId: 'commentRoot'
78103
})
79104
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,42 @@ ruleTester.run('no-multiple-template-root', rule, {
8686
</template>
8787
`,
8888
options: [{ disallowComments: false }]
89+
},
90+
{
91+
filename: 'test.vue',
92+
code: `
93+
<template>
94+
<div>
95+
<!-- comments -->
96+
12333
97+
<span>
98+
<!-- comments -->
99+
12333
100+
</span>
101+
</div>
102+
</template>
103+
`,
104+
options: [{ disallowComments: true }]
105+
},
106+
{
107+
filename: 'test.vue',
108+
code: `
109+
<template>
110+
<div v-if="for">
111+
<!-- comments -->
112+
12333
113+
<span>
114+
<!-- comments -->
115+
12333
116+
</span>
117+
</div>
118+
<div v-else>
119+
<!-- comments -->
120+
12333
121+
</div>
122+
</template>
123+
`,
124+
options: [{ disallowComments: true }]
89125
}
90126
],
91127
invalid: [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy