Skip to content

Commit 7e8eb7d

Browse files
committed
Merge remote-tracking branch 'upstream/master' into bugfix/3279-handle-optional-named-args-correctly
2 parents 9348eae + 82c2d90 commit 7e8eb7d

File tree

70 files changed

+1717
-349
lines changed

Some content is hidden

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

70 files changed

+1717
-349
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ indent_size = 4
1212

1313
# Sort using and Import directives with System.* appearing first
1414
dotnet_sort_system_directives_first = true
15+
dotnet_separate_import_directive_groups = false
16+
csharp_using_directive_placement = inside_namespace:none
1517

1618
# Always use "this." and "Me." when applicable; let StyleCop Analyzers provide the warning and fix
1719
dotnet_style_qualification_for_field = true:none

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

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

8282
var fileHeader = FileHeaderHelpers.ParseFileHeader(root);
8383
SyntaxNode newSyntaxRoot;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
8282
internal static ImmutableArray<string> GenerateStandardText(Document document, BaseMethodDeclarationSyntax methodDeclaration, BaseTypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken)
8383
{
8484
bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration);
85-
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(cancellationToken);
85+
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(methodDeclaration.SyntaxTree, cancellationToken);
8686
var culture = new CultureInfo(settings.DocumentationRules.DocumentationCulture);
8787
var resourceManager = DocumentationResources.ResourceManager;
8888

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
5555
{
5656
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
5757

58-
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
58+
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
5959
var braceToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
60-
var tokenReplacements = GenerateBraceFixes(settings.Indentation, ImmutableArray.Create(braceToken));
60+
var tokenReplacements = GenerateBraceFixes(settings, ImmutableArray.Create(braceToken));
6161

6262
var newSyntaxRoot = syntaxRoot.ReplaceTokens(tokenReplacements.Keys, (originalToken, rewrittenToken) => tokenReplacements[originalToken]);
6363
return document.WithSyntaxRoot(newSyntaxRoot);
6464
}
6565

66-
private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(IndentationSettings indentationSettings, ImmutableArray<SyntaxToken> braceTokens)
66+
private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(StyleCopSettings settings, ImmutableArray<SyntaxToken> braceTokens)
6767
{
6868
var tokenReplacements = new Dictionary<SyntaxToken, SyntaxToken>();
6969

@@ -72,7 +72,7 @@ private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(Indentati
7272
var braceLine = LocationHelpers.GetLineSpan(braceToken).StartLinePosition.Line;
7373
var braceReplacementToken = braceToken;
7474

75-
var indentationSteps = DetermineIndentationSteps(indentationSettings, braceToken);
75+
var indentationSteps = DetermineIndentationSteps(settings.Indentation, braceToken);
7676

7777
var previousToken = braceToken.GetPreviousToken();
7878

@@ -102,19 +102,23 @@ private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(Indentati
102102
AddReplacement(tokenReplacements, previousToken, previousToken.WithTrailingTrivia(previousTokenNewTrailingTrivia));
103103
}
104104

105-
braceReplacementToken = braceReplacementToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationSettings, indentationSteps));
105+
braceReplacementToken = braceReplacementToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, indentationSteps));
106106
}
107107

108108
// Check if we need to apply a fix after the brace. No fix is needed when:
109109
// - The closing brace is followed by a semi-colon or closing paren
110110
// - The closing brace is the last token in the file
111+
// - The closing brace is followed by the while expression of a do/while loop and the
112+
// allowDoWhileOnClosingBrace setting is enabled.
111113
var nextToken = braceToken.GetNextToken();
112114
var nextTokenLine = nextToken.IsKind(SyntaxKind.None) ? -1 : LocationHelpers.GetLineSpan(nextToken).StartLinePosition.Line;
113115
var isMultiDimensionArrayInitializer = braceToken.IsKind(SyntaxKind.OpenBraceToken) && braceToken.Parent.IsKind(SyntaxKind.ArrayInitializerExpression) && braceToken.Parent.Parent.IsKind(SyntaxKind.ArrayInitializerExpression);
116+
var allowDoWhileOnClosingBrace = settings.LayoutRules.AllowDoWhileOnClosingBrace && nextToken.IsKind(SyntaxKind.WhileKeyword) && (braceToken.Parent?.IsKind(SyntaxKind.Block) ?? false) && (braceToken.Parent.Parent?.IsKind(SyntaxKind.DoStatement) ?? false);
114117

