当前位置: 首页>>代码示例>>C#>>正文


C# SyntaxNodeAnalysisContext.IsFromGeneratedCode方法代码示例

本文整理汇总了C#中SyntaxNodeAnalysisContext.IsFromGeneratedCode方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNodeAnalysisContext.IsFromGeneratedCode方法的具体用法?C# SyntaxNodeAnalysisContext.IsFromGeneratedCode怎么用?C# SyntaxNodeAnalysisContext.IsFromGeneratedCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SyntaxNodeAnalysisContext的用法示例。


在下文中一共展示了SyntaxNodeAnalysisContext.IsFromGeneratedCode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TryGetDiagnostic

        static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            var node = nodeContext.Node as InvocationExpressionSyntax;
            var semanticModel = nodeContext.SemanticModel;
            var cancellationToken = nodeContext.CancellationToken;

            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;
            var memberReference = node.Expression as MemberAccessExpressionSyntax;
            if (memberReference == null)
                return false;
            var firstArgument = node.ArgumentList.Arguments.FirstOrDefault();
            if (firstArgument == null || firstArgument.Expression.IsKind(SyntaxKind.NullLiteralExpression))
                return false;
            var expressionSymbol = semanticModel.GetSymbolInfo(node.Expression).Symbol as IMethodSymbol;
            //ignore non-extensions and reduced extensions (so a.Ext, as opposed to B.Ext(a))
            if (expressionSymbol == null || !expressionSymbol.IsExtensionMethod || expressionSymbol.MethodKind == MethodKind.ReducedExtension)
                return false;

            diagnostic = Diagnostic.Create(
                descriptor,
                memberReference.Name.GetLocation()
            );
            return true;
        }
开发者ID:Kavignon,项目名称:RefactoringEssentials,代码行数:26,代码来源:InvokeAsExtensionMethodAnalyzer.cs

示例2: TryGetDiagnostic

 static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
 {
     diagnostic = default(Diagnostic);
     if (nodeContext.IsFromGeneratedCode())
         return false;
     var node = nodeContext.Node as IfStatementSyntax;
     ExpressionSyntax target;
     SyntaxTriviaList assignmentTrailingTriviaList;
     if (ConvertIfToOrExpressionAnalyzer.MatchIfElseStatement(node, SyntaxKind.FalseLiteralExpression, out target, out assignmentTrailingTriviaList))
     {
         var varDeclaration = ConvertIfToOrExpressionAnalyzer.FindPreviousVarDeclaration(node);
         if (varDeclaration != null)
         {
             var targetIdentifier = target as IdentifierNameSyntax;
             if (targetIdentifier == null)
                 return false;
             var declaredVarName = varDeclaration.Declaration.Variables.First().Identifier.Value;
             var assignedVarName = targetIdentifier.Identifier.Value;
             if (declaredVarName != assignedVarName)
                 return false;
             if (!ConvertIfToOrExpressionAnalyzer.CheckTarget(targetIdentifier, node.Condition))
                 return false;
             diagnostic = Diagnostic.Create(descriptor, node.IfKeyword.GetLocation(), GettextCatalog.GetString("Convert to '&&' expression"));
             return true;
         }
         else
         {
             if (!ConvertIfToOrExpressionAnalyzer.CheckTarget(target, node.Condition))
                 return false;
             diagnostic = Diagnostic.Create(descriptor, node.IfKeyword.GetLocation(), GettextCatalog.GetString("Replace with '&='"));
             return true;
         }
     }
     return false;
 }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:35,代码来源:ConvertIfToAndExpressionAnalyzer.cs

示例3: TryGetDiagnostic

        static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;
            var node = nodeContext.Node as CatchClauseSyntax;
            if (node.Declaration != null)
            {
                var type = node.Declaration.Type;
                if (type != null)
                {
                    var typeSymbol = nodeContext.SemanticModel.GetTypeInfo(type).Type;
                    if (typeSymbol == null || typeSymbol.TypeKind == TypeKind.Error || !typeSymbol.GetFullName().Equals("System.Exception"))
                        return false;
                }
            }

            // Don't consider a catch clause with "when (...)" as general
            if (node.Filter != null)
                return false;

            BlockSyntax body = node.Block;
            if (body.Statements.Any())
                return false;

            diagnostic = Diagnostic.Create(
                descriptor,
                node.CatchKeyword.GetLocation()
            );

            return true;
        }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:32,代码来源:EmptyGeneralCatchClauseAnalyzer.cs

