From 6f1a109d2c802a29db8f84fce159ed5aa4905394 Mon Sep 17 00:00:00 2001 From: Konto M Date: Sat, 16 May 2020 11:04:12 -0400 Subject: [PATCH 1/6] Preserve return type attributes --- src/Fantomas.Tests/LetBindingTests.fs | 8 +++++++- src/Fantomas/CodePrinter.fs | 20 +++++++++++++------- src/Fantomas/SourceParser.fs | 6 +++--- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Fantomas.Tests/LetBindingTests.fs b/src/Fantomas.Tests/LetBindingTests.fs index 8432cabf77..42bcb7926c 100644 --- a/src/Fantomas.Tests/LetBindingTests.fs +++ b/src/Fantomas.Tests/LetBindingTests.fs @@ -546,4 +546,10 @@ let ``multiple empty lines between equals and expression`` () = () -""" \ No newline at end of file +""" + +[] +let ``should preserve return attributes`` () = + formatSourceString false """let f x : [] int = x""" config + |> should equal """let f x: [] int = x +""" diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 7b9d3f647e..be218e24ef 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -450,7 +450,7 @@ and addSpaceAfterGenericConstructBeforeColon ctx = sepNone <| ctx -and genExprSepEqPrependType astContext (pat:SynPat) (e: SynExpr) (isPrefixMultiline: bool) ctx = +and genExprSepEqPrependType astContext (pat:SynPat) (e: SynExpr) (valInfo:SynValInfo option) (isPrefixMultiline: bool) ctx = let hasTriviaContentAfterEqual = ctx.Trivia |> List.exists (fun tn -> @@ -486,9 +486,15 @@ and genExprSepEqPrependType astContext (pat:SynPat) (e: SynExpr) (isPrefixMultil +> enterNode t.Range +> ifElse hasLineCommentBeforeColon unindent sepNone) ctx + let genMetadataAttributes ctx = + match valInfo with + | Some(SynValInfo(_, SynArgInfo(attributes, _, _))) -> genOnelinerAttributes astContext attributes ctx + | None -> sepNone ctx + (addExtraSpaceBeforeGenericType +> genCommentBeforeColon +> sepColon + +> genMetadataAttributes +> genType astContext false t +> sepEqual (isPrefixMultiline || hasLineCommentBeforeColon) +> ifElse (isPrefixMultiline || hasTriviaContentAfterEqual || hasLineCommentBeforeColon) @@ -524,7 +530,7 @@ and genTypeParamPostfix astContext tds tcs = genTypeAndParam astContext "" tds t and genLetBinding astContext pref b = match b with - | LetBinding(ats, px, ao, isInline, isMutable, p, e) -> + | LetBinding(ats, px, ao, isInline, isMutable, p, e, valInfo) -> let genPat = match e, p with | TypedExpr(Typed, _, t), PatLongIdent(ao, s, ps, tpso) when (List.length ps > 1)-> @@ -550,7 +556,7 @@ and genLetBinding astContext pref b = +> genAttr // this already contains the `let` or `and` keyword +> leadingExpressionIsMultiline (afterLetKeyword +> genPat +> enterNodeTokenByName rangeBetweenBindingPatternAndExpression "EQUALS") - (genExprSepEqPrependType astContext p e) + (genExprSepEqPrependType astContext p e (Some(valInfo))) | DoBinding(ats, px, e) -> let prefix = if pref.Contains("let") then pref.Replace("let", "do") else "do " @@ -563,7 +569,7 @@ and genLetBinding astContext pref b = |> genTrivia b.RangeOfBindingSansRhs and genShortGetProperty astContext (pat:SynPat) e = - genExprSepEqPrependType astContext pat e false + genExprSepEqPrependType astContext pat e None false and genProperty astContext prefix ao propertyKind ps e = let tuplerize ps = @@ -579,12 +585,12 @@ and genProperty astContext prefix ao propertyKind ps e = !- prefix +> opt sepSpace ao genAccess -- propertyKind +> ifElse (List.atMostOne ps) (col sepComma ps (genPat astContext) +> sepSpace) (sepOpenT +> col sepComma ps (genPat astContext) +> sepCloseT +> sepSpace) - +> genPat astContext p +> genExprSepEqPrependType astContext p e false + +> genPat astContext p +> genExprSepEqPrependType astContext p e None false | ps -> let (_,p) = tuplerize ps !- prefix +> opt sepSpace ao genAccess -- propertyKind +> col sepSpace ps (genPat astContext) - +> genExprSepEqPrependType astContext p e false + +> genExprSepEqPrependType astContext p e None false |> genTrivia e.Range and genPropertyWithGetSet astContext (b1, b2) = @@ -1201,7 +1207,7 @@ and genExpr astContext synExpr = let isFromAst (ctx: Context) = ctx.Content = String.Empty let isInSameLine ctx = match bs with - | [_, LetBinding(_, _, _, _, _, p, _)] -> + | [_, LetBinding(_, _, _, _, _, p, _, _)] -> // the `in` keyword should be a trivia thing not (isFromAst ctx) && p.Range.EndLine = e.Range.StartLine && not (futureNlnCheck (genExpr astContext e) ctx) | _ -> false diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index a5749a6990..fddeb9b1f9 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -463,8 +463,8 @@ let (|DoBinding|LetBinding|MemberBinding|PropertyBinding|ExplicitCtor|) = functi MemberBinding(ats, px, ao, isInline, mf, pat, expr) | SynBinding.Binding(_, DoBinding, _, _, ats, px, _, _, _, expr, _, _) -> DoBinding(ats, px, expr) - | SynBinding.Binding(ao, _, isInline, isMutable, attrs, px, _, pat, _, expr, _, _) -> - LetBinding(attrs, px, ao, isInline, isMutable, pat, expr) + | SynBinding.Binding(ao, _, isInline, isMutable, attrs, px, SynValData(_, valInfo, _), pat, _, expr, _, _) -> + LetBinding(attrs, px, ao, isInline, isMutable, pat, expr, valInfo) // Expressions (55 cases, lacking to handle 11 cases) @@ -1287,7 +1287,7 @@ let (|FunType|) (t, ValInfo(argTypes, returnType)) = /// A rudimentary recognizer for extern functions /// Probably we should use lexing information to improve its accuracy let (|Extern|_|) = function - | Let(LetBinding(ats, px, ao, _, _, PatLongIdent(_, s, [_, PatTuple ps], _), TypedExpr(Typed, _, t))) -> + | Let(LetBinding(ats, px, ao, _, _, PatLongIdent(_, s, [_, PatTuple ps], _), TypedExpr(Typed, _, t), _)) -> let hasDllImportAttr = ats |> List.exists (fun { Attributes = attrs } -> From b665bbd61da14ed951b229d2b4b4941b1165d39d Mon Sep 17 00:00:00 2001 From: Konto M Date: Sun, 17 May 2020 10:42:00 -0400 Subject: [PATCH 2/6] PR feedback --- src/Fantomas.Tests/AttributeTests.fs | 8 +++++++- src/Fantomas.Tests/LetBindingTests.fs | 8 +------- src/Fantomas/SourceParser.fs | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Fantomas.Tests/AttributeTests.fs b/src/Fantomas.Tests/AttributeTests.fs index f3cefdfa6f..56714ba486 100644 --- a/src/Fantomas.Tests/AttributeTests.fs +++ b/src/Fantomas.Tests/AttributeTests.fs @@ -333,4 +333,10 @@ open System.Runtime.InteropServices [] do () -""" \ No newline at end of file +""" + +[] +let ``should preserve return attributes`` () = + formatSourceString false """let f x : [] int = x""" config + |> should equal """let f x: [] int = x +""" diff --git a/src/Fantomas.Tests/LetBindingTests.fs b/src/Fantomas.Tests/LetBindingTests.fs index 42bcb7926c..8432cabf77 100644 --- a/src/Fantomas.Tests/LetBindingTests.fs +++ b/src/Fantomas.Tests/LetBindingTests.fs @@ -546,10 +546,4 @@ let ``multiple empty lines between equals and expression`` () = () -""" - -[] -let ``should preserve return attributes`` () = - formatSourceString false """let f x : [] int = x""" config - |> should equal """let f x: [] int = x -""" +""" \ No newline at end of file diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index fddeb9b1f9..af4f6e2c83 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -219,7 +219,7 @@ let (|SigModuleOrNamespace|) (SynModuleOrNamespaceSig.SynModuleOrNamespaceSig(Lo let (|Attribute|) (a : SynAttribute) = let (LongIdentWithDots s) = a.TypeName - (s, a.ArgExpr, Option.map (|Ident|) a.Target) + (s, a.ArgExpr, Option.map (fun (i:Ident) -> i.idText) a.Target) // Access modifiers From 8334b940b1787ffc68acc4840c970667be79f71a Mon Sep 17 00:00:00 2001 From: Konto M Date: Sun, 17 May 2020 11:03:20 -0400 Subject: [PATCH 3/6] another test --- src/Fantomas.Tests/AttributeTests.fs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Fantomas.Tests/AttributeTests.fs b/src/Fantomas.Tests/AttributeTests.fs index 56714ba486..7ef8f1c6a5 100644 --- a/src/Fantomas.Tests/AttributeTests.fs +++ b/src/Fantomas.Tests/AttributeTests.fs @@ -336,7 +336,13 @@ do () """ [] -let ``should preserve return attributes`` () = +let ``should preserve single return type attribute`` () = formatSourceString false """let f x : [] int = x""" config |> should equal """let f x: [] int = x """ + +[] +let ``should preserve multiple return type attributes`` () = + formatSourceString false """let f x : [] int = x""" config + |> should equal """let f x: [] int = x +""" From 86f6b316b32e4615a6b0ec53c6b29809376ae281 Mon Sep 17 00:00:00 2001 From: Konto M Date: Sun, 17 May 2020 11:08:02 -0400 Subject: [PATCH 4/6] unnecessary sepNone --- src/Fantomas/CodePrinter.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index cb5be06892..24750de7cc 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -489,7 +489,7 @@ and genExprSepEqPrependType astContext (pat:SynPat) (e: SynExpr) (valInfo:SynVal let genMetadataAttributes ctx = match valInfo with | Some(SynValInfo(_, SynArgInfo(attributes, _, _))) -> genOnelinerAttributes astContext attributes ctx - | None -> sepNone ctx + | None -> ctx (addExtraSpaceBeforeGenericType +> genCommentBeforeColon From 9314686574c75e60ddeb463da938ca3645c7c5d7 Mon Sep 17 00:00:00 2001 From: Konto M Date: Tue, 19 May 2020 07:37:56 -0400 Subject: [PATCH 5/6] rename --- src/Fantomas.Tests/AttributeTests.fs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Fantomas.Tests/AttributeTests.fs b/src/Fantomas.Tests/AttributeTests.fs index 7ef8f1c6a5..d9e460da38 100644 --- a/src/Fantomas.Tests/AttributeTests.fs +++ b/src/Fantomas.Tests/AttributeTests.fs @@ -337,12 +337,12 @@ do () [] let ``should preserve single return type attribute`` () = - formatSourceString false """let f x : [] int = x""" config - |> should equal """let f x: [] int = x + formatSourceString false """let f x : [] int = x""" config + |> should equal """let f x: [] int = x """ [] let ``should preserve multiple return type attributes`` () = - formatSourceString false """let f x : [] int = x""" config - |> should equal """let f x: [] int = x + formatSourceString false """let f x : [] int = x""" config + |> should equal """let f x: [] int = x """ From 682f190614b9f486c2f1b73c231ec14f5b8be25b Mon Sep 17 00:00:00 2001 From: Konto M Date: Fri, 22 May 2020 12:39:38 -0400 Subject: [PATCH 6/6] remove ctx --- src/Fantomas/CodePrinter.fs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 24750de7cc..f7b010cc61 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -486,10 +486,10 @@ and genExprSepEqPrependType astContext (pat:SynPat) (e: SynExpr) (valInfo:SynVal +> enterNode t.Range +> ifElse hasLineCommentBeforeColon unindent sepNone) ctx - let genMetadataAttributes ctx = + let genMetadataAttributes = match valInfo with - | Some(SynValInfo(_, SynArgInfo(attributes, _, _))) -> genOnelinerAttributes astContext attributes ctx - | None -> ctx + | Some(SynValInfo(_, SynArgInfo(attributes, _, _))) -> genOnelinerAttributes astContext attributes + | None -> sepNone (addExtraSpaceBeforeGenericType +> genCommentBeforeColon