~technomancy/fennel

Only replace _trailing_ "elseif true" with "else" v1 PROPOSED

Thanks; just pushed this out.

-Phil

Benaiah Mischenko <benaiah@mischenko.com> writes:
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~technomancy/fennel/%3C20190903202425.3911-1-benaiah%40mischenko.com%3E/mbox | git am -3
Learn more about email & git

[PATCH] Only replace _trailing_ "elseif true" with "else" Export this patch

---
 fennel.lua | 4 +++-
 test.lua   | 3 +++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/fennel.lua b/fennel.lua
index c5e451b..1f3bbe7 100644
--- a/fennel.lua
+++ b/fennel.lua
@@ -1493,7 +1493,9 @@ SPECIALS['if'] = function(ast, scope, parent, opts)
         local branch = branches[i]
         local fstr = not branch.nested and 'if %s then' or 'elseif %s then'
         local cond = tostring(branch.cond)
-        local condLine = (cond == "true" and branch.nested) and "else" or fstr:format(cond)
+        local condLine = (cond == "true" and branch.nested and i == #branches)
+            and "else"
+            or fstr:format(cond)
         if branch.nested then
             emit(lastBuffer, branch.condchunk, ast)
         else
diff --git a/test.lua b/test.lua
index c815b6b..a049e5c 100644
--- a/test.lua
+++ b/test.lua
@@ -118,6 +118,9 @@ local cases = {
         ["(var a 884) (when nil (set a 192)) a"]=884,
         -- when body does not run on false
         ["(when (= 12 88) (os.exit 1)) false"]=false,
+        -- make sure bad code isn't emitted when an always-true
+        -- condition exists in the middle of an if
+        ["(if false :y true :x :trailing :condition)"]="x",
     },
 
     core = {
-- 
2.23.0.rc1
Thanks; just pushed this out.

-Phil

Benaiah Mischenko <benaiah@mischenko.com> writes:
View this thread in the archives