Skip to content

Commit 64efd92

Browse files
Merge pull request #2482 from NullVoxPopuli/nvp/template-lint-extract-rule-template-no-negated-comparison
Extract rule: template-no-negated-comparison
2 parents 24deb76 + 883ae4e commit 64efd92

4 files changed

Lines changed: 162 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ rules in templates can be disabled with eslint directives with mustache or html
210210
| [template-no-input-placeholder](docs/rules/template-no-input-placeholder.md) | disallow placeholder attribute on input elements | | | |
211211
| [template-no-input-tagname](docs/rules/template-no-input-tagname.md) | disallow tagName attribute on {{input}} helper | | | |
212212
| [template-no-log](docs/rules/template-no-log.md) | disallow {{log}} in templates | | | |
213+
| [template-no-negated-comparison](docs/rules/template-no-negated-comparison.md) | disallow negated comparisons in templates | | | |
213214
| [template-no-negated-condition](docs/rules/template-no-negated-condition.md) | disallow negated conditions in if/unless | | | |
214215
| [template-no-nested-splattributes](docs/rules/template-no-nested-splattributes.md) | disallow nested ...attributes usage | | | |
215216
| [template-no-obscure-array-access](docs/rules/template-no-obscure-array-access.md) | disallow obscure array access patterns like `objectPath.@each.property` | | | |
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# ember/template-no-negated-comparison
2+
3+
<!-- end auto-generated rule header -->
4+
5+
Disallows negated comparison operators in templates.
6+
7+
## Rule Details
8+
9+
Use positive comparison operators with `{{unless}}` instead of negated comparison operators like `not-eq` or `ne`.
10+
11+
## Examples
12+
13+
Examples of **incorrect** code for this rule:
14+
15+
```gjs
16+
<template>
17+
{{#if (not-eq this.value 5)}}
18+
Not equal
19+
{{/if}}
20+
</template>
21+
```
22+
23+
```gjs
24+
<template>
25+
{{#if (ne this.a this.b)}}
26+
Not equal
27+
{{/if}}
28+
</template>
29+
```
30+
31+
Examples of **correct** code for this rule:
32+
33+
```gjs
34+
<template>
35+
{{#unless (eq this.value 5)}}
36+
Not equal
37+
{{/unless}}
38+
</template>
39+
```
40+
41+
```gjs
42+
<template>
43+
{{#if (eq this.value 5)}}
44+
Equal
45+
{{/if}}
46+
</template>
47+
```
48+
49+
## References
50+
51+
- [eslint-plugin-ember template-no-negated-condition](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/template-no-negated-condition.md)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/** @type {import('eslint').Rule.RuleModule} */
2+
module.exports = {
3+
meta: {
4+
type: 'suggestion',
5+
docs: {
6+
description: 'disallow negated comparisons in templates',
7+
category: 'Best Practices',
8+
url: 'https://github.com/ember-cli/eslint-plugin-ember/tree/master/docs/rules/template-no-negated-comparison.md',
9+
templateMode: 'both',
10+
},
11+
fixable: null,
12+
schema: [],
13+
messages: {
14+
noNegatedComparison: 'Use positive comparison operators instead of negated ones.',
15+
},
16+
},
17+
18+
create(context) {
19+
return {
20+
GlimmerSubExpression(node) {
21+
if (
22+
node.path &&
23+
node.path.type === 'GlimmerPathExpression' &&
24+
(node.path.original === 'not-eq' || node.path.original === 'ne')
25+
) {
26+
context.report({
27+
node,
28+
messageId: 'noNegatedComparison',
29+
});
30+
}
31+
},
32+
};
33+
},
34+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//------------------------------------------------------------------------------
2+
// Requirements
3+
//------------------------------------------------------------------------------
4+
5+
const rule = require('../../../lib/rules/template-no-negated-comparison');
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-negated-comparison', rule, {
18+
valid: [
19+
`<template>
20+
{{#if (eq this.value 5)}}
21+
Equal
22+
{{/if}}
23+
</template>`,
24+
`<template>
25+
{{#unless (eq this.value 5)}}
26+
Not equal
27+
{{/unless}}
28+
</template>`,
29+
`<template>
30+
<div></div>
31+
</template>`,
32+
],
33+
34+
invalid: [
35+
{
36+
code: `<template>
37+
{{#if (not-eq this.value 5)}}
38+
Not equal
39+
{{/if}}
40+
</template>`,
41+
output: null,
42+
errors: [
43+
{
44+
message: 'Use positive comparison operators instead of negated ones.',
45+
type: 'GlimmerSubExpression',
46+
},
47+
],
48+
},
49+
{
50+
code: `<template>
51+
{{#if (ne this.a this.b)}}
52+
Not equal
53+
{{/if}}
54+
</template>`,
55+
output: null,
56+
errors: [
57+
{
58+
message: 'Use positive comparison operators instead of negated ones.',
59+
type: 'GlimmerSubExpression',
60+
},
61+
],
62+
},
63+
{
64+
code: `<template>
65+
<div class={{if (not-eq this.state "active") "inactive"}}></div>
66+
</template>`,
67+
output: null,
68+
errors: [
69+
{
70+
message: 'Use positive comparison operators instead of negated ones.',
71+
type: 'GlimmerSubExpression',
72+
},
73+
],
74+
},
75+
],
76+
});

0 commit comments

Comments
 (0)