Skip to content

Commit

Permalink
Add inline keyword to PropertyGetSetBindingNode.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Dec 29, 2022
1 parent fca32ac commit 6ad1d21
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 51 deletions.
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 @@ -118,6 +118,7 @@
<Compile Include="TypeAnnotationTests.fs" />
<Compile Include="BaseConstructorTests.fs" />
<Compile Include="DallasTests.fs" />
<Compile Include="InlineTests.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Fantomas.Core\Fantomas.Core.fsproj" />
Expand Down
85 changes: 85 additions & 0 deletions src/Fantomas.Core.Tests/InlineTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module Fantomas.Core.Tests.InlineTests

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

[<Test>]
let ``trivia around inline keyword, 2017`` () =
formatSourceString
false
"""
let
#if !DEBUG
inline
#endif
map f ar = Async.map (Result.map f) ar
"""
config
|> prepend newline
|> should
equal
"""
let
#if !DEBUG
inline
#endif
map
f
ar
=
Async.map (Result.map f) ar
"""

[<Test>]
let ``inline in plain member`` () =
formatSourceString
false
"""
type X =
member inline x.Y () = ()
"""
config
|> prepend newline
|> should
equal
"""
type X =
member inline x.Y() = ()
"""

[<Test>]
let ``inline in get/set member`` () =
formatSourceString
false
"""
type X =
member inline x.Y
with inline get () = 4
and inline set y = ()
"""
config
|> prepend newline
|> should
equal
"""
type X =
member inline x.Y
with inline get () = 4
and inline set y = ()
"""

[<Test>]
let ``inline in val`` () =
formatSourceString
true
"""
val inline meh: int -> int
"""
config
|> prepend newline
|> should
equal
"""
val inline meh: int -> int
"""
27 changes: 0 additions & 27 deletions src/Fantomas.Core.Tests/LetBindingTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2133,30 +2133,3 @@ 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 ``trivia around inline keyword, 2017`` () =
formatSourceString
false
"""
let
#if !DEBUG
inline
#endif
map f ar = Async.map (Result.map f) ar
"""
config
|> prepend newline
|> should
equal
"""
let
#if !DEBUG
inline
#endif
map
f
ar
=
Async.map (Result.map f) ar
"""
15 changes: 8 additions & 7 deletions src/Fantomas.Core/CodePrinter2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2645,7 +2645,7 @@ let genBinding (b: BindingNode) (ctx: Context) : Context =

let afterLetKeyword =
ifElse b.IsMutable (!- "mutable ") sepNone
+> genInline b.Inline
+> genInlineOpt b.Inline
+> genAccessOpt b.Accessibility

let genFunctionName =
Expand Down Expand Up @@ -2770,7 +2770,7 @@ let genBinding (b: BindingNode) (ctx: Context) : Context =
let afterLetKeyword =
genAccessOpt b.Accessibility
+> ifElse b.IsMutable (!- "mutable ") sepNone
+> genInline b.Inline
+> genInlineOpt b.Inline

let genDestructedTuples =
expressionFitsOnRestOfLine (genPat pat) (sepOpenT +> genPat pat +> sepCloseT)
Expand Down Expand Up @@ -2805,7 +2805,7 @@ let genBinding (b: BindingNode) (ctx: Context) : Context =

let afterLetKeyword =
ifElse b.IsMutable (!- "mutable ") sepNone
+> genInline b.Inline
+> genInlineOpt b.Inline
+> genAccessOpt b.Accessibility

let genValueName =
Expand Down Expand Up @@ -3477,7 +3477,7 @@ let genTypeAndParam (genTypeName: Context -> Context) (tds: TyparDecls option) =
| Some(TyparDecls.PrefixList _) -> optSingle (fun tds -> genTyparDecls tds +> sepSpace) tds +> genTypeName
| Some(TyparDecls.SinglePrefix singlePrefixNode) -> genTyparDecl true singlePrefixNode +> sepSpace +> genTypeName

