Skip to content

Commit d322b2c

Browse files
Merge pull request #3 from bjornhellander/feature/misc-fixes
Misc cherry-picks from StyleCop Analyzers
2 parents 9ce1369 + 8454890 commit d322b2c

File tree

42 files changed

+1311
-79
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1311
-79
lines changed

NuGet.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,7 @@
1111
<!-- Uncomment this to test unpublished builds from the AppVeyor feed. -->
1212
<!--<add key="appveyor-stylecop" value="https://ci.appveyor.com/nuget/stylecopanalyzers" />-->
1313
</packageSources>
14+
<config>
15+
<add key="defaultPushSource" value="https://api.nuget.org/v3/index.json" />
16+
</config>
1417
</configuration>

README.md

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
# StyleCop Analyzers for the .NET Compiler Platform
2-
3-
[![NuGet](https://img.shields.io/nuget/v/StyleCop.Analyzers.svg)](https://www.nuget.org/packages/StyleCop.Analyzers)[![NuGet Beta](https://img.shields.io/nuget/vpre/StyleCop.Analyzers.svg)](https://www.nuget.org/packages/StyleCop.Analyzers)
4-
5-
[![Join the chat at https://gitter.im/DotNetAnalyzers/StyleCopAnalyzers](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/DotNetAnalyzers/StyleCopAnalyzers?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
6-
7-
[![Build status](https://ci.appveyor.com/api/projects/status/8jw2lq431kgg44jl/branch/master?svg=true)](https://ci.appveyor.com/project/sharwell/stylecopanalyzers/branch/master)
8-
9-
[![codecov.io](https://codecov.io/github/DotNetAnalyzers/StyleCopAnalyzers/coverage.svg?branch=master)](https://codecov.io/github/DotNetAnalyzers/StyleCopAnalyzers?branch=master)
1+
# New StyleCop Analyzers for the .NET Compiler Platform
102

113
This repository contains an implementation of the StyleCop rules using the .NET Compiler Platform. Where possible, code fixes are also provided to simplify the process of correcting violations.
124

13-
## Using StyleCop.Analyzers
5+
**This project has been created by cloning the [StyleCopAnalyzers](https://github.com/DotNetAnalyzers/StyleCopAnalyzers) project and merging some of the pull requests that were created. Hopefully as a temporary solution until maintenance of the original project starts working well again. Changes are kept small until the status of the original project is clearer.**
6+
7+
## Using NewStyleCop.Analyzers
148

15-
The preferable way to use the analyzers is to add the nuget package [StyleCop.Analyzers](http://www.nuget.org/packages/StyleCop.Analyzers/)
9+
The preferable way to use the analyzers is to add the nuget package [NewStyleCop.Analyzers](http://www.nuget.org/packages/NewStyleCop.Analyzers/)
1610
to the project where you want to enforce StyleCop rules.
1711

1812
The severity of individual rules may be configured using [rule set files](https://docs.microsoft.com/en-us/visualstudio/code-quality/using-rule-sets-to-group-code-analysis-rules)
@@ -33,18 +27,6 @@ Not all versions of StyleCop.Analyzers support all features of each C# language
3327
| 7.0 - 7.3 | v1.1.0-beta or higher | VS2017+ |
3428
| 8.0 | v1.2.0-beta or higher | VS2019 |
3529

36-
## Installation
37-
38-
StyleCopAnalyzers can be installed using the NuGet command line or the NuGet Package Manager in Visual Studio 2015.
39-
40-
**Install using the command line:**
41-
```bash
42-
Install-Package StyleCop.Analyzers
43-
```
44-
45-
**Install using the package manager:**
46-
![Install via nuget](https://cloud.githubusercontent.com/assets/1408396/8233513/491f301a-159c-11e5-8b7a-1e16a0695da6.png)
47-
4830
## Team Considerations
4931

5032
If you use older versions of Visual Studio in addition to Visual Studio 2015 or Visual Studio 2017, you may still install these analyzers. They will be automatically disabled when you open the project back up in Visual Studio 2013 or earlier.

SetVersion.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
param([String]$version)
2+
3+
function Update-Nuspec-File {
4+
param ($FileName)
5+
6+
$content = [System.IO.File]::ReadAllText($FileName) -replace '(?<=<Version>).*(?=</Version>)', $version
7+
$encoding = New-Object System.Text.UTF8Encoding $True
8+
[System.IO.File]::WriteAllText($FileName, $content ,$encoding)
9+
}
10+
11+
Update-Nuspec-File -FileName "StyleCop.Analyzers\\StyleCop.Analyzers.CodeFixes\\StyleCop.Analyzers.nuspec"
12+
13+
git commit -m "Created new release $version" .
14+
git tag v$version

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1649CodeFixProvider.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,30 @@ private static async Task<Solution> GetTransformedSolutionAsync(Document documen
5353
{
5454
var solution = document.Project.Solution;
5555
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
56-
5756
var expectedFileName = diagnostic.Properties[SA1649FileNameMustMatchTypeName.ExpectedFileNameKey];
58-
var newPath = document.FilePath != null ? Path.Combine(Path.GetDirectoryName(document.FilePath), expectedFileName) : null;
59-
60-
var newDocumentId = DocumentId.CreateNewId(document.Id.ProjectId);
6157

62-
var newSolution = solution
63-
.RemoveDocument(document.Id)
64-
.AddDocument(newDocumentId, expectedFileName, syntaxRoot, document.Folders, newPath);
58+
var newSolution = ReplaceDocument(solution, document, document.Id, syntaxRoot, expectedFileName);
6559

66-
// Make sure to also add the file to linked projects
60+
// Make sure to also update other projects which reference the same file
6761
foreach (var linkedDocumentId in document.GetLinkedDocumentIds())
6862
{
69-
DocumentId linkedExtractedDocumentId = DocumentId.CreateNewId(linkedDocumentId.ProjectId);
70-
newSolution = newSolution.AddDocument(linkedExtractedDocumentId, expectedFileName, syntaxRoot, document.Folders);
63+
newSolution = ReplaceDocument(newSolution, null, linkedDocumentId, syntaxRoot, expectedFileName);
7164
}
7265

7366
return newSolution;
7467
}
68+
69+
private static Solution ReplaceDocument(Solution solution, Document document, DocumentId documentId, SyntaxNode syntaxRoot, string expectedFileName)
70+
{
71+
document ??= solution.GetDocument(documentId);
72+
73+
var newDocumentFilePath = document.FilePath != null ? Path.Combine(Path.GetDirectoryName(document.FilePath), expectedFileName) : null;
74+
var newDocumentId = DocumentId.CreateNewId(documentId.ProjectId);
75+
76+
var newSolution = solution
77+
.RemoveDocument(documentId)
78+
.AddDocument(newDocumentId, expectedFileName, syntaxRoot, document.Folders, newDocumentFilePath);
79+
return newSolution;
80+
}
7581
}
7682
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/StyleCop.Analyzers.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package>
33
<metadata minClientVersion="2.7">
44
<id>NewStyleCop.Analyzers</id>
5-
<version>0.0.2</version>
5+
<version>1.0.0</version>
66
<title>NewStyleCop.Analyzers</title>
77
<authors>Björn Hellander et. al.</authors>
88
<owners>Björn Hellander</owners>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) Contributors to the New StyleCop Analyzers project.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp10.Lightup
5+
{
6+
using StyleCop.Analyzers.Test.CSharp9.Lightup;
7+
8+
public partial class IImportScopeWrapperCSharp10UnitTests : IImportScopeWrapperCSharp9UnitTests
9+
{
10+
}
11+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp10/ReadabilityRules/SA1135CSharp10UnitTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) Contributors to the New StyleCop Analyzers project.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
#nullable disable
5-
64
namespace StyleCop.Analyzers.Test.CSharp10.ReadabilityRules
75
{
86
using System.Threading;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Contributors to the New StyleCop Analyzers project.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Test.CSharp11.Lightup
5+
{
6+
using System.Collections.Generic;
7+
using System.Collections.Immutable;
8+
using Microsoft.CodeAnalysis;
9+
using Moq;
10+
using StyleCop.Analyzers.Lightup;
11+
using StyleCop.Analyzers.Test.CSharp10.Lightup;
12+
using Xunit;
13+
14+
public partial class IImportScopeWrapperCSharp11UnitTests : IImportScopeWrapperCSharp10UnitTests
15+
{
16+
[Theory]
17+
[InlineData(0)]
18+
[InlineData(1)]
19+
[InlineData(2)]
20+
public void TestCompatibleInstance(int numberOfAliasSymbols)
21+
{
22+
var obj = CreateImportScope(numberOfAliasSymbols);
23+
Assert.True(IImportScopeWrapper.IsInstance(obj));
24+
var wrapper = IImportScopeWrapper.FromObject(obj);
25+
Assert.Equal(obj.Aliases, wrapper.Aliases);
26+
}
27+
28+
private static IImportScope CreateImportScope(int numberOfAliasSymbols)
29+
{
30+
var aliasSymbolMocks = new List<IAliasSymbol>();
31+
for (var i = 0; i < numberOfAliasSymbols; i++)
32+
{
33+
aliasSymbolMocks.Add(Mock.Of<IAliasSymbol>());
34+
}
35+
36+
var importScopeMock = new Mock<IImportScope>();
37+
importScopeMock.Setup(x => x.Aliases).Returns(aliasSymbolMocks.ToImmutableArray());
38+
return importScopeMock.Object;
39+
}
40+
}
41+
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/MaintainabilityRules/SA1404CSharp11UnitTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,58 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp11.MaintainabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
using StyleCop.Analyzers.MaintainabilityRules;
69
using StyleCop.Analyzers.Test.CSharp10.MaintainabilityRules;
10+
using Xunit;
11+
12+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
13+
StyleCop.Analyzers.MaintainabilityRules.SA1404CodeAnalysisSuppressionMustHaveJustification,
14+
StyleCop.Analyzers.MaintainabilityRules.SA1404CodeFixProvider>;
715

816
public partial class SA1404CSharp11UnitTests : SA1404CSharp10UnitTests
917
{
18+
// NOTE: This tests a fix for a c# 10 feature, but the Roslyn API used to solve it wasn't available in the version
19+
// we use in the c# 10 test project, so the test was added here instead.
20+
[Fact]
21+
[WorkItem(3594, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3594")]
22+
public async Task TestUsingNameChangeInGlobalUsingInAnotherFileAsync()
23+
{
24+
var testCode1 = @"
25+
global using MySuppressionAttribute = System.Diagnostics.CodeAnalysis.SuppressMessageAttribute;";
26+
27+
var testCode2 = @"
28+
public class Foo
29+
{
30+
[[|MySuppression(null, null)|]]
31+
public void Bar()
32+
{
33+
34+
}
35+
}";
36+
37+
var fixedCode2 = @"
38+
public class Foo
39+
{
40+
[MySuppression(null, null, Justification = """ + SA1404CodeAnalysisSuppressionMustHaveJustification.JustificationPlaceholder + @""")]
41+
public void Bar()
42+
{
43+
44+
}
45+
}";
46+
47+
await new CSharpTest()
48+
{
49+
TestSources = { testCode1, testCode2 },
50+
FixedSources = { testCode1, fixedCode2 },
51+
RemainingDiagnostics =
52+
{
53+
Diagnostic().WithLocation("/0/Test1.cs", 4, 32),
54+
},
55+
NumberOfIncrementalIterations = 2,
56+
NumberOfFixAllIterations = 2,
57+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
58+
}
1059
}
1160
}

StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp11/ReadabilityRules/SA1121CSharp11UnitTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,43 @@
33

44
namespace StyleCop.Analyzers.Test.CSharp11.ReadabilityRules
55
{
6+
using System.Threading;
7+
using System.Threading.Tasks;
68
using StyleCop.Analyzers.Test.CSharp10.ReadabilityRules;
9+
using Xunit;
10+
11+
using static StyleCop.Analyzers.Test.Verifiers.StyleCopCodeFixVerifier<
12+
StyleCop.Analyzers.ReadabilityRules.SA1121UseBuiltInTypeAlias,
13+
StyleCop.Analyzers.ReadabilityRules.SA1121CodeFixProvider>;
714

815
public partial class SA1121CSharp11UnitTests : SA1121CSharp10UnitTests
916
{
17+
// NOTE: This tests a fix for a c# 10 feature, but the Roslyn API used to solve it wasn't available in the version
18+
// we use in the c# 10 test project, so the test was added here instead.
19+
[Fact]
20+
[WorkItem(3594, "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/issues/3594")]
21+
public async Task TestUsingNameChangeInGlobalUsingInAnotherFileAsync()
22+
{
23+
var source1 = @"
24+
global using MyDouble = System.Double;";
25+
26+
var oldSource2 = @"
27+
class TestClass
28+
{
29+
private [|MyDouble|] x;
30+
}";
31+
32+
var newSource2 = @"
33+
class TestClass
34+
{
35+
private double x;
36+
}";
37+
38+
await new CSharpTest()
39+
{
40+
TestSources = { source1, oldSource2 },
41+
FixedSources = { source1, newSource2 },
42+
}.RunAsync(CancellationToken.None).ConfigureAwait(false);
43+
}
1044
}
1145
}

0 commit comments

Comments
 (0)