Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
mavasani committed Jan 8, 2019
1 parent f0a1a6d commit 5bb5a4b
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Editing;
Expand Down Expand Up @@ -78,15 +79,24 @@ private async Task<Solution> UpdateReferencesAsync(ISymbol symbol, Solution solu
cancellationToken.ThrowIfCancellationRequested();

var references = await SymbolFinder.FindReferencesAsync(symbol, solution, cancellationToken).ConfigureAwait(false);
if (references?.Count() != 1)
if (references.Count() != 1)
{
return solution;
}

// Group references by document and fix references in each document.
foreach (var referenceLocationGroup in references.Single().Locations.GroupBy(r => r.Document))
{
// Get document in current solution
var document = solution.GetDocument(referenceLocationGroup.Key.Id);

// Skip references in projects with different language.
// https://github.com/dotnet/roslyn-analyzers/issues/1986 tracks handling them.
if (!document.Project.Language.Equals(symbol.Language, StringComparison.Ordinal))
{
continue;
}

var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

Expand All @@ -99,7 +109,7 @@ private async Task<Solution> UpdateReferencesAsync(ISymbol symbol, Solution solu
var referenceNode = root.FindNode(referenceLocation.Location.SourceSpan, getInnermostNodeForTie: true);
if (referenceNode != null)
{
var operation = GetOperationWalkingUpParentChain(referenceNode, semanticModel);
var operation = semanticModel.GetOperationWalkingUpParentChain(referenceNode, cancellationToken);
SyntaxNode nodeToReplaceOpt = null;
switch (operation)
{
Expand Down Expand Up @@ -155,24 +165,6 @@ private async Task<Solution> UpdateReferencesAsync(ISymbol symbol, Solution solu
return solution;

// Local functions.
IOperation GetOperationWalkingUpParentChain(SyntaxNode node, SemanticModel semanticModel)
{
// Walk up the parent chain to fetch the first non-null operation.
do
{
var operation = semanticModel.GetOperation(node, cancellationToken);
if (operation != null)
{
return operation;
}

node = node.Parent;
}
while (node != null);

return null;
}

bool IsReplacableOperation(IOperation operation)
{
// We only replace reference operations whose removal cannot change semantics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,6 @@
<value>Do not duplicate indexed element initializations</value>
</data>
<data name="MarkMembersAsStaticCodeFix" xml:space="preserve">
<value>Make static (Shared in VB)</value>
<value>Make static</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="cs" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="de" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="es" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="fr" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="it" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="ja" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="ko" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="pl" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="pt-BR" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="ru" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="tr" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="zh-Hans" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<file datatype="xml" source-language="en" target-language="zh-Hant" original="../MicrosoftQualityGuidelinesAnalyzersResources.resx">
<body>
<trans-unit id="MarkMembersAsStaticCodeFix">
<source>Make static (Shared in VB)</source>
<target state="new">Make static (Shared in VB)</target>
<source>Make static</source>
<target state="new">Make static</target>
<note />
</trans-unit>
<trans-unit id="UseLiteralsWhereAppropriateTitle">
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/Analyzer.Utilities.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<Compile Include="$(MSBuildThisFileDirectory)CodeMetrics\SemanticModelProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CommonAccessibilityUtilities.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\OperationBlockAnalysisContextExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SemanticModelExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)InstanceReferenceKind.cs" />
<Compile Include="$(MSBuildThisFileDirectory)DisposeMethodKind.cs" />
<Compile Include="$(MSBuildThisFileDirectory)HashUtilities.cs" />
Expand Down
28 changes: 28 additions & 0 deletions src/Utilities/Extensions/SemanticModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Threading;
using Microsoft.CodeAnalysis;

namespace Analyzer.Utilities.Extensions
{
internal static class SemanticModelExtensions
{
public static IOperation GetOperationWalkingUpParentChain(this SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
{
// Walk up the parent chain to fetch the first non-null operation.
do
{
var operation = semanticModel.GetOperation(node, cancellationToken);
if (operation != null)
{
return operation;
}

node = node.Parent;
}
while (node != null);

return null;
}
}
}

0 comments on commit 5bb5a4b

Please sign in to comment.