Skip to content

Commit

Permalink
[GH-108] - better way of finding symbol usages
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodolak committed Sep 13, 2020
1 parent a0156dc commit a838b02
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,5 @@ public NonSubstitutableMemberReceivedInOrderAnalyzer()
protected override SyntaxKind InvocationExpressionKind { get; } = SyntaxKind.InvocationExpression;

protected override ImmutableArray<int> IgnoredAncestorPaths { get; } = IgnoredPaths;

protected override ISymbol GetDeclarationSymbol(SemanticModel semanticModel, SyntaxNode node)
{
return semanticModel.GetDeclaredSymbol(node);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ protected sealed override void InitializeAnalyzer(AnalysisContext context)
context.RegisterSyntaxNodeAction(_analyzeInvocationAction, InvocationExpressionKind);
}

protected abstract ISymbol GetDeclarationSymbol(SemanticModel semanticModel, SyntaxNode node);

protected override Location GetSubstitutionNodeActualLocation(in NonSubstitutableMemberAnalysisResult analysisResult)
{
return analysisResult.Member.GetSubstitutionNodeActualLocation<TMemberAccessExpressionSyntax>(analysisResult.Symbol);
Expand Down Expand Up @@ -104,7 +102,7 @@ operation.Parent is IInvocationOperation invocationOperation &&
return false;
}

var symbol = GetDeclarationSymbol(semanticModel, maybeIgnoredExpression);
var symbol = GetVariableDeclaratorSymbol(operation);

if (symbol == null)
{
Expand All @@ -123,6 +121,16 @@ operation.Parent is IInvocationOperation invocationOperation &&
return !dataFlowAnalysis.ReadInside.Contains(symbol);
}

private static ILocalSymbol GetVariableDeclaratorSymbol(IOperation operation)
{
return operation switch
{
IVariableDeclaratorOperation declarator => declarator.Symbol,
IVariableDeclarationOperation declarationOperation => declarationOperation.Declarators.FirstOrDefault()?.Symbol,
_ => null
};
}

private SyntaxNode FindIgnoredEnclosingExpression(SyntaxNode syntaxNode)
{
return syntaxNode.Ancestors().FirstOrDefault(ancestor => IgnoredAncestorPaths.Contains(ancestor.RawKind));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,5 @@ public NonSubstitutableMemberReceivedInOrderAnalyzer()
protected override SyntaxKind InvocationExpressionKind { get; } = SyntaxKind.InvocationExpression;

protected override ImmutableArray<int> IgnoredAncestorPaths { get; } = IgnoredPaths;

protected override ISymbol GetDeclarationSymbol(SemanticModel semanticModel, SyntaxNode node)
{
var symbol = semanticModel.GetDeclaredSymbol(node);

if (symbol != null)
{
return symbol;
}

if (!(node is VariableDeclaratorSyntax variableDeclaratorSyntax))
{
return null;
}

var modifiedIdentifierSyntax = variableDeclaratorSyntax.Names.FirstOrDefault();

if (modifiedIdentifierSyntax == null)
{
return null;
}

return semanticModel.GetDeclaredSymbol(modifiedIdentifierSyntax);
}
}
}

0 comments on commit a838b02

Please sign in to comment.