Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't put a space before a prefix operator, unless we have to. #3134

Merged
merged 4 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/ControlStructureTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ let genPropertyWithGetSet astContext (b1, b2) rangeOfMember =
genPreXmlDoc px
+> genAttributes astContext ats
+> genMemberFlags astContext mf1
+> ifElse isInline (!- "inline ") sepNone
+> ifElse isInline (!-"inline ") sepNone
+> opt sepSpace ao genAccess

assert (ps1 |> Seq.map fst |> Seq.forall Option.isNone)
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<Compile Include="BeginEndTests.fs" />
<Compile Include="NullnessTests.fs" />
<Compile Include="AutoPropertiesTests.fs" />
<Compile Include="PrefixTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas.Core\Fantomas.Core.fsproj" />
Expand Down
14 changes: 0 additions & 14 deletions src/Fantomas.Core.Tests/InterpolatedStringTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -165,20 +165,6 @@ $\"\"\"one: {1}<
>two: {2}\"\"\"
"

[<Test>]
let ``prefix application, 1414`` () =
formatSourceString
"""
!- $".{s}"
"""
config
|> prepend newline
|> should
equal
"""
!- $".{s}"
"""

[<Test>]
let ``format in FillExpr, 1549`` () =
formatSourceString
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/LambdaTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ let genMemberFlagsForMemberBinding astContext (mf: MemberFlags) (rangeOfBindingA
| Token { TokenInfo = { TokenName = "MEMBER" } } -> r.StartLine = rangeOfBindingAndRhs.StartLine

| _ -> false)
|> Option.defaultValue (!- "override ")
|> Option.defaultValue (!-"override ")
<| ctx)
<| ctx
"""
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Core.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ let internal sepSpace =
then
ctx
else
(!- " ") ctx
(!-" ") ctx
"""

[<Test>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ let expr =
es
(fun e ->
match e with
| Paren(_, Lambda _, _) -> !- "lambda"
| Paren(_, Lambda _, _) -> !-"lambda"
| _ -> genExpr astContext e
)
"""
Expand Down
117 changes: 0 additions & 117 deletions src/Fantomas.Core.Tests/OperatorTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,6 @@ open FsUnit
open Fantomas.Core.Tests.TestHelpers
open Fantomas.Core

[<Test>]
let ``should format prefix operators`` () =
formatSourceString
"""let x = -y
let z = !!x
"""
config
|> should
equal
"""let x = -y
let z = !!x
"""

[<Test>]
let ``should keep triple ~~~ operator`` () =
formatSourceString
"""x ~~~FileAttributes.ReadOnly
"""
config
|> should
equal
"""x ~~~FileAttributes.ReadOnly
"""

[<Test>]
let ``should keep single triple ~~~ operator`` () =
formatSourceString
"""~~~FileAttributes.ReadOnly
"""
config
|> should
equal
"""~~~FileAttributes.ReadOnly
"""

[<Test>]
let ``should keep parens around ? operator definition`` () =
formatSourceString
Expand All @@ -62,17 +27,6 @@ let ``should keep parens around ?<- operator definition`` () =
"""let (?<-) f s = f s
"""

[<Test>]
let ``should keep parens around !+ prefix operator definition`` () =
formatSourceString
"""let (!+) x = Include x
"""
config
|> should
equal
"""let (!+) x = Include x
"""

[<Test>]
let ``should keep parens around ++ infix operator definition`` () =
formatSourceString
Expand Down Expand Up @@ -474,19 +428,6 @@ let result =
(typ.GetInterface(typeof<System.Collections.IEnumerable>.FullName) = null)
"""

[<Test>]
let ``operator before verbatim string add extra space, 736`` () =
formatSourceString
"""Target M.Tools (fun _ -> !! @"Tools\Tools.sln" |> rebuild)
"""
config
|> prepend newline
|> should
equal
"""
Target M.Tools (fun _ -> !! @"Tools\Tools.sln" |> rebuild)
"""

[<Test>]
let ``function call before pipe operator, 754`` () =
formatSourceString
Expand Down Expand Up @@ -1178,42 +1119,6 @@ module Foo =
| false -> id)
"""

let operator_application_literal_values =
[ "-86y"
"86uy"
"-86s"
"86us"
"-86"
"-86l"
"86u"
"86ul"
"-123n"
"0x00002D3Fun"
"-86L"
"86UL"
"-4.41F"
"-4.14"
"-12456I"
"-0.7833M"
"'a'"
"\"text\""
"'a'B"
"\"text\"B" ]

[<TestCaseSource("operator_application_literal_values")>]
let ``operators maintain spacing from literal values`` (literalValue: string) =
formatSourceString
$"""
let subtractTwo = + %s{literalValue}
"""
config
|> prepend newline
|> should
equal
$"""
let subtractTwo = + %s{literalValue}
"""

[<Test>]
let ``qualified name to active pattern, 1937`` () =
formatSourceString
Expand Down Expand Up @@ -1515,25 +1420,3 @@ let allDecls =
@+ iimplsLs
@+ ctorLs
"""

