Exemple #1
0
        public virtual EditorConfigOptions InitializeEditorConfigOptions(ImmutableArray <AdditionalText> additionalFiles)
        {
            EditorConfigOptions options = new EditorConfigOptions(DefaultDuplicateTokenThreshold);

            foreach (AdditionalText additionalFile in additionalFiles)
            {
                string         fileName = Path.GetFileName(additionalFile.Path);
                StringComparer comparer = StringComparer.OrdinalIgnoreCase;
                if (comparer.Equals(fileName, @".editorconfig"))
                {
                    LoadEditorConfigOptions(options, additionalFile.GetText());
                }
            }

            return(options);
        }
Exemple #2
0
        public override void Initialize(AnalysisContext context)
        {
            context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
            context.EnableConcurrentExecution();

            context.RegisterCompilationStartAction(compilationContext =>
            {
                EditorConfigOptions options = InitializeEditorConfigOptions(compilationContext.Options.AdditionalFiles);
                HashSet <string> exceptions = new HashSet <string>();
                if (!options.IgnoreExceptionsFile)
                {
                    exceptions = InitializeExceptions(compilationContext.Options.AdditionalFiles);
                }
                var compilationAnalyzer = new CompilationAnalyzer(options.TokenCount, exceptions, options.GenerateExceptionsFile);
                compilationContext.RegisterSyntaxNodeAction(compilationAnalyzer.AnalyzeMethod, SyntaxKind.MethodDeclaration);
                compilationContext.RegisterCompilationEndAction(compilationAnalyzer.EndCompilationAction);
            });
        }
Exemple #3
0
        public virtual void LoadEditorConfigOptions(EditorConfigOptions options, SourceText text)
        {
            if (text == null)
            {
                return;
            }
            string diagnosticId = Helper.ToDiagnosticId(DiagnosticIds.AvoidDuplicateCode);

            foreach (TextLine textLine in text.Lines)
            {
                string line = textLine.ToString();
                if (line.Contains($@"dotnet_code_quality.{diagnosticId}.ignore_exceptions_file"))
                {
                    options.IgnoreExceptionsFile = true;
                }
                if (line.Contains($@"dotnet_code_quality.{diagnosticId}.generate_exceptions_file"))
                {
                    options.GenerateExceptionsFile = true;
                }
                else if (line.Contains($@"dotnet_code_quality.{diagnosticId}.token_count"))
                {
                    if (line.Contains('='))
                    {
                        string tc = line.Substring(line.IndexOf('=') + 1);
                        tc = tc.Trim();
                        try
                        {
                            int duplicateTokenThreshold = int.Parse(tc);
                            if (duplicateTokenThreshold > 100)
                            {
                                duplicateTokenThreshold = 100;
                            }
                            if (duplicateTokenThreshold < 20)
                            {
                                duplicateTokenThreshold = 20;
                            }
                            options.TokenCount = duplicateTokenThreshold;
                        }
                        catch (Exception) { }
                    }
                }
            }
        }
Exemple #4
0
        public virtual EditorConfigOptions InitializeEditorConfigOptions(AnalyzerOptions analyzerOptions, Compilation compilation, out Diagnostic error)
        {
            error = null;
            EditorConfigOptions options = new EditorConfigOptions(DefaultDuplicateTokenThreshold);
            var editorConfigHelper      = new AdditionalFilesHelper(analyzerOptions, compilation);

            ExceptionsOptions exceptionsOptions = editorConfigHelper.LoadExceptionsOptions(Rule.Id);

            options.IgnoreExceptionsFile   = exceptionsOptions.IgnoreExceptionsFile;
            options.GenerateExceptionsFile = exceptionsOptions.GenerateExceptionsFile;

            string strTokenCount = editorConfigHelper.GetValueFromEditorConfig(Rule.Id, @"token_count");

            if (!string.IsNullOrWhiteSpace(strTokenCount))
            {
                strTokenCount = strTokenCount.Trim();
                bool isParseSuccessful = int.TryParse(strTokenCount, out int duplicateTokenThreshold);

                if (!isParseSuccessful)
                {
                    duplicateTokenThreshold = DefaultDuplicateTokenThreshold;
                    error = Diagnostic.Create(InvalidTokenCountRule, null, strTokenCount);
                }

                const int MaxTokenCount = 200;
                if (duplicateTokenThreshold > MaxTokenCount)
                {
                    error = Diagnostic.Create(TokenCountTooBigRule, null, duplicateTokenThreshold, MaxTokenCount);
                    duplicateTokenThreshold = MaxTokenCount;
                }
                const int MinTokenCount = 20;
                if (duplicateTokenThreshold < MinTokenCount)
                {
                    error = Diagnostic.Create(TokenCountTooSmallRule, null, duplicateTokenThreshold, MinTokenCount);
                    duplicateTokenThreshold = MinTokenCount;
                }
                options.TokenCount = duplicateTokenThreshold;
            }
            return(options);
        }