Skip to content

Commit

Permalink
Take commas into account (#2880)
Browse files Browse the repository at this point in the history
* Update FCS to e267bb9f8d590feed1b94b469d78cfce61afecad.

* Use commas as nodes in PatTupleNode.

* Respect commas in SynSimplePats.SimplePats.

* Pick correct comma in pattern in mkPropertyGetSetBinding.

* Update src/Fantomas.Core.Tests/TypeDeclarationTests.fs

Co-authored-by: dawe <[email protected]>

* 6.0.3 release

---------

Co-authored-by: dawe <[email protected]>
  • Loading branch information
nojaf and dawedawe authored May 14, 2023
1 parent 680ec8b commit 25b6f95
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 67 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Changelog

## [Unreleased]
## [6.0.3] - 2023-05-14

### Fixed
* Preserves quotes around type parameter names. [#2875](https://github.com/fsprojects/fantomas/issues/2875)
* Additional whitespace for LineCommentAfterSourceCode when last character is a `,`. [#2589](https://github.com/fsprojects/fantomas/issues/2589)
* Tupled parameter wrapped in conditional directive. [#2877](https://github.com/fsprojects/fantomas/issues/2877)

### Changed
* Update FCS to 'Add commas to tuple pat and simple pats', commit e267bb9f8d590feed1b94b469d78cfce61afecad

## [6.0.2] - 2023-05-05

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Some common use cases include:

<!-- Versions -->
<PropertyGroup>
<FCSCommitHash>ba6647ebf5b94823c4d6fafd1e7d5f806d915ee0</FCSCommitHash>
<FCSCommitHash>e267bb9f8d590feed1b94b469d78cfce61afecad</FCSCommitHash>
<StreamJsonRpcVersion>2.8.28</StreamJsonRpcVersion>
<FSharpCoreVersion>6.0.1</FSharpCoreVersion>
</PropertyGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Fantomas.Core.Tests/CommentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2080,8 +2080,8 @@ type CreateFSharpManifestResourceName public () =
(
fileName: string,
linkFileName: string,
rootNamespace: string, // may be null
dependentUponFileName: string, // may be null
rootNamespace: string, // may be null
dependentUponFileName: string, // may be null
binaryStream: Stream // may be null
) : string =
()
Expand Down Expand Up @@ -2570,7 +2570,7 @@ type MyType2 =
type MyType =
member _.MyMethod
(
[<MyAttribute>] inputA: string, // my comment 1
[<MyAttribute>] inputA: string, // my comment 1
[<MyAttribute>] inputB: string // my comment 2
) =
inputA
Expand Down
36 changes: 36 additions & 0 deletions src/Fantomas.Core.Tests/CompilerDirectivesTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3286,3 +3286,39 @@ module
A =
let f x = x + x
"""

[<Test>]
let ``conditional directives around last tuple pattern, 2877`` () =
formatSourceString
false
"""
// Link all the assemblies together and produce the input typecheck accumulator
let CombineImportedAssembliesTask
(
a,
b
#if !NO_TYPEPROVIDERS
, c
#endif
) =
()
"""
config
|> prepend newline
|> should
equal
"""
// Link all the assemblies together and produce the input typecheck accumulator
let CombineImportedAssembliesTask
(
a,
b
#if !NO_TYPEPROVIDERS
,
c
#endif
) =
()
"""
25 changes: 25 additions & 0 deletions src/Fantomas.Core.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2140,3 +2140,28 @@ let inline (!!) (x: ^a) : ^b = ((^a or ^b): (static member op_Implicit: ^a -> ^b
let inline (!!) (x: ^a) : ^b =
((^a or ^b): (static member op_Implicit: ^a -> ^b) x)
"""

[<Test>]
let ``avoid additional whitespace after comma, 2589`` () =
formatSourceString
false
"""
let x
(
a: string, // test
b: string // test
) =
print "hello"
"""
config
|> prepend newline
|> should
equal
"""
let x
(
a: string, // test
b: string // test
) =
print "hello"
"""
33 changes: 33 additions & 0 deletions src/Fantomas.Core.Tests/TypeDeclarationTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3587,3 +3587,36 @@ type ArrayBuffer =
abstract byteLength: int
abstract slice: ``begin``: int * ?``end``: int -> ArrayBuffer
"""

[<Test>]
let ``trivia before comma in primary constructor`` () =
formatSourceString
false
"""
type Meh
(
a,
b
#if !NO_TYPEPROVIDERS
, c
#endif
) =
class end
"""
config
|> prepend newline
|> should
equal
"""
type Meh
(
a,
b
#if !NO_TYPEPROVIDERS
,
c
#endif
) =
class
end
"""
107 changes: 68 additions & 39 deletions src/Fantomas.Core/ASTTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,18 @@ let (|PatParameter|_|) (p: SynPat) =

let mkUnit (StartEndRange 1 (lpr, m, rpr)) = UnitNode(stn "(" lpr, stn ")" rpr, m)

let mkTuplePat (creationAide: CreationAide) (pats: SynPat list) (commas: range list) (m: range) =
match pats with
| [] -> failwith "SynPat.Tuple with no elements"
| head :: tail ->
let rest =
assert (tail.Length = commas.Length)

List.zip commas tail
|> List.collect (fun (c, e) -> [ yield Choice2Of2(stn "," c); yield Choice1Of2(mkPat creationAide e) ])

PatTupleNode([ yield Choice1Of2(mkPat creationAide head); yield! rest ], m)

let mkPat (creationAide: CreationAide) (p: SynPat) =
let patternRange = p.Range

Expand Down Expand Up @@ -1650,8 +1662,8 @@ let mkPat (creationAide: CreationAide) (p: SynPat) =
| SynPat.Paren(p, StartEndRange 1 (lpr, _, rpr)) ->
PatParenNode(stn "(" lpr, mkPat creationAide p, stn ")" rpr, patternRange)
|> Pattern.Paren
| SynPat.Tuple(false, ps, _) -> PatTupleNode(List.map (mkPat creationAide) ps, patternRange) |> Pattern.Tuple
| SynPat.Tuple(true, ps, _) ->
| SynPat.Tuple(false, ps, commas, _) -> mkTuplePat creationAide ps commas patternRange |> Pattern.Tuple
| SynPat.Tuple(true, ps, _, _) ->
PatStructTupleNode(List.map (mkPat creationAide) ps, patternRange)
|> Pattern.StructTuple
| SynPat.ArrayOrList(isArray, ps, range) ->
Expand Down Expand Up @@ -1862,7 +1874,8 @@ let mkExternBinding
let identifier, openNode, parameters, closeNode =
match pat with
| SynPat.LongIdent(
longDotId = longDotId; argPats = SynArgPats.Pats [ SynPat.Tuple(_, ps, StartEndRange 1 (mOpen, _, mClose)) ]) ->
longDotId = longDotId
argPats = SynArgPats.Pats [ SynPat.Tuple(_, ps, _, StartEndRange 1 (mOpen, _, mClose)) ]) ->
mkSynLongIdent longDotId, stn "(" mOpen, List.map mkExternPat ps, stn ")" mClose
| _ -> failwith "expecting a SynPat.LongIdent for extern binding"

Expand Down Expand Up @@ -2255,6 +2268,28 @@ let mkSynUnionCase
fullRange
)

let mkSynSimplePat creationAide (pat: SynSimplePat) =
match pat with
| SynSimplePat.Attrib(SynSimplePat.Typed(SynSimplePat.Id(ident = ident; isOptional = isOptional), t, _),
attributes,
m) ->
Some(
SimplePatNode(
mkAttributes creationAide attributes,
isOptional,
mkIdent ident,
Some(mkType creationAide t),
m
)
)
| SynSimplePat.Typed(SynSimplePat.Id(ident = ident; isOptional = isOptional), t, m) ->
Some(SimplePatNode(mkAttributes creationAide [], isOptional, mkIdent ident, Some(mkType creationAide t), m))
| SynSimplePat.Attrib(SynSimplePat.Id(ident = ident; isOptional = isOptional), attributes, m) ->
Some(SimplePatNode(mkAttributes creationAide attributes, isOptional, mkIdent ident, None, m))
| SynSimplePat.Id(ident = ident; isOptional = isOptional; range = m) ->
Some(SimplePatNode(mkAttributes creationAide [], isOptional, mkIdent ident, None, m))
| _ -> None

let mkImplicitCtor
creationAide
vis
Expand All @@ -2263,43 +2298,30 @@ let mkImplicitCtor
(self: (range * Ident) option)
(xmlDoc: PreXmlDoc)
=
let openNode, closeNode =
let openNode, pats, commas, closeNode =
match pats with
| SynSimplePats.SimplePats(range = StartEndRange 1 (mOpen, _, mClose))
| SynSimplePats.Typed(range = StartEndRange 1 (mOpen, _, mClose)) -> stn "(" mOpen, stn ")" mClose
| SynSimplePats.SimplePats(pats = pats; commaRanges = commas; range = StartEndRange 1 (mOpen, _, mClose)) ->
stn "(" mOpen, pats, commas, stn ")" mClose

let pats =
match pats with
| SynSimplePats.SimplePats(pats = pats) -> pats
| SynSimplePats.Typed _ -> []
|> List.choose (function
| SynSimplePat.Attrib(SynSimplePat.Typed(SynSimplePat.Id(ident = ident; isOptional = isOptional), t, _),
attributes,
m) ->
Some(
SimplePatNode(
mkAttributes creationAide attributes,
isOptional,
mkIdent ident,
Some(mkType creationAide t),
m
)
)
| SynSimplePat.Typed(SynSimplePat.Id(ident = ident; isOptional = isOptional), t, m) ->
Some(
SimplePatNode(
mkAttributes creationAide [],
isOptional,
mkIdent ident,
Some(mkType creationAide t),
m
)
)
| SynSimplePat.Attrib(SynSimplePat.Id(ident = ident; isOptional = isOptional), attributes, m) ->
Some(SimplePatNode(mkAttributes creationAide attributes, isOptional, mkIdent ident, None, m))
| SynSimplePat.Id(ident = ident; isOptional = isOptional; range = m) ->
Some(SimplePatNode(mkAttributes creationAide [], isOptional, mkIdent ident, None, m))
| _ -> None)
| [] ->
// Unit pattern
[]
| head :: tail ->
let rest =
assert (tail.Length = commas.Length)

List.zip commas tail
|> List.collect (fun (c, p) ->
match mkSynSimplePat creationAide p with
| None -> []
| Some simplePat -> [ Choice2Of2(stn "," c); Choice1Of2 simplePat ])

[ match mkSynSimplePat creationAide head with
| None -> ()
| Some simplePat -> yield Choice1Of2 simplePat
yield! rest ]

let range =
let startRange =
Expand Down Expand Up @@ -2540,18 +2562,25 @@ let mkPropertyGetSetBinding

let pats =
match ps with
| [ SynPat.Tuple(false, [ p1; p2; p3 ], _) ] ->
| [ SynPat.Tuple(false, [ p1; p2; p3 ], [ comma ], _) ] ->
let mTuple = unionRanges p1.Range p2.Range

[ PatParenNode(
stn "(" Range.Zero,
Pattern.Tuple(PatTupleNode([ mkPat creationAide p1; mkPat creationAide p2 ], mTuple)),
Pattern.Tuple(
PatTupleNode(
[ Choice1Of2(mkPat creationAide p1)
Choice2Of2(stn "," comma)
Choice1Of2(mkPat creationAide p2) ],
mTuple
)
),
stn ")" Range.Zero,
mTuple
)
|> Pattern.Paren
mkPat creationAide p3 ]
| [ SynPat.Tuple(false, [ p1; p2 ], _) ] -> [ mkPat creationAide p1; mkPat creationAide p2 ]
| [ SynPat.Tuple(false, [ p1; p2 ], _, _) ] -> [ mkPat creationAide p1; mkPat creationAide p2 ]
| ps -> List.map (mkPat creationAide) ps

let range = unionRanges extraIdent.idRange e.Range
Expand Down
Loading

0 comments on commit 25b6f95

Please sign in to comment.