Skip to content

Commit f1e3d00

Browse files
committed
Fix tuple pattern creation
1 parent e29a068 commit f1e3d00

File tree

7 files changed

+81
-134
lines changed

7 files changed

+81
-134
lines changed

lib/elixir_script/translator/pattern_matching.ex

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,12 @@ defmodule ElixirScript.Translator.PatternMatching do
244244
|> Enum.map(&build_match([&1], env))
245245
|> reduce_patterns
246246

247-
pattern = JS.object_expression([
248-
JS.property(
249-
JS.identifier("values"),
250-
JS.array_expression(patterns)
251-
)
252-
])
253-
254-
{ [type(Primitive.tuple_class, pattern)], params }
247+
tuple_pattern = JS.new_expression(
248+
Primitive.tuple_class(),
249+
patterns
250+
)
251+
252+
{ [tuple_pattern], params }
255253
end
256254

257255
defp do_build_match({:\\, _, [{name, _, _}, default]}, env) do

test/translator/case_test.exs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,11 @@ defmodule ElixirScript.Translator.Case.Test do
155155
end
156156

157157
js_code = """
158-
Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
159-
values: [Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()]
160-
})], function(one, two) {
161-
return console.info(one);
162-
}), Bootstrap.Core.Patterns.clause([Symbol.for('error')], function() {
163-
return null;
164-
})).call(this, data)
158+
Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([new Bootstrap.Core.Tuple(Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable())], function(one, two) {
159+
return console.info(one);
160+
}), Bootstrap.Core.Patterns.clause([Symbol.for('error')], function() {
161+
return null;
162+
})).call(this, data);
165163
"""
166164

167165
assert_translation(ex_ast, js_code)
@@ -181,15 +179,11 @@ defmodule ElixirScript.Translator.Case.Test do
181179
end
182180

183181
js_code = """
184-
Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
185-
values: [Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
186-
values: [Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()]
187-
}), Bootstrap.Core.Patterns.variable()]
188-
})], function(one, two, three) {
189-
return console.info(one);
190-
}), Bootstrap.Core.Patterns.clause([Symbol.for('error')], function() {
191-
return null;
192-
})).call(this, data)
182+
Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([new Bootstrap.Core.Tuple(new Bootstrap.Core.Tuple(Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()), Bootstrap.Core.Patterns.variable())], function(one, two, three) {
183+
return console.info(one);
184+
}), Bootstrap.Core.Patterns.clause([Symbol.for('error')], function() {
185+
return null;
186+
})).call(this, data)
193187
"""
194188

195189
assert_translation(ex_ast, js_code)
@@ -205,15 +199,11 @@ defmodule ElixirScript.Translator.Case.Test do
205199
end
206200

207201
js_code = """
208-
Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
209-
values: [Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
210-
values: [Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()]
211-
})]
212-
})], function(one, two, three) {
213-
return console.info(one);
214-
}), Bootstrap.Core.Patterns.clause([Symbol.for('error')], function() {
215-
return null;
216-
})).call(this, data)
202+
Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([new Bootstrap.Core.Tuple(Bootstrap.Core.Patterns.variable(), new Bootstrap.Core.Tuple(Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()))], function(one, two, three) {
203+
return console.info(one);
204+
}), Bootstrap.Core.Patterns.clause([Symbol.for('error')], function() {
205+
return null;
206+
})).call(this, data)
217207
"""
218208

219209
assert_translation(ex_ast, js_code)

test/translator/for_test.exs

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,11 @@ defmodule ElixirScript.Translator.For.Test do
88
end
99

1010
js_code = """
11-
Bootstrap.Core.SpecialForms._for(
12-
Bootstrap.Core.Patterns.clause(
13-
[Bootstrap.Core.Patterns.variable()],
14-
function(n) {
15-
return n * 2;
16-
},
17-
function() {
18-
return true;
19-
}
20-
),
21-
[
22-
Bootstrap.Core.Patterns.list_generator(
23-
Bootstrap.Core.Patterns.variable(),
24-
Object.freeze([1, 2, 3, 4])
25-
)
26-
],
27-
Elixir.ElixirScript.Collectable.__load(Elixir),
28-
Object.freeze([])
29-
)
11+
Bootstrap.Core.SpecialForms._for(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.variable()], function(n) {
12+
return n * 2;
13+
}, function() {
14+
return true;
15+
}), [Bootstrap.Core.Patterns.list_generator(Bootstrap.Core.Patterns.variable(), Object.freeze([1, 2, 3, 4]))], Elixir.ElixirScript.Collectable.__load(Elixir), Object.freeze([]))
3016
"""
3117

