Skip to content

Commit

Permalink
Fix refactoring 'Inline method' (RR0062) (#1234)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt committed Oct 28, 2023
1 parent facf447 commit d52ec57
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 35 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fix [RCS1234](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1234) ([PR](https://github.com/dotnet/roslynator/pull/1233))
- Fix refactoring [Inline method](https://josefpihrt.github.io/docs/roslynator/refactorings/RR0062) ([PR](https://github.com/dotnet/roslynator/pull/1234))

## [4.6.1] - 2023-10-23

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task ComputeRefactoringsAsync(RefactoringContext context, TNode nod
declarationSemanticModel = await document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
}

InlineRefactoring<TNode, TDeclaration, TSymbol> refactoring = CreateRefactoring(context.Document, nodeIncludingConditionalAccess, enclosingType, symbol, declaration, parameterInfos, semanticModel, declarationSemanticModel, context.CancellationToken);
InlineRefactoring<TNode, TDeclaration, TSymbol> refactoring = CreateRefactoring(context.Document, nodeIncludingConditionalAccess, enclosingType, symbol, declaration, parameterInfos, semanticModel, declarationSemanticModel);

string title = CSharpFacts.GetTitle(declaration);

Expand Down Expand Up @@ -107,6 +107,5 @@ protected abstract InlineRefactoring<TNode, TDeclaration, TSymbol> CreateRefacto
TDeclaration declaration,
ImmutableArray<ParameterInfo> parameterInfos,
SemanticModel nodeSemanticModel,
SemanticModel declarationSemanticModel,
CancellationToken cancellationToken);
SemanticModel declarationSemanticModel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,9 @@ protected override InlineRefactoring<InvocationExpressionSyntax, MethodDeclarati
MethodDeclarationSyntax declaration,
ImmutableArray<ParameterInfo> parameterInfos,
SemanticModel nodeSemanticModel,
SemanticModel declarationSemanticModel,
CancellationToken cancellationToken)
SemanticModel declarationSemanticModel)
{
return new InlineMethodRefactoring(document, node, nodeEnclosingType, symbol, declaration, parameterInfos, nodeSemanticModel, declarationSemanticModel, cancellationToken);
return new InlineMethodRefactoring(document, node, nodeEnclosingType, symbol, declaration, parameterInfos, nodeSemanticModel, declarationSemanticModel);
}

protected override RefactoringDescriptor GetDescriptor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public InlineMethodRefactoring(
MethodDeclarationSyntax declaration,
ImmutableArray<ParameterInfo> parameterInfos,
SemanticModel nodeSemanticModel,
SemanticModel declarationSemanticModel,
CancellationToken cancellationToken) : base(document, node, nodeEnclosingType, symbol, declaration, parameterInfos, nodeSemanticModel, declarationSemanticModel, cancellationToken)
SemanticModel declarationSemanticModel) : base(document, node, nodeEnclosingType, symbol, declaration, parameterInfos, nodeSemanticModel, declarationSemanticModel)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ protected override InlineRefactoring<IdentifierNameSyntax, PropertyDeclarationSy
PropertyDeclarationSyntax declaration,
ImmutableArray<ParameterInfo> parameterInfos,
SemanticModel nodeSemanticModel,
SemanticModel declarationSemanticModel,
CancellationToken cancellationToken)
SemanticModel declarationSemanticModel)
{
return new InlinePropertyRefactoring(document, node, nodeEnclosingType, symbol, declaration, parameterInfos, nodeSemanticModel, declarationSemanticModel, cancellationToken);
return new InlinePropertyRefactoring(document, node, nodeEnclosingType, symbol, declaration, parameterInfos, nodeSemanticModel, declarationSemanticModel);
}

