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

Add rule no-inline-units #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ module.exports = {
"effector/no-unnecessary-combination": "warn",
"effector/no-duplicate-on": "error",
"effector/keep-options-order": "warn",
"effector/no-inline-units": "error",
},
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
"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"),
"no-inline-units": require("./rules/no-inline-units/no-inline-units"),
},
configs: {
recommended: require("./config/recommended"),
Expand Down
2 changes: 1 addition & 1 deletion rules/no-forward/no-forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
messages: {
noForward:
"Instead of `forward` you can use `sample`, it is more extendable.",
replaceWithSample: "Repalce `forward` with `sample`.",
replaceWithSample: "Replace `forward` with `sample`.",
},
schema: [],
hasSuggestions: true,
Expand Down
84 changes: 84 additions & 0 deletions rules/no-inline-units/no-inline-units.js
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",
Copy link
Member

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.

const newEffect = attach({ effect: createEffect(() => null) })

const newEffect = attach({ effect: () => null })

Copy link
Member

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.

"merge",
"combine",
"createApi",
];

const UNIT_CREATORS = ["createStore", "createEvent", "createEffect"];
Copy link
Member

Choose a reason for hiding this comment

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

What about attach?

Copy link
Author

@spitfire1236 spitfire1236 Apr 14, 2022

Choose a reason for hiding this comment

The 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 },
});
},
};
},
};
13 changes: 13 additions & 0 deletions rules/no-inline-units/no-inline-units.md
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
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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 });
```
83 changes: 83 additions & 0 deletions rules/no-inline-units/no-inline-units.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const { RuleTester } = require("eslint");
Copy link
Member

Choose a reason for hiding this comment

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

I do not find any TS-tests. Should we add it?

Copy link
Author

Choose a reason for hiding this comment

The 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.
Can u help me with that?

Copy link
Member

Choose a reason for hiding this comment

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

In mind, we should cover something like sample({ source: createCustomStoreBySomeFactory() }) where createCustomStoreBySomeFactory is not imported from effector, but it returns Store.


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",
},
],
})),
});