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
61 changes: 31 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,36 +197,37 @@ rules in templates can be disabled with eslint directives with mustache or html

### Best Practices

| Name                                          | Description | 💼 | 🔧 | 💡 |
| :----------------------------------------------------------------------------------------------------------- | :---------------------------------------------------------------------- | :- | :- | :- |
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
| [template-no-action-modifiers](docs/rules/template-no-action-modifiers.md) | disallow usage of {{action}} modifiers | | | |
| [template-no-arguments-for-html-elements](docs/rules/template-no-arguments-for-html-elements.md) | disallow @arguments on HTML elements | | | |
| [template-no-array-prototype-extensions](docs/rules/template-no-array-prototype-extensions.md) | disallow usage of Ember Array prototype extensions | | | |
| [template-no-block-params-for-html-elements](docs/rules/template-no-block-params-for-html-elements.md) | disallow block params on HTML elements | | | |
| [template-no-capital-arguments](docs/rules/template-no-capital-arguments.md) | disallow capital arguments (use lowercase @arg instead of @Arg) | | | |
| [template-no-chained-this](docs/rules/template-no-chained-this.md) | disallow redundant `this.this` in templates | | 🔧 | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-dynamic-subexpression-invocations](docs/rules/template-no-dynamic-subexpression-invocations.md) | disallow dynamic subexpression invocations | | | |
| [template-no-element-event-actions](docs/rules/template-no-element-event-actions.md) | disallow element event actions (use {{on}} modifier instead) | | | |
| [template-no-implicit-this](docs/rules/template-no-implicit-this.md) | require explicit `this` in property access | | | |
| [template-no-inline-event-handlers](docs/rules/template-no-inline-event-handlers.md) | disallow DOM event handler attributes | | | |
| [template-no-inline-styles](docs/rules/template-no-inline-styles.md) | disallow inline styles | | | |
| [template-no-input-block](docs/rules/template-no-input-block.md) | disallow block usage of {{input}} helper | | | |
| [template-no-input-placeholder](docs/rules/template-no-input-placeholder.md) | disallow placeholder attribute on input elements | | | |
| [template-no-input-tagname](docs/rules/template-no-input-tagname.md) | disallow tagName attribute on {{input}} helper | | | |
| [template-no-invalid-meta](docs/rules/template-no-invalid-meta.md) | disallow invalid meta tags | | | |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
| [template-no-model-argument-in-route-templates](docs/rules/template-no-model-argument-in-route-templates.md) | disallow @model argument in route templates | | 🔧 | |
| [template-no-multiple-empty-lines](docs/rules/template-no-multiple-empty-lines.md) | disallow multiple consecutive empty lines in templates | | | |
| [template-no-mut-helper](docs/rules/template-no-mut-helper.md) | disallow usage of (mut) helper | | | |
| [template-no-negated-comparison](docs/rules/template-no-negated-comparison.md) | disallow negated comparisons in templates | | | |
| [template-no-negated-condition](docs/rules/template-no-negated-condition.md) | disallow negated conditions in if/unless | | | |
| [template-no-nested-splattributes](docs/rules/template-no-nested-splattributes.md) | disallow nested ...attributes usage | | | |
| [template-no-obscure-array-access](docs/rules/template-no-obscure-array-access.md) | disallow obscure array access patterns like `[email protected]` | | | |
| [template-no-obsolete-elements](docs/rules/template-no-obsolete-elements.md) | disallow obsolete HTML elements | | | |
| [template-no-outlet-outside-routes](docs/rules/template-no-outlet-outside-routes.md) | disallow {{outlet}} outside of route templates | | | |
| [template-no-page-title-component](docs/rules/template-no-page-title-component.md) | disallow usage of ember-page-title component | | | |
| Name                                          | Description | 💼 | 🔧 | 💡 |
| :----------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------- | :- | :- | :- |
| [template-builtin-component-arguments](docs/rules/template-builtin-component-arguments.md) | disallow setting certain attributes on builtin components | | | |
| [template-no-action-modifiers](docs/rules/template-no-action-modifiers.md) | disallow usage of {{action}} modifiers | | | |
| [template-no-arguments-for-html-elements](docs/rules/template-no-arguments-for-html-elements.md) | disallow @arguments on HTML elements | | | |
| [template-no-array-prototype-extensions](docs/rules/template-no-array-prototype-extensions.md) | disallow usage of Ember Array prototype extensions | | | |
| [template-no-block-params-for-html-elements](docs/rules/template-no-block-params-for-html-elements.md) | disallow block params on HTML elements | | | |
| [template-no-capital-arguments](docs/rules/template-no-capital-arguments.md) | disallow capital arguments (use lowercase @arg instead of @Arg) | | | |
| [template-no-chained-this](docs/rules/template-no-chained-this.md) | disallow redundant `this.this` in templates | | 🔧 | |
| [template-no-class-bindings](docs/rules/template-no-class-bindings.md) | disallow passing classBinding or classNameBindings as arguments in templates | | | |
| [template-no-debugger](docs/rules/template-no-debugger.md) | disallow {{debugger}} in templates | | | |
| [template-no-dynamic-subexpression-invocations](docs/rules/template-no-dynamic-subexpression-invocations.md) | disallow dynamic subexpression invocations | | | |
| [template-no-element-event-actions](docs/rules/template-no-element-event-actions.md) | disallow element event actions (use {{on}} modifier instead) | | | |
| [template-no-implicit-this](docs/rules/template-no-implicit-this.md) | require explicit `this` in property access | | | |
| [template-no-inline-event-handlers](docs/rules/template-no-inline-event-handlers.md) | disallow DOM event handler attributes | | | |
| [template-no-inline-styles](docs/rules/template-no-inline-styles.md) | disallow inline styles | | | |
| [template-no-input-block](docs/rules/template-no-input-block.md) | disallow block usage of {{input}} helper | | | |
| [template-no-input-placeholder](docs/rules/template-no-input-placeholder.md) | disallow placeholder attribute on input elements | | | |
| [template-no-input-tagname](docs/rules/template-no-input-tagname.md) | disallow tagName attribute on {{input}} helper | | | |
| [template-no-invalid-meta](docs/rules/template-no-invalid-meta.md) | disallow invalid meta tags | | | |
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
| [template-no-model-argument-in-route-templates](docs/rules/template-no-model-argument-in-route-templates.md) | disallow @model argument in route templates | | 🔧 | |
| [template-no-multiple-empty-lines](docs/rules/template-no-multiple-empty-lines.md) | disallow multiple consecutive empty lines in templates | | | |
| [template-no-mut-helper](docs/rules/template-no-mut-helper.md) | disallow usage of (mut) helper | | | |
| [template-no-negated-comparison](docs/rules/template-no-negated-comparison.md) | disallow negated comparisons in templates | | | |
| [template-no-negated-condition](docs/rules/template-no-negated-condition.md) | disallow negated conditions in if/unless | | | |
| [template-no-nested-splattributes](docs/rules/template-no-nested-splattributes.md) | disallow nested ...attributes usage | | | |
| [template-no-obscure-array-access](docs/rules/template-no-obscure-array-access.md) | disallow obscure array access patterns like `[email protected]` | | | |
| [template-no-obsolete-elements](docs/rules/template-no-obsolete-elements.md) | disallow obsolete HTML elements | | | |
| [template-no-outlet-outside-routes](docs/rules/template-no-outlet-outside-routes.md) | disallow {{outlet}} outside of route templates | | | |
| [template-no-page-title-component](docs/rules/template-no-page-title-component.md) | disallow usage of ember-page-title component | | | |

