Skip to content

Commit 8444483

Browse files
committed
Extract rule: template-no-html-comments
1 parent 484c3c6 commit 8444483

4 files changed

Lines changed: 142 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ rules in templates can be disabled with eslint directives with mustache or html
203203
| [template-no-chained-this](docs/rules/template-no-chained-this.md) | disallow redundant `this.this` in templates | | 🔧 | |
204204
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
205205
| [template-no-element-event-actions](docs/rules/template-no-element-event-actions.md) | disallow element event actions (use {{on}} modifier instead) | | | |
206+
| [template-no-html-comments](docs/rules/template-no-html-comments.md) | disallow HTML comments in templates | | 🔧 | |
206207
| [template-no-inline-event-handlers](docs/rules/template-no-inline-event-handlers.md) | disallow DOM event handler attributes | | | |
207208
| [template-no-inline-styles](docs/rules/template-no-inline-styles.md) | disallow inline styles | | | |
208209
| [template-no-input-placeholder](docs/rules/template-no-input-placeholder.md) | disallow placeholder attribute on input elements | | | |
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
originallyFrom: {
19+
name: 'ember-template-lint',
20+
rule: 'lib/rules/no-html-comments.js',
21+
docs: 'docs/rule/no-html-comments.md',
22+
tests: 'test/unit/rules/no-html-comments-test.js',
23+
},
24+
},
25+
26+
create(context) {
27+
return {
28+
'Program:exit'(node) {
29+
// Check for HTML comments in the source text
30+
const sourceCode = context.sourceCode || context.getSourceCode();
31+
const text = sourceCode.getText(node);
32+
33+
// Find all HTML comments in template blocks
34+
const htmlCommentRegex = /<!--([\S\s]*?)-->/g;
35+
let match;
36+
37+
while ((match = htmlCommentRegex.exec(text)) !== null) {
38+
// Check if this comment is within a template
39+
const beforeComment = text.slice(0, match.index);
40+
const templateStart = beforeComment.lastIndexOf('<template>');
41+
const templateEnd = text.indexOf('</template>', match.index);
42+
43+
if (templateStart !== -1 && templateEnd !== -1) {
44+
const commentContent = match[1];
45+
const startIndex = match.index;
46+
const endIndex = match.index + match[0].length;
47+
48+
context.report({
49+
loc: {
50+
start: sourceCode.getLocFromIndex(startIndex),
51+
end: sourceCode.getLocFromIndex(endIndex),
52+
},
53+
messageId: 'noHtmlComments',
54+
fix(fixer) {
55+
return fixer.replaceTextRange([startIndex, endIndex], `{{!${commentContent}}}`);
56+
},
57+
});
58+
}
59+
}
60+
},
61+
};
62+
},
63+
};
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)