Skip to content

Commit 0143450

Browse files
committed
Merge branch 'master' into optimize-sa1141
2 parents f254f73 + f5843ae commit 0143450

File tree

2,455 files changed

+22092
-16678
lines changed

Some content is hidden

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

2,455 files changed

+22092
-16678
lines changed

.nuget/packages.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Codecov" version="1.9.0" />
3+
<package id="CodecovUploader" version="0.7.1" />
44
<package id="OpenCover" version="4.6.519" />
5-
<package id="ReportGenerator" version="2.3.5.0" targetFramework="net452" />
5+
<package id="ReportGenerator" version="5.2.0" />
66
<package id="xunit.runner.console" version="2.4.1" />
77
</packages>

NuGet.config

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
66
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
77
<add key="dotnet5" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
8+
<add key="dotnet8" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet8/nuget/v3/index.json" />
9+
<add key="dotnet9" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet9/nuget/v3/index.json" />
810

911
<!-- Uncomment this to test unpublished builds from the AppVeyor feed. -->
1012
<!--<add key="appveyor-stylecop" value="https://ci.appveyor.com/nuget/stylecopanalyzers" />-->

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
[![Build status](https://ci.appveyor.com/api/projects/status/8jw2lq431kgg44jl/branch/master?svg=true)](https://ci.appveyor.com/project/sharwell/stylecopanalyzers/branch/master)
88

9-
[![codecov.io](http://codecov.io/github/DotNetAnalyzers/StyleCopAnalyzers/coverage.svg?branch=master)](http://codecov.io/github/DotNetAnalyzers/StyleCopAnalyzers?branch=master)
9+
[![codecov.io](https://codecov.io/github/DotNetAnalyzers/StyleCopAnalyzers/coverage.svg?branch=master)](https://codecov.io/github/DotNetAnalyzers/StyleCopAnalyzers?branch=master)
1010

1111
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.
1212

StyleCop.Analyzers/Directory.Build.props

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<LangVersion>10</LangVersion>
1414
<Nullable>enable</Nullable>
1515
<WarningLevel>99</WarningLevel>
16+
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
1617
</PropertyGroup>
1718

1819
<PropertyGroup Condition="'$(BuildingInsideVisualStudio)' != 'true'">
@@ -39,26 +40,34 @@
3940
<NoWarn>$(NoWarn),1573,1591,1712</NoWarn>
4041
</PropertyGroup>
4142

43+
<PropertyGroup>
44+
<SharedMicrosoftCodeAnalysisAnalyzersVersion>3.11.0-beta1.24052.1</SharedMicrosoftCodeAnalysisAnalyzersVersion>
45+
</PropertyGroup>
46+
4247
<ItemGroup>
4348
<PackageReference Include="Nerdbank.GitVersioning" Version="2.2.13" PrivateAssets="all" />
4449
</ItemGroup>
4550

4651
<ItemGroup>
4752
<PackageReference Include="AsyncUsageAnalyzers" Version="1.0.0-alpha003" PrivateAssets="all" />
4853
<PackageReference Include="DotNetAnalyzers.DocumentationAnalyzers" Version="1.0.0-beta.46" PrivateAssets="all" />
49-
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435" PrivateAssets="all" />
50-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
54+
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556" PrivateAssets="all" />
55+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="$(SharedMicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
5156
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="4.0.1" PrivateAssets="all" />
5257
</ItemGroup>
5358

59+
<ItemGroup>
60+
<PackageReference Include="Microsoft.CodeAnalysis.ResxSourceGenerator" Version="$(SharedMicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
61+
</ItemGroup>
62+
5463
<!-- C# Compiler -->
5564
<ItemGroup>
5665
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.0.1" PrivateAssets="all" />
5766
</ItemGroup>
5867

5968
<!-- Public API -->
6069
<ItemGroup>
61-
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="2.9.7" PrivateAssets="all" />
70+
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="$(SharedMicrosoftCodeAnalysisAnalyzersVersion)" PrivateAssets="all" />
6271
</ItemGroup>
6372

6473
<ItemGroup>

StyleCop.Analyzers/Directory.Build.targets

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33

4-
<ItemGroup>
5-
<!-- Enable code generation for resource files -->
6-
<CodeGenerationEmbeddedResource Include="@(EmbeddedResource)" Exclude="**\*.??-??.resx" />
7-
<EmbeddedResource Update="@(CodeGenerationEmbeddedResource)" Generator="ResXFileCodeGenerator" />
8-
</ItemGroup>
9-
10-
<!-- Special handling for embedded resources and generated files to show as nested in Solution Explorer -->
11-
<ItemGroup>
12-
<!-- Update designer files for *.resx files -->
13-
<Compile Update="@(CodeGenerationEmbeddedResource->'%(RecursiveDir)%(Filename).Designer.cs')" DependentUpon="$([System.IO.Path]::ChangeExtension($([System.IO.Path]::GetFileNameWithoutExtension(%(Identity))), '.resx'))" />
14-
15-
<!-- Localized embedded resources are just dependent on the parent RESX -->
16-
<EmbeddedResource Update="**\*.??-??.resx" DependentUpon="$([System.IO.Path]::ChangeExtension($([System.IO.Path]::GetFileNameWithoutExtension(%(Identity))), '.resx'))" />
17-
</ItemGroup>
18-
194
<ItemGroup>
205
<None Include="$(MSBuildProjectFileDirectory)$(AssemblyOriginatorKeyFile)" Link="%(Filename)%(Extension)" />
216
</ItemGroup>

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
7979
private static async Task<SyntaxNode> GetTransformedSyntaxRootAsync(Document document, CancellationToken cancellationToken)
8080
{
8181
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
82-
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(root.SyntaxTree, cancellationToken);
82+
var settings = document.Project.AnalyzerOptions.GetStyleCopSettingsInCodeFix(root.SyntaxTree, cancellationToken);
8383

8484
var fileHeader = FileHeaderHelpers.ParseFileHeader(root);
8585
SyntaxNode newSyntaxRoot;

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace StyleCop.Analyzers.DocumentationRules
77
{
8+
using System.Collections.Generic;
89
using System.Collections.Immutable;
910
using System.Composition;
1011
using System.Threading;
@@ -29,7 +30,7 @@ internal class SA1629CodeFixProvider : CodeFixProvider
2930
/// <inheritdoc/>
3031
public override FixAllProvider GetFixAllProvider()
3132
{
32-
return CustomFixAllProviders.BatchFixer;
33+
return FixAll.Instance;
3334
}
3435

3536
/// <inheritdoc/>
@@ -54,10 +55,46 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
5455
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
5556
{
5657
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
57-
bool replaceChar = diagnostic.Properties.ContainsKey(SA1629DocumentationTextMustEndWithAPeriod.ReplaceCharKey);
58-
var newText = text.WithChanges(new TextChange(new TextSpan(diagnostic.Location.SourceSpan.Start, replaceChar ? 1 : 0), "."));
58+
var textChange = GetTextChange(diagnostic);
59+
var newText = text.WithChanges(textChange);
5960

6061
return document.WithText(newText);
6162
}
63+
64+
private static TextChange GetTextChange(Diagnostic diagnostic)
65+
{
66+
var replaceChar = diagnostic.Properties.ContainsKey(SA1629DocumentationTextMustEndWithAPeriod.ReplaceCharKey);
67+
var textChange = new TextChange(new TextSpan(diagnostic.Location.SourceSpan.Start, replaceChar ? 1 : 0), ".");
68+
return textChange;
69+
}
70+
71+
private class FixAll : DocumentBasedFixAllProvider
72+
{
73+
public static FixAllProvider Instance { get; } =
74+
new FixAll();
75+
76+
protected override string CodeActionTitle =>
77+
DocumentationResources.SA1629CodeFix;
78+
79+
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
80+
{
81+
if (diagnostics.IsEmpty)
82+
{
83+
return null;
84+
}
85+
86+
var changes = new List<TextChange>();
87+
foreach (var diagnostic in diagnostics)
88+
{
89+
changes.Add(GetTextChange(diagnostic));
90+
}
91+
92+
changes.Sort((left, right) => left.Span.Start.CompareTo(right.Span.Start));
93+
94+
var text = await document.GetTextAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
95+
var tree = await document.GetSyntaxTreeAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
96+
return await tree.WithChangedText(text.WithChanges(changes)).GetRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
97+
}
98+
}
6299
}
63100
}

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace StyleCop.Analyzers.DocumentationRules
88
using System;
99
using System.Collections.Immutable;
1010
using System.Composition;
11-
using System.Globalization;
11+
using System.Diagnostics;
1212
using System.Linq;
1313
using System.Text.RegularExpressions;
1414
using System.Threading;
@@ -20,6 +20,7 @@ namespace StyleCop.Analyzers.DocumentationRules
2020
using Microsoft.CodeAnalysis.CSharp.Syntax;
2121
using Microsoft.CodeAnalysis.Formatting;
2222
using StyleCop.Analyzers.Helpers;
23+
using StyleCop.Analyzers.Lightup;
2324

2425
/// <summary>
2526
/// Implements a code fix for <see cref="SA1642ConstructorSummaryDocumentationMustBeginWithStandardText"/>
@@ -83,8 +84,8 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
8384

8485
internal static ImmutableArray<string> GenerateStandardText(Document document, BaseMethodDeclarationSyntax methodDeclaration, BaseTypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken)
8586
{
86-
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration);
87-
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(methodDeclaration.SyntaxTree, cancellationToken);
87+
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration) || typeDeclaration.IsKind(SyntaxKindEx.RecordStructDeclaration);
88+
var settings = document.Project.AnalyzerOptions.GetStyleCopSettingsInCodeFix(methodDeclaration.SyntaxTree, cancellationToken);
8889
var culture = settings.DocumentationRules.DocumentationCultureInfo;
8990
var resourceManager = DocumentationResources.ResourceManager;
9091

@@ -147,7 +148,19 @@ private static TypeParameterListSyntax GetTypeParameterList(BaseTypeDeclarationS
147148
return classDeclaration.TypeParameterList;
148149
}
149150

150-
return (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
151+
if (typeDeclaration is StructDeclarationSyntax structDeclaration)
152+
{
153+
return structDeclaration.TypeParameterList;
154+
}
155+
156+
if (RecordDeclarationSyntaxWrapper.IsInstance(typeDeclaration))
157+
{
158+
var recordDeclaration = (RecordDeclarationSyntaxWrapper)typeDeclaration;
159+
return recordDeclaration.TypeParameterList;
160+
}
161+
162+
Debug.Assert(false, $"Unhandled type {typeDeclaration.Kind()}");
163+
return null;
151164
}
152165

153166
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlElementSyntax node, CancellationToken cancellationToken)
@@ -202,21 +215,10 @@ private static bool IsMultiLine(XmlElementSyntax node)
202215
private static Task<Document> GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlEmptyElementSyntax node)
203216
{
204217
var typeDeclaration = node.FirstAncestorOrSelf<BaseTypeDeclarationSyntax>();
205-
206-
TypeParameterListSyntax typeParameterList;
207-
if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
208-
{
209-
typeParameterList = classDeclaration.TypeParameterList;
210-
}
211-
else
212-
{
213-
typeParameterList = (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
214-
}
218+
var typeParameterList = GetTypeParameterList(typeDeclaration);
215219

216220
var newRoot = root.ReplaceNode(node, BuildSeeElement(typeDeclaration.Identifier, typeParameterList));
217-
218221
var newDocument = document.WithSyntaxRoot(newRoot);
219-
220222
return Task.FromResult(newDocument);
221223
}
222224

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace StyleCop.Analyzers.LayoutRules
1818
using Microsoft.CodeAnalysis.CSharp;
1919
using Microsoft.CodeAnalysis.Text;
2020
using StyleCop.Analyzers.Helpers;
21+
using StyleCop.Analyzers.Lightup;
2122
using StyleCop.Analyzers.Settings.ObjectModel;
2223

2324
/// <summary>
@@ -57,7 +58,7 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5758
{
5859
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
5960

60-
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
61+
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
6162
var braceToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
6263
var tokenReplacements = GenerateBraceFixes(settings, ImmutableArray.Create(braceToken));
6364

@@ -165,6 +166,7 @@ private static bool IsAccessorWithSingleLineBlock(SyntaxToken previousToken, Syn
165166
{
166167
case SyntaxKind.GetKeyword:
167168
case SyntaxKind.SetKeyword:
169+
case SyntaxKindEx.InitKeyword:
168170
case SyntaxKind.AddKeyword:
169171
case SyntaxKind.RemoveKeyword:
170172
break;
@@ -288,7 +290,7 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
288290
.OrderBy(token => token.SpanStart)
289291
.ToImmutableArray();
290292

291-
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
293+
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
292294

293295
var tokenReplacements = GenerateBraceFixes(settings, tokens);
294296

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
6060
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
6161
{
6262
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
63-
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
63+
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
6464
if (!(syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is StatementSyntax statement))
6565
{
6666
return document;
@@ -304,7 +304,7 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
304304

305305
var tokenReplaceMap = new Dictionary<SyntaxToken, SyntaxToken>();
306306
SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
307-
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
307+
var settings = SettingsHelper.GetStyleCopSettingsInCodeFix(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
308308

309309
foreach (var diagnostic in diagnostics.Sort(DiagnosticComparer.Instance))
310310
{

0 commit comments

Comments
 (0)