Skip to content

Commit 20ebc65

Browse files
committed
fix
1 parent 3782507 commit 20ebc65

2 files changed

Lines changed: 23 additions & 28 deletions

File tree

lib/rules/template-no-html-comments.js

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,34 @@ module.exports = {
2323
},
2424

2525
create(context) {
26+
const glimmerTemplateRanges = [];
27+
2628
return {
27-
'Program:exit'(node) {
28-
// Check for HTML comments in the source text
29+
GlimmerTemplate(node) {
30+
glimmerTemplateRanges.push(node.range);
31+
},
32+
33+
'Program:exit'() {
2934
const sourceCode = context.sourceCode || context.getSourceCode();
30-
const text = sourceCode.getText(node);
35+
const fullText = sourceCode.getText();
3136

32-
// Find all HTML comments in template blocks
33-
const htmlCommentRegex = /<!--([\S\s]*?)-->/g;
34-
let match;
37+
for (const comment of sourceCode.getAllComments()) {
38+
if (comment.type !== 'Block') {continue;}
3539

36-
const hasTemplateTag = text.includes('<template>');
40+
// getAllComments() returns both HTML (<!-- -->) and Handlebars ({{! }}, {{!-- --}})
41+
// comments as Block type — only flag actual HTML comments.
42+
if (!fullText.startsWith('<!--', comment.range[0])) {continue;}
3743

38-
while ((match = htmlCommentRegex.exec(text)) !== null) {
39-
let isInTemplate = false;
40-
41-
if (hasTemplateTag) {
42-
const beforeComment = text.slice(0, match.index);
43-
const templateStart = beforeComment.lastIndexOf('<template>');
44-
const templateEnd = text.indexOf('</template>', match.index);
45-
isInTemplate = templateStart !== -1 && templateEnd !== -1;
46-
} else {
47-
// Standalone .hbs file — entire file is template content
48-
isInTemplate = true;
49-
}
44+
const isInTemplate = glimmerTemplateRanges.some(
45+
([start, end]) => comment.range[0] >= start && comment.range[1] <= end
46+
);
5047

5148
if (isInTemplate) {
52-
const commentContent = match[1];
53-
const startIndex = match.index;
54-
const endIndex = match.index + match[0].length;
55-
5649
context.report({
57-
loc: {
58-
start: sourceCode.getLocFromIndex(startIndex),
59-
end: sourceCode.getLocFromIndex(endIndex),
60-
},
50+
loc: comment.loc,
6151
messageId: 'noHtmlComments',
6252
fix(fixer) {
63-
return fixer.replaceTextRange([startIndex, endIndex], `{{!${commentContent}}}`);
53+
return fixer.replaceTextRange(comment.range, `{{!${comment.value}}}`);
6454
},
6555
});
6656
}

tests/lib/rules/template-no-html-comments.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ ruleTester.run('template-no-html-comments', rule, {
3636
output: '<template>{{!comment here}}</template>',
3737
errors: [{ messageId: 'noHtmlComments' }],
3838
},
39+
{
40+
code: '<template><!--\n line one\n line two\n --></template>',
41+
output: '<template>{{!\n line one\n line two\n }}</template>',
42+
errors: [{ messageId: 'noHtmlComments' }],
43+
},
3944
],
4045
});
4146

0 commit comments

Comments
 (0)