From e0ac240a7cf934287a19fa98846b99d94267585a Mon Sep 17 00:00:00 2001 From: Igor Kamyshev Date: Wed, 29 Mar 2023 14:27:51 +0700 Subject: [PATCH] Fix false-positive in no-ambiguity-target, fixes #133 --- .../examples/correct-example-issue-133.js | 16 +++++++++++++ .../no-ambiguity-target.js | 3 ++- .../no-ambiguity-target.test.js | 23 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 rules/no-ambiguity-target/examples/correct-example-issue-133.js create mode 100644 rules/no-ambiguity-target/no-ambiguity-target.test.js diff --git a/rules/no-ambiguity-target/examples/correct-example-issue-133.js b/rules/no-ambiguity-target/examples/correct-example-issue-133.js new file mode 100644 index 0000000..3869b5d --- /dev/null +++ b/rules/no-ambiguity-target/examples/correct-example-issue-133.js @@ -0,0 +1,16 @@ +// Examples were found in production code-base with exception on 0.10.3 +// https://github.com/igorkamyshev/eslint-plugin-effector/issues/133 + +import { createStore, createEvent, sample } from "effector"; + +const obj = { + fn: () => { + const $store = createStore(0); + const event = createEvent(); + // warning Method `sample` returns `target` and assigns the result to a variable. Consider removing one of them effector/no-ambiguity-target + sample({ + source: event, + target: $store, + }); + }, +}; diff --git a/rules/no-ambiguity-target/no-ambiguity-target.js b/rules/no-ambiguity-target/no-ambiguity-target.js index 75ec197..f947685 100644 --- a/rules/no-ambiguity-target/no-ambiguity-target.js +++ b/rules/no-ambiguity-target/no-ambiguity-target.js @@ -53,7 +53,8 @@ module.exports = { ); const resultPartOfChain = traverseParentByType( node, - "ObjectExpression" + "ObjectExpression", + { stopOnTypes: ["BlockStatement"] } ); if (resultAssignedInVariable || resultPartOfChain) { diff --git a/rules/no-ambiguity-target/no-ambiguity-target.test.js b/rules/no-ambiguity-target/no-ambiguity-target.test.js new file mode 100644 index 0000000..62a80da --- /dev/null +++ b/rules/no-ambiguity-target/no-ambiguity-target.test.js @@ -0,0 +1,23 @@ +const { RuleTester } = require("eslint"); +const { join } = require("path"); + +const { readExample } = require("../../utils/read-example"); +const rule = require("./no-ambiguity-target"); + +const ruleTester = new RuleTester({ + parserOptions: { + ecmaVersion: 2020, + sourceType: "module", + }, +}); + +const readExampleForTheRule = (name) => ({ + code: readExample(__dirname, name), + filename: join(__dirname, "examples", name), +}); + +ruleTester.run("effector/no-ambiguity-target.js.test", rule, { + valid: ["correct-example-issue-133.js"].map(readExampleForTheRule), + + invalid: [], +});