示例4: TryGetDiagnostic

        static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;
            var node = nodeContext.Node as ConditionalExpressionSyntax;

            //pattern is Any(param) ? Last(param) : null/default
            var anyInvocation = node.Condition as InvocationExpressionSyntax;
            var lastInvocation = node.WhenTrue as InvocationExpressionSyntax;
            var nullDefaultWhenFalse = node.WhenFalse;

            if (anyInvocation == null || lastInvocation == null || nullDefaultWhenFalse == null)
                return false;
            var anyExpression = anyInvocation.Expression as MemberAccessExpressionSyntax;
            if (anyExpression == null || anyExpression.Name.Identifier.ValueText != "Any")
                return false;
            var anyParam = anyInvocation.ArgumentList;

            var lastExpression = lastInvocation.Expression as MemberAccessExpressionSyntax;
            if (lastExpression == null || lastExpression.Name.Identifier.ValueText != "Last" || !lastInvocation.ArgumentList.IsEquivalentTo(anyParam))
                return false;

            if (!nullDefaultWhenFalse.IsKind(SyntaxKind.NullLiteralExpression) && !nullDefaultWhenFalse.IsKind(SyntaxKind.DefaultExpression))
                return false;

            diagnostic = Diagnostic.Create(
                descriptor,
                node.GetLocation()
            );
            return true;
        }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:32,代码来源:ReplaceWithLastOrDefaultAnalyzer.cs

示例5: TryGetDiagnostic

        private static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;

            var node = nodeContext.Node as IfStatementSyntax;
            if (node == null)
                return false;

            var methodBody = node.Parent as BlockSyntax;
            if (methodBody == null)
                return false;

            var ifStatementIndex = methodBody.Statements.IndexOf(node);
            if (ifStatementIndex == methodBody.Statements.Count - 1)
                return false;

            if (node.Statement is ReturnStatementSyntax &&
                methodBody.Statements[ifStatementIndex + 1] is ReturnStatementSyntax)
            {
                diagnostic = Diagnostic.Create(descriptor, node.GetLocation());
                return true;
            }
            return false;
        }
开发者ID:RemcovandenBerg,项目名称:RefactoringEssentials,代码行数:26,代码来源:RewriteIfReturnToReturnAnalyzer.cs

示例6: TryGetParamsDiagnostic

        //I think it's a better decision to head in this direction instead of MethodDeclaration.
        private static bool TryGetParamsDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;

            var paramList = nodeContext.Node as ParameterListSyntax;
            var declaration = paramList?.Parent as MethodDeclarationSyntax;

            if (declaration == null)
                return false;

            if (declaration.Modifiers.Count == 0 || !declaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
                return false;

            var lastParam = declaration.ParameterList.Parameters.LastOrDefault();
            SyntaxToken? paramsModifierToken = null;
            if (lastParam == null)
                return false;

            foreach (var x in lastParam.Modifiers)
            {
                if (x.IsKind(SyntaxKind.ParamsKeyword))
                {
                    paramsModifierToken = x;
                    break;
                }
            }

            if (!paramsModifierToken.HasValue)
                return false;

            diagnostic = Diagnostic.Create(descriptor, paramsModifierToken.Value.GetLocation());
            return true;
        }
开发者ID:RemcovandenBerg,项目名称:RefactoringEssentials,代码行数:36,代码来源:RedundantParamsAnalyzer.cs

示例7: TryGetDiagnostic

        static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;
            var node = nodeContext.Node as LiteralExpressionSyntax;
            if (!(node.Token.Value is long || node.Token.Value is ulong))
                return false;

            var literal = node.Token.Text;
            if (literal.Length < 2)
                return false;

            char prevChar = literal[literal.Length - 2];
            char lastChar = literal[literal.Length - 1];

            if (prevChar == 'u' || prevChar == 'U') //ul/Ul is not confusing
                return false;

            if (lastChar == 'l' || prevChar == 'l')
            {
                diagnostic = Diagnostic.Create(
                    descriptor,
                    node.GetLocation()
                );
                return true;
            }
            return false;
        }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:29,代码来源:LongLiteralEndingLowerLAnalyzer.cs

示例8: TryGetDiagnostic

        static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            var node = nodeContext.Node as ClassDeclarationSyntax;
            var semanticModel = nodeContext.SemanticModel;
            var cancellationToken = nodeContext.CancellationToken;

            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;
            ITypeSymbol classType = semanticModel.GetDeclaredSymbol(node);
            if (!node.Modifiers.Any() || node.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword)) || classType.IsAbstract || classType.IsStatic)
                return false;
            if ((node.BaseList != null) && node.BaseList.Types.Any())
                return false;
            IEnumerable<ISymbol> members = classType.GetMembers().Where(m => !(m is ITypeSymbol));
            if (!members.Any(m => !m.IsImplicitlyDeclared)) // Ignore implicitly declared (e.g. default ctor)
                return false;
            if (Enumerable.Any(members, f => (!f.IsStatic && !f.IsImplicitlyDeclared) || (f is IMethodSymbol && IsMainMethod((IMethodSymbol)f))))
                return false;

            diagnostic = Diagnostic.Create(
                descriptor,
                node.Identifier.GetLocation()
            );
            return true;
        }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:26,代码来源:ConvertToStaticTypeAnalyzer.cs

示例9: TryGetDiagnostic

        static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;

            var options = nodeContext.SemanticModel.SyntaxTree.Options as VisualBasicParseOptions;
            if (options != null && options.LanguageVersion < LanguageVersion.VisualBasic14)
                return false;

            var objectCreateExpression = nodeContext.Node as ObjectCreationExpressionSyntax;

            ExpressionSyntax paramNode;
            if (!CheckExceptionType(nodeContext.SemanticModel, objectCreateExpression, out paramNode))
                return false;
            var paramName = GetArgumentParameterName(paramNode);
            if (paramName == null)
                return false;

            var validNames = GetValidParameterNames(objectCreateExpression);

            if (!validNames.Contains(paramName))
                return false;

            diagnostic = Diagnostic.Create(descriptor, paramNode.GetLocation(), paramName);
            return true;
        }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:27,代码来源:NameOfSuggestionAnalyzer.cs

示例10: TryGetDiagnostic

 static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
 {
     diagnostic = default(Diagnostic);
     if (nodeContext.IsFromGeneratedCode())
         return false;
     var node = nodeContext.Node as ParameterSyntax;
     if (!node.Modifiers.Any(m => m.IsKind(SyntaxKind.RefKeyword) || m.IsKind(SyntaxKind.OutKeyword)))
         return false;
     foreach (var attributeLists in node.AttributeLists)
     {
         foreach (var attribute in attributeLists.Attributes)
         {
             var attrSymbol = nodeContext.SemanticModel.GetTypeInfo(attribute).Type;
             if (attrSymbol == null)
                 continue;
             if (attrSymbol.Name == "OptionalAttribute" && attrSymbol.ContainingNamespace.Name == "InteropServices")
             {
                 diagnostic = Diagnostic.Create(
                     descriptor,
                     node.GetLocation()
                 );
                 return true;
             }
         }
     }
     return false;
 }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:27,代码来源:OptionalParameterRefOutAnalyzer.cs

示例11: TryGetDiagnostic

        static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;

            var node = nodeContext.Node as BinaryExpressionSyntax;
            var left = node.Left.SkipParens();
            var right = node.Right.SkipParens();
            ExpressionSyntax expr = null, highlightExpr = null;
            if (left.IsKind(SyntaxKind.NullLiteralExpression))
            {
                expr = right;
                highlightExpr = node.Left;
            }
            if (right.IsKind(SyntaxKind.NullLiteralExpression))
            {
                expr = left;
                highlightExpr = node.Right;
            }
            if (expr == null)
                return false;
            var type = nodeContext.SemanticModel.GetTypeInfo(expr).Type;
            if ((type == null) || (type.TypeKind != TypeKind.TypeParameter) || type.IsReferenceType)
                return false;
            diagnostic = Diagnostic.Create(
                descriptor,
                highlightExpr.GetLocation()
            );
            return true;
        }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:31,代码来源:CompareNonConstrainedGenericWithNullAnalyzer.cs

