Skip to content

Commit 493820f

Browse files
committed
Extract rule: template-no-html-comments
1 parent 0149ef1 commit 493820f

4 files changed

Lines changed: 136 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ rules in templates can be disabled with eslint directives with mustache or html
199199
| [template-no-capital-arguments](docs/rules/template-no-capital-arguments.md) | disallow capital arguments (use lowercase @arg instead of @Arg) | | | |
200200
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
201201
| [template-no-element-event-actions](docs/rules/template-no-element-event-actions.md) | disallow element event actions (use {{on}} modifier instead) | | | |
202+
| [template-no-html-comments](docs/rules/template-no-html-comments.md) | disallow HTML comments in templates | | 🔧 | |
202203
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
203204

204205
### Components
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# ember/template-no-html-comments
2+
3+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4+
5+
<!-- end auto-generated rule header -->
6+
7+
Disallow HTML comments in templates. HTML comments will be visible in the rendered output, which may expose sensitive information or clutter the DOM.
8+
9+
## Rule Details
10+
11+
This rule disallows HTML comments (`<!-- -->`) in templates and suggests using Glimmer comments (`{{! }}` or `{{!-- --}}`) instead.
12+
13+
## Examples
14+
15+
Examples of **incorrect** code for this rule:
16+
17+
```gjs
18+
<template>
19+
<!-- This is an HTML comment -->
20+
<div>Content</div>
21+
</template>
22+
```
23+
24+
Examples of **correct** code for this rule:
25+
26+
```gjs
27+
<template>
28+
{{! This is a Glimmer comment }}
29+
<div>Content</div>
30+
</template>
31+
32+
<template>
33+
{{!-- This is a block Glimmer comment --}}
34+
<div>Content</div>
35+
</template>
36+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'disallow HTML comments in templates',
7+
category: 'Best Practices',
8+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-html-comments.md',
9+
},
10+
fixable: 'code',
11+
schema: [],
12+
messages: {
13+
noHtmlComments:
14+
'HTML comments should not be used in templates. Use Handlebars comments instead.',
15+
},
16+
strictGjs: true,
17+
strictGts: true,
18+
},
19+
20+
create(context) {
21+
return {
22+
'Program:exit'(node) {
23+
// Check for HTML comments in the source text
24+
const sourceCode = context.sourceCode || context.getSourceCode();
25+
const text = sourceCode.getText(node);
26+
27+
// Find all HTML comments in template blocks
28+
const htmlCommentRegex = /<!--([\S\s]*?)-->/g;
29+
let match;
30+
31+
while ((match = htmlCommentRegex.exec(text)) !== null) {
32+
// Check if this comment is within a template
33+
const beforeComment = text.slice(0, match.index);
34+
const templateStart = beforeComment.lastIndexOf('<template>');
35+
const templateEnd = text.indexOf('</template>', match.index);
36+
37+
if (templateStart !== -1 && templateEnd !== -1) {
38+
const commentContent = match[1];
39+
const startIndex = match.index;
40+
const endIndex = match.index + match[0].length;
41+
42+
context.report({
43+
loc: {
44+
start: sourceCode.getLocFromIndex(startIndex),
45+
end: sourceCode.getLocFromIndex(endIndex),
46+
},
47+
messageId: 'noHtmlComments',
48+
fix(fixer) {
49+
return fixer.replaceTextRange([startIndex, endIndex], `{{!${commentContent}}}`);
50+
},
51+
});
52+
}
53+
}
54+
},
55+
};
56+
},
57+
};
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
const rule = require('../../../lib/rules/template-no-html-comments');
6+
const RuleTester = require('eslint').RuleTester;
7+
8+
const ruleTester = new RuleTester({
9+
parser: require.resolve('ember-eslint-parser'),
10+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
11+
});
12+
13+
ruleTester.run('template-no-html-comments', rule, {
14+
valid: [
15+
'<template>{{! This is a comment }}</template>',
16+
'<template>{{!-- This is a comment --}}</template>',
17+
'<template><div>Content</div></template>',
18+
19+
// Test cases ported from ember-template-lint
20+
'<template>{{!-- comment here --}}</template>',
21+
'<template>{{!--comment here--}}</template>',
22+
],
23+
invalid: [
24+
{
25+
code: '<template><!-- HTML comment --></template>',
26+
output: '<template>{{! HTML comment }}</template>',
27+
errors: [{ messageId: 'noHtmlComments' }],
28+
},
29+
30+
// Test cases ported from ember-template-lint
31+
{
32+
code: '<template><!-- comment here --></template>',
33+
output: '<template>{{! comment here }}</template>',
34+
errors: [{ messageId: 'noHtmlComments' }],
35+
},
36+
{
37+
code: '<template><!--comment here--></template>',
38+
output: '<template>{{!comment here}}</template>',
39+
errors: [{ messageId: 'noHtmlComments' }],
40+
},
41+
],
42+
});

0 commit comments

Comments
 (0)