From 3adaed672ebce0566ab466f95115a97a93e09c34 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 27 Nov 2012 18:35:07 -0800 Subject: [PATCH] make mtype not error when passed smart node fix #52 fixes error when returning if/elseif assignment block --- moonscript/transform.lua | 15 +++++++++++++-- moonscript/transform.moon | 12 ++++++++++-- tests/inputs/cond.moon | 6 ++++++ tests/outputs/cond.lua | 16 ++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index a28b133..e2511f6 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -5,7 +5,18 @@ local data = require("moonscript.data") local reversed = util.reversed local ntype, build, smart_node, is_slice, value_is_singular = types.ntype, types.build, types.smart_node, types.is_slice, types.value_is_singular local insert = table.insert -local mtype = util.moon.type +local mtype +do + local moon_type = util.moon.type + mtype = function(val) + local t = type(val) + if "table" == t and rawget(val, "__class") then + return moon_type(val) + else + return t + end + end +end local implicitly_return do local _parent_0 = nil @@ -182,7 +193,7 @@ apply_to_last = function(stms, fn) local last_exp_id = 0 for i = #stms, 1, -1 do local stm = stms[i] - if stm and util.moon.type(stm) ~= Run then + if stm and mtype(stm) ~= Run then last_exp_id = i break end diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 3829162..a83713a 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -9,7 +9,15 @@ import reversed from util import ntype, build, smart_node, is_slice, value_is_singular from types import insert from table -mtype = util.moon.type +mtype = do + moon_type = util.moon.type + -- lets us check a smart node without throwing an error + (val) -> + t = type(val) + if "table" == t and rawget(val, "__class") + moon_type val + else + t export Statement, Value, NameProxy, LocalName, Run @@ -67,7 +75,7 @@ apply_to_last = (stms, fn) -> last_exp_id = 0 for i = #stms, 1, -1 stm = stms[i] - if stm and util.moon.type(stm) != Run + if stm and mtype(stm) != Run last_exp_id = i break diff --git a/tests/inputs/cond.moon b/tests/inputs/cond.moon index d4d2643..8909721 100644 --- a/tests/inputs/cond.moon +++ b/tests/inputs/cond.moon @@ -89,6 +89,12 @@ elseif z = true else four +kzy = -> + if something = true + 1 + elseif another = false + 2 + --- unless true diff --git a/tests/outputs/cond.lua b/tests/outputs/cond.lua index 5da6d04..036f546 100644 --- a/tests/outputs/cond.lua +++ b/tests/outputs/cond.lua @@ -150,6 +150,22 @@ else end end end +local kzy +kzy = function() + do + local something = true + if something then + return 1 + else + do + local another = false + if another then + return 2 + end + end + end + end +end if not (true) then print("cool!") end