-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from buildo/pure-statement
Add no-discarded-pure-expression
- Loading branch information
Showing
20 changed files
with
894 additions
and
25 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,94 @@ | ||
# Disallow expressions returning pure data types (like `Task` or `IO`) where `void` or `unknown` is expected or in statement position (fp-ts/no-discarded-pure-expression) | ||
|
||
Expressions which return a pure data type, such as `IO`, `Task` and their | ||
variants, should normally be passed as an argument, returned, or run. | ||
|
||
Failing to do so causes the program represented by `IO` or `Task` to never be | ||
run, leading to surprising behavior which is normally difficult to debug. | ||
|
||
This rule covers two common scenarios that are common programming errors: | ||
|
||
- returning pure data types where `void` or `unknown` is expected (for instance, | ||
in event handlers) without running them | ||
|
||
- writing expressions that return pure data types in statement position (without | ||
returning them or running them) | ||
|
||
**💡 Fixable**: This rule provides in-editor suggested fixes. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
import { task } from "fp-ts"; | ||
|
||
declare const myCommand: (n: number) => Task<string>; | ||
|
||
function woops() { | ||
myCommand(1); // the task will never run, since is not being run nor returned | ||
} | ||
``` | ||
|
||
```ts | ||
declare const MyComponent: (props: { handler: () => void }) => JSX.Element; | ||
|
||
declare const myCommand: (n: number) => Task<string>; | ||
|
||
export function Foo() { | ||
return ( | ||
<MyComponent | ||
handler={() => myCommand(1)} // bug, the Task will never execute | ||
/>; | ||
) | ||
} | ||
``` | ||
|
||
```ts | ||
import { task } from "fp-ts"; | ||
|
||
declare function foo(arg1: number, callbackUnknown: () => unknown): void; | ||
|
||
declare const myCommand: (n: number) => Task<string>; | ||
|
||
foo( | ||
2, | ||
() => myCommand(1) // bug, the Task will never execute | ||
); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
import { task } from "fp-ts"; | ||
|
||
declare const myCommand: (n: number) => Task<string>; | ||
|
||
function ok() { | ||
return myCommand(1); | ||
} | ||
``` | ||
|
||
```ts | ||
declare const MyComponent: (props: { handler: () => void }) => JSX.Element; | ||
|
||
declare const myCommand: (n: number) => Task<string>; | ||
|
||
export function Foo() { | ||
return ( | ||
<MyComponent | ||
handler={() => myCommand(1)()} | ||
/>; | ||
) | ||
} | ||
``` | ||
|
||
```ts | ||
import { task } from "fp-ts"; | ||
|
||
declare function foo(arg1: number, callbackUnknown: () => unknown): void; | ||
|
||
declare const myCommand: (n: number) => Task<string>; | ||
|
||
foo(2, () => myCommand(1)()); | ||
``` |
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
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
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
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
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
Oops, something went wrong.