Skip to content

Commit

Permalink
Merge pull request #361 from RalfKoban/CodeFixProviders
Browse files Browse the repository at this point in the history
Added Code fix providers and fixed some false positives
  • Loading branch information
RalfKoban authored Nov 21, 2020
2 parents 70e6689 + 9888157 commit 29832bf
Show file tree
Hide file tree
Showing 148 changed files with 1,751 additions and 389 deletions.
4 changes: 2 additions & 2 deletions MiKo.Analyzer.Tests/Helpers/CodeFixVerifier.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ private static Document ApplyFix(Document document, CodeAction codeAction)
/// <returns>A list of Diagnostics that only surfaced in the code after the CodeFix was applied.</returns>
private static IEnumerable<Diagnostic> GetNewDiagnostics(IEnumerable<Diagnostic> diagnostics, IEnumerable<Diagnostic> newDiagnostics)
{
var oldArray = diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
var newArray = newDiagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
var oldArray = diagnostics.OrderBy(_ => _.Location.SourceSpan.Start).ToArray();
var newArray = newDiagnostics.OrderBy(_ => _.Location.SourceSpan.Start).ToArray();

int oldIndex = 0;
int newIndex = 0;
Expand Down
2 changes: 1 addition & 1 deletion MiKo.Analyzer.Tests/Helpers/DiagnosticVerifier.Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private static Diagnostic[] GetSortedDiagnostics(string[] sources, string langua
/// <returns>An IEnumerable containing the Diagnostics in order of Location.</returns>
private static Diagnostic[] SortDiagnostics(IEnumerable<Diagnostic> diagnostics)
{
return diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
return diagnostics.OrderBy(_ => _.Location.SourceSpan.Start).ToArray();
}

/// <summary>
Expand Down
13 changes: 8 additions & 5 deletions MiKo.Analyzer.Tests/MiKo.Analyzer.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>MiKoSolutions.Analyzers.Tests</AssemblyName>
<RootNamespace>MiKoSolutions.Analyzers</RootNamespace>

<!-- needed for ReSharper tests and others, see https://github.com/dotnet/sdk/issues/10506 -->
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand All @@ -24,10 +27,10 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.0" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
</ItemGroup>

<!-- Required by NCrunch on remote machines as those machines do not have the package installed -->
Expand All @@ -40,7 +43,7 @@

<ItemGroup Condition="'$(NCrunch)' != '1'">
<PackageReference Include="Codecov" Version="1.10.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.205">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.261">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
8 changes: 4 additions & 4 deletions MiKo.Analyzer.Tests/Rules/AnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void Resources_contains_texts_([ValueSource(nameof(AllAnalyzers))]
analyzer.DiagnosticId + "_MessageFormat",
analyzer.DiagnosticId + "_Title",
}
.Where(name => string.IsNullOrWhiteSpace(ResourceManager.GetString(name)))
.Where(_ => string.IsNullOrWhiteSpace(ResourceManager.GetString(_)))
.ToList();

findings.Sort();
Expand Down Expand Up @@ -180,7 +180,7 @@ public static void Gaps_in_Analyzer_numbers_([Range(1, 5, 1)] int i)
var number = thousand + j;
var prefix = $"MiKo_{number:####}";

if (diagnosticIds.All(_ => !_.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)))
if (diagnosticIds.All(_ => _.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) is false))
{
gaps.Add(prefix);
}
Expand Down Expand Up @@ -258,7 +258,7 @@ private static Analyzer[] CreateAllAnalyzers()
var baseType = typeof(Analyzer);

var allAnalyzers = baseType.Assembly.GetExportedTypes()
.Where(_ => !_.IsAbstract && baseType.IsAssignableFrom(_))
.Where(_ => _.IsAbstract is false && baseType.IsAssignableFrom(_))
.Select(_ => (Analyzer)_.GetConstructor(Type.EmptyTypes).Invoke(null))
.OrderBy(_ => _.DiagnosticId)
.ToArray();
Expand All @@ -270,7 +270,7 @@ private static CodeFixProvider[] CreateAllCodeFixProviders()
var baseType = typeof(CodeFixProvider);

var allAnalyzers = typeof(Analyzer).Assembly.GetExportedTypes()
.Where(_ => !_.IsAbstract && baseType.IsAssignableFrom(_))
.Where(_ => _.IsAbstract is false && baseType.IsAssignableFrom(_))
.Select(_ => (CodeFixProvider)_.GetConstructor(Type.EmptyTypes).Invoke(null))
.OrderBy(_ => _.GetType().Name)
.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,7 @@ private static string[] CreateMeaninglessPhrases()
"Constructor",
"Ctor",
"Delegate",
"Factory",
"Creator",
"Builder",
"Entity",
"Model",
"View",
Expand All @@ -366,6 +364,11 @@ private static string[] CreateMeaninglessPhrases()
"Action",
"Func",
"Converter",
"Adapter ",
"Builder",
"Factory",
"Proxy ",
"Wrapper ",
};

var phrases = MeaninglessTextPhrases;
Expand All @@ -376,6 +379,7 @@ private static string[] CreateMeaninglessPhrases()
"ITestMe",
"A ",
"An ",
"Builder ",
"Called ",
"Does implement ",
"Extension class of ",
Expand Down Expand Up @@ -409,6 +413,9 @@ private static string[] CreateMeaninglessPhrases()
"Internal ",
"Private ",
"Testclass ",
"Mock ",
"Fake ",
"Stub ",
};

results.AddRange(types);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public sealed class TestMe
");

