diff --git a/spec/transform_spec.moon b/spec/transform_spec.moon index dba44d0..3952fde 100644 --- a/spec/transform_spec.moon +++ b/spec/transform_spec.moon @@ -1,11 +1,107 @@ import with_dev from require "spec.helpers" + describe "moonscript.transform.destructure", -> - local extract_assign_names + local extract_assign_names, split_assign, Block with_dev -> - { :extract_assign_names } = require "moonscript.transform.destructure" + { :extract_assign_names, :split_assign } = require "moonscript.transform.destructure" + {:Block} = require "moonscript.compile" + + describe "split_assign #fff", -> + -- {:hello} = world + it "simple assignment", -> + node = { + "assign" + { + { "table", { + {{"key_literal", "hello"}, {"ref", "hello"}} + } + } + } + { + {"ref", "world"} + } + } + + out = split_assign Block!, node + + assert.same { + "group", { + { + "group", { + { "declare", { {"ref", "hello"} } } + { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "world"}, {"dot", "hello"}} } } + } + } + } + }, out + + -- a, {:hello} = one, two + it "multiple assigns", -> + node = { + "assign" + { + {"ref", "a"} + { "table", { + {{"key_literal", "hello"}, {"ref", "hello"}} + } + } + } + { + {"ref", "one"} + {"ref", "two"} + } + } + + out = split_assign Block!, node + + assert.same { + "group", { + {"assign", { {"ref", "a"} }, { {"ref", "one"} }} + + { + "group", { + { "declare", { {"ref", "hello"} } } + { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "two"}, {"dot", "hello"}} } } + } + } + } + }, out + + -- {:hello}, a = one, two + it "multiple assigns swapped #ddd", -> + node = { + "assign" + { + { "table", { + {{"key_literal", "hello"}, {"ref", "hello"}} + } + } + {"ref", "a"} + } + { + {"ref", "one"} + {"ref", "two"} + } + } + + out = split_assign Block!, node + + assert.same { + "group", { + { + "group", { + { "declare", { {"ref", "hello"} } } + { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "one"}, {"dot", "hello"}} } } + } + } + + {"assign", { {"ref", "a"} }, { {"ref", "two"} }} + } + }, out + it "extracts names from table destructure", -> des = {