Repository: domluna/JuliaFormatter.jl (julia)

Issue:
leading zeros for negativ float literals without space
Thanks for the great package! While using I found a small bug i wanted to report:

```julia
a = -.2
b = - .2
```
becomes
```julia
a = -.2
b = -0.2
```
which is inconsistent.

Well personally I'd rather prefer to allow the concise `.2` and `1.` syntax in general but adding another option for that might be to much to ask for 😬

Pull request:
fixes #500 to the extent that the ruling is now consistent. These could be made optional later.

```
-.2 -> -0.2
```

This also generalizes the rule to other floating point suffixes than just "f32", so as can be seen in the diff 1f-5 will now become 1.0f-5, etc.

Patch:
diff --git a/Project.toml b/Project.toml
index 7f4a49199..bdfcc44ff 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.19.1"
+version = "0.19.2"
 
 [deps]
 CSTParser = "00ebfdb7-1f24-5e51-bd34-a7502290713f"
diff --git a/src/styles/default/pretty.jl b/src/styles/default/pretty.jl
index 041aade42..c6fde732a 100644
--- a/src/styles/default/pretty.jl
+++ b/src/styles/default/pretty.jl
@@ -364,25 +364,28 @@ end
             return FST(LITERAL, loc[2], loc[1], loc[1], "")
         end
 
-        if cst.head === :FLOAT && endswith(cst.val, "f0")
-            # Float32
-            val = val[1:end-2]
+        if cst.head === :FLOAT
+            fidx = findlast(c -> c == 'f', val)
+            float_suffix = ""
+            if fidx !== nothing
+                float_suffix = val[fidx:end]
+                val = val[1:fidx-1]
+            end
+
             dotidx = findlast(c -> c == '.', val)
-            if dotidx === nothing
+
+            if fidx !== nothing && dotidx === nothing
+                # append a trailing zero prior to the float suffix
                 val *= ".0"
             elseif dotidx == length(val)
-                val *= '0'
-            elseif dotidx == 1
-                val = '0' * val
-            end
-            val *= "f0"
-        elseif cst.head === :FLOAT
-            if endswith(cst.val, ".")
                 # If a floating point ends in `.`, add trailing zero.
                 val *= '0'
-            elseif startswith(cst.val, ".")
+            elseif dotidx == 1
                 val = '0' * val
+            elseif dotidx == 2 && val[1] == '-'
+                val = val[1] * '0' * val[2:end]
             end
+            val *= float_suffix
         end
 
         s.offset += length(cst.val) + (cst.fullspan - cst.span)


Test patch:
diff --git a/test/files/ChainRules.jl/test/rulesets/Base/mapreduce.jl b/test/files/ChainRules.jl/test/rulesets/Base/mapreduce.jl
index 7bf23f95d..cd6772276 100644
--- a/test/files/ChainRules.jl/test/rulesets/Base/mapreduce.jl
+++ b/test/files/ChainRules.jl/test/rulesets/Base/mapreduce.jl
@@ -181,7 +181,7 @@ struct SumRuleConfig <: RuleConfig{Union{HasReverseMode}} end
             end
         end
         @testset "Array{Float32}, no zero entries" begin
-            v = [1f-5, 1f-10, 1f-15, 1f-20]
+            v = [1.0f-5, 1.0f-10, 1.0f-15, 1.0f-20]
             @test prod(v) == 0
             @test unthunk(rrule(prod, v)[2](1.0f0)[2]) == zeros(4)
             test_rrule(prod, v)
diff --git a/test/files/ChainRules.jl/test/rulesets/LinearAlgebra/dense.jl b/test/files/ChainRules.jl/test/rulesets/LinearAlgebra/dense.jl
index 9fa6ac563..44b4e5a83 100644
--- a/test/files/ChainRules.jl/test/rulesets/LinearAlgebra/dense.jl
+++ b/test/files/ChainRules.jl/test/rulesets/LinearAlgebra/dense.jl
@@ -25,8 +25,8 @@
             F in (adjoint, permuteddimsarray)
 
             A = F(rand(T, 4, 3)) ⊢ F(rand(T, 4, 3))
-            test_frule(dot, rand(T, 3), A, rand(T, 4); rtol=1f-3)
-            test_rrule(dot, rand(T, 3), A, rand(T, 4); rtol=1f-3)
+            test_frule(dot, rand(T, 3), A, rand(T, 4); rtol=1.0f-3)
+            test_rrule(dot, rand(T, 3), A, rand(T, 4); rtol=1.0f-3)
         end
         @testset "different types" begin
             test_rrule(dot, rand(2), rand(2, 2), rand(ComplexF64, 2))
diff --git a/test/issues.jl b/test/issues.jl
index 2494c8655..23952a90b 100644
--- a/test/issues.jl
+++ b/test/issues.jl
@@ -563,6 +563,38 @@
         f = 0.123f0
         """
         @test fmt(str_) == str
+
+        @testset "500 - leading zeros with '-.'" begin
+            s0 = """
+            a = -.2
+            b = - .2
+            """
+            s1 = """
+            a = -0.2
+            b = -0.2
+            """
+            @test fmt(s0) == s1
+
+            s0 = """
+            a = -.2f32
+            b = - .2f32
+            """
+            s1 = """
+            a = -0.2f32
+            b = -0.2f32
+            """
+            @test fmt(s0) == s1
+
+            s0 = """
+            a = -.2f-5
+            b = - .2f-5
+            """
+            s1 = """
+            a = -0.2f-5
+            b = -0.2f-5
+            """
+            @test fmt(s0) == s1
+        end
     end
 
     @testset "issue 289 - no spaces/nesting for matrix elements" begin