[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1116:SplitParametersMustStartOnLineAfterDeclaration", Justification = "Would look strange otherwise.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1117:ParametersMustBeOnSameLineOrSeparateLines", Justification = "Would look strange otherwise.")]
[Test]
public void Wrong_combined_example_for_documentation_is_reported_on_class() => An_issue_is_reported_for(@"
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void DoSomething(int i) { }
");

[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1116:SplitParametersMustStartOnLineAfterDeclaration", Justification = "Would look strange otherwise.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1117:ParametersMustBeOnSameLineOrSeparateLines", Justification = "Would look strange otherwise.")]
[Test]
public void A_single_issue_is_reported_for_incorrectly_documented_method() => An_issue_is_reported_for(@"
using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.Diagnostics;

using NUnit.Framework;

Expand Down Expand Up @@ -147,8 +148,80 @@ public void DoSomething()
}
");

[Test]
public void Code_gets_fixed_for_qualified_exception_only()
{
const string OriginalCode = @"
using System;
public class TestMe
{
/// <summary>
/// Some documentation.
/// </summary>
/// <exception cref=""System.ObjectDisposedException"">The object.</exception>
public void DoSomething()
{
}
}
";

const string FixedCode = @"
using System;
public class TestMe
{
/// <summary>
/// Some documentation.
/// </summary>
/// <exception cref=""System.ObjectDisposedException"">The object has been disposed.</exception>
public void DoSomething()
{
}
}
";
VerifyCSharpFix(OriginalCode, FixedCode);
}

[Test]
public void Code_gets_fixed_for_exception_only()
{
const string OriginalCode = @"
using System;
public class TestMe
{
/// <summary>
/// Some documentation.
/// </summary>
/// <exception cref=""ObjectDisposedException"">The object.</exception>
public void DoSomething()
{
}
}
";

const string FixedCode = @"
using System;
public class TestMe
{
/// <summary>
/// Some documentation.
/// </summary>
/// <exception cref=""ObjectDisposedException"">The object has been disposed.</exception>
public void DoSomething()
{
}
}
";
VerifyCSharpFix(OriginalCode, FixedCode);
}

protected override string GetDiagnosticId() => MiKo_2056_ObjectDisposedExceptionPhraseAnalyzer.Id;

protected override DiagnosticAnalyzer GetObjectUnderTest() => new MiKo_2056_ObjectDisposedExceptionPhraseAnalyzer();

protected override CodeFixProvider GetCSharpCodeFixProvider() => new MiKo_2056_CodeFixProvider();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public class TestMe
}");

[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1116:SplitParametersMustStartOnLineAfterDeclaration", Justification = "Would look strange otherwise.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1117:ParametersMustBeOnSameLineOrSeparateLines", Justification = "Would look strange otherwise.")]
[Test]
public void An_issue_is_reported_for_incorrectly_documented_equality_operator_summary() => An_issue_is_reported_for(@"
using System;
Expand All @@ -102,6 +103,7 @@ public class TestMe
", 1);

[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1116:SplitParametersMustStartOnLineAfterDeclaration", Justification = "Would look strange otherwise.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1117:ParametersMustBeOnSameLineOrSeparateLines", Justification = "Would look strange otherwise.")]
[Test]
public void An_issue_is_reported_for_incorrectly_documented_equality_operator_returnValue() => An_issue_is_reported_for(@"
using System;
Expand All @@ -117,6 +119,7 @@ public class TestMe
", 1);

[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1116:SplitParametersMustStartOnLineAfterDeclaration", Justification = "Would look strange otherwise.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1117:ParametersMustBeOnSameLineOrSeparateLines", Justification = "Would look strange otherwise.")]
[Test]
public void An_issue_is_reported_for_incorrectly_documented_equality_operator_parameter() => An_issue_is_reported_for(@"
using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class TestMe
");

[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1116:SplitParametersMustStartOnLineAfterDeclaration", Justification = "Would look strange otherwise.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1117:ParametersMustBeOnSameLineOrSeparateLines", Justification = "Would look strange otherwise.")]
[Test]
public void An_issue_is_reported_for_incorrectly_documented_inequality_operator_summary() => An_issue_is_reported_for(@"
using System;
Expand All @@ -103,6 +104,7 @@ public class TestMe
", 1);

[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1116:SplitParametersMustStartOnLineAfterDeclaration", Justification = "Would look strange otherwise.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1117:ParametersMustBeOnSameLineOrSeparateLines", Justification = "Would look strange otherwise.")]
[Test]
public void An_issue_is_reported_for_incorrectly_documented_inequality_operator_returnValue() => An_issue_is_reported_for(@"
using System;
Expand All @@ -118,6 +120,7 @@ public class TestMe
", 1);

[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1116:SplitParametersMustStartOnLineAfterDeclaration", Justification = "Would look strange otherwise.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1117:ParametersMustBeOnSameLineOrSeparateLines", Justification = "Would look strange otherwise.")]
[Test]
public void An_issue_is_reported_for_incorrectly_documented_inequality_operator_parameter() => An_issue_is_reported_for(@"
using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public void DoSomething()
");

[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1116:SplitParametersMustStartOnLineAfterDeclaration", Justification = "Would look strange otherwise.")]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1117:ParametersMustBeOnSameLineOrSeparateLines", Justification = "Would look strange otherwise.")]
[Test]
public void An_issue_is_reported_for_string_interpolation_comment() => An_issue_is_reported_for(@"
using System;
Expand Down
Loading

0 comments on commit 29832bf

Please sign in to comment.