Created
July 10, 2015 20:51
-
-
Save jaredpar/4b2467622cd37fc1f654 to your computer and use it in GitHub Desktop.
Sometimes you just gotta Roslyn Roslyn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CodeAnalysis.CSharp; | |
using Microsoft.CodeAnalysis.Text; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
using Microsoft.CodeAnalysis.CSharp.Syntax; | |
using Microsoft.CodeAnalysis; | |
namespace FixCode | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var root = @"E:\dd\ros\Open\src\Compilers\Core\MSBuildTask"; | |
Fix(Path.Combine(root, "Csc.cs")); | |
Fix(Path.Combine(root, "Vbc.cs")); | |
Fix(Path.Combine(root, "ManagedCompiler.cs")); | |
} | |
static void Fix(string fileName) | |
{ | |
var lines = File.ReadAllText(fileName); | |
var tree = CSharpSyntaxTree.ParseText(lines); | |
var fixer = new Fixer(); | |
var root = fixer.Visit(tree.GetRoot()); | |
using (var writer = new StreamWriter(File.Open(fileName, FileMode.Create))) | |
{ | |
root.WriteTo(writer); | |
} | |
} | |
} | |
internal sealed class Fixer : CSharpSyntaxRewriter | |
{ | |
private PropertyDeclarationSyntax _currentProperty; | |
public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node) | |
{ | |
_currentProperty = node; | |
var ret = base.VisitPropertyDeclaration(node); | |
_currentProperty = null; | |
return ret; | |
} | |
public override SyntaxNode VisitLiteralExpression(LiteralExpressionSyntax node) | |
{ | |
if (_currentProperty != null && | |
node.Token.Kind() == SyntaxKind.StringLiteralToken && | |
node.Token.ValueText == _currentProperty.Identifier.ValueText) | |
{ | |
return SyntaxFactory.InvocationExpression( | |
SyntaxFactory.IdentifierName("nameof"), | |
SyntaxFactory.ArgumentList( | |
SyntaxFactory.Token(SyntaxKind.OpenParenToken), | |
SyntaxFactory.SeparatedList<ArgumentSyntax>( | |
SyntaxFactory.NodeOrTokenList( | |
SyntaxFactory.Argument( | |
SyntaxFactory.IdentifierName(_currentProperty.Identifier.ValueText)))), | |
SyntaxFactory.Token(SyntaxKind.CloseParenToken))); | |
} | |
return base.VisitLiteralExpression(node); | |
} | |
public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node) | |
{ | |
var modifiers = node.Modifiers; | |
var index = 0; | |
while ( | |
index + 1 < modifiers.Count && | |
modifiers[index].Kind() == SyntaxKind.OverrideKeyword && | |
SyntaxFacts.IsAccessibilityModifier(modifiers[index + 1].Kind())) | |
{ | |
var overrideToken = modifiers[index]; | |
var accessToken = modifiers[index + 1]; | |
var newOverrideToken = overrideToken.WithTriviaFrom(accessToken); | |
var newAccessToken = accessToken.WithTriviaFrom(overrideToken); | |
modifiers = modifiers | |
.RemoveAt(index) | |
.Insert(index, newAccessToken) | |
.RemoveAt(index + 1) | |
.Insert(index + 1, newOverrideToken); | |
index++; | |
} | |
node = node.WithModifiers(modifiers); | |
return base.VisitMethodDeclaration(node); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment