未验证 提交 50d13b6d 编写于 作者: P Petr Pokorny 提交者: GitHub

Don't crash when looking up XML docs for signature with apostrophe (#14311)

* Don't crash when looking up XML docs for signature with apostrophe
上级 5f0313e4
......@@ -72,6 +72,7 @@
<InternalsVisibleTo Include="fsiArm64" />
<InternalsVisibleTo Include="VisualFSharp.Salsa" />
<InternalsVisibleTo Include="VisualFSharp.UnitTests" />
<InternalsVisibleTo Include="FSharp.Compiler.ComponentTests" />
<InternalsVisibleTo Include="FSharp.Compiler.UnitTests" />
<InternalsVisibleTo Include="FSharp.Compiler.Service.Tests" />
<InternalsVisibleTo Include="HostedCompilerServer" />
......
......@@ -320,13 +320,23 @@ type XmlDocumentationInfo private (tryGetXmlDocument: unit -> XmlDocument option
keepMax = cacheMaxSize
)
let tryGetSummaryNode xmlDocSig =
tryGetXmlDocument ()
|> Option.bind (fun doc ->
match doc.SelectSingleNode(sprintf "doc/members/member[@name='%s']" xmlDocSig) with
| null -> None
| node when node.HasChildNodes -> Some node
| _ -> None)
let tryGetSummaryNode (xmlDocSig: string) =
if xmlDocSig.Contains "'" && xmlDocSig.Contains "\"" then
// No easy way to find this signature with XPath
None
else
tryGetXmlDocument ()
|> Option.bind (fun doc ->
let name =
if xmlDocSig.Contains "'" then
$"\"{xmlDocSig}\""
else
$"'{xmlDocSig}'"
match doc.SelectSingleNode $"doc/members/member[@name={name}]" with
| null -> None
| node when node.HasChildNodes -> Some node
| _ -> None)
member _.TryGetXmlDocBySig(xmlDocSig: string) =
tryGetSummaryNode xmlDocSig
......
......@@ -202,6 +202,7 @@
<Compile Include="Globalization\GlobalizationTestCases.fs" />
<Compile Include="OCamlCompat\OCamlCompat.fs" />
<Compile Include="Miscellaneous\ListLiterals.fs" />
<Compile Include="Miscellaneous\XmlDoc.fs" />
<Compile Include="Signatures\TestHelpers.fs" />
<Compile Include="Signatures\ModuleOrNamespaceTests.fs" />
<Compile Include="Signatures\RecordTests.fs" />
......@@ -212,7 +213,7 @@
<Compile Include="FSharpChecker\SymbolUse.fs" />
<Compile Include="FSharpChecker\FindReferences.fs" />
<None Include="**\*.cs;**\*.fs;**\*.fsx;**\*.fsi" Exclude="@(Compile)">
<Link>%(RelativeDir)\TestSource\%(Filename)%(Extension)</Link>
<Link>%(RelativeDir)\TestSource\%(Filename)%(Extension)</Link>
</None>
</ItemGroup>
......
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
module FSharp.Compiler.ComponentTests.Miscellaneous.XmlDoc
open System.IO
open Xunit
open FSharp.Compiler.Xml
open TestFramework
let memberDoc = "<summary>Summary</summary>"
let xmlFileContents signature = $"""<?xml version="1.0" encoding="utf-8"?>
<doc>
<assembly>
<name>FSharp.Core</name>
</assembly>
<members>
<member name="T:Microsoft.FSharp.Collections.list`1">
<summary>The type of immutable singly-linked lists. </summary>
</member>
<member name="{signature}">
{memberDoc}
</member>
</members>
</doc>
"""
[<Theory>]
[<InlineData("P:Microsoft.FSharp.Collections.FSharpList`1.Length")>]
[<InlineData("P:Microsoft.FSharp.Collections.FSharpList`1.Length'")>]
let ``Can extract XML docs from a file for a signature`` signature =
let xmlFileName = tryCreateTemporaryFileName () + ".xml"
try
File.WriteAllText(xmlFileName, xmlFileContents signature)
let docInfo =
XmlDocumentationInfo.TryCreateFromFile(xmlFileName)
|> Option.defaultWith (fun () -> failwith "Couldn't create XmlDoc from file")
match docInfo.TryGetXmlDocBySig(signature) with
| None -> failwith "Got no doc"
| Some doc -> Assert.Equal(memberDoc, doc.UnprocessedLines |> String.concat "\n")
finally
File.Delete xmlFileName
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册