|
6 | 6 |
|
7 | 7 | const utils = require('../utils')
|
8 | 8 |
|
| 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 | +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 |
| 59 | +} |
| 60 | + |
9 | 61 | module.exports = {
|
10 | 62 | meta: {
|
11 | 63 | type: 'problem',
|
@@ -52,48 +104,7 @@ module.exports = {
|
52 | 104 |
|
53 | 105 | const comments = element.comments
|
54 | 106 | if (disallowComments && comments.length > 0) {
|
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 | | - } |
88 | | - |
89 | | - if ( |
90 | | - elementIndex < elementRanges.length - 1 && |
91 | | - commentStart > elementEnd |
92 | | - ) { |
93 | | - elementIndex += 1 |
94 | | - } |
95 | | - } |
96 | | - |
| 107 | + const needReportComments = getReportComments(comments, element) |
97 | 108 | if (needReportComments.length > 0) {
|
98 | 109 | for (const comment of needReportComments) {
|
99 | 110 | context.report({
|
|
0 commit comments