Skip to content

Commit

Permalink
Added support for SynExpr.Set, fsprojects#368
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jan 22, 2019
1 parent 782ebb5 commit 73440bf
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>2.9.1</Version>
<NoWarn>FS0988</NoWarn>
Expand Down Expand Up @@ -44,6 +44,7 @@
<Compile Include="LazyTests.fs" />
<Compile Include="LongIdentWithDotsTests.fs" />
<Compile Include="DynamicOperatorTests.fs" />
<Compile Include="SynExprSetTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas\Fantomas.fsproj" />
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Tests/FormattingPropertyTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ let rec shrinkSynExpr = function
| SynExpr.DotIndexedGet(expr, _, _, _)
| SynExpr.DotGet(expr, _, _, _)
| SynExpr.DotSet(expr, _, _, _)
| SynExpr.Set(expr, _, _)
| SynExpr.TypeTest(expr, _, _)
| SynExpr.Upcast(expr, _, _)
| SynExpr.Downcast(expr, _, _)
Expand Down
46 changes: 46 additions & 0 deletions src/Fantomas.Tests/SynExprSetTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Fantomas.Tests.SynExprSetTests

open NUnit.Framework
open FsUnit
open Fantomas.Tests.TestHelper

/// See https://github.com/fsharp/fsharp/blob/master/src/fsharp/ast.fs#L633
/// F# syntax: expr <- expr
/// | Set of SynExpr * SynExpr * range:range
[<Test>]
let ``array indexer set`` () =
formatSourceString false """
let arr = [|0|]
(arr.[0]) <- 1
""" config
|> should equal """let arr = [| 0 |]
(arr.[0]) <- 1
"""

[<Test>]
let ``setter of type set`` () =
formatSourceString false """
type T() =
member val X = 0 with get, set
(T().X) <- 1
""" config
|> prepend newline
|> should equal """
type T() =
member val X = 0 with get, set
(T().X) <- 1
"""

[<Test>]
let ``mutable value set`` () =
formatSourceString false """
let mutable x = 0
(x) <- 1
""" config
|> should equal """let mutable x = 0
(x) <- 1
"""
3 changes: 2 additions & 1 deletion src/Fantomas/CodeFormatterImpl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ let isValidAST ast =
| SynExpr.DotGet(synExpr, _dotm, _longIdent, _range) ->
validateExpr synExpr

| SynExpr.DotSet(synExpr1, _longIdent, synExpr2, _range) ->
| SynExpr.DotSet(synExpr1, _, synExpr2, _)
| SynExpr.Set (synExpr1, synExpr2, _) ->
List.forall validateExpr [synExpr1; synExpr2]

| SynExpr.DotIndexedGet(synExpr, IndexerArgList synExprList, _range, _range2) ->
Expand Down
7 changes: 6 additions & 1 deletion src/Fantomas/CodePrinter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ and genSigModuleOrNamespace astContext (SigModuleOrNamespace(ats, px, ao, s, mds
+> opt sepSpace ao genAccess -- s +> rep 2 sepNln)
+> genSigModuleDeclList astContext mds

and genModuleDeclList astContext = function
and genModuleDeclList astContext e =
match e with
| [x] -> genModuleDecl astContext x

| OpenL(xs, ys) ->
Expand Down Expand Up @@ -713,6 +714,10 @@ and genExpr astContext synExpr =
let exprF = genExpr { astContext with IsInsideDotGet = true }
addParenIfAutoNln e exprF -- (sprintf ".%s" s)
| DotSet(e1, s, e2) -> addParenIfAutoNln e1 (genExpr astContext) -- sprintf ".%s <- " s +> genExpr astContext e2

| SynExpr.Set(e1,e2, _) ->
addParenIfAutoNln e1 (genExpr astContext) -- sprintf " <- " +> genExpr astContext e2

| LetOrUseBang(isUse, p, e1, e2) ->
atCurrentColumn (ifElse isUse (!- "use! ") (!- "let! ")
+> genPat astContext p -- " = " +> genExpr astContext e1 +> sepNln +> genExpr astContext e2)
Expand Down

0 comments on commit 73440bf

Please sign in to comment.