protected override RefactoringDescriptor GetDescriptor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public InlinePropertyRefactoring(
PropertyDeclarationSyntax declaration,
ImmutableArray<ParameterInfo> parameterInfos,
SemanticModel invocationSemanticModel,
SemanticModel declarationSemanticModel,
CancellationToken cancellationToken) : base(document, node, nodeEnclosingType, symbol, declaration, parameterInfos, invocationSemanticModel, declarationSemanticModel, cancellationToken)
SemanticModel declarationSemanticModel) : base(document, node, nodeEnclosingType, symbol, declaration, parameterInfos, invocationSemanticModel, declarationSemanticModel)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ protected InlineRefactoring(
TDeclaration declaration,
ImmutableArray<ParameterInfo> parameterInfos,
SemanticModel invocationSemanticModel,
SemanticModel declarationSemanticModel,
CancellationToken cancellationToken)
SemanticModel declarationSemanticModel)
{
Document = document;
Node = node;
Expand All @@ -40,7 +39,6 @@ protected InlineRefactoring(
ParameterInfos = parameterInfos;
InvocationSemanticModel = invocationSemanticModel;
DeclarationSemanticModel = declarationSemanticModel;
CancellationToken = cancellationToken;
}

public abstract SyntaxNode BodyOrExpressionBody { get; }
Expand All @@ -63,14 +61,12 @@ protected InlineRefactoring(

public SemanticModel DeclarationSemanticModel { get; }

public CancellationToken CancellationToken { get; }

public virtual Task<Document> InlineAsync(
SyntaxNode node,
ExpressionSyntax expression,
CancellationToken cancellationToken = default)
{
ExpressionSyntax newExpression = RewriteExpression(node, expression);
ExpressionSyntax newExpression = RewriteExpression(node, expression, cancellationToken);

return Document.ReplaceNodeAsync(node, newExpression, cancellationToken);
}
Expand All @@ -84,7 +80,7 @@ public virtual async Task<Solution> InlineAndRemoveAsync(
{
DocumentEditor editor = await DocumentEditor.CreateAsync(Document, cancellationToken).ConfigureAwait(false);

ExpressionSyntax newExpression = RewriteExpression(node, expression);
ExpressionSyntax newExpression = RewriteExpression(node, expression, cancellationToken);

editor.ReplaceNode(node, newExpression);

Expand All @@ -104,9 +100,9 @@ public virtual async Task<Solution> InlineAndRemoveAsync(
}
}

private ParenthesizedExpressionSyntax RewriteExpression(SyntaxNode node, ExpressionSyntax expression)
private ParenthesizedExpressionSyntax RewriteExpression(SyntaxNode node, ExpressionSyntax expression, CancellationToken cancellationToken)
{
return RewriteNode(expression)
return RewriteNode(expression, cancellationToken)
.WithTriviaFrom(node)
.Parenthesize()
.WithFormatterAnnotation();
Expand All @@ -119,7 +115,7 @@ public virtual Task<Document> InlineAsync(
{
int count = statements.Count;

StatementSyntax[] newStatements = RewriteStatements(statements);
StatementSyntax[] newStatements = RewriteStatements(statements, cancellationToken);

newStatements[0] = newStatements[0].WithLeadingTrivia(expressionStatement.GetLeadingTrivia());
newStatements[count - 1] = newStatements[count - 1].WithTrailingTrivia(expressionStatement.GetTrailingTrivia());
Expand Down Expand Up @@ -147,7 +143,7 @@ public virtual async Task<Solution> InlineAndRemoveAsync(
{
DocumentEditor editor = await DocumentEditor.CreateAsync(Document, cancellationToken).ConfigureAwait(false);

StatementSyntax[] newStatements = RewriteStatements(statements);
StatementSyntax[] newStatements = RewriteStatements(statements, cancellationToken);

int count = statements.Count;

Expand Down Expand Up @@ -183,28 +179,28 @@ public virtual async Task<Solution> InlineAndRemoveAsync(
}
}

private StatementSyntax[] RewriteStatements(SyntaxList<StatementSyntax> statements)
private StatementSyntax[] RewriteStatements(SyntaxList<StatementSyntax> statements, CancellationToken cancellationToken)
{
var newStatements = new StatementSyntax[statements.Count];

for (int i = 0; i < statements.Count; i++)
newStatements[i] = RewriteNode(statements[i]).WithFormatterAnnotation();
newStatements[i] = RewriteNode(statements[i], cancellationToken).WithFormatterAnnotation();

return newStatements;
}

private T RewriteNode<T>(T node) where T : SyntaxNode
private T RewriteNode<T>(T node, CancellationToken cancellationToken) where T : SyntaxNode
{
Dictionary<ISymbol, string> symbolMap = GetSymbolsToRename();
Dictionary<ISymbol, string> symbolMap = GetSymbolsToRename(cancellationToken);

Dictionary<SyntaxNode, object> replacementMap = GetReplacementMap(node, symbolMap);
Dictionary<SyntaxNode, object> replacementMap = GetReplacementMap(node, symbolMap, cancellationToken);

var rewriter = new InlineRewriter(replacementMap);

return (T)rewriter.Visit(node);
}

private Dictionary<SyntaxNode, object> GetReplacementMap(SyntaxNode node, Dictionary<ISymbol, string> symbolMap)
private Dictionary<SyntaxNode, object> GetReplacementMap(SyntaxNode node, Dictionary<ISymbol, string> symbolMap, CancellationToken cancellationToken)
{
var replacementMap = new Dictionary<SyntaxNode, object>();

Expand All @@ -216,7 +212,7 @@ private Dictionary<SyntaxNode, object> GetReplacementMap(SyntaxNode node, Dictio
{
var identifierName = (IdentifierNameSyntax)descendant;

ISymbol symbol = DeclarationSemanticModel.GetSymbol(identifierName, CancellationToken);
ISymbol symbol = DeclarationSemanticModel.GetSymbol(identifierName, cancellationToken);

if (symbol is not null)
{
Expand Down Expand Up @@ -286,7 +282,7 @@ private Dictionary<SyntaxNode, object> GetReplacementMap(SyntaxNode node, Dictio
case SyntaxKind.ForEachStatement:
case SyntaxKind.ForEachVariableStatement:
{
ISymbol symbol = DeclarationSemanticModel.GetDeclaredSymbol(descendant, CancellationToken);
ISymbol symbol = DeclarationSemanticModel.GetDeclaredSymbol(descendant, cancellationToken);

Debug.Assert(symbol is not null || (descendant as ForEachVariableStatementSyntax)?.Variable?.Kind() == SyntaxKind.TupleExpression, kind.ToString());

Expand Down Expand Up @@ -327,12 +323,12 @@ static bool ParameterEquals(in ParameterInfo parameterInfo, IParameterSymbol par
}
}

private Dictionary<ISymbol, string> GetSymbolsToRename()
private Dictionary<ISymbol, string> GetSymbolsToRename(CancellationToken cancellationToken)
{
ImmutableArray<ISymbol> declarationSymbols = DeclarationSemanticModel.GetDeclaredSymbols(
BodyOrExpressionBody,
excludeAnonymousTypeProperty: true,
cancellationToken: CancellationToken);
cancellationToken: cancellationToken);

if (!declarationSymbols.Any())
return null;
Expand All @@ -347,7 +343,7 @@ private Dictionary<ISymbol, string> GetSymbolsToRename()
ImmutableArray<ISymbol> invocationSymbols = InvocationSemanticModel.GetSymbolsDeclaredInEnclosingSymbol(
position,
excludeAnonymousTypeProperty: true,
cancellationToken: CancellationToken);
cancellationToken: cancellationToken);

invocationSymbols = invocationSymbols.AddRange(InvocationSemanticModel.LookupSymbols(position));

Expand Down

0 comments on commit d52ec57

Please sign in to comment.