3218
assert_translation(ex_ast, js_code)
@@ -173,21 +159,11 @@ defmodule ElixirScript.Translator.For.Test do
173159
end
174160

175161
js_code = """
176-
Bootstrap.Core.SpecialForms._for(
177-
Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
178-
values: [Symbol.for('user'), Bootstrap.Core.Patterns.variable()]
179-
})], function(name) {
180-
return Elixir.ElixirScript.String.__load(Elixir).upcase(name);
181-
}, function() {
182-
return true;
183-
}),
184-
[
185-
Bootstrap.Core.Patterns.list_generator(Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
186-
values: [Symbol.for('user'), Bootstrap.Core.Patterns.variable()]
187-
}), Object.freeze([new Bootstrap.Core.Tuple(Symbol.for('user'), 'john'), new Bootstrap.Core.Tuple(Symbol.for('admin'), 'john'), new Bootstrap.Core.Tuple(Symbol.for('user'), 'meg')]))
188-
],
189-
Elixir.ElixirScript.Collectable.__load(Elixir),
190-
Object.freeze([]))
162+
Bootstrap.Core.SpecialForms._for(Bootstrap.Core.Patterns.clause([new Bootstrap.Core.Tuple(Symbol.for('user'), Bootstrap.Core.Patterns.variable())], function(name) {
163+
return Elixir.ElixirScript.String.__load(Elixir).upcase(name);
164+
}, function() {
165+
return true;
166+
}), [Bootstrap.Core.Patterns.list_generator(new Bootstrap.Core.Tuple(Symbol.for('user'), Bootstrap.Core.Patterns.variable()), Object.freeze([new Bootstrap.Core.Tuple(Symbol.for('user'), 'john'), new Bootstrap.Core.Tuple(Symbol.for('admin'), 'john'), new Bootstrap.Core.Tuple(Symbol.for('user'), 'meg')]))], Elixir.ElixirScript.Collectable.__load(Elixir), Object.freeze([]))
191167
"""
192168

193169
assert_translation(ex_ast, js_code)

test/translator/function_test.exs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,13 @@ defmodule ElixirScript.Translator.Function.Test do
111111
end
112112

113113
js_code = """
114-
const test1 = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()], function(alpha, beta) {
115-
let [a, b] = Bootstrap.Core.Patterns.match(Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
116-
values: [Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()]
117-
}), new Bootstrap.Core.Tuple(1, 2));
118-
let _ref = new Bootstrap.Core.Tuple(a, b);
119-
return _ref;
120-
}));
114+
const test1 = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()], function(alpha, beta) {
115+
let [a, b] = Bootstrap.Core.Patterns.match(new Bootstrap.Core.Tuple(Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()), new Bootstrap.Core.Tuple(1, 2));
116+
117+
let _ref = new Bootstrap.Core.Tuple(a, b);
118+
119+
return _ref;
120+
}));
121121
"""
122122

123123
assert_translation(ex_ast, js_code)
@@ -429,11 +429,9 @@ defmodule ElixirScript.Translator.Function.Test do
429429

430430

431431
js_code = """
432-
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
433-
values: [Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()]
434-
})], function(apple, fruits) {
435-
return null;
436-
}));
432+
const something = Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([new Bootstrap.Core.Tuple(Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable())], function(apple, fruits) {
433+
return null;
434+
}));
437435
"""
438436

439437
assert_translation(ex_ast, js_code)

