Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0cd3dff

Browse files
committed
refactor(no-multiple-template-root): optimize code
1 parentea93477 commit0cd3dff

File tree

2 files changed

+107
-42
lines changed

2 files changed

+107
-42
lines changed

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

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,58 @@
66

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

9+
/**
10+
* Get all comments that need to be reported
11+
*@param {(HTMLComment | HTMLBogusComment | Comment)[]} comments
12+
*@param {VRootElement} element
13+
*@returns {(HTMLComment | HTMLBogusComment | Comment)[]}
14+
*/
15+
functiongetReportComments(comments,element){
16+
constcommentRanges=comments.map((comment)=>comment.range)
17+
constelementRanges=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+
returncomments
24+
}
25+
26+
letcommentIndex=0
27+
letelementIndex=0
28+
29+
constneedReportComments=[]
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+
returnneedReportComments
59+
}
60+
961
module.exports={
1062
meta:{
1163
type:'problem',
@@ -52,48 +104,7 @@ module.exports = {
52104

53105
constcomments=element.comments
54106
if(disallowComments&&comments.length>0){
55-
constcommentRanges=comments.map((comment)=>comment.range)
56-
constelementRanges=element.children
57-
.filter((child)=>child.type==='VElement')
58-
.map((child)=>child.range)
59-
60-
letcommentIndex=0
61-
letelementIndex=0
62-
63-
constneedReportComments=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-
}
88-
89-
if(
90-
elementIndex<elementRanges.length-1&&
91-
commentStart>elementEnd
92-
){
93-
elementIndex+=1
94-
}
95-
}
96-
107+
constneedReportComments=getReportComments(comments,element)
97108
if(needReportComments.length>0){
98109
for(constcommentofneedReportComments){
99110
context.report({

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,60 @@ ruleTester.run('no-multiple-template-root', rule, {
203203
}
204204
]
205205
},
206+
{
207+
code:`
208+
<template>
209+
<!-- comments -->
210+
<div v-if="for">
211+
<!-- comments -->
212+
12333
213+
<span>
214+
<!-- comments -->
215+
12333
216+
</span>
217+
</div>
218+
<!-- comments -->
219+
<div v-else>
220+
<!-- comments -->
221+
12333
222+
</div>
223+
<!-- comments -->
224+
</template>
225+
`,
226+
options:[{disallowComments:true}],
227+
errors:[
228+
{
229+
message:'The template root disallows comments.',
230+
line:3
231+
},
232+
{
233+
message:'The template root disallows comments.',
234+
line:12
235+
},
236+
{
237+
message:'The template root disallows comments.',
238+
line:17
239+
}
240+
]
241+
},
242+
{
243+
code:`
244+
<template>
245+
<div>
246+
12333
247+
<!-- comments -->
248+
</div>
249+
<!-- comments -->
250+
</template>
251+
`,
252+
options:[{disallowComments:true}],
253+
errors:[
254+
{
255+
message:'The template root disallows comments.',
256+
line:7
257+
}
258+
]
259+
},
206260
{
207261
code:`
208262
<template>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp