public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) { if (node.FirstAncestorOrSelf <PropertyDeclarationSyntax>() != null && node.FirstAncestorOrSelf <PropertyDeclarationSyntax>().Identifier.Text == "SelectorExpression") { var customExpressions = node.Initializer.Expressions .Where(p => this._finder.IsNodeWithinCustomCode(p)) .Select(p => p.WithoutTrivia()) .ToList(); var nodeTokenList = SyntaxFactory.NodeOrTokenList(); foreach (var existingExp in customExpressions) { nodeTokenList = nodeTokenList.Add(existingExp); nodeTokenList = nodeTokenList.Add(SyntaxFactory.Token(SyntaxKind.CommaToken).AppendNewLine()); } var res = node.WithInitializer(node.Initializer .WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken)) .WithExpressions(SyntaxFactory.SeparatedList <ExpressionSyntax>(nodeTokenList))); return(res); } return(node); }
private static ISymbol GetAssignedVariableSymbol(ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel) { var variable = objectCreation.FirstAncestorOrSelf <AssignmentExpressionSyntax>()?.Left; if (variable != null) { return(semanticModel.GetSymbolInfo(variable).Symbol); } var identifier = objectCreation.FirstAncestorOrSelf <VariableDeclaratorSyntax>(); return(identifier != null ? semanticModel.GetDeclaredSymbol(identifier) : null); }
private bool IsLaterAssignedWithAllowedValue(ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel) { var statement = objectCreation.FirstAncestorOrSelf <StatementSyntax>(); if (statement == null) { return(false); } var variableSymbol = GetAssignedVariableSymbol(objectCreation, semanticModel); var nextStatements = GetNextStatements(statement); var innerStatements = GetInnerStatements(statement); return(variableSymbol != null && nextStatements.Union(innerStatements) .OfType <ExpressionStatementSyntax>() .Select(x => x.Expression) .OfType <AssignmentExpressionSyntax>() .Any(TrackedPropertySetWithAllowedValue)); bool TrackedPropertySetWithAllowedValue(AssignmentExpressionSyntax assignment) => variableSymbol.Equals(GetAssignedVariableSymbol(assignment, semanticModel)) && IsTrackedPropertyName(assignment.Left) && IsAllowedValue(assignment.Right, semanticModel); }
internal static bool TryGetDependencyProperty(ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel, CancellationToken cancellationToken, out IFieldSymbol dependencyProperty) { dependencyProperty = null; var declarator = objectCreation.FirstAncestorOrSelf <VariableDeclaratorSyntax>(); if (declarator == null) { return(false); } dependencyProperty = semanticModel.GetDeclaredSymbolSafe(declarator, cancellationToken) as IFieldSymbol; return(dependencyProperty != null); }
private static List <Param> GetSqlParams( SyntaxNodeAnalysisContext context, ObjectCreationExpressionSyntax node, IEnumerable <SyntaxNode> siblings) { var parameters = new List <Param>(); var variableDeclarator = node.FirstAncestorOrSelf <VariableDeclaratorSyntax>(); if (variableDeclarator is null) { return(parameters); } var variableSymbol = context.SemanticModel.GetDeclaredSymbol(variableDeclarator); foreach (var sibling in siblings) { // command.Parameters.AddWithValue if (sibling is InvocationExpressionSyntax invocation && invocation.Expression is MemberAccessExpressionSyntax addWithValue && addWithValue.Name.ToString() == "AddWithValue" && addWithValue.Expression is MemberAccessExpressionSyntax paramsExpr && paramsExpr.Name.ToString() == "Parameters" && paramsExpr.Expression is IdentifierNameSyntax command && context.SemanticModel.GetSymbolInfo(command).Symbol == variableSymbol) { var paramNameExpression = invocation.ArgumentList.Arguments[0].Expression; var valueExpression = invocation.ArgumentList.Arguments[1].Expression; var paramName = paramNameExpression.GetConstantValue(context.Compilation); if (paramName.StartsWith("@") && paramName.Length >= 2) { paramName = paramName.Substring(1); } parameters.Add(new Param { Name = paramName, Location = invocation.GetLocation(), Value = valueExpression.GetDummyValue(context.SemanticModel) }); } } return(parameters); }
public static bool IsPartOfStaticMemberDeclaration(this ObjectCreationExpressionSyntax objectCreationExprSyntax) { var memberForObjectCreationExpr = objectCreationExprSyntax.FirstAncestorOrSelf <MemberDeclarationSyntax>(); switch (memberForObjectCreationExpr.Kind()) { case SyntaxKind.ConstructorDeclaration: var constructorDeclaration = (ConstructorDeclarationSyntax)memberForObjectCreationExpr; return(constructorDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword)); case SyntaxKind.FieldDeclaration: var fieldDeclaration = (FieldDeclarationSyntax)memberForObjectCreationExpr; return(fieldDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword)); } return(false); }
internal static bool TryGetDependencyProperty(ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel, CancellationToken cancellationToken, out BackingFieldOrProperty fieldOrProperty) { fieldOrProperty = default; var invocation = objectCreation.FirstAncestorOrSelf <InvocationExpressionSyntax>(); if (invocation == null) { return(false); } if (DependencyProperty.TryGetRegisterCall(invocation, semanticModel, cancellationToken, out _) || DependencyProperty.TryGetRegisterReadOnlyCall(invocation, semanticModel, cancellationToken, out _) || DependencyProperty.TryGetRegisterAttachedCall(invocation, semanticModel, cancellationToken, out _) || DependencyProperty.TryGetRegisterAttachedReadOnlyCall(invocation, semanticModel, cancellationToken, out _)) { if (objectCreation.TryFirstAncestor <FieldDeclarationSyntax>(out var fieldDeclaration) && semanticModel.TryGetSymbol(fieldDeclaration, cancellationToken, out var field)) { return(BackingFieldOrProperty.TryCreateForDependencyProperty(field, out fieldOrProperty)); } if (objectCreation.TryFirstAncestor <PropertyDeclarationSyntax>(out var propertyDeclaration) && semanticModel.TryGetSymbol(propertyDeclaration, cancellationToken, out var property)) { return(BackingFieldOrProperty.TryCreateForDependencyProperty(property, out fieldOrProperty)); } return(false); } if (invocation.Expression is MemberAccessExpressionSyntax memberAccess && (DependencyProperty.TryGetAddOwnerCall(invocation, semanticModel, cancellationToken, out _) || DependencyProperty.TryGetOverrideMetadataCall(invocation, semanticModel, cancellationToken, out _)) && semanticModel.TryGetSymbol(memberAccess.Expression, cancellationToken, out ISymbol? candidate)) { return(BackingFieldOrProperty.TryCreateForDependencyProperty(candidate, out fieldOrProperty)); } return(false); }
public override SyntaxNode VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) { if (((CSharpCompilation)semanticModel.Compilation).LanguageVersion.MapSpecifiedToEffectiveVersion() != LanguageVersion.CSharp9) { return(base.VisitObjectCreationExpression(node)); } if (node.NewKeyword == null || node.Parent.IsKind(SyntaxKind.LocalDeclarationStatement) || node.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression) || node.Parent.IsKind(SyntaxKind.UsingStatement)) { return(base.VisitObjectCreationExpression(node)); } //if (node.Parent.IsKind(SyntaxKind.LocalDeclarationStatement)) // return base.VisitObjectCreationExpression(node); //if (node.Parent.IsKind(SyntaxKind.SimpleMemberAccessExpression)) // return base.VisitObjectCreationExpression(node); //if (node.Parent.IsKind(SyntaxKind.UsingStatement)) // return base.VisitObjectCreationExpression(node); var newNode = node.WithType(SyntaxFactory.ParseTypeName("")) .WithNewKeyword(node.NewKeyword.WithoutWhiteSpaceTrivia()) .WithArgumentList(node.ArgumentList ?? SyntaxFactory.ParseArgumentList("()")); var nodeTypeinfo = semanticModel.GetTypeInfo(node); var parentSymbol = semanticModel.GetSymbolInfo(node.Parent).Symbol; if (parentSymbol?.Kind == SymbolKind.Method && (parentSymbol as IMethodSymbol)?.MethodKind == MethodKind.AnonymousFunction) { return(base.VisitObjectCreationExpression(node)); } if (node.Parent.IsKind(SyntaxKind.ReturnStatement)) { var methodDeclaration = node.FirstAncestorOrSelf <MethodDeclarationSyntax>(); var propertyDeclaration = node.FirstAncestorOrSelf <PropertyDeclarationSyntax>(); if (methodDeclaration != null) { if (methodDeclaration.ReturnType.ToString() != nodeTypeinfo.Type.Name) { return(base.VisitObjectCreationExpression(node)); } var symbol = semanticModel.GetDeclaredSymbol(methodDeclaration); if (symbol.ReturnType.TypeKind != TypeKind.Class) { return(base.VisitObjectCreationExpression(node)); } } else if (propertyDeclaration != null) { var symbol = semanticModel.GetDeclaredSymbol(propertyDeclaration); if (symbol.GetMethod.ReturnType.TypeKind != TypeKind.Class) { return(base.VisitObjectCreationExpression(node)); } } } else if (node.Parent.IsKind(SyntaxKind.Argument)) { var methodInvocation = node.FirstAncestorOrSelf <InvocationExpressionSyntax>(); if (methodInvocation != null) { var methodSymbol = semanticModel.GetSymbolInfo(methodInvocation).Symbol; var countofMethod = methodSymbol?.ContainingType?.GetMembers() .Count(x => x.Name == (methodInvocation.Expression.IsKind(SyntaxKind.SimpleMemberAccessExpression) ? methodInvocation.GetRightSideNameSyntax().ToString() : methodInvocation.Expression.ToString())); if (countofMethod > 1) { return(base.VisitObjectCreationExpression(node)); } var indexOfMethod = node.FirstAncestorOrSelf <ArgumentListSyntax>().Arguments .IndexOf(node.AncestorsAndSelf().FirstOrDefault(x => x.Parent.IsKind(SyntaxKind.ArgumentList)) as ArgumentSyntax); if ((methodSymbol as IMethodSymbol)?.OriginalDefinition.Parameters[indexOfMethod].IsParams == true) { return(base.VisitObjectCreationExpression(node)); } if ((methodSymbol as IMethodSymbol)?.OriginalDefinition.IsGenericMethod == true && (methodInvocation.Expression.IsKind(SyntaxKind.SimpleMemberAccessExpression) ? !methodInvocation.GetRightSideNameSyntax().IsKind(SyntaxKind.GenericName) : !methodInvocation.Expression.IsKind(SyntaxKind.GenericName))) { return(base.VisitObjectCreationExpression(node)); } } else { return(base.VisitObjectCreationExpression(node)); } } else if (node.Parent.IsKind(SyntaxKind.ArrayInitializerExpression)) { if (node.Parent.Parent.IsKind(SyntaxKind.ImplicitArrayCreationExpression)) { return(base.VisitObjectCreationExpression(node)); } } if (nodeTypeinfo.ConvertedType.ToString() == nodeTypeinfo.Type.ToString()) { if (isReportOnlyMode) { var lineSpan = node.GetFileLinePosSpan(); AddReport(new ChangesReport(node) { LineNumber = lineSpan.StartLinePosition.Line, Column = lineSpan.StartLinePosition.Character, Message = "Object Creation new Syntax in c# v9", Generator = nameof(CSharpSyntaxUpgrade) }); } return(newNode); } return(base.VisitObjectCreationExpression(node)); }
internal static bool TryGetRegisteredName(ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel, CancellationToken cancellationToken, out string registeredName) { registeredName = null; return(TryGetConstructor(objectCreation, semanticModel, cancellationToken, out _) && DependencyProperty.TryGetRegisteredName(objectCreation?.FirstAncestorOrSelf <InvocationExpressionSyntax>(), semanticModel, cancellationToken, out registeredName)); }