示例12: TryGetDiagnostic

        static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;

            var node = nodeContext.Node as PrefixUnaryExpressionSyntax;

            if (node.IsKind(SyntaxKind.LogicalNotExpression))
            {
                var innerUnaryOperatorExpr = node.Operand.SkipParens() as PrefixUnaryExpressionSyntax;
                if (innerUnaryOperatorExpr == null || !innerUnaryOperatorExpr.IsKind(SyntaxKind.LogicalNotExpression))
                    return false;
                diagnostic = Diagnostic.Create(descriptor, node.GetLocation());
                return true;

            }

            if (node.IsKind(SyntaxKind.BitwiseNotExpression))
            {
                var innerUnaryOperatorExpr = node.Operand.SkipParens() as PrefixUnaryExpressionSyntax;
                if (innerUnaryOperatorExpr == null || !innerUnaryOperatorExpr.IsKind(SyntaxKind.BitwiseNotExpression))
                    return false;
                diagnostic = Diagnostic.Create(descriptor, node.GetLocation());
                return true;
            }
            return false;
        }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:28,代码来源:DoubleNegationOperatorAnalyzer.cs

示例13: TryGetDiagnostic

        static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;

            InitializerExpressionSyntax initializer = null;
            var node = nodeContext.Node as ArrayCreationExpressionSyntax;
            if (node != null) initializer = node.Initializer;
            var inode = nodeContext.Node as ImplicitArrayCreationExpressionSyntax;
            if (inode != null) initializer = inode.Initializer;

            if (initializer == null)
                return false;
            var varInitializer = nodeContext.Node.Parent.Parent;
            if (varInitializer == null)
                return false;
            var variableDeclaration = varInitializer.Parent as VariableDeclarationSyntax;
            if (variableDeclaration != null)
            {
                if (!variableDeclaration.Type.IsKind(SyntaxKind.ArrayType))
                    return false;
                diagnostic = Diagnostic.Create(
                    descriptor,
                    Location.Create(nodeContext.SemanticModel.SyntaxTree, TextSpan.FromBounds((node != null ? node.NewKeyword : inode.NewKeyword).Span.Start, initializer.Span.Start))
                );
                return true;
            }
            return false;
        }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:30,代码来源:ArrayCreationCanBeReplacedWithArrayInitializerAnalyzer.cs

示例14: TryGetDiagnostic

        static bool TryGetDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;
            var node = nodeContext.Node as FieldDeclarationSyntax;
            if (node.Modifiers.Any(m => m.IsKind(SyntaxKind.StaticKeyword)))
                return false;

            foreach (var attributeLists in node.AttributeLists)
            {
                foreach (var attribute in attributeLists.Attributes)
                {
                    var attrSymbol = nodeContext.SemanticModel.GetTypeInfo(attribute).Type;
                    if (attrSymbol == null)
                        continue;
                    if (attrSymbol.Name == "ThreadStaticAttribute" && attrSymbol.ContainingNamespace.Name == "System")
                    {
                        diagnostic = Diagnostic.Create(
                            descriptor,
                            attribute.GetLocation()
                        );
                        return true;
                    }
                }
            }
            return false;
        }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:28,代码来源:ThreadStaticAtInstanceFieldAnalyzer.cs

示例15: TryGetRedundantNullableDiagnostic

        private static bool TryGetRedundantNullableDiagnostic(SyntaxNodeAnalysisContext nodeContext, out Diagnostic diagnostic)
        {
            diagnostic = default(Diagnostic);
            if (nodeContext.IsFromGeneratedCode())
                return false;

            var objectCreation = nodeContext.Node as ObjectCreationExpressionSyntax;
            if (objectCreation == null)
                return false;

            // No Nullable, but "var"
            var parentVarDeclaration = objectCreation?.Parent?.Parent?.Parent as VariableDeclarationSyntax;
            if (parentVarDeclaration != null && parentVarDeclaration.Type.IsVar)
                return false;

            var objectCreationSymbol = nodeContext.SemanticModel.GetTypeInfo(objectCreation);
            if (objectCreationSymbol.Type != null && objectCreationSymbol.Type.IsNullableType())
            {
                var creationTypeLocation = objectCreation.Type.GetLocation();
                var newKeywordLocation = objectCreation.NewKeyword.GetLocation();
                diagnostic = Diagnostic.Create(descriptor, newKeywordLocation, (IEnumerable<Location>) (new[] { creationTypeLocation }));
                return true;
            }
            return false;
        }
开发者ID:pgrefviau,项目名称:RefactoringEssentials,代码行数:25,代码来源:RedundantExplicitNullableCreationAnalyzer.cs


注:本文中的SyntaxNodeAnalysisContext.IsFromGeneratedCode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。