115118
if ((nextTokenLine == braceLine) &&
116119
(!braceToken.IsKind(SyntaxKind.CloseBraceToken) || !IsValidFollowingToken(nextToken)) &&
117-
!isMultiDimensionArrayInitializer)
120+
!isMultiDimensionArrayInitializer &&
121+
!allowDoWhileOnClosingBrace)
118122
{
119123
var sharedTrivia = nextToken.LeadingTrivia.WithoutTrailingWhitespace();
120124
var newTrailingTrivia = braceReplacementToken.TrailingTrivia
@@ -135,7 +139,7 @@ private static Dictionary<SyntaxToken, SyntaxToken> GenerateBraceFixes(Indentati
135139
newIndentationSteps = Math.Max(0, newIndentationSteps - 1);
136140
}
137141

138-
AddReplacement(tokenReplacements, nextToken, nextToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationSettings, newIndentationSteps)));
142+
AddReplacement(tokenReplacements, nextToken, nextToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, newIndentationSteps)));
139143
}
140144

141145
braceReplacementToken = braceReplacementToken.WithTrailingTrivia(newTrailingTrivia);
@@ -282,9 +286,9 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
282286
.OrderBy(token => token.SpanStart)
283287
.ToImmutableArray();
284288

285-
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
289+
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
286290

287-
var tokenReplacements = GenerateBraceFixes(settings.Indentation, tokens);
291+
var tokenReplacements = GenerateBraceFixes(settings, tokens);
288292

289293
return syntaxRoot.ReplaceTokens(tokenReplacements.Keys, (originalToken, rewrittenToken) => tokenReplacements[originalToken]);
290294
}

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

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

303303
var tokenReplaceMap = new Dictionary<SyntaxToken, SyntaxToken>();
304-
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
305304
SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
305+
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
306306

