Repository: domluna/JuliaFormatter.jl (julia)

Issue:
Error formatting nested for...in
I am running into a bug that causes the formatter to error on nested `for..in` calls. Here is a minimal example that causes an error:

```
x = [[1,2,3], [4,5,6]]
print([z for y in x for z in y])
```

Let me know if there is a way to fix this, I'm not sure what the best approach would be. The following code works with the formatter, but doesn't produce the appropriate list and instead produces generators:

```
x = [[1,2,3], [4,5,6]]

print([(z for z in y) for y in x])
```

Pull request description:
fix #419 

verifies the latter part of a generator expression does not contain a FOR keyword before assuming it's a tuple expression

Patch:
diff --git a/Project.toml b/Project.toml
index adc4c4051..3d16ac011 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,7 +1,7 @@
 name = "JuliaFormatter"
 uuid = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
 authors = ["Dominique Luna <dluna132@gmail.com>"]
-version = "0.14.5"
+version = "0.14.6"
 
 [deps]
 CSTParser = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
diff --git a/src/styles/yas/pretty.jl b/src/styles/yas/pretty.jl
index 675957397..85860e8e4 100644
--- a/src/styles/yas/pretty.jl
+++ b/src/styles/yas/pretty.jl
@@ -337,8 +337,13 @@ function p_generator(ys::YASStyle, cst::CSTParser.EXPR, s::State)
                 for j = i+1:length(cst)
                     push!(tupargs, cst[j])
                 end
+
+                # verify this is not another for loop
+                any(b -> b.head === :FOR, tupargs) && continue
+
                 tup = p_tuple(style, tupargs, s)
                 add_node!(t, tup, s, join_lines = true)
+
                 if has_for_kw
                     for nn in tup.nodes
                         eq_to_in_normalization!(nn, s.opts.always_for_in)
@@ -366,7 +371,7 @@ function p_filter(ys::YASStyle, cst::CSTParser.EXPR, s::State)
 end
 
 function p_flatten(ys::YASStyle, cst::CSTParser.EXPR, s::State)
-    p_generator(ys, cst, s)
+    t = p_generator(ys, cst, s)
     t.typ = Flatten
     t
 end


Test patch:
diff --git a/test/issues.jl b/test/issues.jl
index 1da3e370c..4395f28f0 100644
--- a/test/issues.jl
+++ b/test/issues.jl
@@ -737,4 +737,27 @@
         """
         @test fmt(str) == str
     end
+
+    @testset "issue 419" begin
+        str = """
+        [z for y in x for z in y]
+        """
+        @test yasfmt(str) == str
+        @test yasfmt(str, margin = 25) == str
+
+        str_ = """
+        [z
+         for y in x for z in y]
+        """
+        @test yasfmt(str_, margin = 24) == str
+
+        str_ = """
+        [z
+         for y in
+             x
+         for z in
+             y]
+        """
+        @test yasfmt(str_, margin = 1) == str
+    end
 end