Skip to content

Commit ea8af47

Browse files
josh-degrawnojaf
andcommitted
Consolidate and share logic for Records and AnonymousRecords (#2750)
* Remove AnonRecordFieldNode * Consolidate a lot of logic Introduce active pattern to join implementation for Record & AnonRecords Consolidate some more logic to fix an edge case Consolidate a bit more Remove unintentional change, cleanup Refactor to remove active pattern * Add ExprCopyableRecordNode subclass to better separate concerns * Clean up a bit * Remove a type, rename some others * Inline some one-off functions * Format CodePrinter.fs * Add some additional XML documentation. --------- Co-authored-by: nojaf <[email protected]>
1 parent 9681ea5 commit ea8af47

File tree

6 files changed

+306
-248
lines changed

6 files changed

+306
-248
lines changed

src/Fantomas.Core.Tests/CrampedMultilineBracketStyleTests.fs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,41 @@ let person =
656656
()
657657
"
658658

659+
[<Test>]
660+
let ``multiline string before closing brace with anonymous record`` () =
661+
formatSourceString
662+
false
663+
"
664+
let person =
665+
let y =
666+
let x =
667+
{| Story = \"\"\"
668+
foo
669+
bar
670+
\"\"\"
671+
|}
672+
()
673+
()
674+
"
675+
config
676+
|> prepend newline
677+
|> should
678+
equal
679+
"
680+
let person =
681+
let y =
682+
let x =
683+
{| Story =
684+
\"\"\"
685+
foo
686+
bar
687+
\"\"\" |}
688+
689+
()
690+
691+
()
692+
"
693+
659694
[<Test>]
660695
let ``issue 457`` () =
661696
formatSourceString

src/Fantomas.Core/ASTTransformer.fs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -952,14 +952,6 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr =
952952
ExprArrayOrListNode(o, [ mkExpr creationAide singleExpr ], c, exprRange)
953953
|> Expr.ArrayOrList
954954
| SynExpr.Record(baseInfo, copyInfo, recordFields, StartEndRange 1 (mOpen, _, mClose)) ->
955-
let extra =
956-
match baseInfo, copyInfo with
957-
| Some _, Some _ -> failwith "Unexpected that both baseInfo and copyInfo are present in SynExpr.Record"
958-
| Some(t, e, mInherit, _, m), None ->
959-
mkInheritConstructor creationAide t e mInherit m |> RecordNodeExtra.Inherit
960-
| None, Some(copyExpr, _) -> mkExpr creationAide copyExpr |> RecordNodeExtra.With
961-
| None, None -> RecordNodeExtra.None
962-
963955
let fieldNodes =
964956
recordFields
965957
|> List.choose (function
@@ -968,20 +960,37 @@ let mkExpr (creationAide: CreationAide) (e: SynExpr) : Expr =
968960
Some(RecordFieldNode(mkSynLongIdent fieldName, stn "=" mEq, mkExpr creationAide expr, m))
969961
| _ -> None)
970962

971-
ExprRecordNode(stn "{" mOpen, extra, fieldNodes, stn "}" mClose, exprRange)
972-
|> Expr.Record
973-
| SynExpr.AnonRecd(isStruct, copyInfo, recordFields, EndRange 2 (mClose, _), trivia) ->
963+
match baseInfo, copyInfo with
964+
| Some _, Some _ -> failwith "Unexpected that both baseInfo and copyInfo are present in SynExpr.Record"
965+
| Some(t, e, mInherit, _, m), None ->
966+
let inheritCtor = mkInheritConstructor creationAide t e mInherit m
967+
968+
ExprInheritRecordNode(stn "{" mOpen, inheritCtor, fieldNodes, stn "}" mClose, exprRange)
969+
|> Expr.InheritRecord
970+
| None, Some(copyExpr, _) ->
971+
let copyExpr = mkExpr creationAide copyExpr
972+
973+
ExprRecordNode(stn "{" mOpen, Some copyExpr, fieldNodes, stn "}" mClose, exprRange)
974+
|> Expr.Record
975+
| None, None ->
976+
ExprRecordNode(stn "{" mOpen, None, fieldNodes, stn "}" mClose, exprRange)
977+
|> Expr.Record
978+
| SynExpr.AnonRecd(isStruct, copyInfo, recordFields, StartEndRange 2 (mOpen, _, mClose)) ->
974979
let fields =
975980
recordFields
976981
|> List.choose (function
977982
| ident, Some mEq, e ->
978983
let m = unionRanges ident.idRange e.Range
979-
Some(AnonRecordFieldNode(mkIdent ident, stn "=" mEq, mkExpr creationAide e, m))
984+
985+
let longIdent =
986+
IdentListNode([ IdentifierOrDot.Ident(mkIdent ident) ], ident.idRange)
987+
988+
Some(RecordFieldNode(longIdent, stn "=" mEq, mkExpr creationAide e, m))
980989
| _ -> None)
981990

982991
ExprAnonRecordNode(
983992
isStruct,
984-
stn "{|" trivia.OpeningBraceRange,
993+
stn "{|" mOpen,
985994
Option.map (fst >> mkExpr creationAide) copyInfo,
986995
fields,
987996
stn "|}" mClose,

0 commit comments

Comments
 (0)