Skip to content

Commit

Permalink
Merge pull request #765 from DedSec256/dedsec256-fix-for-operators
Browse files Browse the repository at this point in the history
Fix arguments naming and escape operator name in usageHtml
  • Loading branch information
dsyme authored Sep 7, 2022
2 parents 217d4e0 + ff92b18 commit 65074af
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/FSharp.Formatting.ApiDocs/GenerateModel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ module internal TypeFormatter =
argName, argType

// Format each argument, including its name and type
let formatArgUsageAsHtml _ctx i (arg: FSharpParameter) =
let formatArgUsageAsHtml i (arg: FSharpParameter) =
let argName, _argType = formatArgNameAndType i arg
!!argName

Expand Down Expand Up @@ -1410,32 +1410,24 @@ module internal SymbolReader =
| _, false, _, name, _ when PrettyNaming.IsMangledOpName v.CompiledName ->
match argInfos with
// binary operators (taking a tuple)
| [ [ x; y ] ] ->
let left = formatCurriedArgsUsageAsHtml true false [ [ x ] ]

let nm = PrettyNaming.DecompileOpName v.CompiledName

let right = formatCurriedArgsUsageAsHtml true false [ [ y ] ]

span [] [ left; !! " "; !!nm; !! " "; right ]

| [ [ x; y ] ]
// binary operators (curried, like in FSharp.Core.Operators)
| [ args1; args2 ] ->
let left = formatCurriedArgsUsageAsHtml true false [ args1 ]
| [ [ x ]; [ y ] ] ->
let left = formatArgUsageAsHtml 0 x

let nm = PrettyNaming.DecompileOpName v.CompiledName

let right = formatCurriedArgsUsageAsHtml true false [ args2 ]
let right = formatArgUsageAsHtml 1 y

span [] [ left; !! " "; !!nm; !! " "; right ]
span [] [ left; !! " "; encode nm; !! " "; right ]

// unary operators
| [ [ x ] ] ->
let nm = PrettyNaming.DecompileOpName v.CompiledName

let right = formatCurriedArgsUsageAsHtml true false [ [ x ] ]
let right = formatArgUsageAsHtml 0 x

span [] [ !!nm; right ]
span [] [ encode nm; right ]
| _ ->
span
[]
Expand Down
18 changes: 18 additions & 0 deletions tests/FSharp.ApiDocs.Tests/ApiDocsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ let ``ApiDocs works on two sample F# assemblies`` (format: OutputFormat) =
files.[(sprintf "fslib-union.%s" format.Extension)]
|> shouldContainText "<span>Naming(<span>rate,&#32;string</span>)</span>"

// Check that operators are encoded

// arg0 <&> arg1
files[$"fslib-operatorswithfsi.%s{format.Extension}"]
|> shouldContainText "arg0&#32;&lt;&amp;&gt;&#32;arg1"

// x ?<? y
files[$"fslib-operatorswithfsi.%s{format.Extension}"]
|> shouldContainText "x&#32;?&lt;?&#32;y"

// <?arg0
files[$"fslib-operatorswithfsi.%s{format.Extension}"]
|> shouldContainText "&lt;?arg0"

// <?>x
files[$"fslib-operatorswithfsi.%s{format.Extension}"]
|> shouldContainText "&lt;?&gt;x"

(* This may be addressed in a separate issue or removed if not an issue.
// Check that implict cast operator is generated correctly
files.[(sprintf "fslib-space-missing-implicit-cast.%s" format.Extension)] |> shouldContainText "<code><span>op_Implicit&#32;<span>source</span></span></code>"
Expand Down
2 changes: 2 additions & 0 deletions tests/FSharp.ApiDocs.Tests/files/FsLib1/FsLib1.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="Library1.fs" />
<Compile Include="OperatorsWithFsi.fsi" />
<Compile Include="OperatorsWithFsi.fs" />
<None Include="paket.references" />
</ItemGroup>
<Import Project="..\..\..\..\.paket\Paket.Restore.targets" />
Expand Down
6 changes: 6 additions & 0 deletions tests/FSharp.ApiDocs.Tests/files/FsLib1/OperatorsWithFsi.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module FsLib.OperatorsWithFsi

let (<&>) x y = x > y
let (?<?) x y = x > y
let (<?) x = x
let (<?>) x = x
13 changes: 13 additions & 0 deletions tests/FSharp.ApiDocs.Tests/files/FsLib1/OperatorsWithFsi.fsi
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module FsLib.OperatorsWithFsi

/// <summary>The binary operator 1</summary>
val (<&>): 'T -> 'T -> bool when 'T: comparison

/// <summary>The binary operator 2</summary>
val (?<?): x: 'T -> y: 'T -> bool when 'T: comparison

/// <summary>The unary operator 1</summary>
val (<?): 'T -> 'T

/// <summary>The unary operator 2</summary>
val (<?>): x: 'T -> 'T

0 comments on commit 65074af

Please sign in to comment.