Skip to content

Commit

Permalink
adds support for nameof expression (issue #95)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianoc committed Oct 9, 2021
1 parent 9c56b62 commit d1c0631
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Cecilifier.Core.Tests/Tests/Unit/NameOfTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using NUnit.Framework;

namespace Cecilifier.Core.Tests.Tests.Unit
{
[TestFixture]
public class NameOfTests : CecilifierUnitTestBase
{
[TestCase("using System; [Obsolete(nameof(Foo))] public class Foo { }", "new CustomAttributeArgument(assembly.MainModule.TypeSystem.String, \"Foo\")", TestName = "Attribute Parameter")]
[TestCase("public class Foo { string ConstString = nameof(ConstString); }", "Ldstr, \"ConstString\"",TestName = "Field Initialization")]
[TestCase("public class Foo { string Name() => nameof(Name); }", "Ldstr, \"Name\"",TestName = "Method Return")]
[TestCase("public class Foo { string StringInterpolation() => $\"Name={nameof(StringInterpolation)}\"; }", "??",TestName = "String Interpolation", IgnoreReason = "Depends on #107")]
public void Test(string code, string expectedLiteral)
{
var result = RunCecilifier(code);
var cecilifiedCode = result.GeneratedCode.ReadToEnd();
Assert.That(cecilifiedCode, Contains.Substring(expectedLiteral));
}
}
}
7 changes: 7 additions & 0 deletions Cecilifier.Core/AST/ExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,13 @@ public override void VisitDeclarationExpression(DeclarationExpressionSyntax node

public override void VisitInvocationExpression(InvocationExpressionSyntax node)
{
var constantValue = Context.SemanticModel.GetConstantValue(node);
if (constantValue.HasValue && node.Expression is IdentifierNameSyntax { Identifier: { Text: "nameof" }} nameofExpression)
{
AddCilInstruction(ilVar, OpCodes.Ldstr, $"\"{node.ArgumentList.Arguments[0].ToFullString()}\"");
return;
}

HandleMethodInvocation(node.Expression, node.ArgumentList);
StoreTopOfStackInLocalVariableAndLoadItsAddressIfNeeded(node);
}
Expand Down
10 changes: 10 additions & 0 deletions Cecilifier.Core/Extensions/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public override string VisitTypeOfExpression(TypeOfExpressionSyntax node)
return _context.TypeResolver.Resolve(typeSymbol.Type);
}

public override string? VisitInvocationExpression(InvocationExpressionSyntax node)
{
if (node.Expression is IdentifierNameSyntax { Identifier: { Text: "nameof" } } nameOf)
{
return $"\"{node.ArgumentList.Arguments[0].Expression}\"";
}

return string.Empty;
}

public override string VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
{
var type = _context.SemanticModel.GetTypeInfo(node.Expression);
Expand Down

0 comments on commit d1c0631

Please sign in to comment.