Skip to content

Commit 5372ac4

Browse files
Merge pull request #2381 from ember-cli/copilot/split-lint-rule-example
Port template-no-log rule from PR #2371
2 parents f45316e + c3e7682 commit 5372ac4

File tree

5 files changed

+178
-1
lines changed

5 files changed

+178
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ rules in templates can be disabled with eslint directives with mustache or html
174174
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
175175
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
176176

177+
### Best Practices
178+
179+
| Name | Description | 💼 | 🔧 | 💡 |
180+
| :----------------------------------------------- | :---------------------------- | :- | :- | :- |
181+
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
182+
177183
### Components
178184

179185
| Name                         | Description | 💼 | 🔧 | 💡 |

docs/rules/template-no-log.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# ember/template-no-log
2+
3+
<!-- end auto-generated rule header -->
4+
5+
Disallows usage of `{{log}}` in templates.
6+
7+
The `{{log}}` helper is useful for debugging but should not be present in production code. Use proper logging libraries or console statements in JavaScript code instead.
8+
9+
## Rule Details
10+
11+
This rule disallows the use of `{{log}}` statements in templates.
12+
13+
## Examples
14+
15+
Examples of **incorrect** code for this rule:
16+
17+
```gjs
18+
<template>
19+
{{log "debug message"}}
20+
<div>Content</div>
21+
</template>
22+
```
23+
24+
```gjs
25+
<template>
26+
{{#if condition}}
27+
{{log this.value}}
28+
{{/if}}
29+
</template>
30+
```
31+
32+
Examples of **correct** code for this rule:
33+
34+
```gjs
35+
<template>
36+
<div>Content</div>
37+
</template>
38+
```
39+
40+
```gjs
41+
<template>
42+
{{this.log}}
43+
</template>
44+
```
45+
46+
```gjs
47+
<template>
48+
{{logger "info"}}
49+
</template>
50+
```
51+
52+
## Related Rules
53+
54+
- [no-console](https://eslint.org/docs/rules/no-console) from ESLint
55+
56+
## References
57+
58+
- [ember-template-lint no-log](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/no-log.md)

lib/rules/template-no-log.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'problem',
5+
docs: {
6+
description: 'disallow {{log}} in templates',
7+
category: 'Best Practices',
8+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-log.md',
9+
},
10+
fixable: null,
11+
schema: [],
12+
messages: {
13+
unexpected: 'Unexpected log statement in template.',
14+
},
15+
},
16+
17+
create(context) {
18+
function checkForLog(node) {
19+
if (node.path && node.path.type === 'GlimmerPathExpression' && node.path.original === 'log') {
20+
context.report({
21+
node,
22+
messageId: 'unexpected',
23+
});
24+
}
25+
}
26+
27+
return {
28+
GlimmerMustacheStatement(node) {
29+
checkForLog(node);
30+
},
31+
32+
GlimmerBlockStatement(node) {
33+
checkForLog(node);
34+
},
35+
};
36+
},
37+
};

scripts/update-rules.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ function generate(filename, filter) {
4242
* In order to update its content based on rules'
4343
* definitions, execute "npm run update"
4444
*/
45-
module.exports = ${JSON.stringify(recommendedRules, null, 2)}`;
45+
module.exports = ${JSON.stringify(recommendedRules, null, 2)};
46+
`;
4647

4748
fs.writeFileSync(recommendedRulesFile, recommendedRulesContent);
4849
}

tests/lib/rules/template-no-log.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
const rule = require('../../../lib/rules/template-no-log');
6+
const RuleTester = require('eslint').RuleTester;
7+
8+
//------------------------------------------------------------------------------
9+
// Tests
10+
//------------------------------------------------------------------------------
11+
12+
const ruleTester = new RuleTester({
13+
parser: require.resolve('ember-eslint-parser'),
14+
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
15+
});
16+
17+
ruleTester.run('template-no-log', rule, {
18+
valid: [
19+
`<template>
20+
<div>Hello World</div>
21+
</template>`,
22+
`<template>
23+
{{this.log}}
24+
</template>`,
25+
`<template>
26+
{{logger}}
27+
</template>`,
28+
`<template>
29+
<div data-test-log={{true}}></div>
30+
</template>`,
31+
],
32+
33+
invalid: [
34+
{
35+
code: `<template>
36+
{{log "debug message"}}
37+
</template>`,
38+
output: null,
39+
errors: [
40+
{
41+
message: 'Unexpected log statement in template.',
42+
type: 'GlimmerMustacheStatement',
43+
},
44+
],
45+
},
46+
{
47+
code: `<template>
48+
{{#if condition}}
49+
{{log this.value}}
50+
{{/if}}
51+
</template>`,
52+
output: null,
53+
errors: [
54+
{
55+
message: 'Unexpected log statement in template.',
56+
type: 'GlimmerMustacheStatement',
57+
},
58+
],
59+
},
60+
{
61+
code: `<template>
62+
{{#log "test"}}
63+
content
64+
{{/log}}
65+
</template>`,
66+
output: null,
67+
errors: [
68+
{
69+
message: 'Unexpected log statement in template.',
70+
type: 'GlimmerBlockStatement',
71+
},
72+
],
73+
},
74+
],
75+
});

0 commit comments

Comments
 (0)