test/translator/match_test.exs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ defmodule ElixirScript.Translator.Match.Test do
1919
{a, b} = {1, 2}
2020
end
2121
js_code = """
22-
let [a, b] = Bootstrap.Core.Patterns.match(Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
23-
values: [Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()]
24-
}), new Bootstrap.Core.Tuple(1, 2));
25-
let _ref = new Bootstrap.Core.Tuple(a, b);
22+
let [a, b] = Bootstrap.Core.Patterns.match(
23+
new Bootstrap.Core.Tuple(
24+
Bootstrap.Core.Patterns.variable(),
25+
Bootstrap.Core.Patterns.variable()
26+
),
27+
new Bootstrap.Core.Tuple(1, 2)
28+
);
2629
"""
2730

2831
assert_translation(ex_ast, js_code)
2932

3033
ex_ast = quote do: {a, _, c} = {1, 2, 3}
3134
js_code = """
32-
let [a, __ignored__, c] = Bootstrap.Core.Patterns.match(Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
33-
values: [Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()]
34-
}), new Bootstrap.Core.Tuple(1, 2, 3));
35-
let _ref = new Bootstrap.Core.Tuple(a, __ignored__, c);
35+
let [a, __ignored__, c] = Bootstrap.Core.Patterns.match(new Bootstrap.Core.Tuple(Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()), new Bootstrap.Core.Tuple(1, 2, 3));
3636
"""
3737

3838
assert_translation(ex_ast, js_code)
@@ -43,10 +43,7 @@ defmodule ElixirScript.Translator.Match.Test do
4343
{^a, _, c} = {1, 2, 3}
4444
end
4545
js_code = """
46-
let [, __ignored__, c] = Bootstrap.Core.Patterns.match(Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple, {
47-
values: [Bootstrap.Core.Patterns.bound(a), Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()]
48-
}), new Bootstrap.Core.Tuple(1, 2, 3));
49-
let _ref = new Bootstrap.Core.Tuple(undefined, __ignored__, c);
46+
let [, __ignored__, c] = Bootstrap.Core.Patterns.match(new Bootstrap.Core.Tuple(Bootstrap.Core.Patterns.bound(a), Bootstrap.Core.Patterns.variable(), Bootstrap.Core.Patterns.variable()), new Bootstrap.Core.Tuple(1, 2, 3));
5047
"""
5148

5249
assert_translation(ex_ast, js_code)

test/translator/pattern_matching_test.exs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ defmodule ElixirScript.Translator.PatternMatching.Test do
193193
params = [{:{}, [], [1, {:b, [], Elixir}, 3]}]
194194
result = PatternMatching.build_match(params, scope )
195195
expected_result = {
196-
[PatternMatching.type(Primitive.tuple_class, JS.object_expression([JS.property(
197-
JS.identifier("values"),
198-
JS.array_expression([JS.literal(1), PatternMatching.parameter, JS.literal(3)])
199-
) ] )) ],
196+
[JS.new_expression(
197+
Primitive.tuple_class(),
198+
[JS.literal(1), PatternMatching.parameter, JS.literal(3)]
199+
)],
200200
[JS.identifier("b")]
201201
}
202202

@@ -205,10 +205,10 @@ defmodule ElixirScript.Translator.PatternMatching.Test do
205205
params = [{1, {:b, [], Elixir}}]
206206
result = PatternMatching.build_match(params, scope )
207207
expected_result = {
208-
[PatternMatching.type(Primitive.tuple_class, JS.object_expression([JS.property(
209-
JS.identifier("values"),
210-
JS.array_expression([JS.literal(1), PatternMatching.parameter])
211-
) ] )) ],
208+
[JS.new_expression(
209+
Primitive.tuple_class(),
210+
[JS.literal(1), PatternMatching.parameter]
211+
)],
212212
[JS.identifier("b")]
213213
}
214214

test/translator/with_test.exs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@ defmodule ElixirScript.Translator.With.Test do
1111
end
1212

