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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ rules in templates can be disabled with eslint directives with mustache or html
| [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 | | | |
| [template-simple-modifiers](docs/rules/template-simple-modifiers.md) | require simple modifier syntax | | | |
| [template-simple-unless](docs/rules/template-simple-unless.md) | require simple conditions in unless blocks | | | |
| [template-splat-attributes-only](docs/rules/template-splat-attributes-only.md) | disallow ...spread other than ...attributes | | | |
| [template-style-concatenation](docs/rules/template-style-concatenation.md) | disallow string concatenation in inline styles | | | |
Expand Down
60 changes: 60 additions & 0 deletions docs/rules/template-simple-modifiers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# ember/template-simple-modifiers

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

Require simple modifier syntax.

This rule strongly advises against passing complex statements or conditionals to the
first argument of the `{{modifier}}` helper. Instead, the first argument should be
either:

- a string literal such as `{{(modifier "track-interaction")}}`
- a path expression such as `{{(modifier this.trackInteraction)}}`

A common issue this rule catches is declaring the modifier name conditionally, which
works because `modifier` ignores `null` and `undefined`, but makes the intent much
harder to read. Prefer placing the conditional around the modifier invocation instead.

## Examples

This rule **forbids** the following:

```gjs
<template>
<div
{{(modifier
(unless this.hasBeenClicked 'track-interaction')
'click'
customizeData=this.customizeClickData
)}}
></div>
</template>
```

```gjs
<template>
<div {{(modifier)}}></div>
</template>
```

This rule **allows** the following:

```gjs
<template>
<div
{{(unless
this.hasBeenClicked
(modifier 'track-interaction' 'click' customizeData=this.customizeClickData)
)}}
></div>
</template>
```

## Why?

Using complex expressions as the modifier name reduces readability and makes it harder
to understand which modifier is being applied.

## References

- [Ember.js Guides - Modifiers](https://guides.emberjs.com/release/components/template-lifecycle-dom-and-modifiers/#toc_event-handlers)
62 changes: 62 additions & 0 deletions lib/rules/template-simple-modifiers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function isModifierHelper(node) {
return node.path?.original === 'modifier';
}

function isValidFirstParam(node) {
return (
node.type === 'GlimmerStringLiteral' || (node.type === 'GlimmerPathExpression' && node.original)
);
}

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require simple modifier syntax',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-simple-modifiers.md',
templateMode: 'both',
},
fixable: null,
schema: [],
messages: {
invalidFirstArgument:
'The modifier helper should have a string or a variable name containing the modifier name as a first argument.',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/simple-modifiers.js',
docs: 'docs/rule/simple-modifiers.md',
tests: 'test/unit/rules/simple-modifiers-test.js',
},
},

create(context) {
return {
// This catches {{(modifier ...)}} expressions
GlimmerSubExpression(node) {
if (!isModifierHelper(node)) {
return;
}

const firstParam = node.params?.[0];

if (!firstParam) {
context.report({
node,
messageId: 'invalidFirstArgument',
});
return;
}

if (!isValidFirstParam(firstParam)) {
context.report({
node: firstParam,
messageId: 'invalidFirstArgument',
});
}
},
};
},
};
89 changes: 89 additions & 0 deletions tests/lib/rules/template-simple-modifiers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const rule = require('../../../lib/rules/template-simple-modifiers');
const RuleTester = require('eslint').RuleTester;

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

ruleTester.run('template-simple-modifiers', rule, {
valid: [
'<template><div {{(modifier "track-interaction" @controlName)}}></div></template>',
'<template><div {{(modifier trackInteraction @controlName)}}></div></template>',
'<template><div {{(modifier this.trackInteraction @controlName)}}></div></template>',
'<template><div {{my-modifier}}></div></template>',

'<template><div {{(modifier @trackInteraction @controlName)}}></div></template>',
'<template><div {{(if @isActionVisible (modifier "track-interaction" eventName=myEventName eventBody=myEventbody))}}></div></template>',
'<template><div {{(my-modifier (unless this.hasBeenClicked "track-interaction") "click" customizeData=this.customizeClickData)}}></div></template>',
'<template><MyComponent @people={{array "Tom Dale" "Yehuda Katz" this.myOtherPerson}} /></template>',
'<template><div {{(if this.foo (modifier "foo-bar"))}}></div></template>',
],
invalid: [
{
code: '<template><div {{(modifier)}}></div></template>',
output: null,
errors: [
{
message:
'The modifier helper should have a string or a variable name containing the modifier name as a first argument.',
},
],
},

{
code: '<template><div {{(modifier (unless this.hasBeenClicked "track-interaction") "click" customizeData=this.customizeClickData)}}></div></template>',
output: null,
errors: [
{
message:
'The modifier helper should have a string or a variable name containing the modifier name as a first argument.',
},
],
},
],
});

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

hbsRuleTester.run('template-simple-modifiers', rule, {
valid: [
'<div {{(modifier "track-interaction" @controlName)}}></div>',
'<div {{(modifier trackInteraction @controlName)}}></div>',
'<div {{(modifier this.trackInteraction @controlName)}}></div>',
'<div {{(modifier @trackInteraction @controlName)}}></div>',
'<div {{(if @isActionVisible (modifier "track-interaction" eventName=myEventName eventBody=myEventbody))}}></div>',
'<div {{(my-modifier (unless this.hasBeenClicked "track-interaction") "click" customizeData=this.customizeClickData)}}></div>',
'<div {{my-modifier}}></div>',
'<MyComponent @people={{array "Tom Dale" "Yehuda Katz" this.myOtherPerson}} />',
'<div {{(if this.foo (modifier "foo-bar"))}}></div>',
],
invalid: [
{
code: '<div {{(modifier (unless this.hasBeenClicked "track-interaction") "click" customizeData=this.customizeClickData)}}></div>',
output: null,
errors: [
{
message:
'The modifier helper should have a string or a variable name containing the modifier name as a first argument.',
},
],
},
{
code: '<div {{(modifier)}}></div>',
output: null,
errors: [
{
message:
'The modifier helper should have a string or a variable name containing the modifier name as a first argument.',
},
],
},
],
});
Loading