let genInline (inlineNode: SingleTextNode option) =
let genInlineOpt (inlineNode: SingleTextNode option) =
match inlineNode with
| None -> sepNone
| Some inlineNode -> genSingleTextNodeWithSpaceSuffix sepSpace inlineNode
Expand All @@ -3491,7 +3491,7 @@ let genVal (node: ValNode) (optGetSet: MultipleTextsNode option) =
genXml node.XmlDoc
+> genAttributes node.Attributes
+> optSingle (fun lk -> genMultipleTextsNode lk +> sepSpace) node.LeadingKeyword
+> onlyIf node.IsInline (!- "inline ")
+> genInlineOpt node.Inline
+> onlyIf node.IsMutable (!- "mutable ")
+> genAccessOpt node.Accessibility
+> genTypeAndParam (genSingleTextNode node.Identifier) node.TypeParams
Expand Down Expand Up @@ -3579,7 +3579,8 @@ let genMemberDefn (md: MemberDefn) =
|> genNode (MemberDefn.Node md)
| MemberDefn.PropertyGetSet node ->
let genProperty (node: PropertyGetSetBindingNode) =
genAccessOpt node.Accessibility
genInlineOpt node.Inline
+> genAccessOpt node.Accessibility
+> genSingleTextNode node.LeadingKeyword
+> sepSpace
+> col sepSpace node.Parameters genPat
Expand All @@ -3592,7 +3593,7 @@ let genMemberDefn (md: MemberDefn) =
+> genAttributes node.Attributes
+> genMultipleTextsNode node.LeadingKeyword
+> sepSpace
+> onlyIf node.IsInline (!- "inline ")
+> genInlineOpt node.Inline
+> genAccessOpt node.Accessibility
+> genIdentListNode node.MemberName
+> indent
Expand Down
26 changes: 14 additions & 12 deletions src/Fantomas.Core/Fangorn.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2323,7 +2323,8 @@ let mkPropertyGetSetBinding
headPat = SynPat.LongIdent(extraId = Some extraIdent; accessibility = ao; argPats = SynArgPats.Pats ps)
returnInfo = returnInfo
expr = expr
trivia = { EqualsRange = Some mEq }) ->
trivia = { EqualsRange = Some mEq
InlineKeyword = inlineKw }) ->
let e = parseExpressionInSynBinding returnInfo expr
let returnTypeNode = mkBindingReturnInfo creationAide returnInfo

Expand All @@ -2346,6 +2347,7 @@ let mkPropertyGetSetBinding
let range = unionRanges extraIdent.idRange e.Range