1313
js_code = """
14-
Bootstrap.Core.SpecialForms._with([Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple,{
15-
values: [Symbol.for('ok'), Bootstrap.Core.Patterns.variable()]
16-
}), function() {
17-
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts,Symbol.for('width'));
18-
}],[Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple,{
19-
values: [Symbol.for('ok'), Bootstrap.Core.Patterns.variable()]
20-
}), function(width) {
21-
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts,Symbol.for('height'));
22-
}],function(width,height) {
23-
return new Bootstrap.Core.Tuple(Symbol.for('ok'),width * height);
14+
Bootstrap.Core.SpecialForms._with([new Bootstrap.Core.Tuple(Symbol.for('ok'), Bootstrap.Core.Patterns.variable()), function() {
15+
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts, Symbol.for('width'));
16+
}], [new Bootstrap.Core.Tuple(Symbol.for('ok'), Bootstrap.Core.Patterns.variable()), function(width) {
17+
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts, Symbol.for('height'));
18+
}], function(width, height) {
19+
return new Bootstrap.Core.Tuple(Symbol.for('ok'), width * height);
2420
})
2521
"""
2622

@@ -37,18 +33,14 @@ defmodule ElixirScript.Translator.With.Test do
3733
end
3834

3935
js_code = """
40-
Bootstrap.Core.SpecialForms._with([Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple,{
41-
values: [Symbol.for('ok'), Bootstrap.Core.Patterns.variable()]
42-
}), function() {
43-
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts,Symbol.for('width'));
44-
}],[Bootstrap.Core.Patterns.variable(), function(width) {
45-
return width * 2;
46-
}],[Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple,{
47-
values: [Symbol.for('ok'), Bootstrap.Core.Patterns.variable()]
48-
}), function(width,double_width) {
49-
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts,Symbol.for('height'));
50-
}],function(width,double_width,height) {
51-
return new Bootstrap.Core.Tuple(Symbol.for('ok'),double_width * height);
36+
Bootstrap.Core.SpecialForms._with([new Bootstrap.Core.Tuple(Symbol.for('ok'), Bootstrap.Core.Patterns.variable()), function() {
37+
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts, Symbol.for('width'));
38+
}], [Bootstrap.Core.Patterns.variable(), function(width) {
39+
return width * 2;
40+
}], [new Bootstrap.Core.Tuple(Symbol.for('ok'), Bootstrap.Core.Patterns.variable()), function(width, double_width) {
41+
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts, Symbol.for('height'));
42+
}], function(width, double_width, height) {
43+
return new Bootstrap.Core.Tuple(Symbol.for('ok'), double_width * height);
5244
})
5345
"""
5446

@@ -67,19 +59,15 @@ defmodule ElixirScript.Translator.With.Test do
6759
end
6860

6961
js_code = """
70-
Bootstrap.Core.SpecialForms._with([Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple,{
71-
values: [Symbol.for('ok'), Bootstrap.Core.Patterns.variable()]
72-
}), function() {
73-
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts,Symbol.for('width'));
74-
}],[Bootstrap.Core.Patterns.type(Bootstrap.Core.Tuple,{
75-
values: [Symbol.for('ok'), Bootstrap.Core.Patterns.variable()]
76-
}), function(width) {
77-
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts,Symbol.for('height'));
78-
}],function(width,height) {
79-
return new Bootstrap.Core.Tuple(Symbol.for('ok'),width * height);
80-
},Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Symbol.for('error')],function() {
81-
return new Bootstrap.Core.Tuple(Symbol.for('error'),Symbol.for('wrong_data'));
82-
})))
62+
Bootstrap.Core.SpecialForms._with([new Bootstrap.Core.Tuple(Symbol.for('ok'), Bootstrap.Core.Patterns.variable()), function() {
63+
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts, Symbol.for('width'));
64+
}], [new Bootstrap.Core.Tuple(Symbol.for('ok'), Bootstrap.Core.Patterns.variable()), function(width) {
65+
return Elixir.ElixirScript.Map.__load(Elixir).fetch(opts, Symbol.for('height'));
66+
}], function(width, height) {
67+
return new Bootstrap.Core.Tuple(Symbol.for('ok'), width * height);
68+
}, Bootstrap.Core.Patterns.defmatch(Bootstrap.Core.Patterns.clause([Symbol.for('error')], function() {
69+
return new Bootstrap.Core.Tuple(Symbol.for('error'), Symbol.for('wrong_data'));
70+
})))
8371
"""
8472

8573
assert_translation(ex_ast, js_code)

0 commit comments

Comments
 (0)