Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ rules in templates can be disabled with eslint directives with mustache or html
🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

### Best Practices

| Name | Description | 💼 | 🔧 | 💡 |
| :----------------------------------------------- | :---------------------------- | :- | :- | :- |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |

### Components

| Name                         | Description | 💼 | 🔧 | 💡 |
Expand Down
58 changes: 58 additions & 0 deletions docs/rules/template-no-log.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# ember/template-no-log

<!-- end auto-generated rule header -->

Disallows usage of `{{log}}` in templates.

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.

## Rule Details

This rule disallows the use of `{{log}}` statements in templates.

## Examples

Examples of **incorrect** code for this rule:

```gjs
<template>
{{log "debug message"}}
<div>Content</div>
</template>
```

```gjs
<template>
{{#if condition}}
{{log this.value}}
{{/if}}
</template>
```

Examples of **correct** code for this rule:

```gjs
<template>
<div>Content</div>
</template>
```

```gjs
<template>
{{this.log}}
</template>
```

```gjs
<template>
{{logger "info"}}
</template>
```

## Related Rules

- [no-console](https://eslint.org/docs/rules/no-console) from ESLint

## References

- [ember-template-lint no-log](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/no-log.md)
37 changes: 37 additions & 0 deletions lib/rules/template-no-log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow {{log}} in templates',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-log.md',
},
fixable: null,
schema: [],
messages: {
unexpected: 'Unexpected log statement in template.',
},
},

create(context) {
function checkForLog(node) {
if (node.path && node.path.type === 'GlimmerPathExpression' && node.path.original === 'log') {
context.report({
node,
messageId: 'unexpected',
});
}
}

return {
GlimmerMustacheStatement(node) {
checkForLog(node);
},

GlimmerBlockStatement(node) {
checkForLog(node);
},
};
},
};
3 changes: 2 additions & 1 deletion scripts/update-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function generate(filename, filter) {
* In order to update its content based on rules'
* definitions, execute "npm run update"
*/
module.exports = ${JSON.stringify(recommendedRules, null, 2)}`;
module.exports = ${JSON.stringify(recommendedRules, null, 2)};
`;

fs.writeFileSync(recommendedRulesFile, recommendedRulesContent);
}
Expand Down
75 changes: 75 additions & 0 deletions tests/lib/rules/template-no-log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require('../../../lib/rules/template-no-log');
const RuleTester = require('eslint').RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester({
parser: require.resolve('ember-eslint-parser'),
parserOptions: { ecmaVersion: 2022, sourceType: 'module' },
});

ruleTester.run('template-no-log', rule, {
valid: [
`<template>
<div>Hello World</div>
</template>`,
`<template>
{{this.log}}
</template>`,
`<template>
{{logger}}
</template>`,
`<template>
<div data-test-log={{true}}></div>
</template>`,
],

invalid: [
{
code: `<template>
{{log "debug message"}}
</template>`,
output: null,
errors: [
{
message: 'Unexpected log statement in template.',
type: 'GlimmerMustacheStatement',
},
],
},
{
code: `<template>
{{#if condition}}
{{log this.value}}
{{/if}}
</template>`,
output: null,
errors: [
{
message: 'Unexpected log statement in template.',
type: 'GlimmerMustacheStatement',
},
],
},
{
code: `<template>
{{#log "test"}}
content
{{/log}}
</template>`,
output: null,
errors: [
{
message: 'Unexpected log statement in template.',
type: 'GlimmerBlockStatement',
},
],
},
],
});
Loading