[<Test>]
let ``adding space after prefix operator breaks code, 2796`` () =
formatSourceString
"""
let inline (~%%) id = int id

let f a b = a + b

let foo () = f %%"17" %%"42"
"""
config
|> prepend newline
|> should
equal
"""
let inline (~%%) id = int id

let f a b = a + b

let foo () = f %%"17" %%"42"
"""
169 changes: 169 additions & 0 deletions src/Fantomas.Core.Tests/PrefixTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
module Fantomas.Core.Tests.PrefixTests

open NUnit.Framework
open FsUnit
open Fantomas.Core.Tests.TestHelpers
open Fantomas.Core

[<Test>]
let ``should format prefix operators`` () =
formatSourceString
"""let x = -y
let z = !!x
"""
config
|> should
equal
"""let x = -y
let z = !!x
"""

[<Test>]
let ``should keep triple ~~~ operator`` () =
formatSourceString
"""x ~~~FileAttributes.ReadOnly
"""
config
|> should
equal
"""x ~~~FileAttributes.ReadOnly
"""

[<Test>]
let ``should keep single triple ~~~ operator`` () =
formatSourceString
"""~~~FileAttributes.ReadOnly
"""
config
|> should
equal
"""~~~FileAttributes.ReadOnly
"""

[<Test>]
let ``operator before verbatim string add extra space, 736`` () =
formatSourceString
"""Target M.Tools (fun _ -> !! @"Tools\Tools.sln" |> rebuild)
"""
config
|> prepend newline
|> should
equal
"""
Target M.Tools (fun _ -> !! @"Tools\Tools.sln" |> rebuild)
"""

[<Test>]
let ``should keep parens around !+ prefix operator definition`` () =
formatSourceString
"""let (!+) x = Include x
"""
config
|> should
equal
"""let (!+) x = Include x
"""

[<Test>]
let ``adding space after prefix operator breaks code, 2796`` () =
formatSourceString
"""
let inline (~%%) id = int id

let f a b = a + b

let foo () = f %%"17" %%"42"
"""
config
|> prepend newline
|> should
equal
"""
let inline (~%%) id = int id

let f a b = a + b

let foo () = f %%"17" %%"42"
"""

[<Test>]
let ``tilde unary operator with literal and variable, 3131`` () =
formatSourceString
"""
let x = ~~1
let y = ~~x
"""
config
|> prepend newline
|> should
equal
"""
let x = ~~1
let y = ~~x
"""

[<Test>]
let ``prefix application with interpolated string, 1414`` () =
formatSourceString
"""
!- $".{s}"
"""
config
|> prepend newline
|> should
equal
"""
!- $".{s}"
"""

let operator_application_literal_values_with_sign =
[ "-86y"
"-86s"
"-86"
"-86l"
"-123n"
"-86L"
"-4.41F"
"-4.14"
"-12456I"
"-0.7833M" ]

[<TestCaseSource("operator_application_literal_values_with_sign")>]
let ``operators maintain spacing from literal values which start with + or -`` (literalValue: string) =
formatSourceString
$"""
let subtractTwo = + %s{literalValue}
"""
config
|> prepend newline
|> should
equal
$"""
let subtractTwo = + %s{literalValue}
"""

let operator_application_literal_values_without_sign =
[ "86uy"
"86us"
"86u"
"86ul"
"0x00002D3Fun"
"86UL"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"86UL"
"86UL"
"1<m>

Or in a separate test if you like.

"'a'"
"\"text\""
"'a'B"
"\"text\"B" ]

[<TestCaseSource("operator_application_literal_values_without_sign")>]
let ``operators maintain spacing from literal values which start without + or -`` (literalValue: string) =
formatSourceString
$"""
let subtractTwo = + %s{literalValue}
"""
config
|> prepend newline
|> should
equal
$"""
let subtractTwo = +%s{literalValue}
"""
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ let ``ignore setting when function call is the argument of prefix application``
|> should
equal
"""
!- String.Empty.padLeft(braceSize + spaceAround)
(!- System.String.Empty.padRight(delta)) ({ ctx with RecordBraceStart = rest })
!- meh()
!-String.Empty.padLeft(braceSize + spaceAround)
(!-System.String.Empty.padRight(delta)) ({ ctx with RecordBraceStart = rest })
!-meh()
"""

[<Test>]
Expand Down
Loading
Loading