307307
foreach (var diagnostic in diagnostics.Sort(DiagnosticComparer.Instance))
308308
{

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1502CodeFixProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
5353
private async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
5454
{
5555
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
56-
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
56+
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
5757
var newDocument = this.CreateCodeFix(document, settings.Indentation, diagnostic, syntaxRoot);
5858

5959
return newDocument;

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1504CodeFixProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private static bool IsAllowedTrivia(SyntaxTrivia trivia)
109109
private static async Task<Document> GetTransformedDocumentForSingleLineAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
110110
{
111111
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
112-
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
112+
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
113113

114114
var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
115115
var accessorList = GetAccessorList(node);
@@ -188,7 +188,7 @@ private static BlockSyntax ReformatBodyAsSingleLine(BlockSyntax body)
188188
private static async Task<Document> GetTransformedDocumentForMutipleLinesAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
189189
{
190190
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
191-
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
191+
var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
192192

193193
var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
194194
var accessorList = GetAccessorList(node);

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1506CodeFixProvider.cs

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.LayoutRules
55
{
66
using System.Collections.Immutable;
77
using System.Composition;
8+
using System.Linq;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.CodeAnalysis;
@@ -27,7 +28,7 @@ internal class SA1506CodeFixProvider : CodeFixProvider
2728
/// <inheritdoc/>
2829
public override FixAllProvider GetFixAllProvider()
2930
{
30-
return CustomFixAllProviders.BatchFixer;
31+
return FixAll.Instance;
3132
}
3233

3334
/// <inheritdoc/>
@@ -48,53 +49,72 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
4849

4950
private static async Task<Document> GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
5051
{
51-
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
52+
var newRoot = await GetTransformedDocumentAsync(document, ImmutableArray.Create(diagnostic), cancellationToken).ConfigureAwait(false);
53+
return document.WithSyntaxRoot(newRoot);
54+
}
5255

53-
var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
54-
var triviaList = token.LeadingTrivia;
56+
private static async Task<SyntaxNode> GetTransformedDocumentAsync(Document document, ImmutableArray<Diagnostic> diagnostics, CancellationToken cancellationToken)
57+
{
58+
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
59+
return syntaxRoot.ReplaceTokens(
60+
diagnostics.Select(diagnostic => syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start)),
61+
(originalToken, rewrittenToken) =>
62+
{
63+
var triviaList = rewrittenToken.LeadingTrivia;
5564

56-
var index = triviaList.IndexOf(SyntaxKind.SingleLineDocumentationCommentTrivia);
65+
var index = triviaList.IndexOf(SyntaxKind.SingleLineDocumentationCommentTrivia);
5766

58-
int currentLineStart = index + 1;
59-
bool onBlankLine = true;
60-
for (int currentIndex = currentLineStart; currentIndex < triviaList.Count; currentIndex++)
61-
{
62-
switch (triviaList[currentIndex].Kind())
63-
{
64-
case SyntaxKind.EndOfLineTrivia:
65-
if (onBlankLine)
67+
int currentLineStart = index + 1;
68+
bool onBlankLine = true;
69+
for (int currentIndex = currentLineStart; currentIndex < triviaList.Count; currentIndex++)
6670
{
67-
triviaList = triviaList.RemoveRange(currentLineStart, currentIndex - currentLineStart + 1);
68-
currentIndex = currentLineStart - 1;
69-
continue;
70-
}
71-
else
72-
{
73-
currentLineStart = currentIndex + 1;
74-
onBlankLine = true;
75-
break;
76-
}
71+
switch (triviaList[currentIndex].Kind())
72+
{
73+
case SyntaxKind.EndOfLineTrivia:
74+
if (onBlankLine)
75+
{
76+
triviaList = triviaList.RemoveRange(currentLineStart, currentIndex - currentLineStart + 1);
77+
currentIndex = currentLineStart - 1;
78+
continue;
79+
}
80+
else
81+
{
82+
currentLineStart = currentIndex + 1;
83+
onBlankLine = true;
84+
break;
85+
}
7786

78-
case SyntaxKind.WhitespaceTrivia:
79-
break;
87+
case SyntaxKind.WhitespaceTrivia:
88+
break;
8089

81-
default:
82-
if (triviaList[currentIndex].HasBuiltinEndLine())
83-
{
84-
currentLineStart = currentIndex + 1;
85-
onBlankLine = true;
86-
break;
87-
}
88-
else
89-
{
90-
onBlankLine = false;
91-
break;
90+
default:
91+
if (triviaList[currentIndex].HasBuiltinEndLine())
92+
{
93+
currentLineStart = currentIndex + 1;
94+
onBlankLine = true;
95+
break;
96+
}
97+
else
98+
{
99+
onBlankLine = false;
100+
break;
101+
}
102+
}
92103
}
93-
}
94-
}
95104

96-
var newSyntaxRoot = syntaxRoot.ReplaceToken(token, token.WithLeadingTrivia(triviaList));
97-
return document.WithSyntaxRoot(newSyntaxRoot);
105+
return rewrittenToken.WithLeadingTrivia(triviaList);
106+
});
107+
}
108+
109+
private class FixAll : DocumentBasedFixAllProvider
110+
{
111+
public static FixAllProvider Instance { get; } =
112+
new FixAll();
113+
114+
protected override string CodeActionTitle => LayoutResources.SA1506CodeFix;
115+
116+
protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray<Diagnostic> diagnostics)
117+
=> await GetTransformedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
98118
}
99119
}
100120
}

0 commit comments

Comments
 (0)