Skip to content

Commit

Permalink
Make sure we escape constants XML comments
Browse files Browse the repository at this point in the history
This ensures we get valid XML doc comments, rather than potentially nested (invalid) XML if the property happens to contain XML.
  • Loading branch information
kzu committed Oct 9, 2024
1 parent a316274 commit 1b384c9
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/ThisAssembly.Constants/CSharp.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{{~ if $0.Comment ~}}
{{ $0.Comment }}
{{~ else ~}}
/// => @"{{ $0.Value }}"
/// => @"{{ $0.EscapedValue }}"
{{~ end ~}}
/// </summary>
{{- end -}}
Expand Down
5 changes: 3 additions & 2 deletions src/ThisAssembly.Constants/ConstantsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Devlooped.Sponsors;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -93,9 +94,9 @@ void GenerateConstant(SourceProductionContext spc,
}

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

// Revert normalization of newlines performed in MSBuild to workaround the limitation in editorconfig.
var rootArea = Area.Load([new(name, value.Replace("\\n", Environment.NewLine).Trim(['\r', '\n']), comment, type ?? "string"),], root, rootComment);
Expand Down
2 changes: 2 additions & 0 deletions src/ThisAssembly.Constants/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.CSharp;

namespace ThisAssembly;
Expand Down Expand Up @@ -110,5 +111,6 @@ static Area GetArea(Area area, IEnumerable<string> areaPath)
[DebuggerDisplay("{Name} = {Value}")]
record Constant(string Name, string? Value, string? Comment, string Type = "string")
{
public string? EscapedValue => Value == null ? null : new XText(Value).ToString();
public bool IsText => Type.Equals("string", StringComparison.OrdinalIgnoreCase);
}
6 changes: 4 additions & 2 deletions src/ThisAssembly.Tests/ThisAssembly.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
// Some comments too.</Description>
<TargetFramework Condition="'$(BuildingInsideVisualStudio)' == 'true'">net472</TargetFramework>
<RootNamespace>ThisAssemblyTests</RootNamespace>
<!--<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>-->
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<NoWarn>CS0618;CS8981;TA100;$(NoWarn)</NoWarn>
<PackageScribanIncludeSource>false</PackageScribanIncludeSource>
<ProjectFile>$([System.IO.File]::ReadAllText($(MSBuildProjectFullPath)))</ProjectFile>
<ProjectFileComment>$(ProjectFile)</ProjectFileComment>
</PropertyGroup>

<Import Project="..\*\ThisAssembly*.props" />
Expand Down Expand Up @@ -69,7 +70,8 @@
<ProjectProperty Include="Foo" />
<ProjectProperty Include="Description" />
<ProjectProperty Include="Multiline" />
<ProjectProperty Include="ProjectFile" Comment="Full project contents" />
<ProjectProperty Include="ProjectFileComment" Comment="Full project contents" />
<ProjectProperty Include="ProjectFile" />
<Constant Include="Foo.Raw" Value="$(Multiline)" Comment="$(Multiline)" />
<Constant Include="Foo.Bar" Value="Baz" Comment="Yay!" />
<Constant Include="Foo.Hello" Value="World" Comment="Comments make everything better 😍" />
Expand Down

0 comments on commit 1b384c9

Please sign in to comment.