-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Use less-than instead of greater-than comparisons (`prefer-less-than`) | ||
|
||
Yes, this is the rule for [Ben Lesh comparisons](https://twitter.com/BenLesh/status/1397593619096166400). | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
if (x > a && x < b) { /* .. */ } | ||
``` | ||
|
||
```ts | ||
if (x >= a && x =< b) { /* .. */ } | ||
``` | ||
Examples of **correct** code for this rule: | ||
```ts | ||
if (a < x && x < b) { /* .. */ } | ||
``` | ||
```ts | ||
if (a <= x && x =< b) { /* .. */ } | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* @license Use of this source code is governed by an MIT-style license that | ||
* can be found in the LICENSE file at https://github.com/cartant/eslint-plugin-etc | ||
*/ | ||
|
||
import { | ||
TSESLint as eslint, | ||
TSESTree as es, | ||
} from "@typescript-eslint/experimental-utils"; | ||
import { ruleCreator } from "../utils"; | ||
|
||
const rule = ruleCreator({ | ||
defaultOptions: [], | ||
meta: { | ||
docs: { | ||
category: "Best Practices", | ||
description: "Forbids greater-than comparisons.", | ||
recommended: false, | ||
}, | ||
fixable: "code", | ||
messages: { | ||
forbiddenGT: "Greater-than comparisons are forbidden.", | ||
forbiddenGTE: "Greater-than-or-equal comparisons are forbidden.", | ||
suggestLT: "Use a less-than comparison instead.", | ||
suggestLTE: "Use a less-than-or-equal comparison instead.", | ||
}, | ||
schema: [], | ||
type: "suggestion", | ||
}, | ||
name: "prefer-less-than", | ||
create: (context) => { | ||
return { | ||
"BinaryExpression[operator=/^(>|>=)$/]": ( | ||
expression: es.BinaryExpression | ||
) => { | ||
const gte = expression.operator === ">="; | ||
function fix(fixer: eslint.RuleFixer) { | ||
const { left, right } = expression; | ||
const sourceCode = context.getSourceCode(); | ||
const operator = sourceCode.getTokenAfter(left); | ||
return operator | ||
? [ | ||
fixer.replaceText(left, sourceCode.getText(right)), | ||
fixer.replaceTextRange(operator.range, gte ? "<=" : "<"), | ||
fixer.replaceText(right, sourceCode.getText(left)), | ||
] | ||
: []; | ||
} | ||
context.report({ | ||
fix, | ||
messageId: gte ? "forbiddenGTE" : "forbiddenGT", | ||
node: expression, | ||
suggest: [ | ||
{ | ||
fix, | ||
messageId: gte ? "suggestLTE" : "suggestLT", | ||
}, | ||
], | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
export = rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* @license Use of this source code is governed by an MIT-style license that | ||
* can be found in the LICENSE file at https://github.com/cartant/eslint-plugin-etc | ||
*/ | ||
|
||
import { stripIndent } from "common-tags"; | ||
import { fromFixture } from "eslint-etc"; | ||
import rule = require("../../source/rules/prefer-less-than"); | ||
import { ruleTester } from "../utils"; | ||
|
||
ruleTester({ types: false }).run("prefer-less-than", rule, { | ||
valid: [ | ||
`const result = 42 < 54;`, | ||
`const result = 42 <= 54;`, | ||
`const result = 42 == 54;`, | ||
`const result = 42 === 54;`, | ||
`const result = 42 != 54;`, | ||
`const result = 42 !== 54;`, | ||
`if (a < x && x < b) { /* .. */ }`, | ||
`if (a <= x && x <= b) { /* .. */ }`, | ||
], | ||
invalid: [ | ||
fromFixture( | ||
stripIndent` | ||
const result = 54 > 42; | ||
~~~~~~~ [forbiddenGT] | ||
`, | ||
{ | ||
output: stripIndent` | ||
const result = 42 < 54; | ||
`, | ||
} | ||
), | ||
fromFixture( | ||
stripIndent` | ||
const result = 54 >= 42; | ||
~~~~~~~~ [forbiddenGTE] | ||
`, | ||
{ | ||
output: stripIndent` | ||
const result = 42 <= 54; | ||
`, | ||
} | ||
), | ||
fromFixture( | ||
stripIndent` | ||
const result = 54.0 > 42; | ||
~~~~~~~~~ [forbiddenGT] | ||
`, | ||
{ | ||
output: stripIndent` | ||
const result = 42 < 54.0; | ||
`, | ||
} | ||
), | ||
fromFixture( | ||
stripIndent` | ||
const result = 54.0 >= 42; | ||
~~~~~~~~~~ [forbiddenGTE] | ||
`, | ||
{ | ||
output: stripIndent` | ||
const result = 42 <= 54.0; | ||
`, | ||
} | ||
), | ||
fromFixture( | ||
stripIndent` | ||
const result = 54 > 42.0; | ||
~~~~~~~~~ [forbiddenGT] | ||
`, | ||
{ | ||
output: stripIndent` | ||
const result = 42.0 < 54; | ||
`, | ||
} | ||
), | ||
fromFixture( | ||
stripIndent` | ||
const result = 54 >= 42.0; | ||
~~~~~~~~~~ [forbiddenGTE] | ||
`, | ||
{ | ||
output: stripIndent` | ||
const result = 42.0 <= 54; | ||
`, | ||
} | ||
), | ||
fromFixture( | ||
stripIndent` | ||
if (x > a && x < b) { /* .. */ } | ||
~~~~~ [forbiddenGT] | ||
`, | ||
{ | ||
output: stripIndent` | ||
if (a < x && x < b) { /* .. */ } | ||
`, | ||
} | ||
), | ||
fromFixture( | ||
stripIndent` | ||
if (x >= a && x <= b) { /* .. */ } | ||
~~~~~~ [forbiddenGTE] | ||
`, | ||
{ | ||
output: stripIndent` | ||
if (a <= x && x <= b) { /* .. */ } | ||
`, | ||
} | ||
), | ||
], | ||
}); |