-
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
Add rule no-inline-units
#96
base: master
Are you sure you want to change the base?
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,84 @@ | ||
const { extractImportedFrom } = require("../../utils/extract-imported-from"); | ||
const { createLinkToRule } = require("../../utils/create-link-to-rule"); | ||
const { method } = require("../../utils/method"); | ||
|
||
const METHODS = [ | ||
"forward", | ||
"sample", | ||
"guard", | ||
"attach", | ||
"merge", | ||
"combine", | ||
"createApi", | ||
]; | ||
|
||
const UNIT_CREATORS = ["createStore", "createEvent", "createEffect"]; | ||
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. What about 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. ohh i lost it 🤪 |
||
|
||
module.exports = { | ||
meta: { | ||
type: "problem", | ||
docs: { | ||
description: "Forbids to use inline units in methods", | ||
category: "Quality", | ||
recommended: true, | ||
url: createLinkToRule("no-inline-units"), | ||
}, | ||
messages: { | ||
noInlineUnits: | ||
'Declare inline method "{{ inlineMethodName }}" in a variable', | ||
}, | ||
schema: [], | ||
}, | ||
create(context) { | ||
const importNodes = new Map(); | ||
const importedFromEffector = new Map(); | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
extractImportedFrom({ | ||
importMap: importedFromEffector, | ||
nodeMap: importNodes, | ||
node, | ||
packageName: "effector", | ||
}); | ||
}, | ||
CallExpression(node) { | ||
const isEffectorMethod = method.is(METHODS, { | ||
node, | ||
importMap: importedFromEffector, | ||
}); | ||
|
||
if (!isEffectorMethod) { | ||
return; | ||
} | ||
|
||
const configNode = node?.arguments?.[0]; | ||
const optionsNodes = configNode?.properties; | ||
let inlineMethodName; | ||
|
||
const hasInlineUnits = optionsNodes?.some((optionNode) => { | ||
const isFound = method.is(UNIT_CREATORS, { | ||
node: optionNode?.value, | ||
importMap: importedFromEffector, | ||
}); | ||
|
||
if (isFound) { | ||
inlineMethodName = optionNode?.value?.callee?.name; | ||
} | ||
|
||
return isFound; | ||
}); | ||
|
||
if (!hasInlineUnits) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: "noInlineUnits", | ||
data: { inlineMethodName }, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# effector/no-inline-units | ||
|
||
Disallows to use inline units in methods | ||
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. Could you provide further information, why it is a bad idea to use inline units? 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. ye sure |
||
|
||
```ts | ||
// 👎 Bad | ||
sample({ source: $somestore, target: createEffect() }); | ||
|
||
// 👍 Good | ||
const effectFx = createEffect(); | ||
|
||
sample({ source: $somestore, target: effectFx }); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const { RuleTester } = require("eslint"); | ||
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. I do not find any TS-tests. Should we add it? 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. I can add TS-tests, but I don't understand what specific cases they should cover. 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. In mind, we should cover something like |
||
|
||
const rule = require("./no-inline-units"); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}); | ||
|
||
ruleTester.run("effector/no-inline-units.test", rule, { | ||
valid: [ | ||
` | ||
import { sample } from 'effector'; | ||
sample({ clock: event, target: effectFx }); | ||
`, | ||
` | ||
import { sample } from 'someLibrary'; | ||
sample({ | ||
clock: event, | ||
source: $shouldExecute, | ||
filter: (shouldExecute) => shouldExecute, | ||
target: effectFx | ||
}); | ||
`, | ||
` | ||
import { sample, attach } from 'effector'; | ||
sample({ | ||
clock: event, | ||
target: attach({ | ||
effect: originalFx, | ||
mapParams: params => { | ||
return { wrapped: params } | ||
}, | ||
}) | ||
}); | ||
`, | ||
].map((code) => ({ code })), | ||
invalid: [ | ||
` | ||
import { forward, createEffect } from 'effector'; | ||
forward({ from: eventOne, to: createEffect() }); | ||
`, | ||
` | ||
import { forward, createStore } from 'effector'; | ||
forward({ from: eventOne, to: createStore(null) }); | ||
`, | ||
` | ||
import { forward, createEvent } from 'effector'; | ||
forward({ from: eventOne, to: createEvent() }); | ||
`, | ||
` | ||
import { attach, createEffect } from 'effector'; | ||
attach({ | ||
effect: createEffect(), | ||
mapParams: params => { | ||
return {wrapped: params} | ||
}, | ||
}); | ||
`, | ||
` | ||
import { sample, attach, createEffect } from 'effector'; | ||
sample({ | ||
clock: event, | ||
target: attach({ | ||
effect: createEffect(), | ||
mapParams: params => { | ||
return {wrapped: params} | ||
}, | ||
}) | ||
}); | ||
`, | ||
].map((code) => ({ | ||
code, | ||
errors: [ | ||
{ | ||
messageId: "noInlineUnits", | ||
type: "CallExpression", | ||
}, | ||
], | ||
})), | ||
}); |
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.
I do not see, why inline effect in
attach
in bad 🤔It looks like other rule to omit
createEffect
at all in this case.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.
I think it is because the name of the effect is lost. Sometimes you can't just understand the meaning of the unit from the context.