Skip to content

Commit

Permalink
Fix MEN018 code fixer bug with ArgumentSyntax
Browse files Browse the repository at this point in the history
  • Loading branch information
menees committed May 28, 2024
1 parent 63f585b commit 22ed41f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<AssemblyOriginatorKeyFile>../Analyzers.snk</AssemblyOriginatorKeyFile>

<!-- NOTE: Change the version in Vsix\source.extension.vsixmanifest to match this! -->
<Version>3.2.0</Version>
<Version>3.2.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
Expand Down
11 changes: 8 additions & 3 deletions src/Menees.Analyzers.CodeFixes/Men018UseDigitSeparatorsFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ private static async Task<Document> GetTransformedDocumentAsync(
SyntaxNode? syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
if (syntaxRoot != null)
{
SyntaxNode oldNode = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
if (oldNode is LiteralExpressionSyntax literalExpression)
// Because we're finding the node by its location, this may return a parent node
// (e.g., ArgumentSyntax that wraps the LiteralExpressionSyntax in Test(123456);).
SyntaxNode locationNode = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
LiteralExpressionSyntax? literalExpression = locationNode.DescendantNodesAndSelf()
.OfType<LiteralExpressionSyntax>()
.FirstOrDefault();
if (literalExpression != null)
{
SyntaxToken literalToken = literalExpression.Token;
SyntaxTriviaList lead = literalToken.LeadingTrivia;
Expand All @@ -88,7 +93,7 @@ private static async Task<Document> GetTransformedDocumentAsync(
SyntaxKind.NumericLiteralExpression,
newToken.Value);

var newSyntaxRoot = syntaxRoot.ReplaceNode(oldNode, newNode);
var newSyntaxRoot = syntaxRoot.ReplaceNode(literalExpression, newNode);
result = document.WithSyntaxRoot(newSyntaxRoot);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Menees.Analyzers.Vsix/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="Menees.Analyzers.Vsix" Version="3.2.0" Language="en-US" Publisher="Bill Menees"/>
<Identity Id="Menees.Analyzers.Vsix" Version="3.2.1" Language="en-US" Publisher="Bill Menees"/>
<DisplayName>Menees.Analyzers.Vsix</DisplayName>
<Description xml:space="preserve">Provides analyzers for validating that tabs are used for indentation, that the lengths of lines, methods, properties, and files are acceptable, and that #regions are used within long files and files that contain multiple types.</Description>
<License>License.txt</License>
Expand Down
2 changes: 2 additions & 0 deletions tests/Menees.Analyzers.Test/Men018UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void InvalidCodeFixerTest()
const string before = @"
public class Test
{
private static readonly DateTime SomeDay = new(2024, 5, 28);
private const int Million = 1000000;
private const uint MaxUint = 0xFFFFFFFFu;
private const decimal TenBillion = 10000000000m;
Expand All @@ -125,6 +126,7 @@ public Test()
const string after = @"
public class Test
{
private static readonly DateTime SomeDay = new(2_024, 5, 28);
private const int Million = 1_000_000;
private const uint MaxUint = 0x_FF_FF_FF_FFu;
private const decimal TenBillion = 10_000_000_000m;
Expand Down

0 comments on commit 22ed41f

Please sign in to comment.