branch: elpa/loopy-dash
commit 6fab22cbf5d11fdf890e7785db805e51a7608984
Author: okamsn <[email protected]>
Commit: GitHub <[email protected]>

    Update `loopy-dash--destructure-for-with-vars` for change in Loopy. (#4)
    
    The structure of `loopy--with-vars` is changing in Loopy.  See
    https://github.com/okamsn/loopy/pull/260
    
    - Update `loopy-dash--destructure-for-with-vars`.
    - Add new test `dash-with-var-destructured-still-detected`.
    - Update test `dash-with-destructuring` to test order of `with`-bound 
variables.
---
 loopy-dash.el       | 21 ++++++++++++++++++---
 tests/dash-tests.el | 21 +++++++++++++++++----
 2 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/loopy-dash.el b/loopy-dash.el
index 10880229944..d87294203cd 100644
--- a/loopy-dash.el
+++ b/loopy-dash.el
@@ -92,9 +92,24 @@
   "Return a way to destructure BINDINGS as if by `-let*'.
 
 Returns a list of two elements:
-1. The symbol `-let*'.
-2. A new list of bindings."
-  (list '-let* bindings))
+1. A list of symbols being all the variables to be bound in BINDINGS.
+2. A function to be called with the code to be wrapped, which
+  should produce wrapped code appropriate for BINDINGS,
+  such as a `let*' form."
+  ;; See also `loopy--pcase-destructure-for-with-vars'
+  (let ((var-list nil)
+        (new-binds))
+    (dolist (b bindings)
+      (let ((pairs (apply #'dash--match b)))
+        (setq var-list (-union var-list (-map #'car pairs)))
+        (push pairs new-binds)))
+    (list var-list
+          (lambda (body)
+            (let ((result (macroexp-progn body)))
+              (dolist (nb new-binds)
+                (setq result `(let* ,nb
+                                ,result)))
+              result)))))
 
 (defun loopy-dash--destructure-for-iteration (var val)
   "Destructure VAL according to VAR as if by `-let'.
diff --git a/tests/dash-tests.el b/tests/dash-tests.el
index d2782e1a954..791bf4f4923 100644
--- a/tests/dash-tests.el
+++ b/tests/dash-tests.el
@@ -65,10 +65,11 @@
 
 
 (ert-deftest dash-with-destructuring ()
-  (should (= 7 (eval (quote (loopy (flag dash)
-                                   (with ((&plist :a a  :b b) '(:a 3 :b 4)))
-                                   (repeat 1)
-                                   (return (+ a b))))))))
+  (should (= 14 (eval (quote (loopy (flag dash)
+                                    (with ((&plist :a a  :b b) '(:a 3 :b 4))
+                                          (c (+ a b)))
+                                    (repeat 1)
+                                    (return (+ a b c))))))))
 
 ;; Make sure all variables for the needed settings are properly bound.
 (ert-deftest destructuring-settings-not-escape ()
@@ -221,3 +222,15 @@
                                      (6 7 8 (:k2 . 9) (:k1 . 10))])
                         (collect (a _ c &alist :k1 :k2) elem)
                         (finally-return a c k1 k2)))))
+
+(ert-deftest dash-with-var-destructured-still-detected ()
+  "Make sure destructured `with' variables are still detected by other 
commands.
+For example, make sure we don't see an error for incompatible accumulations
+since we are binding `acc' in `with'."
+  (should (= 45 (eval '(loopy (flag dash)
+                              (with ((acc b) '(3 4)))
+                              (list i '(1 2 3))
+                              (sum acc i)
+                              (multiply acc i)
+                              (finally-return acc))
+                      t))))

Reply via email to