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-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
71 changes: 71 additions & 0 deletions docs/rules/template-simple-unless.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# ember/template-simple-unless

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

Require simple conditions in `{{#unless}}` blocks. Complex expressions should use `{{#if}}` with negation instead.

## Rule Details

This rule enforces using simple property paths in `{{#unless}}` blocks rather than complex helper expressions.

## Examples

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

```gjs
<template>
{{#unless (or (eq a 1) (gt b 2))}}
Complex condition
{{/unless}}
</template>
```

```gjs
<template>
{{#unless (and isAdmin (not isBanned))}}
Not allowed
{{/unless}}
</template>
```

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

```gjs
<template>
{{#unless isHidden}}
Visible
{{/unless}}
</template>
```

```gjs
<template>
{{#unless (eq value 1)}}
Not one
{{/unless}}
</template>
```

```gjs
<template>
{{#if (not (or a b))}}
Neither
{{/if}}
</template>
```

## Options

| Name | Type | Default | Description |
| ------------ | ---------- | ------- | --------------------------------------------------------------------------- |
| `allowlist` | `string[]` | `[]` | Helper names allowed inside `{{unless}}`. |
| `denylist` | `string[]` | `[]` | Helper names explicitly denied inside `{{unless}}`. |
| `maxHelpers` | `integer` | `1` | Maximum number of helpers allowed inside `{{unless}}` (`-1` for unlimited). |

## Related Rules

- [no-negated-condition](template-no-negated-condition.md)

## References

- [Wikipedia/boolean algebra](https://en.wikipedia.org/wiki/Boolean_algebra)
155 changes: 155 additions & 0 deletions lib/rules/template-simple-unless.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
function isUnless(node) {
return node.path?.type === 'GlimmerPathExpression' && node.path.original === 'unless';
}

function isIf(node) {
return node.path?.type === 'GlimmerPathExpression' && node.path.original === 'if';
}

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'require simple conditions in unless blocks',
category: 'Best Practices',
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-simple-unless.md',
templateMode: 'both',
},
schema: [
{
type: 'object',
properties: {
allowlist: { type: 'array', items: { type: 'string' } },
denylist: { type: 'array', items: { type: 'string' } },
maxHelpers: { type: 'integer' },
},
additionalProperties: false,
},
],
messages: {
followingElseBlock: 'Using an `else` block with `unless` should be avoided.',
asElseUnlessBlock: 'Using an `else unless` block should be avoided.',
withHelper: '{{message}}',
},
originallyFrom: {
name: 'ember-template-lint',
rule: 'lib/rules/simple-unless.js',
docs: 'docs/rule/simple-unless.md',
tests: 'test/unit/rules/simple-unless-test.js',
},
},

create(context) {
const options = context.options[0] || {};
const allowlist = options.allowlist || [];
const denylist = options.denylist || [];
const maxHelpers = options.maxHelpers === undefined ? 1 : options.maxHelpers;
const sourceCode = context.getSourceCode();

function isElseUnlessBlock(node) {
if (!node) {
return false;
}
if (node.path?.type === 'GlimmerPathExpression' && node.path.original === 'unless') {
const text = sourceCode.getText(node);
return text.startsWith('{{else ');
}
return false;
}

function checkWithHelper(node) {
let helperCount = 0;
let nextParams = node.params || [];

do {
const currentParams = nextParams;
nextParams = [];

for (const param of currentParams) {
if (param.type === 'GlimmerSubExpression') {
helperCount++;
const helperName = param.path?.original || '';

if (maxHelpers > -1 && helperCount > maxHelpers) {
context.report({
node: param,
messageId: 'withHelper',
data: {
message: `Using {{unless}} in combination with other helpers should be avoided. MaxHelpers: ${maxHelpers}`,
},
});
return;
}

if (allowlist.length > 0 && !allowlist.includes(helperName)) {
context.report({
node: param,
messageId: 'withHelper',
data: {
message: `Using {{unless}} in combination with other helpers should be avoided. Allowed helper${allowlist.length > 1 ? 's' : ''}: ${allowlist}`,
},
});
return;
}

if (denylist.length > 0 && denylist.includes(helperName)) {
context.report({
node: param,
messageId: 'withHelper',
data: {
message: `Using {{unless}} in combination with other helpers should be avoided. Restricted helper${denylist.length > 1 ? 's' : ''}: ${denylist}`,
},
});
return;
}

if (param.params) {
nextParams.push(...param.params);
}
}
}
} while (nextParams.some((p) => p.type === 'GlimmerSubExpression'));
}

return {
GlimmerMustacheStatement(node) {
if (node.path?.type === 'GlimmerPathExpression' && node.path.original === 'unless') {
if (node.params?.[0]?.path) {
checkWithHelper(node);
}
}
},

GlimmerBlockStatement(node) {
const nodeInverse = node.inverse;

if (nodeInverse && nodeInverse.body?.length > 0) {
if (isUnless(node)) {
// Check for {{#unless}}...{{else if}}
if (nodeInverse.body[0] && isIf(nodeInverse.body[0])) {
context.report({
node: node.program || node,
messageId: 'followingElseBlock',
});
} else {
// {{#unless}}...{{else}}
context.report({
node: node.program || node,
messageId: 'followingElseBlock',
});
}
} else if (isElseUnlessBlock(nodeInverse.body[0])) {
// {{#if}}...{{else unless}}
context.report({
node: nodeInverse.body[0],
messageId: 'asElseUnlessBlock',
});
}
} else if (isUnless(node) && node.params?.[0]?.path) {
checkWithHelper(node);
}
},
};
},
};
Loading
Loading