@@ -9,53 +9,16 @@ const utils = require('../utils')
9
9
/**
10
10
* Get all comments that need to be reported
11
11
* @param {(HTMLComment | HTMLBogusComment | Comment)[] } comments
12
- * @param {VRootElement } element
12
+ * @param {Range[] } elementRanges
13
13
* @returns {(HTMLComment | HTMLBogusComment | Comment)[] }
14
14
*/
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
+ )
59
22
}
60
23
61
24
module . exports = {
@@ -103,16 +66,14 @@ module.exports = {
103
66
}
104
67
105
68
const comments = element . comments
69
+ const elementRanges = element . children . map ( ( child ) => child . range )
106
70
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
+ } )
116
77
}
117
78
}
118
79
0 commit comments