-
Notifications
You must be signed in to change notification settings - Fork 16
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
no-guard
#88
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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", | ||
}, | ||
}; |
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, | ||
}); | ||
}, | ||
}, | ||
], | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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), | ||
filter: Boolean, | ||
target: reaction, | ||
}); | ||
|
||
// 👍 makes sense | ||
sample({ | ||
clock: trigger, | ||
soruce: $data, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Feel free to send PR with this fixes. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks) will do it now There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
}); | ||
``` |
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 |
---|---|---|
@@ -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 }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo in word. source*