Skip to content

Commit 0f04789

Browse files
authored
Merge pull request #63 from buildo/fix-assignments
Exclude assignement expressions from linting
2 parents 0638969 + e1f9d35 commit 0f04789

File tree

3 files changed

+59
-34
lines changed

3 files changed

+59
-34
lines changed

.github/release-drafter.yml

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ template: |
44
$CHANGES
55
66
categories:
7+
- title: "🐞 Bug fixes"
8+
labels:
9+
- "bug"
710
- title: "🔧 Dependency updates"
811
labels:
912
- "dependencies"

src/rules/no-discarded-pure-expression.ts

+39-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { ParserServices } from "@typescript-eslint/experimental-utils";
1+
import {
2+
ParserServices,
3+
AST_NODE_TYPES,
4+
} from "@typescript-eslint/experimental-utils";
25
import { array, option, readonlyArray } from "fp-ts";
36
import { constVoid, pipe } from "fp-ts/function";
47
import { Option } from "fp-ts/Option";
@@ -78,41 +81,43 @@ export default createRule({
7881

7982
return {
8083
ExpressionStatement(node) {
81-
pipe(
82-
node.expression,
83-
typeOfNode,
84-
option.filter((t) => {
85-
if (t.isUnion()) {
86-
return pipe(t.types, array.every(isPureDataType));
87-
}
88-
return isPureDataType(t);
89-
}),
90-
option.fold(constVoid, (t) => {
91-
context.report({
92-
node: node.expression,
93-
messageId: "pureExpressionInStatementPosition",
94-
data: {
95-
dataType: t.isUnion()
96-
? t.types[0]!.symbol.escapedName
97-
: t.symbol.escapedName,
98-
},
99-
suggest: [
100-
{
101-
messageId: "addReturn",
102-
fix(fixer) {
103-
return fixer.insertTextBefore(node.expression, "return ");
104-
},
84+
if (node.expression.type !== AST_NODE_TYPES.AssignmentExpression) {
85+
pipe(
86+
node.expression,
87+
typeOfNode,
88+
option.filter((t) => {
89+
if (t.isUnion()) {
90+
return pipe(t.types, array.every(isPureDataType));
91+
}
92+
return isPureDataType(t);
93+
}),
94+
option.fold(constVoid, (t) => {
95+
context.report({
96+
node: node.expression,
97+
messageId: "pureExpressionInStatementPosition",
98+
data: {
99+
dataType: t.isUnion()
100+
? t.types[0]!.symbol.escapedName
101+
: t.symbol.escapedName,
105102
},
106-
{
107-
messageId: "runExpression",
108-
fix(fixer) {
109-
return fixer.insertTextAfter(node.expression, "()");
103+
suggest: [
104+
{
105+
messageId: "addReturn",
106+
fix(fixer) {
107+
return fixer.insertTextBefore(node.expression, "return ");
108+
},
110109
},
111-
},
112-
],
113-
});
114-
})
115-
);
110+
{
111+
messageId: "runExpression",
112+
fix(fixer) {
113+
return fixer.insertTextAfter(node.expression, "()");
114+
},
115+
},
116+
],
117+
});
118+
})
119+
);
120+
}
116121
},
117122
JSXAttribute(node) {
118123
const parameterWithVoidOrUknownReturnType = (

tests/rules/no-discarded-pure-expression.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ ruleTester.run("no-discarded-pure-expression", rule, {
7272
foo(2, () => myCommand(), () => myCommand())
7373
`,
7474
},
75+
{
76+
// https://github.com/buildo/eslint-plugin-fp-ts/issues/62
77+
code: stripIndent`
78+
import { IO } from "fp-ts/IO";
79+
import { IORef } from "fp-ts/IORef";
80+
81+
class Foo {
82+
private readonly errors: IORef<string[]>;
83+
readonly getErrors: IO<string[]>;
84+
85+
constructor() {
86+
this.errors = new IORef([]);
87+
this.getErrors = this.errors.read;
88+
}
89+
}
90+
`,
91+
},
7592
],
7693
invalid: [
7794
{

0 commit comments

Comments
 (0)