-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ec18fd
commit a3eb863
Showing
7 changed files
with
427 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
StyleCop.Analyzers/StyleCop.Analyzers.Test/Settings/SettingsFileCodeFixProviderUnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
namespace StyleCop.Analyzers.Test.Settings | ||
{ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using StyleCop.Analyzers.DocumentationRules; | ||
using StyleCop.Analyzers.Settings; | ||
using TestHelper; | ||
using Xunit; | ||
|
||
/// <summary> | ||
/// Unit tests for the <see cref="SettingsFileCodeFixProvider"/> | ||
/// </summary> | ||
public class SettingsFileCodeFixProviderUnitTests : CodeFixVerifier | ||
{ | ||
private const string StyleCopSettingsFileName = "stylecop.json"; | ||
private const string TestCode = @" | ||
namespace Foo | ||
{ | ||
} | ||
"; | ||
|
||
private Project originalProject; | ||
private bool createSettingsFile; | ||
|
||
/// <summary> | ||
/// Verifies that a file without a header, but with leading trivia will produce the correct diagnostic message. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestMissingFileHeaderWithLeadingTriviaAsync() | ||
{ | ||
this.createSettingsFile = false; | ||
|
||
var expectedDiagnostic = this.CSharpDiagnostic().WithLocation(1, 1).WithArguments("is missing or not located at the top of the file."); | ||
await this.VerifyCSharpDiagnosticAsync(TestCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false); | ||
|
||
// verify that the codefix does not alter the document | ||
await this.VerifyCSharpFixAsync(TestCode, TestCode).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that a code fix will be offered if the settings file does not exist. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestSettingsFileDoesNotExistAsync() | ||
{ | ||
this.createSettingsFile = false; | ||
|
||
var offeredFixes = await this.GetOfferedCSharpFixesAsync(TestCode).ConfigureAwait(false); | ||
Assert.Equal(1, offeredFixes.Length); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that a code fix will not be offered if the settings file is already present. | ||
/// </summary> | ||
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns> | ||
[Fact] | ||
public async Task TestSettingsFileAlreadyExistsAsync() | ||
{ | ||
this.createSettingsFile = true; | ||
|
||
var offeredFixes = await this.GetOfferedCSharpFixesAsync(TestCode).ConfigureAwait(false); | ||
Assert.Empty(offeredFixes); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers() | ||
{ | ||
yield return new SA1633FileMustHaveHeader(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override CodeFixProvider GetCSharpCodeFixProvider() | ||
{ | ||
return new SettingsFileCodeFixProvider(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override Project CreateProject(string[] sources, string language = "C#", string[] filenames = null) | ||
{ | ||
this.originalProject = base.CreateProject(sources, language, filenames); | ||
if (this.createSettingsFile) | ||
{ | ||
this.originalProject = this.originalProject.AddAdditionalDocument(StyleCopSettingsFileName, string.Empty).Project; | ||
} | ||
|
||
return this.originalProject; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
StyleCop.Analyzers/StyleCop.Analyzers/Settings/SettingsFileCodeFixProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
namespace StyleCop.Analyzers.Settings | ||
{ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using StyleCop.Analyzers.DocumentationRules; | ||
using StyleCop.Analyzers.Helpers; | ||
|
||
/// <summary> | ||
/// Implements a codefix that will generate a StyleCop settings file if it does not exist yet. | ||
/// </summary> | ||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SettingsFileCodeFixProvider))] | ||
[Shared] | ||
public class SettingsFileCodeFixProvider : CodeFixProvider | ||
{ | ||
private const string StyleCopSettingsFileName = "stylecop.json"; | ||
private const string DefaultSettingsFileContent = @"{ | ||
""settings"": { | ||
""documentationRules"": { | ||
""companyName"": ""PlaceholderCompany"" | ||
} | ||
} | ||
} | ||
"; | ||
|
||
/// <inheritdoc/> | ||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = | ||
ImmutableArray.Create( | ||
SA1633FileMustHaveHeader.DiagnosticId, | ||
SA1634FileHeaderMustShowCopyright.DiagnosticId, | ||
SA1635FileHeaderMustHaveCopyrightText.DiagnosticId, | ||
SA1636FileHeaderCopyrightTextMustMatch.DiagnosticId, | ||
SA1637FileHeaderMustContainFileName.DiagnosticId, | ||
SA1638FileHeaderFileNameDocumentationMustMatchFileName.DiagnosticId, | ||
SA1639FileHeaderMustHaveSummary.DiagnosticId, | ||
SA1640FileHeaderMustHaveValidCompanyText.DiagnosticId, | ||
SA1641FileHeaderCompanyNameTextMustMatch.DiagnosticId, | ||
SA1649FileHeaderFileNameDocumentationMustMatchTypeName.DiagnosticId); | ||
|
||
/// <inheritdoc/> | ||
public override Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
var project = context.Document.Project; | ||
var workspace = project.Solution.Workspace; | ||
|
||
// check if the settings file already exists | ||
if (project.AdditionalDocuments.Any(IsStyleCopSettingsDocument)) | ||
{ | ||
return SpecializedTasks.CompletedTask; | ||
} | ||
|
||
// check if we are allowed to add it | ||
if (!workspace.CanApplyChange(ApplyChangesKind.AddAdditionalDocument)) | ||
{ | ||
return SpecializedTasks.CompletedTask; | ||
} | ||
|
||
foreach (var diagnostic in context.Diagnostics.Where(d => this.FixableDiagnosticIds.Contains(d.Id))) | ||
{ | ||
context.RegisterCodeFix(CodeAction.Create(SettingsResources.SettingsFileCodeFix, token => GetTransformedSolutionAsync(context.Document, diagnostic, token), equivalenceKey: nameof(SettingsFileCodeFixProvider)), diagnostic); | ||
} | ||
|
||
return SpecializedTasks.CompletedTask; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override FixAllProvider GetFixAllProvider() | ||
{ | ||
// Added this to make it explicitly clear that this codefix does not support fix all actions. | ||
return null; | ||
} | ||
|
||
private static bool IsStyleCopSettingsDocument(TextDocument document) | ||
{ | ||
return string.Equals(document.Name, StyleCopSettingsFileName, StringComparison.OrdinalIgnoreCase); | ||
} | ||
|
||
private static Task<Solution> GetTransformedSolutionAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken) | ||
{ | ||
var project = document.Project; | ||
var solution = project.Solution; | ||
|
||
var newDocumentId = DocumentId.CreateNewId(project.Id); | ||
|
||
var newSolution = solution.AddAdditionalDocument(newDocumentId, StyleCopSettingsFileName, DefaultSettingsFileContent); | ||
|
||
return Task.FromResult(newSolution); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
StyleCop.Analyzers/StyleCop.Analyzers/Settings/SettingsResources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.