PropertyGetSetBindingNode(
Option.map (stn "inline") inlineKw,
mkSynAccess ao,
leadingKeyword,
pats,
Expand Down Expand Up @@ -2509,11 +2511,11 @@ let mkMemberDefn (creationAide: CreationAide) (md: SynMemberDefn) =
|> MemberDefn.AbstractSlot
| SynMemberDefn.GetSetMember(Some(SynBinding(
accessibility = ao
isInline = isInline
attributes = ats
xmlDoc = px
headPat = SynPat.LongIdent(longDotId = memberName)
trivia = { LeadingKeyword = lk }) as getBinding),
trivia = { LeadingKeyword = lk
InlineKeyword = inlineKw }) as getBinding),
Some setBinding,
_,
{ GetKeyword = Some getKeyword
Expand All @@ -2532,7 +2534,7 @@ let mkMemberDefn (creationAide: CreationAide) (md: SynMemberDefn) =
mkXmlDoc px,
mkAttributes creationAide ats,
mkSynLeadingKeyword lk,
isInline,
Option.map (stn "inline") inlineKw,
mkSynAccess ao,
mkSynLongIdent memberName,
stn "with" withKeyword,
Expand All @@ -2545,22 +2547,22 @@ let mkMemberDefn (creationAide: CreationAide) (md: SynMemberDefn) =
| SynMemberDefn.GetSetMember(None,
Some(SynBinding(
accessibility = ao
isInline = isInline
attributes = ats
xmlDoc = px
headPat = SynPat.LongIdent(longDotId = memberName)
trivia = { LeadingKeyword = lk }) as binding),
trivia = { LeadingKeyword = lk
InlineKeyword = inlineKw }) as binding),
_,
{ WithKeyword = withKeyword
GetKeyword = getKeyword
SetKeyword = setKeyword })
| SynMemberDefn.GetSetMember(Some(SynBinding(
accessibility = ao
isInline = isInline
attributes = ats
xmlDoc = px
headPat = SynPat.LongIdent(longDotId = memberName)
trivia = { LeadingKeyword = lk }) as binding),
trivia = { LeadingKeyword = lk
InlineKeyword = inlineKw }) as binding),
None,
_,
{ WithKeyword = withKeyword
Expand All @@ -2576,7 +2578,7 @@ let mkMemberDefn (creationAide: CreationAide) (md: SynMemberDefn) =
mkXmlDoc px,
mkAttributes creationAide ats,
mkSynLeadingKeyword lk,
isInline,
Option.map (stn "inline") inlineKw,
mkSynAccess ao,
mkSynLongIdent memberName,
stn "with" withKeyword,
Expand All @@ -2594,7 +2596,7 @@ let mkMemberDefn (creationAide: CreationAide) (md: SynMemberDefn) =
mkXmlDoc px,
mkAttributes creationAide ats,
mkSynLeadingKeyword lk,
isInline,
Option.map (stn "inline") inlineKw,
mkSynAccess ao,
mkSynLongIdent memberName,
stn "with" withKeyword,
Expand All @@ -2609,7 +2611,7 @@ let mkMemberDefn (creationAide: CreationAide) (md: SynMemberDefn) =

let mkVal
(creationAide: CreationAide)
(SynValSig(ats, synIdent, vtd, t, _vi, isInline, isMutable, px, ao, eo, range, trivia))
(SynValSig(ats, synIdent, vtd, t, _vi, _isInline, isMutable, px, ao, eo, range, trivia))
: ValNode =
let lk =
match trivia.LeadingKeyword with
Expand All @@ -2620,7 +2622,7 @@ let mkVal
mkXmlDoc px,
mkAttributes creationAide ats,
lk,
isInline,
Option.map (stn "inline") trivia.InlineKeyword,
isMutable,
mkSynAccess ao,
mkSynIdent synIdent,
Expand Down
13 changes: 8 additions & 5 deletions src/Fantomas.Core/SyntaxOak.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2713,6 +2713,7 @@ type MemberDefnAbstractSlotNode

type PropertyGetSetBindingNode
(
inlineNode: SingleTextNode option,
accessibility: SingleTextNode option,
leadingKeyword: SingleTextNode,
parameters: Pattern list,
Expand All @@ -2724,13 +2725,15 @@ type PropertyGetSetBindingNode
inherit NodeBase(range)

override this.Children =
[| yield! noa accessibility
[| yield! noa inlineNode
yield! noa accessibility
yield leadingKeyword
yield! List.map Pattern.Node parameters
yield! noa returnType
yield equals
yield Expr.Node expr |]

member x.Inline = inlineNode
member x.Accessibility = accessibility
member x.LeadingKeyword = leadingKeyword
member x.Parameters = parameters
Expand All @@ -2743,7 +2746,7 @@ type MemberDefnPropertyGetSetNode
xmlDoc: XmlDocNode option,
attributes: MultipleAttributeListNode option,
leadingKeyword: MultipleTextsNode,
isInline: bool,
inlineNode: SingleTextNode option,
accessibility: SingleTextNode option,
memberName: IdentListNode,
withKeyword: SingleTextNode,
Expand All @@ -2768,7 +2771,7 @@ type MemberDefnPropertyGetSetNode
member x.XmlDoc = xmlDoc
member x.Attributes = attributes
member x.LeadingKeyword = leadingKeyword
member x.IsInline = isInline
member x.Inline = inlineNode
member x.Accessibility = accessibility
member x.MemberName = memberName
member x.WithKeyword = withKeyword
Expand All @@ -2781,7 +2784,7 @@ type ValNode
xmlDoc: XmlDocNode option,
attributes: MultipleAttributeListNode option,
leadingKeyword: MultipleTextsNode option,
isInline: bool,
inlineNode: SingleTextNode option,
isMutable: bool,
accessibility: SingleTextNode option,
identifier: SingleTextNode,
Expand All @@ -2807,7 +2810,7 @@ type ValNode
member x.XmlDoc = xmlDoc
member x.Attributes = attributes
member x.LeadingKeyword = leadingKeyword
member x.IsInline = isInline
member x.Inline = inlineNode
member x.IsMutable = isMutable
member x.Accessibility = accessibility
member x.Identifier = identifier
Expand Down

0 comments on commit 6ad1d21

Please sign in to comment.