/// Action to be executed at completion of semantic analysis of an invocation expression in the target project. /// <param name="context">context for a symbol action.</param> private static void AnalyzeNode(SyntaxNodeAnalysisContext context) { UEASettingReader uEASettingReader = new UEASettingReader(); UEASettings uEASettings = uEASettingReader.ReadConfigFile(); List <string> noConstansMethods = uEASettings.noConstansFunctionRegexStrings; // check for exclusion if (context.IsExcluded(DiagnosticIDs.DoNotUseConstantStringMethodsFromConfig)) { return; } // early out var invocation = context.Node as InvocationExpressionSyntax; if (invocation == null) { return; } var methodSymbol = context.SemanticModel.GetSymbolInfo(invocation).Symbol; var symbolInfo = methodSymbol as IMethodSymbol; var methodFullDefinition = symbolInfo.OriginalDefinition.ToDisplayString().Split('(')[0]; bool notMatchMethod = true; foreach (var noConstansMethod in noConstansMethods) { if (noConstansMethod.Equals(methodFullDefinition)) { notMatchMethod = false; break; } } if (notMatchMethod) { return; } bool hasContent = false; var argumentList = invocation.ArgumentList.ToFullString().Replace("(", "").Replace(")", "").Split(','); foreach (var argument in argumentList) { if (System.Text.RegularExpressions.Regex.IsMatch(argument, "\"")) { hasContent = true; } } if (hasContent) { // represents a diagnostic, such as a compiler error or a warning, along with the location where it occurred var diagnostic = Diagnostic.Create(DiagnosticDescriptors.DoNotUseConstantStringMethodsFromConfig, invocation.GetLocation(), methodFullDefinition); // report a Diagnostic result context.ReportDiagnostic(diagnostic); } }
/// Action to be executed at completion of syntax of method declaration in the target project. /// <param name="context">context for a syntax action.</param> private static void AnalyzeSymbol(SyntaxNodeAnalysisContext context) { UEASettingReader uEASettingReader = new UEASettingReader(); UEASettings uEASettings = uEASettingReader.ReadConfigFile(); // check for exclusion if (context.IsExcluded(DiagnosticIDs.EmptyMonoBehaviourMethod)) { return; } // retrieve method syntax var methodSyntax = context.Node as MethodDeclarationSyntax; // retrieve method symbol var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodSyntax); if (methodSymbol == null) { return; } // check if method name is a MonoBehaviour method name if (!MonoBehaviourMethods.Contains(methodSymbol.Name)) { return; } int rowsCount2ThisMethod = CheckExceedRowMethod(context, methodSymbol, new Dictionary <IMethodSymbol, int>()); // from the method syntax, check if awake or start method is out off rawlimeted if (rowsCount2ThisMethod <= uEASettings.startAndAwakeFunctinMAXRows) { return; } // at this point, we have a method with a MonoBehaviour method name and an empty body // finally, check if this method is contained in a class which extends UnityEngine.MonoBehaviour var containingClass = methodSymbol.ContainingType; var baseClass = containingClass.BaseType; if (baseClass?.ContainingNamespace?.Name.Equals("UnityEngine") == true && baseClass?.Name?.Equals("MonoBehaviour") == true) { // represents a diagnostic, such as a compiler error or a warning, along with the location where it occurred var diagnostic = Diagnostic.Create(DiagnosticDescriptors.BeyondRowLimitMonoBehaviourMethod, methodSyntax.GetLocation(), containingClass.Name, methodSymbol.Name, rowsCount2ThisMethod); // report a Diagnostic result context.ReportDiagnostic(diagnostic); } }
/// Action to be executed at completion of semantic analysis of an invocation expression in the target project. /// <param name="context">context for a symbol action.</param> private static void AnalyzeNode(SyntaxNodeAnalysisContext context) { UEASettingReader uEASettingReader = new UEASettingReader(); UEASettings uEASettings = uEASettingReader.ReadConfigFile(); List <string> forbiddenMethods = uEASettings.prohibitedFunctionRegexStrings; // check for exclusion if (context.IsExcluded(DiagnosticIDs.ForbiddenMethods)) { return; } // early out var invocation = context.Node as InvocationExpressionSyntax; if (invocation == null) { return; } var methodSymbol = context.SemanticModel.GetSymbolInfo(invocation).Symbol; var symbolInfo = methodSymbol as IMethodSymbol; var methodFullDefinition = symbolInfo.OriginalDefinition.ToDisplayString().Split('(')[0]; // check if any forbidden methods are used bool isForbidden = false; foreach (var forbiddenMethod in forbiddenMethods) { if (forbiddenMethod.Equals(methodFullDefinition)) { isForbidden = true; break; } } if (isForbidden) { // represents a diagnostic, such as a compiler error or a warning, along with the location where it occurred var diagnostic = Diagnostic.Create(DiagnosticDescriptors.ForbiddenMethods, invocation.GetLocation(), methodFullDefinition); // report a Diagnostic result context.ReportDiagnostic(diagnostic); } }