Skip to content

Commit bffda3a

Browse files
committed
Ensure we properly format multiline raw strings
We had weird newline formatting after applying C# whitespace normalization. A quick rewriter fixes that now. We use the devlooped JWT which requies preserving formatting for proper parsing as a test.
1 parent 62a0881 commit bffda3a

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

src/ThisAssembly.Constants/CSharp.sbntxt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
{{ obsolete }}
4646
{{~ if RawStrings && value.IsText ~}}
4747
public {{ Modifier }} string {{ value.Name | string.replace "-" "_" | string.replace " " "_" }} ={{ Lambda }}
48-
4948
"""
5049
{{ value.Value }}
5150
""";

src/ThisAssembly.Constants/ConstantsGenerator.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,32 @@ void GenerateConstant(SourceProductionContext spc,
123123

124124
var output = template.Render(model, member => member.Name);
125125

126-
// Apply formatting since indenting isn't that nice in Scriban when rendering nested
127-
// structures via functions.
128126
if (parse.Language == LanguageNames.CSharp)
129127
{
130-
output = SyntaxFactory
131-
.ParseCompilationUnit(output, options: cs)
132-
.NormalizeWhitespace()
133-
.GetText()
134-
.ToString();
128+
// Apply formatting since indenting isn't that nice in Scriban when rendering nested
129+
// structures via functions.
130+
// We alos rewrite to prepend a newline leading trivia before the raw string literals if any
131+
var node = new RawStringLiteralRewriter().Visit(
132+
SyntaxFactory.ParseCompilationUnit(output, options: cs).NormalizeWhitespace());
133+
134+
output = node.GetText().ToString();
135135
}
136136

137137
spc.AddSource($"{root}.{name}.g.cs", SourceText.From(output, Encoding.UTF8));
138138
}
139-
}
139+
140+
class RawStringLiteralRewriter : CSharpSyntaxRewriter
141+
{
142+
public override SyntaxToken VisitToken(SyntaxToken token)
143+
{
144+
// See https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.csharp.syntaxkind?view=roslyn-dotnet-4.13.0
145+
// MultiLineRawStringLiteralToken = 8519
146+
// Utf8MultiLineRawStringLiteralToken = 8522
147+
if (token.RawKind == 8519 || token.RawKind == 8522)
148+
return token.WithLeadingTrivia(
149+
token.LeadingTrivia.Add(SyntaxFactory.CarriageReturnLineFeed));
150+
151+
return base.VisitToken(token);
152+
}
153+
}
154+
}

src/ThisAssembly.Tests/Tests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
33
using System.IO;
4+
using Microsoft.IdentityModel.Tokens;
45
using Xunit;
56
using Xunit.Abstractions;
67
//using ThisAssembly = ThisAssemblyTests
@@ -191,4 +192,8 @@ public void CanUseGitBranchConstants()
191192
[Fact]
192193
public void CanUseSemicolonsInConstant()
193194
=> Assert.Equal("A;B;C", ThisAssembly.Constants.WithSemiColon);
195+
196+
/// <summary />
197+
[Fact]
198+
public void CanReadJsonConstant() => JsonWebKey.Create(ThisAssembly.Metadata.Funding.GitHub.devlooped);
194199
}

src/ThisAssembly.Tests/ThisAssembly.Tests.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,18 @@
119119
<CompilerVisibleProperty Include="ThisAssembly" />
120120
</ItemGroup>
121121

122+
<Target Name="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes" Inputs="$(MSBuildProjectFullPath)" Outputs="$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk">
123+
<Exec Command="curl --silent --output $(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk https://sponsorlink.devlooped.com/jwk" />
124+
</Target>
125+
126+
<Target Name="ReadDevloopedJwk" DependsOnTargets="DownloadDevloopedJwk" BeforeTargets="GetAssemblyAttributes">
127+
<PropertyGroup>
128+
<!-- Read public key we validate manifests against -->
129+
<DevloopedJwk>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\$(BaseIntermediateOutputPath)devlooped.jwk'))</DevloopedJwk>
130+
</PropertyGroup>
131+
<ItemGroup>
132+
<AssemblyMetadata Include="Funding.GitHub.devlooped" Value="$(DevloopedJwk.Trim())" />
133+
</ItemGroup>
134+
</Target>
135+
122136
</Project>

0 commit comments

Comments
 (0)