Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no-guard #88

Merged
merged 4 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ This preset is recommended for projects that use [Fork API](https://effector.dev

This preset is recommended for projects that use [React](https://reactjs.org) with Effector.

#### plugin:effector/future

This preset contains rules wich enforce _future-effector_ code-style.

### Supported rules

- [effector/enforce-store-naming-convention](rules/enforce-store-naming-convention/enforce-store-naming-convention.md)
Expand All @@ -62,6 +66,7 @@ This preset is recommended for projects that use [React](https://reactjs.org) wi
- [effector/no-unnecessary-combination](rules/no-unnecessary-combination/no-unnecessary-combination.md)
- [effector/no-useless-methods](rules/no-useless-methods/no-useless-methods.md)
- [effector/no-forward](rules/no-forward/no-forward.md)
- [effector/no-guard](rules/no-guard/no-guard.md)
- [effector/no-ambiguity-target](rules/no-ambiguity-target/no-ambiguity-target.md)
- [effector/no-duplicate-on](rules/no-duplicate-on/no-duplicate-on.md)
- [effector/no-getState](rules/no-getState/no-getState.md)
Expand Down
7 changes: 7 additions & 0 deletions config/future.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
rules: {
"effector/prefer-sample-over-forward-with-mapping": "off",
"effector/no-forward": "warn",
"effector/no-guard": "warn",
},
};
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ module.exports = {
"enforce-gate-naming-convention": require("./rules/enforce-gate-naming-convention/enforce-gate-naming-convention"),
"keep-options-order": require("./rules/keep-options-order/keep-options-order"),
"no-forward": require("./rules/no-forward/no-forward"),
"no-guard": require("./rules/no-guard/no-guard"),
},
configs: {
recommended: require("./config/recommended"),
scope: require("./config/scope"),
react: require("./config/react"),
future: require("./config/future"),
},
};
8 changes: 2 additions & 6 deletions rules/no-forward/no-forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { extractImportedFrom } = require("../../utils/extract-imported-from");
const { createLinkToRule } = require("../../utils/create-link-to-rule");
const { method } = require("../../utils/method");
const { replaceForwardBySample } = require("../../utils/replace-by-sample");
const { extractConfig } = require("../../utils/extract-config");

module.exports = {
meta: {
Expand Down Expand Up @@ -43,12 +44,7 @@ module.exports = {
return;
}

const forwardConfig = {
from: node.arguments?.[0]?.properties.find(
(n) => n.key?.name === "from"
),
to: node.arguments?.[0]?.properties.find((n) => n.key?.name === "to"),
};
const forwardConfig = extractConfig(["from", "to"], { node });

if (!forwardConfig.from || !forwardConfig.to) {
return;
Expand Down
78 changes: 78 additions & 0 deletions rules/no-guard/no-guard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const { extractImportedFrom } = require("../../utils/extract-imported-from");
const { createLinkToRule } = require("../../utils/create-link-to-rule");
const { method } = require("../../utils/method");
const { replaceGuardBySample } = require("../../utils/replace-by-sample");
const { extractConfig } = require("../../utils/extract-config");

module.exports = {
meta: {
type: "problem",
docs: {
description: "Prefer `sample` over `guard`",
category: "Quality",
recommended: true,
url: createLinkToRule("no-guard"),
},
messages: {
noGuard:
"Instead of `guard` you can use `sample`, it is more extendable.",
replaceWithSample: "Repalce `guard` with `sample`.",
},
schema: [],
hasSuggestions: true,
},
create(context) {
const importNodes = new Map();
const importedFromEffector = new Map();

return {
ImportDeclaration(node) {
extractImportedFrom({
importMap: importedFromEffector,
nodeMap: importNodes,
node,
packageName: "effector",
});
},
CallExpression(node) {
if (
method.isNot("guard", {
node,
importMap: importedFromEffector,
})
) {
return;
}

const guardConfig = extractConfig(
["source", "clock", "target", "filter"],
{
node,
}
);

if (!guardConfig.clock || !guardConfig.filter) {
return;
}

context.report({
messageId: "noGuard",
node,
suggest: [
{
messageId: "replaceWithSample",
*fix(fixer) {
yield* replaceGuardBySample(guardConfig, {
fixer,
node,
context,
importNodes,
});
},
},
],
});
},
};
},
};
32 changes: 32 additions & 0 deletions rules/no-guard/no-guard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# effector/no-guard

Any `guard` call could be replaced with `sample` call.

```ts
// 👎 could be replaced
guard({ clock: trigger, soruce: $data, filter: Boolean, target: reaction });

// 👍 makes sense
sample({ clock: trigger, source: $data, filter: Boolean, target: reaction });
```

Nice bonus: `sample` is extendable. You can add transformation by `fn`.

```ts
// 👎 could be replaced
guard({
clock: trigger,
soruce: $data.map((data) => data.length),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in word. source*

filter: Boolean,
target: reaction,
});

// 👍 makes sense
sample({
clock: trigger,
soruce: $data,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Feel free to send PR with this fixes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but I have no access)

// Please make sure you have the correct access rights

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to fork repo, commit changes to your fork and send a PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks) will do it now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, waiting for your approve

filter: Boolean,
fn: (data) => data.length,
target: reaction,
});
```
114 changes: 114 additions & 0 deletions rules/no-guard/no-guard.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const { RuleTester } = require("eslint");

const rule = require("./no-guard");

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
});

ruleTester.run("effector/no-guard.test", rule, {
valid: [
`
import { sample } from 'effector';
sample({ clock: eventOne, target: eventTwo });
`,
`
import { guard } from 'effector';
guard({ clock: eventOne.map(() => true), target: eventTwo });
`,
`
import { guard } from 'someLibrary';
forward({ clock: eventOne.prepend((v) => v.length), target: eventTwo });
`,
].map((code) => ({ code })),

invalid: [
{
code: `
import { guard } from 'effector';
guard({ clock: eventOne, target: eventTwo, filter: Boolean });
`,
errors: [
{
messageId: "noGuard",
type: "CallExpression",
suggestions: [
{
messageId: "replaceWithSample",
output: `
import { sample } from 'effector';
sample({ clock: eventOne, filter: Boolean, target: eventTwo });
`,
},
],
},
],
},
{
code: `
import { guard } from 'effector';
guard({ clock: eventOne, target: eventTwo.prepend((v) => v.length), filter: (v) => v.length > 0 });
`,
errors: [
{
messageId: "noGuard",
type: "CallExpression",
suggestions: [
{
messageId: "replaceWithSample",
output: `
import { sample } from 'effector';
sample({ clock: eventOne, filter: (v) => v.length > 0, fn: (v) => v.length, target: eventTwo });
`,
},
],
},
],
},
{
code: `
import { guard } from 'effector';
guard({ clock: eventOne, target: serviceOne.featureOne.eventTwo.prepend((v) => v.length), filter: $store });
`,
errors: [
{
messageId: "noGuard",
type: "CallExpression",
suggestions: [
{
messageId: "replaceWithSample",
output: `
import { sample } from 'effector';
sample({ clock: eventOne, filter: $store, fn: (v) => v.length, target: serviceOne.featureOne.eventTwo });
`,
},
],
},
],
},
{
code: `
import { guard } from 'effector';
guard({ source: $someStore, clock: merge(eventOne, eventOneOne), target: eventTwo, filter: Boolean });
`,
errors: [
{
messageId: "noGuard",
type: "CallExpression",
suggestions: [
{
messageId: "replaceWithSample",
output: `
import { sample } from 'effector';
sample({ clock: merge(eventOne, eventOneOne), source: $someStore, filter: Boolean, target: eventTwo });
`,
},
],
},
],
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
const { createLinkToRule } = require("../../utils/create-link-to-rule");
const { method } = require("../../utils/method");
const { replaceForwardBySample } = require("../../utils/replace-by-sample");
const { extractConfig } = require("../../utils/extract-config");

module.exports = {
meta: {
Expand Down Expand Up @@ -48,12 +49,7 @@ module.exports = {
return;
}

const forwardConfig = {
from: node.arguments?.[0]?.properties.find(
(n) => n.key?.name === "from"
),
to: node.arguments?.[0]?.properties.find((n) => n.key?.name === "to"),
};
const forwardConfig = extractConfig(["from", "to"], { node });

if (!forwardConfig.from || !forwardConfig.to) {
return;
Expand Down
13 changes: 13 additions & 0 deletions utils/extract-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function extractConfig(fields, { node }) {
const config = {};

fields.forEach((field) => {
config[field] = node.arguments?.[0]?.properties.find(
(n) => n.key?.name === field
);
});

return config;
}

module.exports = { extractConfig };
34 changes: 31 additions & 3 deletions utils/replace-by-sample.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
const { buildObjectInText } = require("./builders");

function* replaceGuardBySample(
guardConfig,
{ fixer, node, context, importNodes }
) {
let mapperFunctionNode = null;

let clockNode = guardConfig.clock?.value;
let targetNode = guardConfig.target?.value;
let sourceNode = guardConfig.source?.value;
let filterNode = guardConfig.filter?.value;

if (
targetNode.type === "CallExpression" &&
targetNode?.callee?.property?.name === "prepend"
) {
mapperFunctionNode = targetNode?.arguments?.[0];
targetNode = targetNode.callee.object;
targetMapperUsed = true;
}

yield* replaceBySample(
{ clockNode, sourceNode, filterNode, mapperFunctionNode, targetNode },
{ node, fixer, context, importNodes, methodName: "guard" }
);
}

function* replaceForwardBySample(
forwardConfig,
{ fixer, node, context, importNodes }
Expand Down Expand Up @@ -39,20 +65,22 @@ function* replaceForwardBySample(
}

yield* replaceBySample(
{ clockNode, targetNode, mapperFunctionNode },
{ clockNode, mapperFunctionNode, targetNode },
{ node, fixer, context, importNodes, methodName: "forward" }
);
}

function* replaceBySample(
{ clockNode, targetNode, mapperFunctionNode },
{ clockNode, sourceNode, filterNode, mapperFunctionNode, targetNode },
{ node, fixer, context, importNodes, methodName }
) {
yield fixer.replaceText(
node,
`sample(${buildObjectInText.fromMapOfNodes({
properties: {
clock: clockNode,
source: sourceNode,
filter: filterNode,
fn: mapperFunctionNode,
target: targetNode,
},
Expand All @@ -63,4 +91,4 @@ function* replaceBySample(
yield fixer.replaceText(importNodes.get(methodName), "sample");
}

module.exports = { replaceForwardBySample };
module.exports = { replaceForwardBySample, replaceGuardBySample };