private async Task <Document> ReplaceThreadWithTask(Document document, MemberAccessExpressionSyntax node, CancellationToken cancellationToken) { var block = node.Ancestors().OfType <BlockSyntax>().First(); var variableName = node.Expression.ToString(); var threadArgument = FindThreadArgument(variableName, block); if (threadArgument == null) { return(document); } var invocationStatement = TaskSyntaxHelper.CreateInvocationStatement(threadArgument); var documentEditor = await DocumentEditor.CreateAsync(document, cancellationToken); documentEditor.ReplaceNode(node.Parent, invocationStatement); RemoveThreadingCode(variableName, block, documentEditor); var newDocument = documentEditor.GetChangedDocument(); newDocument = await AddUsingsToDocument(newDocument); return(newDocument); }
private Diagnostic AnalyzeSimpleMemberAccessExpression(MemberAccessExpressionSyntax call, SemanticModel semanticModel) { if (IsCall(call, semanticModel)) { foreach (var ancestor in call.Ancestors()) { switch (ancestor) { case BlockSyntax block: return(AnalyzeSimpleMemberAccessExpression(block.Statements, call, semanticModel)); case SwitchSectionSyntax section: return(AnalyzeSimpleMemberAccessExpression(section.Statements, call, semanticModel)); // case IfStatementSyntax _: required by MiKo_3201 case ElseClauseSyntax _: case ParenthesizedLambdaExpressionSyntax _: case MethodDeclarationSyntax _: case ClassDeclarationSyntax _: return(null); // stop lookup as there is no valid ancestor anymore } } } return(null); }
private bool ReducableIdentifierNode(MemberAccessExpressionSyntax node) { if (node.Expression.Kind() != SyntaxKind.IdentifierName) { return(false); } if (_semanticModel == null) { _semanticModel = _document.GetSemanticModelAsync(_cancellationToken).Result; } var symbol = _semanticModel.GetSymbolInfo(node, _cancellationToken).Symbol; var containingSymbol = symbol != null ? symbol.ContainingSymbol : null; if (containingSymbol != null) { foreach (var ancestorNode in node.Ancestors()) { ISymbol ancestorSymbol = _semanticModel.GetDeclaredSymbol(ancestorNode, _cancellationToken); if (ancestorSymbol == containingSymbol) { // if declared in same class - we can simplify return(true); } } return(false); } return(false); }
private static async Task <Document> FixResourceContextAPI(Document document, MemberAccessExpressionSyntax memberAccessExpression, CancellationToken cancellationToken) { var oldRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); var invocation = memberAccessExpression.Ancestors().OfType <InvocationExpressionSyntax>().FirstOrDefault(); if (invocation == null) { return(document); } var comment = await CSharpSyntaxTree.ParseText(@"/* TODO ResourceContext.GetForCurrentView and ResourceContext.GetForViewIndependentUse do not exist in Windows App SDK Use your ResourceManager instance to create a ResourceContext as below. If you already have a ResourceManager instance, replace the new instance created below with correct instance. Read: https://docs.microsoft.com/en-us/windows/apps/windows-app-sdk/migrate-to-windows-app-sdk/guides/mrtcore */", cancellationToken : cancellationToken).GetRootAsync(cancellationToken).ConfigureAwait(false); var newMemberInitiationRoot = await CSharpSyntaxTree.ParseText("new Microsoft.Windows.ApplicationModel.Resources.ResourceManager().CreateResourceContext()", cancellationToken : cancellationToken) .GetRootAsync(cancellationToken).ConfigureAwait(false); var newMemberInitiation = newMemberInitiationRoot.DescendantNodesAndSelf().OfType <InvocationExpressionSyntax>().First(); var newMemberInitiationWithComment = newMemberInitiation.WithLeadingTrivia(comment.GetLeadingTrivia()); return(document.WithSyntaxRoot(oldRoot !.ReplaceNode(invocation, newMemberInitiationWithComment))); }
public SyntaxNode ChangeTree(Document doc, SemanticModel model, MemberAccessExpressionSyntax memberAccess, char?commitKey) { var statement = memberAccess.Ancestors().OfType <StatementSyntax>().FirstOrDefault(); if (statement != null) { // insert return statement after current statement SyntaxNode oldNode; BlockSyntax block; int index; if (statement.Parent is BlockSyntax) { // after current statement in the block oldNode = block = statement.Parent as BlockSyntax; index = block.Statements.IndexOf(statement) + 1; block = block.ReplaceNode(statement, statement.ReplaceNode(memberAccess, (memberAccess as MemberAccessExpressionSyntax).Expression)).FixStatement(); } else { // if it is not in block, insert one // if (...) Method().return ---> if (...) { Method(); return; } oldNode = statement; block = SyntaxFactory.Block(statement.ReplaceNode(memberAccess, (memberAccess as MemberAccessExpressionSyntax).Expression).FixStatement().WithoutTrivia()); index = 1; } // insert return to the found or created block SyntaxNode newBlock = block.WithStatements(block.Statements.Insert(index, SyntaxFactory.ReturnStatement())) .WithAdditionalAnnotations(Formatter.Annotation); return(model.SyntaxTree.GetRoot().ReplaceNode(oldNode, newBlock)); } return(null); }
public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { // Dong Xie: node.Name.GetText() if (node.Name.GetText().ToString() != PublishKeyword) return; PublicationCalls.Add(node); Publications.Add(node.Ancestors().OfType<InvocationExpressionSyntax>().Last()); }
public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax node) { // Dong Xie: node.Name.GetText() if (node.Name.GetText().ToString() != PublishKeyword) { return; } PublicationCalls.Add(node); Publications.Add(node.Ancestors().OfType <InvocationExpressionSyntax>().Last()); }
private static bool AccessMethodWithDynamicArgumentInsideStructConstructor(this MemberAccessExpressionSyntax memberAccess, SemanticModel semanticModel) { var constructor = memberAccess.Ancestors().OfType <ConstructorDeclarationSyntax>().SingleOrDefault(); if (constructor == null || constructor.Parent.Kind() != SyntaxKind.StructDeclaration) { return(false); } return(semanticModel.GetSymbolInfo(memberAccess.Name).CandidateReason == CandidateReason.LateBound); }
private void Initialize(int depth, MemberAccessExpressionSyntax memberAccessExpressionSyntax, IPropertySymbol propertySymbol, SemanticModel semanticModel) { Depth = depth; IsProperty = true; InvocationString = memberAccessExpressionSyntax.ToString(); var containingNode = memberAccessExpressionSyntax.Ancestors().OfType <StatementSyntax>().FirstOrDefault(); OwnerStatementString = containingNode?.ToString(); var objectAccessedSyntax = memberAccessExpressionSyntax.Expression; // .ToString() => var name var objectAccessedSymbol = semanticModel.GetSymbolInfo(objectAccessedSyntax).Symbol; var objectAccessedTypeSymbol = semanticModel.GetTypeInfo(objectAccessedSyntax).Type; MemberOwnerType = new TypeDescriptor(objectAccessedTypeSymbol); MemberName = propertySymbol.Name; ReturnType = new TypeDescriptor(propertySymbol.Type); var containingTypeSymbol = propertySymbol.ContainingType; ContainingType = new TypeDescriptor(containingTypeSymbol); PropertyDeclarationSyntax = SymbolHelper.GetSyntaxReferences(memberAccessExpressionSyntax, semanticModel) .FirstOrDefault() ?.GetSyntax() as PropertyDeclarationSyntax; var assignment = memberAccessExpressionSyntax.Ancestors().OfType <AssignmentExpressionSyntax>().FirstOrDefault(); if (assignment != null && assignment.Left.DescendantNodesAndSelf().Contains(memberAccessExpressionSyntax)) { // is a set property IsPropertySetter = true; } else { // is a get property } }
private static async Task <Document> FixContentDialogAPI(Document document, MemberAccessExpressionSyntax contentDialogMemberAccess, CancellationToken cancellationToken) { var newMethodDeclarationSibling = contentDialogMemberAccess.Ancestors().OfType <MethodDeclarationSyntax>().First(); var newMethodCall = SyntaxFactory.InvocationExpression(SyntaxFactory.ParseExpression("SetContentDialogRoot"), SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(new[] { SyntaxFactory.Argument(contentDialogMemberAccess.Expression), SyntaxFactory.Argument(SyntaxFactory.ParseExpression("this")) }))); var newMethodCallComment = SyntaxFactory.Comment(DialogSetterComment); var documentEditor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); documentEditor.ReplaceNode(contentDialogMemberAccess.Expression, newMethodCall.WithLeadingTrivia(newMethodCallComment)); if (!newMethodDeclarationSibling.Parent !.ChildNodes().OfType <MethodDeclarationSyntax>() .Any(sibling => sibling.Identifier.ValueText == "SetContentDialogRoot")) { var newMethodRoot = await CSharpSyntaxTree.ParseText(@" class A { private static ContentDialog SetContentDialogRoot(ContentDialog contentDialog, UserControl control) { if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent(""Windows.Foundation.UniversalApiContract"", 8)) { contentDialog.XamlRoot = control.Content.XamlRoot; } return contentDialog; } }", cancellationToken : cancellationToken).GetRootAsync(cancellationToken).ConfigureAwait(false); var newMethodDeclaration = newMethodRoot.DescendantNodes().OfType <MethodDeclarationSyntax>().First(); documentEditor.InsertAfter(newMethodDeclarationSibling, newMethodDeclaration); } return(document.WithSyntaxRoot(documentEditor.GetChangedRoot())); }