Skip to content

Commit 1b384c9

Browse files
committed
Make sure we escape constants XML comments
This ensures we get valid XML doc comments, rather than potentially nested (invalid) XML if the property happens to contain XML.
1 parent a316274 commit 1b384c9

File tree

4 files changed

+10
-5
lines changed

4 files changed

+10
-5
lines changed

src/ThisAssembly.Constants/CSharp.sbntxt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{{~ if $0.Comment ~}}
1414
{{ $0.Comment }}
1515
{{~ else ~}}
16-
/// => @"{{ $0.Value }}"
16+
/// => @"{{ $0.EscapedValue }}"
1717
{{~ end ~}}
1818
/// </summary>
1919
{{- end -}}

src/ThisAssembly.Constants/ConstantsGenerator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Text;
6+
using System.Xml.Linq;
67
using Devlooped.Sponsors;
78
using Microsoft.CodeAnalysis;
89
using Microsoft.CodeAnalysis.CSharp;
@@ -93,9 +94,9 @@ void GenerateConstant(SourceProductionContext spc,
9394
}
9495

9596
if (comment != null)
96-
comment = "/// " + string.Join(Environment.NewLine + "/// ", comment.Trim().Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None));
97+
comment = "/// " + string.Join(Environment.NewLine + "/// ", new XText(comment).ToString().Trim().Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None));
9798
else
98-
comment = "/// " + string.Join(Environment.NewLine + "/// ", value.Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None));
99+
comment = "/// " + string.Join(Environment.NewLine + "/// ", new XText(value).ToString().Replace("\\n", Environment.NewLine).Trim(['\r', '\n']).Split([Environment.NewLine], StringSplitOptions.None));
99100

100101
// Revert normalization of newlines performed in MSBuild to workaround the limitation in editorconfig.
101102
var rootArea = Area.Load([new(name, value.Replace("\\n", Environment.NewLine).Trim(['\r', '\n']), comment, type ?? "string"),], root, rootComment);

src/ThisAssembly.Constants/Model.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Text.RegularExpressions;
7+
using System.Xml.Linq;
78
using Microsoft.CodeAnalysis.CSharp;
89

910
namespace ThisAssembly;
@@ -110,5 +111,6 @@ static Area GetArea(Area area, IEnumerable<string> areaPath)
110111
[DebuggerDisplay("{Name} = {Value}")]
111112
record Constant(string Name, string? Value, string? Comment, string Type = "string")
112113
{
114+
public string? EscapedValue => Value == null ? null : new XText(Value).ToString();
113115
public bool IsText => Type.Equals("string", StringComparison.OrdinalIgnoreCase);
114116
}

src/ThisAssembly.Tests/ThisAssembly.Tests.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
// Some comments too.</Description>
2020
<TargetFramework Condition="'$(BuildingInsideVisualStudio)' == 'true'">net472</TargetFramework>
2121
<RootNamespace>ThisAssemblyTests</RootNamespace>
22-
<!--<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>-->
22+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
2323
<NoWarn>CS0618;CS8981;TA100;$(NoWarn)</NoWarn>
2424
<PackageScribanIncludeSource>false</PackageScribanIncludeSource>
2525
<ProjectFile>$([System.IO.File]::ReadAllText($(MSBuildProjectFullPath)))</ProjectFile>
26+
<ProjectFileComment>$(ProjectFile)</ProjectFileComment>
2627
</PropertyGroup>
2728

2829
<Import Project="..\*\ThisAssembly*.props" />
@@ -69,7 +70,8 @@
6970
<ProjectProperty Include="Foo" />
7071
<ProjectProperty Include="Description" />
7172
<ProjectProperty Include="Multiline" />
72-
<ProjectProperty Include="ProjectFile" Comment="Full project contents" />
73+
<ProjectProperty Include="ProjectFileComment" Comment="Full project contents" />
74+
<ProjectProperty Include="ProjectFile" />
7375
<Constant Include="Foo.Raw" Value="$(Multiline)" Comment="$(Multiline)" />
7476
<Constant Include="Foo.Bar" Value="Baz" Comment="Yay!" />
7577
<Constant Include="Foo.Hello" Value="World" Comment="Comments make everything better 😍" />

0 commit comments

Comments
 (0)