### Components

Expand Down
41 changes: 41 additions & 0 deletions docs/rules/template-no-class-bindings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ember/template-no-class-bindings

> **HBS Only**: This rule applies to classic `.hbs` template files only (loose mode). It is not relevant for `gjs`/`gts` files (strict mode), where these patterns cannot occur.

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

Disallow passing `classBinding` or `classNameBindings` as arguments within templates. These are legacy Ember Classic patterns that should be replaced with modern approaches.

## Examples

This rule **forbids** the following:

```hbs
<SomeThing @classBinding='isActive:active' />
```

```hbs
{{some-thing classNameBindings='isActive:active:inactive'}}
```

```hbs
<SomeThing @classNameBindings='isActive:active:inactive' />
```

This rule **allows** the following:

```hbs
<SomeThing class={{if this.isActive 'active'}} />
```

```hbs
<SomeThing />
```

## Migration

- find in templates and remove `classBinding` and/or `classNameBindings`.

## References

- [ember-template-lint no-class-bindings](https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/no-class-bindings.md)
56 changes: 56 additions & 0 deletions lib/rules/template-no-class-bindings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'disallow passing classBinding or classNameBindings as arguments in templates',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-class-bindings.md',
templateMode: 'loose',
},
fixable: null,
schema: [],
messages: {
noClassBindings:
'Passing the `{{name}}` property as an argument within templates is not allowed.',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/no-class-bindings.js',
docs: 'docs/rule/no-class-bindings.md',
tests: 'test/unit/rules/no-class-bindings-test.js',
},
},

