public MethodError(MethodDeclarationSyntax methodDeclaration, string code) : base(code) { this.InterfaceName = methodDeclaration.FirstAncestorOrSelf <InterfaceDeclarationSyntax>()?.Identifier.Text; this.MethodName = methodDeclaration.Identifier.Text; this.SetLocation(methodDeclaration.GetLocation()); }
private static bool IsMessage(SyntaxNodeAnalysisContext context, MethodDeclarationSyntax method, Type metadata, string methodName) { var classDeclaration = method?.FirstAncestorOrSelf <ClassDeclarationSyntax>(); if (classDeclaration == null) { return(false); } var methodSymbol = context.SemanticModel.GetDeclaredSymbol(method); var typeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration); var scriptInfo = new ScriptInfo(typeSymbol); if (!scriptInfo.HasMessages) { return(false); } if (!scriptInfo.IsMessage(methodSymbol)) { return(false); } return(scriptInfo.Metadata == metadata && methodSymbol.Name == methodName); }
private static SyntaxNode FixMethodSignature(ref MethodDeclarationSyntax method, ITypeSymbol returnTypeSymbol, SyntaxNode syntaxRoot) { if (method.Modifiers.Any(SyntaxKind.AsyncKeyword)) { return(syntaxRoot); } TypeSyntax typeSyntax; if (returnTypeSymbol.SpecialType == SpecialType.System_Void) { typeSyntax = ParseTypeName(typeof(Task).FullName); } else { typeSyntax = ParseTypeName(typeof(Task).FullName + "<" + method.ReturnType.WithoutTrivia().ToFullString() + ">"); } var newMethod = method .WithReturnType(typeSyntax.WithTrailingTrivia(Space)); if (method.FirstAncestorOrSelf <InterfaceDeclarationSyntax>() == null) { newMethod = newMethod.AddModifiers(Token(SyntaxKind.AsyncKeyword).WithTrailingTrivia(Space)); } //var trackedRoot = syntaxRoot.TrackNodes(method); //var tempRoot = trackedRoot.ReplaceNode(typeSyntax, typeSyntax.WithTrailingTrivia(Space)); //var temp = tempRoot.GetCurrentNode(method); syntaxRoot = syntaxRoot.ReplaceNode(method, newMethod); method = RefindMethod(method, syntaxRoot); return(syntaxRoot); }
private static async Task <Document> MakeMethodAsPublic(Document document, MethodDeclarationSyntax declaration, CancellationToken cancellationToken) { var oldNode = declaration.FirstAncestorOrSelf <MethodDeclarationSyntax>(); var newNode = oldNode.WithModifiers(SyntaxTokenList.Create(SyntaxFactory.Token(SyntaxKind.PublicKeyword))); return(await document.ReplaceNodeAsync(oldNode, newNode, cancellationToken).ConfigureAwait(false)); }
private static bool IsUsedAsEventHandler(MethodDeclarationSyntax methodDeclaration) => methodDeclaration.FirstAncestorOrSelf <ClassDeclarationSyntax>() .DescendantNodes() .OfType <AssignmentExpressionSyntax>() .Where(aes => aes.IsKind(SyntaxKind.AddAssignmentExpression)) .Select(aes => aes.Right) .OfType <IdentifierNameSyntax>() .Any(ins => ins.Identifier.ValueText == methodDeclaration.Identifier.ValueText);
public override void VisitMethodDeclaration(MethodDeclarationSyntax node) { var classDeclaration = node.FirstAncestorOrSelf <ClassDeclarationSyntax>(); Console.WriteLine($"Found method with name {node.Identifier.Text} that is in class with name {classDeclaration.Identifier.Text}"); base.VisitMethodDeclaration(node); }
public static IEnumerable <SyntaxNodeWithSymbol <InvocationExpressionSyntax> > ScanMethodCallers(this MethodDeclarationSyntax declaration, SemanticModel sm, Func <IMethodSymbol, bool> predicate) { var @class = declaration.FirstAncestorOrSelf <ClassDeclarationSyntax>(); return(@class.DescendantNodes() .OfType <InvocationExpressionSyntax>() .Select(w => new SyntaxNodeWithSymbol <InvocationExpressionSyntax> { Info = sm.GetSymbolInfo(w), Node = w }) .Where(w => w.Info.Symbol is IMethodSymbol symbol && predicate.Invoke(symbol)) .ToList()); }
public override void VisitMethodDeclaration(MethodDeclarationSyntax node) { var containingTypeSyntax = node.FirstAncestorOrSelf <TypeDeclarationSyntax>(); var methodSymbol = this.SemanticModel.GetDeclaredSymbol(node); if (node.ExpressionBody != null) { this.ProcessArrowExpressionDecl(node.ExpressionBody, containingTypeSyntax, methodSymbol); } else { this.ProcessMethodDecl(node, containingTypeSyntax, methodSymbol); } base.VisitMethodDeclaration(node); }
private static bool IsCriticalMessage(SyntaxNodeAnalysisContext context, MethodDeclarationSyntax method) { var methodSymbol = context.SemanticModel.GetDeclaredSymbol(method); var classDeclaration = method.FirstAncestorOrSelf <ClassDeclarationSyntax>(); if (classDeclaration == null) { return(false); } var typeSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclaration); var scriptInfo = new ScriptInfo(typeSymbol); if (!scriptInfo.HasMessages) { return(false); } if (!scriptInfo.IsMessage(methodSymbol)) { return(false); } switch (methodSymbol.Name) { case "Update": case "FixedUpdate": case "LateUpdate": case "OnGUI": return(true); default: return(false); } }
MethodDeclarationSyntax RewriteMethod(MethodDeclarationSyntax inMethodSyntax, SemanticModel semanticModel, ITypeSymbol cancellationTokenSymbol) { var inMethodSymbol = semanticModel.GetDeclaredSymbol(inMethodSyntax); // ASYNC_TODO: Find all references //Log.LogMessage("Method {0}: {1}", inMethodInfo.Symbol.Name, inMethodInfo.Symbol.); var outMethodName = inMethodSyntax.Identifier.Text + "Async"; _log.Debug(" Rewriting method {0} to {1}", inMethodSymbol.Name, outMethodName); // Visit all method invocations inside the method, rewrite them to async if needed var rewriter = new MethodInvocationRewriter(_log, semanticModel, excludedTypesSet, cancellationTokenSymbol, GenerateConfigureAwait); var outMethod = (MethodDeclarationSyntax)rewriter.Visit(inMethodSyntax); // Method signature outMethod = outMethod .WithIdentifier(SyntaxFactory.Identifier(outMethodName)) .WithAttributeLists(new SyntaxList <AttributeListSyntax>()); if (inMethodSyntax.FirstAncestorOrSelf <TypeDeclarationSyntax>().Kind() == SyntaxKind.InterfaceDeclaration) { outMethod = outMethod .WithModifiers(inMethodSyntax.Modifiers); } else { outMethod = outMethod .WithModifiers(inMethodSyntax.Modifiers .Add(SyntaxFactory.Token(SyntaxKind.AsyncKeyword))); } //.Remove(SyntaxFactory.Token(SyntaxKind.OverrideKeyword)) //.Remove(SyntaxFactory.Token(SyntaxKind.NewKeyword)) // Insert the cancellation token into the parameter list at the right place outMethod = outMethod .WithParameterList(SyntaxFactory.ParameterList(inMethodSyntax.ParameterList.Parameters.Insert( inMethodSyntax.ParameterList.Parameters.TakeWhile(p => p.Default == null && !p.Modifiers.Any(m => m.IsKind(SyntaxKind.ParamsKeyword))).Count(), SyntaxFactory.Parameter( SyntaxFactory.List <AttributeListSyntax>(), SyntaxFactory.TokenList(), SyntaxFactory.ParseTypeName("CancellationToken"), SyntaxFactory.Identifier("cancellationToken"), null )))); // Transform return type adding Task<> var returnType = inMethodSyntax.ReturnType.ToString(); outMethod = outMethod.WithReturnType(SyntaxFactory.ParseTypeName( returnType == "void" ? "Task" : $"Task<{returnType}>") ); // Remove the override and new attributes. Seems like the clean .Remove above doesn't work... for (var i = 0; i < outMethod.Modifiers.Count;) { var text = outMethod.Modifiers[i].Text; if (text == "override" || text == "new") { outMethod = outMethod.WithModifiers(outMethod.Modifiers.RemoveAt(i)); continue; } i++; } var attr = inMethodSymbol.GetAttributes().Single(a => a.AttributeClass.Name == "RewriteAsyncAttribute"); if (attr.ConstructorArguments.Length > 0 && (bool)attr.ConstructorArguments[0].Value) { outMethod = outMethod.AddModifiers(SyntaxFactory.Token(SyntaxKind.OverrideKeyword)); } return(outMethod); }
private static bool IsUsedAsEventHandler(MethodDeclarationSyntax methodDeclaration) => methodDeclaration.FirstAncestorOrSelf <ClassDeclarationSyntax>() is ClassDeclarationSyntax parentClass &&