From b18c9d40fb6e87b1438642781a67d58ba47e23b8 Mon Sep 17 00:00:00 2001 From: Kireev Mikhail <29187880+kireevmp@users.noreply.github.com> Date: Fri, 1 Nov 2024 01:59:36 +0100 Subject: [PATCH] fix: propagate name when chaining methods --- .changeset/orange-seahorses-tickle.md | 5 +++++ src/visitors/sid/mod.rs | 16 +++++++++------- tests/fixtures/store/chaining/config.json | 1 + tests/fixtures/store/chaining/input.js | 4 ++++ tests/fixtures/store/chaining/output.js | 9 +++++++++ 5 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 .changeset/orange-seahorses-tickle.md create mode 100644 tests/fixtures/store/chaining/config.json create mode 100644 tests/fixtures/store/chaining/input.js create mode 100644 tests/fixtures/store/chaining/output.js diff --git a/.changeset/orange-seahorses-tickle.md b/.changeset/orange-seahorses-tickle.md new file mode 100644 index 0000000..e73ac79 --- /dev/null +++ b/.changeset/orange-seahorses-tickle.md @@ -0,0 +1,5 @@ +--- +"@effector/swc-plugin": patch +--- + +Fix propagating `name` when chaining methods (issue #25) diff --git a/src/visitors/sid/mod.rs b/src/visitors/sid/mod.rs index b4ac82b..d266e21 100644 --- a/src/visitors/sid/mod.rs +++ b/src/visitors/sid/mod.rs @@ -1,12 +1,12 @@ use std::rc::Rc; use swc_core::{ - common::{sync::Lrc, SourceMapper}, + common::{SourceMapper, sync::Lrc}, ecma::{ ast::*, atoms::JsWord, utils::private_ident, - visit::{noop_visit_mut_type, VisitMut, VisitMutWith}, + visit::{VisitMut, VisitMutWith, noop_visit_mut_type}, }, quote, }; @@ -16,11 +16,11 @@ use self::{ method::MethodTransformer, }; use crate::{ + Config, constants::EffectorMethod, state::EffectorImport, - utils::{to_domain_method, to_method, TryKeyOf}, + utils::{TryKeyOf, to_domain_method, to_method}, visitors::{MutableState, VisitorMeta}, - Config, }; mod call_identity; @@ -185,9 +185,11 @@ impl VisitMut for UnitIdentifier { } fn visit_mut_call_expr(&mut self, node: &mut CallExpr) { - // 'consume' name information, so that nested nodes - // don't infer their names from far above - self.visit_stacked(None, node); + // CallExpr -> callee do infer `name` + node.callee.visit_mut_children_with(self); + + // CallExpr -> args do not inherit the name + self.visit_stacked(None, &mut node.args); self.transform_method(node); self.transform_factory(node); diff --git a/tests/fixtures/store/chaining/config.json b/tests/fixtures/store/chaining/config.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/fixtures/store/chaining/config.json @@ -0,0 +1 @@ +{} diff --git a/tests/fixtures/store/chaining/input.js b/tests/fixtures/store/chaining/input.js new file mode 100644 index 0000000..d54dfe6 --- /dev/null +++ b/tests/fixtures/store/chaining/input.js @@ -0,0 +1,4 @@ +import { createEvent, createStore } from 'effector'; + +const event = createEvent(); +const $store = createStore(0).on(event, (_, value) => value); diff --git a/tests/fixtures/store/chaining/output.js b/tests/fixtures/store/chaining/output.js new file mode 100644 index 0000000..389e393 --- /dev/null +++ b/tests/fixtures/store/chaining/output.js @@ -0,0 +1,9 @@ +import { createEvent, createStore } from 'effector'; +const event = createEvent({ + sid: "3g0kdzwe", + name: "event" +}); +const $store = createStore(0, { + sid: "cjscsjrm", + name: "$store" +}).on(event, (_, value)=>value);