create(context) {
const FORBIDDEN_ATTR_NAMES = new Set([
'classBinding',
'@classBinding',
'classNameBindings',
'@classNameBindings',
]);

const FORBIDDEN_HASH_KEYS = new Set(['classBinding', 'classNameBindings']);

return {
'GlimmerElementNode > GlimmerAttrNode'(node) {
if (FORBIDDEN_ATTR_NAMES.has(node.name)) {
context.report({
node,
messageId: 'noClassBindings',
data: { name: node.name },
});
}
},
GlimmerHashPair(node) {
if (FORBIDDEN_HASH_KEYS.has(node.key)) {
context.report({
node,
messageId: 'noClassBindings',
data: { name: node.key },
});
}
},
};
},
};
113 changes: 113 additions & 0 deletions tests/lib/rules/template-no-class-bindings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const rule = require('../../../lib/rules/template-no-class-bindings');
const RuleTester = require('eslint').RuleTester;

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

ruleTester.run('template-no-class-bindings', rule, {
valid: [
'<template><SomeThing /></template>',
'<template>{{lol-wat}}</template>',
'<template>{{true}}</template>',
'<template>{{"hehe"}}</template>',
'<template><div class="foo"></div></template>',
],
invalid: [
{
code: '<template>{{some-thing classBinding="lol:wat"}}</template>',
output: null,
errors: [
{
messageId: 'noClassBindings',
data: { name: 'classBinding' },
},
],
},
{
code: '<template><SomeThing @classBinding="lol:wat" /></template>',
output: null,
errors: [
{
messageId: 'noClassBindings',
data: { name: '@classBinding' },
},
],
},
{
code: '<template>{{some-thing classNameBindings="lol:foo:bar"}}</template>',
output: null,
errors: [
{
messageId: 'noClassBindings',
data: { name: 'classNameBindings' },
},
],
},
{
code: '<template><SomeThing @classNameBindings="lol:foo:bar" /></template>',
output: null,
errors: [
{
messageId: 'noClassBindings',
data: { name: '@classNameBindings' },
},
],
},
],
});

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

hbsRuleTester.run('template-no-class-bindings', rule, {
valid: ['<SomeThing />', '{{lol-wat}}', '{{true}}', '{{"hehe"}}'],
invalid: [
{
code: '{{some-thing classBinding="lol:wat"}}',
output: null,
errors: [
{
message:
'Passing the `classBinding` property as an argument within templates is not allowed.',
},
],
},
{
code: '<SomeThing @classBinding="lol:wat" />',
output: null,
errors: [
{
message:
'Passing the `@classBinding` property as an argument within templates is not allowed.',
},
],
},
{
code: '{{some-thing classNameBindings="lol:foo:bar"}}',
output: null,
errors: [
{
message:
'Passing the `classNameBindings` property as an argument within templates is not allowed.',
},
],
},
{
code: '<SomeThing @classNameBindings="lol:foo:bar" />',
output: null,
errors: [
{
message:
'Passing the `@classNameBindings` property as an argument within templates is not allowed.',
},
],
},
],
});
Loading