Esempio n. 1
0
        private static async Task <Solution> CreateTestSolutionAsync(CSharpCompilationOptions compilationOptions)
        {
            var workspace = GenericAnalyzerTest.CreateWorkspace();
            var projectId = ProjectId.CreateNewId();

            var references = await GenericAnalyzerTest.ReferenceAssemblies
                             .ResolveAsync(LanguageNames.CSharp, CancellationToken.None).ConfigureAwait(false);

            var solution = workspace.CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
                           .WithProjectCompilationOptions(projectId, compilationOptions)
                           .AddMetadataReferences(projectId, references);

            return(solution);
        }
        private static Document CreateTestDocument(string source, int indentationSize = 4, bool useTabs = false, int tabSize = 4)
        {
            var workspace = GenericAnalyzerTest.CreateWorkspace();

            workspace.Options = workspace.Options
                                .WithChangedOption(FormattingOptions.IndentationSize, LanguageNames.CSharp, indentationSize)
                                .WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, useTabs)
                                .WithChangedOption(FormattingOptions.TabSize, LanguageNames.CSharp, tabSize);

            var projectId          = ProjectId.CreateNewId();
            var documentId         = DocumentId.CreateNewId(projectId);
            var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);

            var solution = workspace.CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
                           .WithProjectCompilationOptions(projectId, compilationOptions)
                           .AddMetadataReference(projectId, MetadataReferences.CorlibReference)
                           .AddMetadataReference(projectId, MetadataReferences.SystemReference)
                           .AddMetadataReference(projectId, MetadataReferences.SystemCoreReference)
                           .AddMetadataReference(projectId, GenericAnalyzerTest.CSharpSymbolsReference)
                           .AddMetadataReference(projectId, MetadataReferences.CodeAnalysisReference)
                           .AddDocument(documentId, TestFilename, SourceText.From(source));

            StyleCopSettings defaultSettings = new StyleCopSettings();

            if (indentationSize != defaultSettings.Indentation.IndentationSize ||
                useTabs != defaultSettings.Indentation.UseTabs ||
                tabSize != defaultSettings.Indentation.TabSize)
            {
                string settings           = $@"
{{
  ""settings"": {{
    ""indentation"": {{
      ""indentationSize"": {indentationSize},
      ""useTabs"": {useTabs.ToString().ToLowerInvariant()},
      ""tabSize"": {tabSize}
    }}
  }}
}}
";
                var    settingsDocumentId = DocumentId.CreateNewId(projectId);
                solution = solution.AddAdditionalDocument(documentId, SettingsHelper.SettingsFileName, settings);
            }

            return(solution.GetDocument(documentId));
        }
        private static async Task <SyntaxTreeAnalysisContext> CreateAnalysisContextAsync(string stylecopJSON, string settingsFileName = SettingsHelper.SettingsFileName)
        {
            var projectId  = ProjectId.CreateNewId();
            var documentId = DocumentId.CreateNewId(projectId);

            var solution = GenericAnalyzerTest.CreateWorkspace()
                           .CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
                           .AddDocument(documentId, "Test0.cs", SourceText.From(string.Empty));

            var document   = solution.GetDocument(documentId);
            var syntaxTree = await document.GetSyntaxTreeAsync().ConfigureAwait(false);

            var stylecopJSONFile = new AdditionalTextHelper(settingsFileName, stylecopJSON);
            var additionalFiles  = ImmutableArray.Create <AdditionalText>(stylecopJSONFile);
            var analyzerOptions  = new AnalyzerOptions(additionalFiles);

            return(new SyntaxTreeAnalysisContext(syntaxTree, analyzerOptions, rd => { }, isd => { return true; }, CancellationToken.None));
        }
        private static async Task <Document> CreateTestDocumentAsync(string source, int indentationSize = 4, bool useTabs = false, int tabSize = 4, CancellationToken cancellationToken = default)
        {
            var workspace = GenericAnalyzerTest.CreateWorkspace();

            workspace.Options = workspace.Options
                                .WithChangedOption(FormattingOptions.IndentationSize, LanguageNames.CSharp, indentationSize)
                                .WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, useTabs)
                                .WithChangedOption(FormattingOptions.TabSize, LanguageNames.CSharp, tabSize);

            var projectId          = ProjectId.CreateNewId();
            var documentId         = DocumentId.CreateNewId(projectId);
            var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, allowUnsafe: true);
            var references         = await GenericAnalyzerTest.ReferenceAssemblies.ResolveAsync(LanguageNames.CSharp, cancellationToken).ConfigureAwait(false);

            var solution = workspace.CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
                           .WithProjectCompilationOptions(projectId, compilationOptions)
                           .AddMetadataReferences(projectId, references)
                           .AddDocument(documentId, TestFilename, SourceText.From(source));

            StyleCopSettings defaultSettings = new StyleCopSettings();

            if (indentationSize != defaultSettings.Indentation.IndentationSize ||
                useTabs != defaultSettings.Indentation.UseTabs ||
                tabSize != defaultSettings.Indentation.TabSize)
            {
                string settings = $@"
{{
  ""settings"": {{
    ""indentation"": {{
      ""indentationSize"": {indentationSize},
      ""useTabs"": {useTabs.ToString().ToLowerInvariant()},
      ""tabSize"": {tabSize}
    }}
  }}
}}
";

                solution = solution.AddAdditionalDocument(documentId, SettingsHelper.SettingsFileName, settings);
            }

            return(solution.GetDocument(documentId));
        }
        private async Task <SyntaxTreeAnalysisContext> CreateAnalysisContextFromEditorConfigAsync(string editorConfig)
        {
            var projectId  = ProjectId.CreateNewId();
            var documentId = DocumentId.CreateNewId(projectId);
            var analyzerConfigDocumentId = DocumentId.CreateNewId(projectId);

            var solution = GenericAnalyzerTest.CreateWorkspace()
                           .CurrentSolution
                           .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp)
                           .AddDocument(documentId, "/0/Test0.cs", SourceText.From(string.Empty))
                           .AddAnalyzerConfigDocument(analyzerConfigDocumentId, "/.editorconfig", SourceText.From(editorConfig), filePath: "/.editorconfig");

            var document   = solution.GetDocument(documentId);
            var syntaxTree = await document.GetSyntaxTreeAsync(CancellationToken.None).ConfigureAwait(false);

            var analyzerConfigSet = AnalyzerConfigSet.Create(new[] { AnalyzerConfig.Parse(SourceText.From(editorConfig), "/.editorconfig") });
            var additionalFiles   = ImmutableArray <AdditionalText> .Empty;
            var optionsProvider   = this.CreateAnalyzerConfigOptionsProvider(analyzerConfigSet);
            var analyzerOptions   = new AnalyzerOptions(additionalFiles, optionsProvider);

            return(new SyntaxTreeAnalysisContext(syntaxTree, analyzerOptions, reportDiagnostic: _ => { }, isSupportedDiagnostic: _ => true, CancellationToken.None));
        }