Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
Fix, UniRxAnalyzer 1.0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jun 26, 2015
1 parent c36486c commit d12e27c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Analyzer/UniRxAnalyzer/UniRxAnalyzer/Diagnostic.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>UniRxAnalyzer</id>
<version>1.0.3.0</version>
<version>1.0.3.1</version>
<title>UniRxAnalyzer</title>
<authors>neuecc</authors>
<projectUrl>https://github.com/neuecc/UniRx</projectUrl>
Expand Down
50 changes: 38 additions & 12 deletions Analyzer/UniRxAnalyzer/UniRxAnalyzer/HandleObservableAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,59 @@ private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
.DescendantNodes(descendIntoChildren: x => !(x is InvocationExpressionSyntax))
.OfType<InvocationExpressionSyntax>();

foreach (var expr in invocationExpressions)
{
var type = context.SemanticModel.GetTypeInfo(expr).Type;
// UniRx.IObservable? System.IObservable?
if (new[] { type }.Concat(type.AllInterfaces).Any(x => x.Name == "IObservable"))
{
if (ValidateInvocation(expr)) continue;

// Report Warning
var diagnostic = Diagnostic.Create(Rule, expr.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}

// in lambda expression

var inlambdaInvocationExpressions = context.Node.DescendantNodes()
.OfType<LambdaExpressionSyntax>()
.SelectMany(x => x.DescendantNodes(descendIntoChildren: y => !(y is InvocationExpressionSyntax)))
.OfType<InvocationExpressionSyntax>();
.SelectMany(x => x.DescendantNodes(descendIntoChildren: y => !(y is InvocationExpressionSyntax)).OfType<InvocationExpressionSyntax>(),
(lambda, invocation) => new { lambda, invocation });

foreach (var expr in invocationExpressions.Concat(inlambdaInvocationExpressions))
foreach (var inlambda in inlambdaInvocationExpressions)
{
if (!inlambda.lambda.ChildNodes().OfType<BlockSyntax>().Any()) continue;

var expr = inlambda.invocation;

var type = context.SemanticModel.GetTypeInfo(expr).Type;
// UniRx.IObservable? System.IObservable?
if (new[] { type }.Concat(type.AllInterfaces).Any(x => x.Name == "IObservable"))
{
// Okay => x = M(), var x = M(), return M(), from x in M(), (bool) ? M() : M()
if (expr.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression)) continue;
if (expr.Parent.IsKind(SyntaxKind.EqualsValueClause) && expr.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator)) continue;
if (expr.Parent.IsKind(SyntaxKind.ReturnStatement)) continue;
if (expr.Parent.IsKind(SyntaxKind.FromClause)) continue;
if (expr.Parent.IsKind(SyntaxKind.ConditionalExpression)) continue;

// Okay => M().M()
if (expr.DescendantNodes().OfType<InvocationExpressionSyntax>().Any()) continue;
if (ValidateInvocation(expr)) continue;

// Report Warning
var diagnostic = Diagnostic.Create(Rule, expr.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}
}

static bool ValidateInvocation(InvocationExpressionSyntax expr)
{
// Okay => x = M(), var x = M(), return M(), from x in M(), (bool) ? M() : M()
if (expr.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression)) return true;
if (expr.Parent.IsKind(SyntaxKind.EqualsValueClause) && expr.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator)) return true;
if (expr.Parent.IsKind(SyntaxKind.ReturnStatement)) return true;
if (expr.Parent.IsKind(SyntaxKind.FromClause)) return true;
if (expr.Parent.IsKind(SyntaxKind.ConditionalExpression)) return true;

// Okay => M().M()
if (expr.DescendantNodes().OfType<InvocationExpressionSyntax>().Any()) return true;

return false;
}
}
}

0 comments on commit d12e27c

Please sign in to comment.