Skip to content

Commit

Permalink
Fix complex cases in no-unnecessary-duplication (#22)
Browse files Browse the repository at this point in the history
* Add test for cases from issue #21

* Use text representation of node for comparing

* Add test case with complex formatting

* Use same formatting for text node comparing
  • Loading branch information
igorkamyshev committed Aug 30, 2021
1 parent e9c28f3 commit 315a486
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 2 deletions.
Binary file not shown.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
"peerDependencies": {
"effector": "*",
"eslint": "*"
},
"dependencies": {
"prettier": "^2.3.2"
}
}
25 changes: 25 additions & 0 deletions rules/no-unnecessary-duplication/examples/correct-examples-01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { guard } from "effector";

// Examples were found in production code-base with false-poitive detection on 0.1.2

guard({
source: someService.$aviaForm,
clock: someService.submitted,
filter: (state) =>
Boolean(state && isValidDirection(state.origin, state.destination)),
target: drawAttention,
});

guard({
source: promocodeModel.$verifyError,
clock: [promocodeChanged, promocodeInputBlurred],
filter: Boolean,
target: promocodeModel.verifyErrorReset,
});

guard({
source: [someService.$aviaForm, $backendFilters, currencyService.$currency],
clock: [$backendFilters, publicApi.allPricesShown],
filter: $isValid,
target: requestPricesFx,
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
extractImportedFromEffector,
} = require("../../utils/extract-imported-from-effector");
const { areNodesSameInText } = require("../../utils/are-nodes-same-in-text");

module.exports = {
meta: {
Expand All @@ -20,6 +21,8 @@ module.exports = {
},
create(context) {
const importedFromEffector = new Map();
const sourceCode = context.getSourceCode();

return {
ImportDeclaration(node) {
extractImportedFromEffector(importedFromEffector, node);
Expand Down Expand Up @@ -49,8 +52,10 @@ module.exports = {
return;
}

const sameSourceAndClock =
params?.source?.value?.name === params?.clock?.value?.name;
const sameSourceAndClock = areNodesSameInText({
context,
nodes: [params.source?.value, params.clock?.value],
});
if (!sameSourceAndClock) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const { RuleTester } = require("eslint");

const { readExample } = require("../../utils/read-example");

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

const readExampleForTheRule = (name) => readExample(__dirname, name);

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
Expand All @@ -11,6 +15,7 @@ const ruleTester = new RuleTester({

ruleTester.run("effector/no-unnecessary-duplication.test", rule, {
valid: [
readExampleForTheRule("correct-examples-01.js"),
`
import { sample } from 'effector';
sample({ source: event });
Expand Down Expand Up @@ -59,6 +64,21 @@ guard({ source: event, filter: (v) => v > 0 });
].map((code) => ({ code })),

invalid: [
// cases with complex formatting
{
code: `
import { sample } from 'effector';
sample({ source: [$store], clock: [
$store
] });
`,
errors: [
{
messageId: "unnecessaryDuplication",
type: "CallExpression",
},
],
},
// source + clock in sample
{
code: `
Expand Down
13 changes: 13 additions & 0 deletions utils/are-nodes-same-in-text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const prettier = require("prettier");

function areNodesSameInText({ context, nodes }) {
const texts = nodes.map((node) =>
prettier.format(context.getSourceCode().getText(node), {
parser: "babel-ts",
})
);

return texts.every((text) => text === texts[0]);
}

module.exports = { areNodesSameInText };
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,7 @@ __metadata:
eslint: ^7.32.0
glob: ^7.1.7
jest: ^27.1.0
prettier: ^2.3.2
typescript: ^4.4.2
peerDependencies:
effector: "*"
Expand Down Expand Up @@ -3779,6 +3780,15 @@ fsevents@^2.3.2:
languageName: node
linkType: hard

"prettier@npm:^2.3.2":
version: 2.3.2
resolution: "prettier@npm:2.3.2"
bin:
prettier: bin-prettier.js
checksum: 17ce5784ac67621c292df58e2da60b2ee150c2d6aebea22a6ad9e52fcd6a5e66c349d0a8436ea3bd8ff9d778920a5f68000d7625b74f43558718a49755aa5259
languageName: node
linkType: hard

"pretty-format@npm:^27.1.0":
version: 27.1.0
resolution: "pretty-format@npm:27.1.0"
Expand Down

0 comments on commit 315a486

Please sign in to comment.