Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.

Commit 0568091

Browse files
committed
Merge pull request #65 from bendemboski/HtmlCommentsRule
Implement html-comments rule
2 parents 81be85d + 83d4757 commit 0568091

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ The following values are valid configuration:
111111
* "tab" -- To indicate tab style indentation (1 char)
112112

113113

114+
#### html-comments
115+
116+
Html comments in your templates will get compiled and rendered into the DOM at runtime. Instead you can annotate your templates using Handlebars comments, which will be stripped out when the template is compiled and have no effect at runtime.
117+
118+
This rule forbids the following:
119+
120+
``` hbs
121+
<!-- comment goes here -->
122+
```
123+
124+
but allows the following:
125+
126+
```hbs
127+
{{!-- comment goes here --}}
128+
```
129+
130+
Html comments containing linting instructions such as:
131+
132+
```hbs
133+
<!-- template-lint bare-strings=false -->
134+
```
135+
136+
are of course allowed (and since the linter strips them during processing, they will not get compiled and rendered into the DOM regardless of this rule).
137+
138+
114139
#### triple-curlies
115140

116141
Usage of triple curly braces to allow raw HTML to be injected into the DOM is large vector for exploits of your application (especially when the raw HTML is user controllable ). Instead of using `{{{foo}}}`, you should use appropriate helpers or computed properties that return a `SafeString` (via `Ember.String.htmlSafe` generally) and ensure that user supplied data is properly escaped.

blueprints/ember-cli-template-lint/files/.template-lintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
module.exports = {
55
'bare-strings': ['(', ')', ',', '.', '&', '+', '-', '=', '*', '/', '#', '%', '!', '?', ':', '[', ']', '{', '}'],
66
'block-indentation': 2,
7+
'html-comments': true,
78
'triple-curlies': true
89
};

ext/plugins/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
module.exports = {
44
'bare-strings': require('./lint-bare-strings'),
55
'block-indentation': require('./lint-block-indentation'),
6+
'html-comments': require('./lint-html-comments'),
67
'triple-curlies': require('./lint-triple-curlies')
78
};

ext/plugins/lint-html-comments.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
var calculateLocationDisplay = require('../helpers/calculate-location-display');
4+
var buildPlugin = require('./base');
5+
6+
module.exports = function(addonContext) {
7+
var LogHtmlComments = buildPlugin(addonContext, 'html-comments');
8+
9+
LogHtmlComments.prototype.parseConfig = function(config) {
10+
var configType = typeof config;
11+
12+
var errorMessage = 'The html-comments rule accepts one of the following values.\n ' +
13+
' * boolean - `true` to enable / `false` to disable\n' +
14+
'\nYou specified `' + JSON.stringify(config) + '`';
15+
16+
switch (configType) {
17+
case 'boolean':
18+
return config;
19+
case 'undefined':
20+
return false;
21+
default:
22+
throw new Error(errorMessage);
23+
}
24+
};
25+
26+
LogHtmlComments.prototype.detect = function(node) {
27+
return node.type === 'CommentStatement';
28+
};
29+
30+
LogHtmlComments.prototype.process = function(node) {
31+
var location = calculateLocationDisplay(this.options.moduleName, node.loc && node.loc.start);
32+
this.log('Html comment detected `<!--' + node.value + '-->` at ' + location +
33+
'. Use Handlebars comment instead `{{!--' + node.value +'--}}`');
34+
};
35+
36+
return LogHtmlComments;
37+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
var generateRuleTests = require('../../helpers/rule-test-harness');
4+
5+
generateRuleTests({
6+
name: 'html-comments',
7+
8+
config: true,
9+
10+
good: [
11+
'{{!-- comment here --}}',
12+
'{{!--comment here--}}',
13+
'<!-- template-lint bare-strings=false -->'
14+
],
15+
16+
bad: [
17+
{
18+
template: '<!-- comment here -->',
19+
message: 'Html comment detected `<!-- comment here -->` at (\'layout.hbs\'). ' +
20+
'Use Handlebars comment instead `{{!-- comment here --}}`'
21+
},
22+
{
23+
template: '<!--comment here-->',
24+
message: 'Html comment detected `<!--comment here-->` at (\'layout.hbs\'). ' +
25+
'Use Handlebars comment instead `{{!--comment here--}}`'
26+
}
27+
]
28+
});

0 commit comments

Comments
 (0)