Skip to content

Commit 9b1ec90

Browse files
authored
Merge pull request #20 from elixirscript/update_await_generation
Add parenthesis when an await is within an await or call
2 parents 8964889 + 28cb3fc commit 9b1ec90

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
sudo: false
2+
language: elixir
3+
elixir:
4+
- 1.4.2
5+
otp_release:
6+
- 19.3

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## Elixir-ESTree [![Documentation](https://img.shields.io/badge/docs-hexpm-blue.svg)](http://hexdocs.pm/estree/) [![Downloads](https://img.shields.io/hexpm/dt/estree.svg)](https://hex.pm/packages/estree)
2+
[![Build Status](https://travis-ci.org/elixirscript/elixir-estree.svg?branch=master)](https://travis-ci.org/elixirscript/elixir-estree)
23

34
Defines structs that represent the JavaScript AST nodes from the ESTree spec.
45

lib/es_tree/tools/generator.ex

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ defmodule ESTree.Tools.Generator do
263263

264264
# AwaitExpression
265265

266+
defp do_generate(%AwaitExpression{argument: %AwaitExpression{} = argument, all: _all}, opts) do
267+
["await ", "(", do_generate(argument, opts), ")"]
268+
end
269+
266270
defp do_generate(%AwaitExpression{argument: argument, all: _all}, opts) do
267271
["await ", do_generate(argument, opts)]
268272
end
@@ -347,7 +351,12 @@ defmodule ESTree.Tools.Generator do
347351
end
348352

349353
arguments = arguments
350-
|> Enum.map(&do_generate(&1, opts))
354+
|> Enum.map(fn
355+
%s{} = x when s in [AwaitExpression, YieldExpression] ->
356+
["("] ++ do_generate(x, opts) ++ [")"]
357+
x ->
358+
do_generate(x, opts)
359+
end)
351360
|> Enum.intersperse(opts.comma_sep)
352361

353362
[callee, "(", arguments, ")"]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
defmodule ESTree.Tools.Generator.AwaitExpression.Test do
2+
use ShouldI
3+
4+
alias ESTree.Tools.Builder
5+
import ESTree.Test.Support
6+
7+
should "convert await expression" do
8+
ast = Builder.await_expression(
9+
Builder.literal(1)
10+
)
11+
12+
assert_gen ast, "await 1"
13+
end
14+
15+
should "convert await expression that contains an await" do
16+
ast = Builder.await_expression(
17+
Builder.await_expression(
18+
Builder.literal(1)
19+
)
20+
)
21+
22+
assert_gen ast, "await (await 1)"
23+
end
24+
end

test/tools/generator/call_expression_test.exs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,32 @@ defmodule ESTree.Tools.Generator.CallExpression.Test do
9191
assert_gen ast, "(a + b).x()"
9292
assert_gen ast, "(a+b).x()", beauty: false
9393
end
94+
95+
should "convert call expression with one argument that is an await" do
96+
ast = Builder.call_expression(
97+
Builder.identifier(:x),
98+
[
99+
Builder.await_expression(
100+
Builder.identifier(:a)
101+
)
102+
]
103+
);
104+
105+
assert_gen ast, "x((await a))"
106+
assert_gen ast, "x((await a))", beauty: false
107+
end
108+
109+
should "convert call expression with one argument that is a yield" do
110+
ast = Builder.call_expression(
111+
Builder.identifier(:x),
112+
[
113+
Builder.yield_expression(
114+
Builder.identifier(:a)
115+
)
116+
]
117+
);
118+
119+
assert_gen ast, "x((yield a))"
120+
assert_gen ast, "x((yield a))", beauty: false
121+
end
94122
end

0 commit comments

Comments
 (0)