private static async Task<Document> ReplaceWithExpressionBodiedMember(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken) { var newDeclaration = declaration .WithExpressionBody(SyntaxFactory.ArrowExpressionClause(GetExpression(declaration.Body))) .WithBody(null) .WithSemicolonToken(GetSemicolon(declaration.Body)) .WithAdditionalAnnotations(Formatter.Annotation); var root = await document.GetSyntaxRootAsync(cancellationToken); var newRoot = root.ReplaceNode(declaration, newDeclaration); return document.WithSyntaxRoot(newRoot); }
/// <summary> /// Normalizes the <paramref name="methodDeclaration" />. /// </summary> protected override SyntaxNode Normalize(MethodDeclarationSyntax methodDeclaration) { // Nothing to do here for methods without expression bodies if (methodDeclaration.ExpressionBody == null) return methodDeclaration; var methodSymbol = methodDeclaration.GetMethodSymbol(SemanticModel); StatementSyntax statementBody; if (methodSymbol.ReturnsVoid) statementBody = (StatementSyntax)Syntax.ExpressionStatement(methodDeclaration.ExpressionBody.Expression); else statementBody = (StatementSyntax)Syntax.ReturnStatement(methodDeclaration.ExpressionBody.Expression); return methodDeclaration.WithExpressionBody(null).WithBody(SyntaxFactory.Block(statementBody).NormalizeWhitespace()); }