From 6cd4acc218b3f10c4fa158a4e265ba381652d6ad Mon Sep 17 00:00:00 2001 From: Vida Xie Date: Sat, 22 Nov 2025 14:57:50 +0800 Subject: [PATCH 1/5] fix(no-multiple-template-root): skip comments outside `template` --- lib/rules/no-multiple-template-root.js | 9 ++++----- lib/utils/index.js | 5 +++-- tests/lib/rules/no-multiple-template-root.js | 11 ++++++++++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/rules/no-multiple-template-root.js b/lib/rules/no-multiple-template-root.js index 524b7a2b1..1d0a270d2 100644 --- a/lib/rules/no-multiple-template-root.js +++ b/lib/rules/no-multiple-template-root.js @@ -14,10 +14,7 @@ const utils = require('../utils') */ function getReportComments(comments, elementRanges) { return comments.filter( - (comment) => - !elementRanges.some( - (range) => range[0] <= comment.range[0] && comment.range[1] <= range[1] - ) + (comment) => !elementRanges.some((range) => utils.inRange(range, comment)) ) } @@ -65,7 +62,9 @@ module.exports = { return } - const comments = element.comments + const comments = element.comments.filter((comment) => + utils.inRange(comment, element) + ) const elementRanges = element.children.map((child) => child.range) if (disallowComments && comments.length > 0) { for (const comment of getReportComments(comments, elementRanges)) { diff --git a/lib/utils/index.js b/lib/utils/index.js index dabdc2ba3..e204da794 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -1969,10 +1969,11 @@ module.exports = { }, /** * Checks whether the target node is within the given range. - * @param { [number, number] } range + * @param { [number, number] | ASTNode | Token } rangeOrNode * @param {ASTNode | Token} target */ - inRange(range, target) { + inRange(rangeOrNode, target) { + const range = Array.isArray(rangeOrNode) ? rangeOrNode : rangeOrNode.range return range[0] <= target.range[0] && target.range[1] <= range[1] }, /** diff --git a/tests/lib/rules/no-multiple-template-root.js b/tests/lib/rules/no-multiple-template-root.js index 8df286804..52b601dbc 100644 --- a/tests/lib/rules/no-multiple-template-root.js +++ b/tests/lib/rules/no-multiple-template-root.js @@ -122,6 +122,15 @@ ruleTester.run('no-multiple-template-root', rule, { `, options: [{ disallowComments: true }] + }, + { + code: ` + + + `, + options: [{ disallowComments: true }] } ], invalid: [ @@ -371,7 +380,7 @@ ruleTester.run('no-multiple-template-root', rule, { { code: `