Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rule no-unused-expressions (#272)
This rule extends the ESLint's no-unused-expressions to ignore type cast expressions. ESLint's no-unused-expressions catches some likely mistakes, but it also complains about bare type cast expressions. These are unlikely to be mistakes and are useful in several situations.
- Loading branch information
Showing
6 changed files
with
76 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,34 @@ | ||
### `no-unused-expressions` | ||
|
||
An extension of [ESLint's `no-unused-expressions`](https://eslint.org/docs/rules/no-unused-expressions). | ||
This rule ignores type cast expressions, but otherwise behaves the same as ESLint's | ||
`no-unused-expressions`. | ||
|
||
Bare type casts are useful, for example to assert the exhaustiveness of a `switch`: | ||
|
||
```js | ||
type Action | ||
= { type: 'FOO', doFoo: (_: number) => void } | ||
| { type: 'BAR', doBar: (_: string) => void }; | ||
|
||
type State = { foo: number, bar: string }; | ||
|
||
function runFooBar(action: Action, state: State): void { | ||
switch (action.type) { | ||
case 'FOO': | ||
doFoo(state.foo); | ||
break; | ||
case 'BAR': | ||
doBar(state.bar); | ||
break; | ||
default: | ||
(action: empty); // type error when `Action` is extended with new types | ||
console.error(`Impossible action: ${action.toString()}`); | ||
} | ||
} | ||
``` | ||
|
||
This rule takes the same arguments as ESLint's `no-unused-expressions`. See | ||
[that rule's documentation](https://eslint.org/docs/rules/no-unused-expressions) for details. | ||
|
||
<!-- assertions noUnusedExpressions --> |
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,23 @@ | ||
// A wrapper around ESLint's core rule no-unused-expressions, additionally ignores type cast | ||
// expressions. | ||
|
||
import coreNOE from 'eslint/lib/rules/no-unused-expressions'; | ||
|
||
const meta = coreNOE.meta; | ||
|
||
const create = (context) => { | ||
const coreChecks = coreNOE.create(context); | ||
|
||
return { | ||
ExpressionStatement (node) { | ||
if (node.expression.type !== 'TypeCastExpression') { | ||
coreChecks.ExpressionStatement(node); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
meta | ||
}; |
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,15 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'foo + 1', | ||
errors: [{ | ||
message: 'Expected an assignment or function call and instead saw an expression.' | ||
}] | ||
} | ||
], | ||
valid: [ | ||
{ | ||
code: '(foo: number)' | ||
} | ||
] | ||
}; |
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