Skip to content

Commit 02a20ea

Browse files
committed
add some specs for name extract
1 parent e944947 commit 02a20ea

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

moonscript/transform/destructure.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,6 @@ end
228228
return {
229229
has_destructure = has_destructure,
230230
split_assign = split_assign,
231-
build_assign = build_assign
231+
build_assign = build_assign,
232+
extract_assign_names = extract_assign_names
232233
}

moonscript/transform/destructure.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,4 @@ split_assign = (scope, assign) ->
123123

124124
build.group g
125125

126-
{ :has_destructure, :split_assign, :build_assign }
126+
{ :has_destructure, :split_assign, :build_assign, :extract_assign_names }

spec/transform_spec.moon

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
11

22
import with_dev from require "spec.helpers"
33

4+
describe "moonscript.transform.destructure", ->
5+
local extract_assign_names
6+
7+
with_dev ->
8+
{ :extract_assign_names } = require "moonscript.transform.destructure"
9+
10+
it "extracts names from table destructure", ->
11+
des = {
12+
"table"
13+
{
14+
{{"key_literal", "hi"}, {"ref", "hi"}}
15+
{{"key_literal", "world"}, {"ref", "world"}}
16+
}
17+
}
18+
19+
assert.same {
20+
{
21+
{"ref", "hi"} -- target
22+
{
23+
{"dot", "hi"}
24+
} -- chain suffix
25+
}
26+
27+
{
28+
{"ref", "world"}
29+
{
30+
{"dot", "world"}
31+
}
32+
}
33+
34+
}, extract_assign_names des
35+
36+
it "extracts names from array destructure", ->
37+
des = {
38+
"table"
39+
{
40+
{{"ref", "hi"}}
41+
}
42+
}
43+
44+
assert.same {
45+
{
46+
{"ref", "hi"}
47+
{
48+
{"index", {"number", 1}}
49+
}
50+
}
51+
}, extract_assign_names des
52+
453
describe "moonscript.transform.statements", ->
554
local last_stm, transform_last_stm, Run
655

0 commit comments

Comments
 (0)