Skip to content

Commit

Permalink
Merge pull request #26 from mika-f/fix/invalid-constructor-renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mika-f committed Mar 2, 2024
2 parents 7455642 + 639bded commit 3145351
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// ------------------------------------------------------------------------------------------
// Copyright (c) Natsuneko. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
// ------------------------------------------------------------------------------------------

using Microsoft.CodeAnalysis.CSharp.Syntax;

using Plana.Testing;

namespace Plana.Composition.RenameSymbols.Tests;

public partial class RenameSymbolsPluginTest
{
[Fact]
public async Task RenameClasses_Constructor()
{
var container = new PlanaContainer<RenameSymbolsPlugin>("rename-classes");
await container.RunAsync();

var reference = await container.GetSourceByPathAsync("Plana.Composition.Extensions/PlanaPluginOption.cs");

// PlanaPluginOption -> _0x4e115ed4
const string identifier = "_0x4e115ed4";

var @class = await reference.GetFirstSyntax<ClassDeclarationSyntax>();
Assert.Equal(identifier, @class.Identifier.ToFullString());

var constructor = await reference.GetFirstSyntax<ConstructorDeclarationSyntax>();
Assert.Equal(identifier, constructor.Identifier.ToFullString());
}
}
14 changes: 14 additions & 0 deletions src/Plana.Composition.RenameSymbols/CSharpSymbolsRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ internal class CSharpSymbolsRewriter(IDocument document, bool keepNameOnInspecto
return newNode;
}

public override SyntaxNode? VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
{
var newNode = base.VisitConstructorDeclaration(node);
if (newNode is ConstructorDeclarationSyntax constructor)
{
var symbol = document.SemanticModel.GetDeclaredSymbol(node);
// constructor name must be equals to containing type name
if (symbol != null && dict.TryGetValue(symbol.ContainingType, out var value))
return constructor.WithIdentifier(SyntaxFactory.Identifier(value));
}

return newNode;
}

public override SyntaxNode? VisitMethodDeclaration(MethodDeclarationSyntax node)
{
var newNode = base.VisitMethodDeclaration(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public string GetAlphaNumericalString(int length)
return GetString(""abcedf0123456789"".ToCharArray(), length);
}
private readonly Random _random;
private readonly List<string> _items = [];
private readonly Random _random;
public void Shuffle<T>(Span<T> array)
{
_random.Shuffle(array);
Expand Down Expand Up @@ -86,8 +86,7 @@ public PlanaRandom()
{
_random = new Random();
}
}
".Trim());
}".Trim());
}

[Fact]
Expand Down

0 comments on commit 3145351

Please sign in to comment.