-
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 #10 from buildo/no-redundant-flow
Add no-redundant-flow
- Loading branch information
Showing
7 changed files
with
163 additions
and
1 deletion.
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,31 @@ | ||
# Remove redundant uses of flow (fp-ts/no-redundant-flow) | ||
|
||
Suggest removing `flow` when it only has one argument. This can happen after a | ||
refactoring that removed some combinators from a flow expression. | ||
|
||
**Fixable**: This rule is automatically fixable using the `--fix` flag on the | ||
command line. | ||
|
||
## Rule Details | ||
|
||
Example of **incorrect** code for this rule: | ||
|
||
```ts | ||
import { flow } from "fp-ts/pipeable"; | ||
import { some, Option } from "fp-ts/Option"; | ||
|
||
const f: (n: number): Option<number> = flow(some); | ||
``` | ||
|
||
Example of **correct** code for this rule: | ||
|
||
```ts | ||
import { flow } from "fp-ts/pipeable"; | ||
import { some, filter, Option } from "fp-ts/Option"; | ||
|
||
const f: (n: number): Option<number> = | ||
flow( | ||
some, | ||
filter((n) => n > 2) | ||
); | ||
``` |
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,44 @@ | ||
import { TSESLint } from "@typescript-eslint/experimental-utils"; | ||
import { isFlowExpression } from "../utils"; | ||
|
||
const messages = { | ||
redundantFlow: "flow can be removed because it takes only one argument", | ||
removeFlow: "remove flow", | ||
} as const; | ||
type MessageIds = keyof typeof messages; | ||
|
||
export const meta: TSESLint.RuleMetaData<MessageIds> = { | ||
type: "suggestion", | ||
fixable: "code", | ||
schema: [], | ||
messages, | ||
}; | ||
|
||
export function create( | ||
context: TSESLint.RuleContext<MessageIds, []> | ||
): TSESLint.RuleListener { | ||
return { | ||
CallExpression(node) { | ||
if (node.arguments.length === 1 && isFlowExpression(node, context)) { | ||
context.report({ | ||
node, | ||
messageId: "redundantFlow", | ||
suggest: [ | ||
{ | ||
messageId: "removeFlow", | ||
fix(fixer) { | ||
return [ | ||
fixer.removeRange([ | ||
node.callee.range[0], | ||
node.callee.range[1] + 1, | ||
]), | ||
fixer.removeRange([node.range[1] - 1, node.range[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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import * as rule from "../../src/rules/no-redundant-flow"; | ||
import { ESLintUtils } from "@typescript-eslint/experimental-utils"; | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
sourceType: "module", | ||
}, | ||
}); | ||
|
||
ruleTester.run("no-redundant-flow", rule, { | ||
valid: [ | ||
`import { flow } from "fp-ts/function" | ||
flow(foo, bar) | ||
`, | ||
`import { flow } from "fp-ts/function" | ||
flow( | ||
foo, | ||
bar | ||
) | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
import { flow } from "fp-ts/function" | ||
const a = flow(foo) | ||
`, | ||
errors: [ | ||
{ | ||
messageId: "redundantFlow", | ||
suggestions: [ | ||
{ | ||
messageId: "removeFlow", | ||
output: ` | ||
import { flow } from "fp-ts/function" | ||
const a = foo | ||
`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
import { flow } from "fp-ts/function" | ||
const a = flow( | ||
foo | ||
) | ||
`, | ||
errors: [ | ||
{ | ||
messageId: "redundantFlow", | ||
suggestions: [ | ||
{ | ||
messageId: "removeFlow", | ||
output: ` | ||
import { flow } from "fp-ts/function" | ||
const a = ${""} | ||
foo | ||
`, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); |