diff --git a/.editorconfig b/.editorconfig
index e660180db..3f333c552 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -1,5 +1,13 @@
root = true
+[*.{csproj,props,targets}]
+indent_style = space
+indent_size = 2
+
+[*.yml]
+indent_style = space
+indent_size = 2
+
[*.cs]
charset = utf-8-bom
insert_final_newline = true
@@ -8,6 +16,8 @@ indent_size = 4
# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = true
+dotnet_separate_import_directive_groups = false
+csharp_using_directive_placement = inside_namespace:none
# Always use "this." and "Me." when applicable; let StyleCop Analyzers provide the warning and fix
dotnet_style_qualification_for_field = true:none
diff --git a/.nuget/packages.config b/.nuget/packages.config
index bc0964b13..d409bf15b 100644
--- a/.nuget/packages.config
+++ b/.nuget/packages.config
@@ -1,8 +1,7 @@
-
-
+
-
-
+
+
\ No newline at end of file
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index c0c793e45..8917cb3a6 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -7,6 +7,12 @@ for issues suitable if you are unfamiliar with roslyn.
You can also help by filing issues, participating in discussions and doing code review.
+## Building prerequisites
+
+* Visual Studio 2017 (Community Edition or higher) is required for building this repository.
+* The version of the [.NET Core SDK](https://dotnet.microsoft.com/download/dotnet-core) as specified in the global.json file at the root of this repo.
+ Use the init script at the root of the repo to conveniently acquire and install the right version.
+
## Implementing a diagnostic
1. To start working on a diagnostic, add a comment to the issue indicating you are working on implementing it.
@@ -23,7 +29,3 @@ You can also help by filing issues, participating in discussions and doing code
2. A new issue was created for implementing tests for the item (e.g. #176).
3. Evidence was given that the feature is currently operational, and the code appears to be a solid starting point
for other contributors to continue the implementation effort.
-
-## Building
-
-Visual Studio 2017 (Community Edition or higher) is required for building this repository.
diff --git a/LICENSE b/LICENSE
index d2ec097a3..5b50c0966 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,69 +1,21 @@
-Copyright (c) Tunnel Vision Laboratories, LLC. All rights reserved.
-
-Licensed under the Apache License, Version 2.0 (the "License"); you may not use
-these files except in compliance with the License. You may obtain a copy of the
-License at
-
-http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software distributed
-under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
-CONDITIONS OF ANY KIND, either express or implied. See the License for the
-specific language governing permissions and limitations under the License.
-
----
-
-This project uses other open source projects, which are used under the terms
-of the following license(s).
-
-.NET Compiler Platform ("Roslyn")
-
- Copyright Microsoft.
-
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
- these files except in compliance with the License. You may obtain a copy of the
- License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software distributed
- under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied. See the License for the
- specific language governing permissions and limitations under the License.
-
-Code Cracker
-
- Copyright 2014 Giovanni Bassi and Elemar Jr.
-
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
- these files except in compliance with the License. You may obtain a copy of the
- License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software distributed
- under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied. See the License for the
- specific language governing permissions and limitations under the License.
-
-LightJson
-
- Copyright (c) 2017 Marcos López C.
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
+MIT License
+
+Copyright (c) Tunnel Vision Laboratories, LLC
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/NuGet.config b/NuGet.config
index 3b82964e7..e72a4de73 100644
--- a/NuGet.config
+++ b/NuGet.config
@@ -1,11 +1,13 @@
-
-
-
+
+
+
+
+
-
+
diff --git a/README.md b/README.md
index e52299e24..b9a2ec69c 100644
--- a/README.md
+++ b/README.md
@@ -29,8 +29,9 @@ Not all versions of StyleCop.Analyzers support all features of each C# language
| C# version | StyleCop.Analyzers version | Visual Studio version |
|------------|----------------------------|-----------------------|
-| 1.0 - 6.0 | v1.0.2 or higher | VS2015+ |
-| 7.0 - 7.3 | v1.1.0-beta or higher | VS2017 |
+| 1.0 - 6.0 | v1.0.2 or higher | VS2015+ |
+| 7.0 - 7.3 | v1.1.0-beta or higher | VS2017+ |
+| 8.0 | v1.2.0-beta or higher | VS2019 |
## Installation
diff --git a/StyleCop.Analyzers/Directory.Build.props b/StyleCop.Analyzers/Directory.Build.props
index d0c608bd2..f3a08336b 100644
--- a/StyleCop.Analyzers/Directory.Build.props
+++ b/StyleCop.Analyzers/Directory.Build.props
@@ -10,8 +10,10 @@
- 7.2
- strict
+ 10
+ enable
+ 99
+ true
@@ -20,7 +22,8 @@
- portable
+ full
+ portable
true
@@ -37,6 +40,10 @@
$(NoWarn),1573,1591,1712
+
+ 3.11.0-beta1.23472.1
+
+
@@ -44,7 +51,28 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/StyleCop.Analyzers/Directory.Build.targets b/StyleCop.Analyzers/Directory.Build.targets
index 862c5c1c8..f3adcdfc6 100644
--- a/StyleCop.Analyzers/Directory.Build.targets
+++ b/StyleCop.Analyzers/Directory.Build.targets
@@ -1,21 +1,6 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs
index 0b0386faf..5e076a3fc 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/FileHeaderCodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
@@ -66,6 +68,9 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
return SpecializedTasks.CompletedTask;
}
+ private static string GetFileName(Document document)
+ => Path.GetFileName(document.FilePath ?? document.Name);
+
private static async Task GetTransformedDocumentAsync(Document document, CancellationToken cancellationToken)
{
return document.WithSyntaxRoot(await GetTransformedSyntaxRootAsync(document, cancellationToken).ConfigureAwait(false));
@@ -74,13 +79,13 @@ private static async Task GetTransformedDocumentAsync(Document documen
private static async Task GetTransformedSyntaxRootAsync(Document document, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(cancellationToken);
+ var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(root.SyntaxTree, cancellationToken);
var fileHeader = FileHeaderHelpers.ParseFileHeader(root);
SyntaxNode newSyntaxRoot;
if (fileHeader.IsMissing)
{
- newSyntaxRoot = AddHeader(document, root, document.Name, settings);
+ newSyntaxRoot = AddHeader(document, root, GetFileName(document), settings);
}
else
{
@@ -144,9 +149,9 @@ private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader(Document docum
// Pad line that used to be next to a /*
triviaStringParts[0] = commentIndentation + interlinePadding + " " + triviaStringParts[0];
StringBuilder sb = StringBuilderPool.Allocate();
- string fileName = Path.GetFileName(document.FilePath);
+ string fileName = GetFileName(document);
var copyrightText = GetCopyrightText(commentIndentation + interlinePadding, settings.DocumentationRules.GetCopyrightText(fileName), newLineText);
- var newHeader = WrapInXmlComment(commentIndentation + interlinePadding, copyrightText, document.Name, settings, newLineText);
+ var newHeader = WrapInXmlComment(commentIndentation + interlinePadding, copyrightText, fileName, settings, newLineText);
sb.Append(commentIndentation);
sb.Append("/*");
@@ -310,13 +315,13 @@ private static SyntaxNode ReplaceHeader(Document document, SyntaxNode root, Styl
string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
var newLineTrivia = SyntaxFactory.EndOfLine(newLineText);
- var newHeaderTrivia = CreateNewHeader(leadingSpaces + "//", document.Name, settings, newLineText);
+ var newHeaderTrivia = CreateNewHeader(leadingSpaces + "//", GetFileName(document), settings, newLineText);
if (!isMalformedHeader && copyrightTriviaIndex.HasValue)
{
// Does the copyright element have leading whitespace? If so remove it.
if ((copyrightTriviaIndex.Value > 0) && trivia[copyrightTriviaIndex.Value - 1].IsKind(SyntaxKind.WhitespaceTrivia))
{
- copyrightTriviaIndex = copyrightTriviaIndex - 1;
+ copyrightTriviaIndex--;
trivia = trivia.RemoveAt(copyrightTriviaIndex.Value);
}
@@ -444,12 +449,12 @@ private static SyntaxTriviaList RemoveHeaderDecorationLines(SyntaxTriviaList tri
for (int i = 0; i < trivia.Count; i++)
{
var triviaLine = trivia[i];
- if (triviaLine.Kind() == SyntaxKind.SingleLineCommentTrivia && triviaLine.ToFullString().Contains(settings.DocumentationRules.HeaderDecoration))
+ if (triviaLine.IsKind(SyntaxKind.SingleLineCommentTrivia) && triviaLine.ToFullString().Contains(settings.DocumentationRules.HeaderDecoration))
{
decorationRemovalList.Add(i);
// also remove the line break
- if (i + 1 < trivia.Count && trivia[i + 1].Kind() == SyntaxKind.EndOfLineTrivia)
+ if (i + 1 < trivia.Count && trivia[i + 1].IsKind(SyntaxKind.EndOfLineTrivia))
{
decorationRemovalList.Add(i + 1);
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/InheritdocCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/InheritdocCodeFixProvider.cs
index f5dd35aea..24ce9ac74 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/InheritdocCodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/InheritdocCodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
@@ -90,36 +92,36 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
DocumentationResources.InheritdocCodeFix,
- cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, root, identifierToken, cancellationToken),
+ cancellationToken => GetTransformedDocumentAsync(context.Document, root, identifierToken, cancellationToken),
nameof(InheritdocCodeFixProvider)),
diagnostic);
}
}
- private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, SyntaxNode root, SyntaxToken identifierToken, CancellationToken cancellationToken)
+ private static async Task GetTransformedDocumentAsync(Document document, SyntaxNode root, SyntaxToken identifierToken, CancellationToken cancellationToken)
{
SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
switch (identifierToken.Parent.Kind())
{
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.EventDeclaration:
- return GetTransformedDocumentForBasePropertyDeclaration(document, diagnostic, root, semanticModel, (BasePropertyDeclarationSyntax)identifierToken.Parent, cancellationToken);
+ return GetTransformedDocumentForBasePropertyDeclaration(document, root, semanticModel, (BasePropertyDeclarationSyntax)identifierToken.Parent, cancellationToken);
case SyntaxKind.MethodDeclaration:
- return GetTransformedDocumentForMethodDeclaration(document, diagnostic, root, semanticModel, (MethodDeclarationSyntax)identifierToken.Parent, cancellationToken);
+ return GetTransformedDocumentForMethodDeclaration(document, root, semanticModel, (MethodDeclarationSyntax)identifierToken.Parent, cancellationToken);
case SyntaxKind.VariableDeclarator:
- return GetTransformedDocumentForEventFieldDeclaration(document, diagnostic, root, semanticModel, (EventFieldDeclarationSyntax)identifierToken.Parent.Parent.Parent, cancellationToken);
+ return GetTransformedDocumentForEventFieldDeclaration(document, root, semanticModel, (EventFieldDeclarationSyntax)identifierToken.Parent.Parent.Parent, cancellationToken);
case SyntaxKind.IndexerDeclaration:
- return GetTransformedDocumentForIndexerDeclaration(document, diagnostic, root, semanticModel, (IndexerDeclarationSyntax)identifierToken.Parent, cancellationToken);
+ return GetTransformedDocumentForIndexerDeclaration(document, root, semanticModel, (IndexerDeclarationSyntax)identifierToken.Parent, cancellationToken);
default:
return document;
}
}
- private static Document GetTransformedDocumentForBasePropertyDeclaration(Document document, Diagnostic diagnostic, SyntaxNode root, SemanticModel semanticModel, BasePropertyDeclarationSyntax basePropertyDeclaration, CancellationToken cancellationToken)
+ private static Document GetTransformedDocumentForBasePropertyDeclaration(Document document, SyntaxNode root, SemanticModel semanticModel, BasePropertyDeclarationSyntax basePropertyDeclaration, CancellationToken cancellationToken)
{
if (basePropertyDeclaration.ExplicitInterfaceSpecifier == null && !basePropertyDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
{
@@ -130,10 +132,10 @@ private static Document GetTransformedDocumentForBasePropertyDeclaration(Documen
}
}
- return InsertInheritdocComment(document, diagnostic, root, basePropertyDeclaration, cancellationToken);
+ return InsertInheritdocComment(document, root, basePropertyDeclaration, cancellationToken);
}
- private static Document GetTransformedDocumentForMethodDeclaration(Document document, Diagnostic diagnostic, SyntaxNode root, SemanticModel semanticModel, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
+ private static Document GetTransformedDocumentForMethodDeclaration(Document document, SyntaxNode root, SemanticModel semanticModel, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
{
if (methodDeclaration.ExplicitInterfaceSpecifier == null && !methodDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
{
@@ -144,10 +146,10 @@ private static Document GetTransformedDocumentForMethodDeclaration(Document docu
}
}
- return InsertInheritdocComment(document, diagnostic, root, methodDeclaration, cancellationToken);
+ return InsertInheritdocComment(document, root, methodDeclaration, cancellationToken);
}
- private static Document GetTransformedDocumentForEventFieldDeclaration(Document document, Diagnostic diagnostic, SyntaxNode root, SemanticModel semanticModel, EventFieldDeclarationSyntax eventFieldDeclaration, CancellationToken cancellationToken)
+ private static Document GetTransformedDocumentForEventFieldDeclaration(Document document, SyntaxNode root, SemanticModel semanticModel, EventFieldDeclarationSyntax eventFieldDeclaration, CancellationToken cancellationToken)
{
if (!eventFieldDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
{
@@ -164,10 +166,10 @@ private static Document GetTransformedDocumentForEventFieldDeclaration(Document
}
}
- return InsertInheritdocComment(document, diagnostic, root, eventFieldDeclaration, cancellationToken);
+ return InsertInheritdocComment(document, root, eventFieldDeclaration, cancellationToken);
}
- private static Document GetTransformedDocumentForIndexerDeclaration(Document document, Diagnostic diagnostic, SyntaxNode root, SemanticModel semanticModel, IndexerDeclarationSyntax indexerDeclaration, CancellationToken cancellationToken)
+ private static Document GetTransformedDocumentForIndexerDeclaration(Document document, SyntaxNode root, SemanticModel semanticModel, IndexerDeclarationSyntax indexerDeclaration, CancellationToken cancellationToken)
{
if (indexerDeclaration.ExplicitInterfaceSpecifier == null && !indexerDeclaration.Modifiers.Any(SyntaxKind.OverrideKeyword))
{
@@ -178,11 +180,14 @@ private static Document GetTransformedDocumentForIndexerDeclaration(Document doc
}
}
- return InsertInheritdocComment(document, diagnostic, root, indexerDeclaration, cancellationToken);
+ return InsertInheritdocComment(document, root, indexerDeclaration, cancellationToken);
}
- private static Document InsertInheritdocComment(Document document, Diagnostic diagnostic, SyntaxNode root, SyntaxNode syntaxNode, CancellationToken cancellationToken)
+ private static Document InsertInheritdocComment(Document document, SyntaxNode root, SyntaxNode syntaxNode, CancellationToken cancellationToken)
{
+ // Currently unused
+ _ = cancellationToken;
+
SyntaxTriviaList leadingTrivia = syntaxNode.GetLeadingTrivia();
int insertionIndex = leadingTrivia.Count;
while (insertionIndex > 0 && !leadingTrivia[insertionIndex - 1].HasBuiltinEndLine())
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/PropertySummaryDocumentationCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/PropertySummaryDocumentationCodeFixProvider.cs
index 540b2c99c..bf6b4487c 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/PropertySummaryDocumentationCodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/PropertySummaryDocumentationCodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
@@ -14,14 +16,13 @@ namespace StyleCop.Analyzers.DocumentationRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Helpers;
- using StyleCop.Analyzers.Helpers.ObjectPools;
///
/// Implements the code fix for property summary documentation.
///
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PropertySummaryDocumentationCodeFixProvider))]
[Shared]
- public class PropertySummaryDocumentationCodeFixProvider : CodeFixProvider
+ internal class PropertySummaryDocumentationCodeFixProvider : CodeFixProvider
{
///
public override ImmutableArray FixableDiagnosticIds { get; } =
@@ -62,7 +63,7 @@ private static async Task GetTransformedDocumentAsync(Document documen
var documentation = node.GetDocumentationCommentTriviaSyntax();
var summaryElement = (XmlElementSyntax)documentation.Content.GetFirstXmlElement(XmlCommentHelper.SummaryXmlTag);
- var textElement = (XmlTextSyntax)summaryElement.Content.FirstOrDefault();
+ var textElement = XmlCommentHelper.TryGetFirstTextElementWithContent(summaryElement);
if (textElement == null)
{
return document;
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1600CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1600CodeFixProvider.cs
index 04ec8da93..6480d6594 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1600CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1600CodeFixProvider.cs
@@ -1,9 +1,10 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
- using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
@@ -59,7 +60,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
DocumentationResources.ConstructorDocumentationCodeFix,
- cancellationToken => GetConstructorOrDestructorDocumentationTransformedDocumentAsync(context.Document, diagnostic, root, (BaseMethodDeclarationSyntax)identifierToken.Parent, cancellationToken),
+ cancellationToken => GetConstructorOrDestructorDocumentationTransformedDocumentAsync(context.Document, root, (BaseMethodDeclarationSyntax)identifierToken.Parent, cancellationToken),
nameof(SA1600CodeFixProvider)),
diagnostic);
break;
@@ -72,7 +73,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
DocumentationResources.MethodDocumentationCodeFix,
- cancellationToken => GetMethodDocumentationTransformedDocumentAsync(context.Document, diagnostic, root, semanticModel, (MethodDeclarationSyntax)identifierToken.Parent, cancellationToken),
+ cancellationToken => GetMethodDocumentationTransformedDocumentAsync(context.Document, root, semanticModel, (MethodDeclarationSyntax)identifierToken.Parent, cancellationToken),
nameof(SA1600CodeFixProvider)),
diagnostic);
}
@@ -98,7 +99,7 @@ private static bool IsCoveredByInheritDoc(SemanticModel semanticModel, MethodDec
return (declaredSymbol != null) && NamedTypeHelpers.IsImplementingAnInterfaceMember(declaredSymbol);
}
- private static Task GetConstructorOrDestructorDocumentationTransformedDocumentAsync(Document document, Diagnostic diagnostic, SyntaxNode root, BaseMethodDeclarationSyntax declaration, CancellationToken cancellationToken)
+ private static Task GetConstructorOrDestructorDocumentationTransformedDocumentAsync(Document document, SyntaxNode root, BaseMethodDeclarationSyntax declaration, CancellationToken cancellationToken)
{
SyntaxTriviaList leadingTrivia = declaration.GetLeadingTrivia();
int insertionIndex = GetInsertionIndex(ref leadingTrivia);
@@ -136,7 +137,7 @@ private static Task GetConstructorOrDestructorDocumentationTransformed
return Task.FromResult(document.WithSyntaxRoot(root.ReplaceNode(declaration, newElement)));
}
- private static Task GetMethodDocumentationTransformedDocumentAsync(Document document, Diagnostic diagnostic, SyntaxNode root, SemanticModel semanticModel, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
+ private static Task GetMethodDocumentationTransformedDocumentAsync(Document document, SyntaxNode root, SemanticModel semanticModel, MethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
{
SyntaxTriviaList leadingTrivia = methodDeclaration.GetLeadingTrivia();
int insertionIndex = GetInsertionIndex(ref leadingTrivia);
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1609SA1610CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1609SA1610CodeFixProvider.cs
index 3a8c9e861..8082963b6 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1609SA1610CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1609SA1610CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
@@ -174,7 +176,7 @@ private bool TryRemoveSummaryPrefix(ref SyntaxList summaryContent
}
// Find the token containing the prefix, such as "Gets or sets "
- SyntaxToken prefixToken = default(SyntaxToken);
+ SyntaxToken prefixToken = default;
foreach (SyntaxToken textToken in firstText.TextTokens)
{
if (textToken.IsMissing)
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1615SA1616CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1615SA1616CodeFixProvider.cs
index d7039072d..f3b40d918 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1615SA1616CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1615SA1616CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1617CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1617CodeFixProvider.cs
index f8f77f933..75e76316c 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1617CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1617CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1626CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1626CodeFixProvider.cs
index 4c6c54893..189f05a3b 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1626CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1626CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
@@ -53,7 +55,6 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
- var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
TextChange textChange = new TextChange(new TextSpan(diagnostic.Location.SourceSpan.Start, 1), string.Empty);
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs
index 432845636..223cc203e 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1629CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
@@ -52,7 +54,8 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
- var newText = text.WithChanges(new TextChange(new TextSpan(diagnostic.Location.SourceSpan.Start, 0), "."));
+ bool replaceChar = diagnostic.Properties.ContainsKey(SA1629DocumentationTextMustEndWithAPeriod.ReplaceCharKey);
+ var newText = text.WithChanges(new TextChange(new TextSpan(diagnostic.Location.SourceSpan.Start, replaceChar ? 1 : 0), "."));
return document.WithText(newText);
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs
index 2dee644d6..716937ebd 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1642SA1643CodeFixProvider.cs
@@ -1,12 +1,14 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
using System;
using System.Collections.Immutable;
using System.Composition;
- using System.Globalization;
+ using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
@@ -18,6 +20,7 @@ namespace StyleCop.Analyzers.DocumentationRules
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;
using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
///
/// Implements a code fix for
@@ -81,9 +84,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
internal static ImmutableArray GenerateStandardText(Document document, BaseMethodDeclarationSyntax methodDeclaration, BaseTypeDeclarationSyntax typeDeclaration, CancellationToken cancellationToken)
{
- bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration);
- var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(cancellationToken);
- var culture = new CultureInfo(settings.DocumentationRules.DocumentationCulture);
+ bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration) || typeDeclaration.IsKind(SyntaxKindEx.RecordStructDeclaration);
+ var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(methodDeclaration.SyntaxTree, cancellationToken);
+ var culture = settings.DocumentationRules.DocumentationCultureInfo;
var resourceManager = DocumentationResources.ResourceManager;
if (methodDeclaration is ConstructorDeclarationSyntax)
@@ -117,17 +120,19 @@ internal static ImmutableArray GenerateStandardText(Document document, B
}
}
+ internal static SyntaxList BuildStandardTextSyntaxList(BaseTypeDeclarationSyntax typeDeclaration, string preText, string postText)
+ {
+ TypeParameterListSyntax typeParameterList = GetTypeParameterList(typeDeclaration);
+
+ return XmlSyntaxFactory.List(
+ XmlSyntaxFactory.Text(preText),
+ BuildSeeElement(typeDeclaration.Identifier, typeParameterList),
+ XmlSyntaxFactory.Text(postText.EndsWith(".") ? postText : (postText + ".")));
+ }
+
internal static SyntaxList BuildStandardTextSyntaxList(BaseTypeDeclarationSyntax typeDeclaration, string newLineText, string preText, string postText)
{
- TypeParameterListSyntax typeParameterList;
- if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
- {
- typeParameterList = classDeclaration.TypeParameterList;
- }
- else
- {
- typeParameterList = (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
- }
+ TypeParameterListSyntax typeParameterList = GetTypeParameterList(typeDeclaration);
return XmlSyntaxFactory.List(
XmlSyntaxFactory.NewLine(newLineText),
@@ -136,6 +141,28 @@ internal static SyntaxList BuildStandardTextSyntaxList(BaseTypeDe
XmlSyntaxFactory.Text(postText.EndsWith(".") ? postText : (postText + ".")));
}
+ private static TypeParameterListSyntax GetTypeParameterList(BaseTypeDeclarationSyntax typeDeclaration)
+ {
+ if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
+ {
+ return classDeclaration.TypeParameterList;
+ }
+
+ if (typeDeclaration is StructDeclarationSyntax structDeclaration)
+ {
+ return structDeclaration.TypeParameterList;
+ }
+
+ if (RecordDeclarationSyntaxWrapper.IsInstance(typeDeclaration))
+ {
+ var recordDeclaration = (RecordDeclarationSyntaxWrapper)typeDeclaration;
+ return recordDeclaration.TypeParameterList;
+ }
+
+ Debug.Assert(false, $"Unhandled type {typeDeclaration.Kind()}");
+ return null;
+ }
+
private static Task GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlElementSyntax node, CancellationToken cancellationToken)
{
var typeDeclaration = node.FirstAncestorOrSelf();
@@ -143,11 +170,9 @@ private static Task GetTransformedDocumentAsync(Document document, Syn
var standardText = GenerateStandardText(document, declarationSyntax, typeDeclaration, cancellationToken);
- string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
-
string trailingString = string.Empty;
- var newContent = RemoveMalformattedStandardText(node.Content, typeDeclaration.Identifier, standardText[0], standardText[1], ref trailingString);
+ var newContent = RemoveMalformattedStandardText(node.Content, standardText[0], standardText[1], ref trailingString);
if (newContent.Count == 1 && newContent[0] is XmlTextSyntax xmlText)
{
@@ -157,7 +182,17 @@ private static Task GetTransformedDocumentAsync(Document document, Syn
}
}
- var list = BuildStandardTextSyntaxList(typeDeclaration, newLineText, standardText[0], standardText[1] + trailingString);
+ SyntaxList list;
+ if (IsMultiLine(node))
+ {
+ string newLineText = document.Project.Solution.Workspace.Options.GetOption(FormattingOptions.NewLine, LanguageNames.CSharp);
+ list = BuildStandardTextSyntaxList(typeDeclaration, newLineText, standardText[0], standardText[1] + trailingString);
+ }
+ else
+ {
+ list = BuildStandardTextSyntaxList(typeDeclaration, standardText[0], standardText[1] + trailingString);
+ }
+
newContent = newContent.InsertRange(0, list);
newContent = RemoveTrailingEmptyLines(newContent);
@@ -171,30 +206,23 @@ private static Task GetTransformedDocumentAsync(Document document, Syn
return Task.FromResult(newDocument);
}
+ private static bool IsMultiLine(XmlElementSyntax node)
+ {
+ var lineSpan = node.GetLineSpan();
+ return lineSpan.StartLinePosition.Line != lineSpan.EndLinePosition.Line;
+ }
+
private static Task GetTransformedDocumentAsync(Document document, SyntaxNode root, XmlEmptyElementSyntax node)
{
var typeDeclaration = node.FirstAncestorOrSelf();
- var declarationSyntax = node.FirstAncestorOrSelf();
- bool isStruct = typeDeclaration.IsKind(SyntaxKind.StructDeclaration);
-
- TypeParameterListSyntax typeParameterList;
- if (typeDeclaration is ClassDeclarationSyntax classDeclaration)
- {
- typeParameterList = classDeclaration.TypeParameterList;
- }
- else
- {
- typeParameterList = (typeDeclaration as StructDeclarationSyntax)?.TypeParameterList;
- }
+ var typeParameterList = GetTypeParameterList(typeDeclaration);
var newRoot = root.ReplaceNode(node, BuildSeeElement(typeDeclaration.Identifier, typeParameterList));
-
var newDocument = document.WithSyntaxRoot(newRoot);
-
return Task.FromResult(newDocument);
}
- private static SyntaxList RemoveMalformattedStandardText(SyntaxList content, SyntaxToken identifier, string preText, string postText, ref string trailingString)
+ private static SyntaxList RemoveMalformattedStandardText(SyntaxList content, string preText, string postText, ref string trailingString)
{
var regex = new Regex(@"^\s*" + Regex.Escape(preText) + "[^ ]+" + Regex.Escape(postText));
var item = content.OfType().FirstOrDefault();
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1649CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1649CodeFixProvider.cs
index 06b359353..09089b456 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1649CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1649CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1651CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1651CodeFixProvider.cs
index e7eb36763..e30f77d8b 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1651CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/DocumentationRules/SA1651CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.DocumentationRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/CustomBatchFixAllProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/CustomBatchFixAllProvider.cs
index d074e6290..3fbc40e28 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/CustomBatchFixAllProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/CustomBatchFixAllProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.Helpers
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/CustomFixAllProviders.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/CustomFixAllProviders.cs
index f29941a21..e874b9349 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/CustomFixAllProviders.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/CustomFixAllProviders.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.Helpers
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/DocumentBasedFixAllProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/DocumentBasedFixAllProvider.cs
index d18f73751..af8cdd098 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/DocumentBasedFixAllProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/DocumentBasedFixAllProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.Helpers
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/FixAllContextHelper.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/FixAllContextHelper.cs
index bbc1799be..59c0db505 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/FixAllContextHelper.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/FixAllContextHelper.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.Helpers
{
@@ -12,7 +14,6 @@ namespace StyleCop.Analyzers.Helpers
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
- using Microsoft.CodeAnalysis.Diagnostics;
internal static class FixAllContextHelper
{
@@ -56,7 +57,8 @@ public static async Task
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/IndentationHelper.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/IndentationHelper.cs
index c8a22b318..5786af80f 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/IndentationHelper.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/IndentationHelper.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.Helpers
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/QueryIndentationHelpers.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/QueryIndentationHelpers.cs
index 48e468b61..d2c2cac69 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/QueryIndentationHelpers.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/QueryIndentationHelpers.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.Helpers
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/RenameHelper.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/RenameHelper.cs
index f5cc9adc0..c3cbdd62b 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/RenameHelper.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/RenameHelper.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.Helpers
{
@@ -162,8 +164,7 @@ public static SyntaxNode GetParentDeclaration(SyntaxToken token)
return parent;
default:
- var declarationParent = parent as MemberDeclarationSyntax;
- if (declarationParent != null)
+ if (parent is MemberDeclarationSyntax declarationParent)
{
return declarationParent;
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/TaskHelper.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/TaskHelper.cs
index 41d6d2f4e..26f9a851a 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/TaskHelper.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/TaskHelper.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.Helpers
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs
index 09bbb207e..b16c739be 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
@@ -16,6 +18,7 @@ namespace StyleCop.Analyzers.LayoutRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.Settings.ObjectModel;
///
@@ -55,15 +58,15 @@ private static async Task GetTransformedDocumentAsync(Document documen
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var braceToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
- var tokenReplacements = GenerateBraceFixes(document, settings.Indentation, ImmutableArray.Create(braceToken));
+ var tokenReplacements = GenerateBraceFixes(settings, ImmutableArray.Create(braceToken));
var newSyntaxRoot = syntaxRoot.ReplaceTokens(tokenReplacements.Keys, (originalToken, rewrittenToken) => tokenReplacements[originalToken]);
return document.WithSyntaxRoot(newSyntaxRoot);
}
- private static Dictionary GenerateBraceFixes(Document document, IndentationSettings indentationSettings, ImmutableArray braceTokens)
+ private static Dictionary GenerateBraceFixes(StyleCopSettings settings, ImmutableArray braceTokens)
{
var tokenReplacements = new Dictionary();
@@ -72,7 +75,7 @@ private static Dictionary GenerateBraceFixes(Document
var braceLine = LocationHelpers.GetLineSpan(braceToken).StartLinePosition.Line;
var braceReplacementToken = braceToken;
- var indentationSteps = DetermineIndentationSteps(indentationSettings, braceToken);
+ var indentationSteps = DetermineIndentationSteps(settings.Indentation, braceToken);
var previousToken = braceToken.GetPreviousToken();
@@ -102,19 +105,23 @@ private static Dictionary GenerateBraceFixes(Document
AddReplacement(tokenReplacements, previousToken, previousToken.WithTrailingTrivia(previousTokenNewTrailingTrivia));
}
- braceReplacementToken = braceReplacementToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationSettings, indentationSteps));
+ braceReplacementToken = braceReplacementToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, indentationSteps));
}
// Check if we need to apply a fix after the brace. No fix is needed when:
// - The closing brace is followed by a semi-colon or closing paren
// - The closing brace is the last token in the file
+ // - The closing brace is followed by the while expression of a do/while loop and the
+ // allowDoWhileOnClosingBrace setting is enabled.
var nextToken = braceToken.GetNextToken();
var nextTokenLine = nextToken.IsKind(SyntaxKind.None) ? -1 : LocationHelpers.GetLineSpan(nextToken).StartLinePosition.Line;
var isMultiDimensionArrayInitializer = braceToken.IsKind(SyntaxKind.OpenBraceToken) && braceToken.Parent.IsKind(SyntaxKind.ArrayInitializerExpression) && braceToken.Parent.Parent.IsKind(SyntaxKind.ArrayInitializerExpression);
+ var allowDoWhileOnClosingBrace = settings.LayoutRules.AllowDoWhileOnClosingBrace && nextToken.IsKind(SyntaxKind.WhileKeyword) && (braceToken.Parent?.IsKind(SyntaxKind.Block) ?? false) && (braceToken.Parent.Parent?.IsKind(SyntaxKind.DoStatement) ?? false);
if ((nextTokenLine == braceLine) &&
(!braceToken.IsKind(SyntaxKind.CloseBraceToken) || !IsValidFollowingToken(nextToken)) &&
- !isMultiDimensionArrayInitializer)
+ !isMultiDimensionArrayInitializer &&
+ !allowDoWhileOnClosingBrace)
{
var sharedTrivia = nextToken.LeadingTrivia.WithoutTrailingWhitespace();
var newTrailingTrivia = braceReplacementToken.TrailingTrivia
@@ -135,7 +142,7 @@ private static Dictionary GenerateBraceFixes(Document
newIndentationSteps = Math.Max(0, newIndentationSteps - 1);
}
- AddReplacement(tokenReplacements, nextToken, nextToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(indentationSettings, newIndentationSteps)));
+ AddReplacement(tokenReplacements, nextToken, nextToken.WithLeadingTrivia(IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, newIndentationSteps)));
}
braceReplacementToken = braceReplacementToken.WithTrailingTrivia(newTrailingTrivia);
@@ -159,6 +166,7 @@ private static bool IsAccessorWithSingleLineBlock(SyntaxToken previousToken, Syn
{
case SyntaxKind.GetKeyword:
case SyntaxKind.SetKeyword:
+ case SyntaxKindEx.InitKeyword:
case SyntaxKind.AddKeyword:
case SyntaxKind.RemoveKeyword:
break;
@@ -282,9 +290,9 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
.OrderBy(token => token.SpanStart)
.ToImmutableArray();
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
- var tokenReplacements = GenerateBraceFixes(document, settings.Indentation, tokens);
+ var tokenReplacements = GenerateBraceFixes(settings, tokens);
return syntaxRoot.ReplaceTokens(tokenReplacements.Keys, (originalToken, rewrittenToken) => tokenReplacements[originalToken]);
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs
index 0df9b0182..592c958c5 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
@@ -58,7 +60,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
if (!(syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is StatementSyntax statement))
{
return document;
@@ -301,8 +303,8 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
}
var tokenReplaceMap = new Dictionary();
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
SyntaxNode syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
foreach (var diagnostic in diagnostics.Sort(DiagnosticComparer.Instance))
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1502CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1502CodeFixProvider.cs
index feaaace70..f24bd818e 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1502CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1502CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
@@ -53,7 +55,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var newDocument = this.CreateCodeFix(document, settings.Indentation, diagnostic, syntaxRoot);
return newDocument;
@@ -69,6 +71,8 @@ private Document CreateCodeFix(Document document, IndentationSettings indentatio
case SyntaxKind.ClassDeclaration:
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.StructDeclaration:
+ case SyntaxKindEx.RecordDeclaration:
+ case SyntaxKindEx.RecordStructDeclaration:
case SyntaxKind.EnumDeclaration:
newSyntaxRoot = this.RegisterBaseTypeDeclarationCodeFix(syntaxRoot, (BaseTypeDeclarationSyntax)node, indentationSettings);
break;
@@ -117,11 +121,6 @@ private SyntaxNode RegisterLocalFunctionStatementCodeFix(SyntaxNode syntaxRoot,
return this.ReformatElement(syntaxRoot, node, node.Body.OpenBraceToken, node.Body.CloseBraceToken, indentationSettings);
}
- private SyntaxNode RegisterEnumDeclarationCodeFix(SyntaxNode syntaxRoot, EnumDeclarationSyntax node, IndentationSettings indentationSettings)
- {
- return this.ReformatElement(syntaxRoot, node, node.OpenBraceToken, node.CloseBraceToken, indentationSettings);
- }
-
private SyntaxNode RegisterNamespaceDeclarationCodeFix(SyntaxNode syntaxRoot, NamespaceDeclarationSyntax node, IndentationSettings indentationSettings)
{
return this.ReformatElement(syntaxRoot, node, node.OpenBraceToken, node.CloseBraceToken, indentationSettings);
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1503CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1503CodeFixProvider.cs
index b6b8878aa..b28dc8323 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1503CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1503CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
@@ -64,6 +66,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
private static Task GetTransformedDocumentAsync(Document document, SyntaxNode root, StatementSyntax node, CancellationToken cancellationToken)
{
+ // Currently unused
+ _ = cancellationToken;
+
var newSyntaxRoot = root.ReplaceNode(node, SyntaxFactory.Block(node));
return Task.FromResult(document.WithSyntaxRoot(newSyntaxRoot));
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1504CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1504CodeFixProvider.cs
index ec12d3d41..51c7acb51 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1504CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1504CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
@@ -109,7 +111,7 @@ private static bool IsAllowedTrivia(SyntaxTrivia trivia)
private static async Task GetTransformedDocumentForSingleLineAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
var accessorList = GetAccessorList(node);
@@ -188,7 +190,7 @@ private static BlockSyntax ReformatBodyAsSingleLine(BlockSyntax body)
private static async Task GetTransformedDocumentForMutipleLinesAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
var accessorList = GetAccessorList(node);
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1505CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1505CodeFixProvider.cs
index 0b0489a88..71f6082ea 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1505CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1505CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1506CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1506CodeFixProvider.cs
index a4939ea23..259a1a401 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1506CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1506CodeFixProvider.cs
@@ -1,10 +1,13 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
using System.Collections.Immutable;
using System.Composition;
+ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
@@ -27,7 +30,7 @@ internal class SA1506CodeFixProvider : CodeFixProvider
///
public override FixAllProvider GetFixAllProvider()
{
- return CustomFixAllProviders.BatchFixer;
+ return FixAll.Instance;
}
///
@@ -48,53 +51,72 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
- var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
+ var newRoot = await GetTransformedDocumentAsync(document, ImmutableArray.Create(diagnostic), cancellationToken).ConfigureAwait(false);
+ return document.WithSyntaxRoot(newRoot);
+ }
- var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
- var triviaList = token.LeadingTrivia;
+ private static async Task GetTransformedDocumentAsync(Document document, ImmutableArray diagnostics, CancellationToken cancellationToken)
+ {
+ var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
+ return syntaxRoot.ReplaceTokens(
+ diagnostics.Select(diagnostic => syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start)),
+ (originalToken, rewrittenToken) =>
+ {
+ var triviaList = rewrittenToken.LeadingTrivia;
- var index = triviaList.IndexOf(SyntaxKind.SingleLineDocumentationCommentTrivia);
+ var index = triviaList.IndexOf(SyntaxKind.SingleLineDocumentationCommentTrivia);
- int currentLineStart = index + 1;
- bool onBlankLine = true;
- for (int currentIndex = currentLineStart; currentIndex < triviaList.Count; currentIndex++)
- {
- switch (triviaList[currentIndex].Kind())
- {
- case SyntaxKind.EndOfLineTrivia:
- if (onBlankLine)
- {
- triviaList = triviaList.RemoveRange(currentLineStart, currentIndex - currentLineStart + 1);
- currentIndex = currentLineStart - 1;
- continue;
- }
- else
+ int currentLineStart = index + 1;
+ bool onBlankLine = true;
+ for (int currentIndex = currentLineStart; currentIndex < triviaList.Count; currentIndex++)
{
- currentLineStart = currentIndex + 1;
- onBlankLine = true;
- break;
- }
+ switch (triviaList[currentIndex].Kind())
+ {
+ case SyntaxKind.EndOfLineTrivia:
+ if (onBlankLine)
+ {
+ triviaList = triviaList.RemoveRange(currentLineStart, currentIndex - currentLineStart + 1);
+ currentIndex = currentLineStart - 1;
+ continue;
+ }
+ else
+ {
+ currentLineStart = currentIndex + 1;
+ onBlankLine = true;
+ break;
+ }
- case SyntaxKind.WhitespaceTrivia:
- break;
+ case SyntaxKind.WhitespaceTrivia:
+ break;
- default:
- if (triviaList[currentIndex].HasBuiltinEndLine())
- {
- currentLineStart = currentIndex + 1;
- onBlankLine = true;
- break;
+ default:
+ if (triviaList[currentIndex].HasBuiltinEndLine())
+ {
+ currentLineStart = currentIndex + 1;
+ onBlankLine = true;
+ break;
+ }
+ else
+ {
+ onBlankLine = false;
+ break;
+ }
+ }
}
- else
- {
- onBlankLine = false;
- break;
- }
- }
- }
- var newSyntaxRoot = syntaxRoot.ReplaceToken(token, token.WithLeadingTrivia(triviaList));
- return document.WithSyntaxRoot(newSyntaxRoot);
+ return rewrittenToken.WithLeadingTrivia(triviaList);
+ });
+ }
+
+ private class FixAll : DocumentBasedFixAllProvider
+ {
+ public static FixAllProvider Instance { get; } =
+ new FixAll();
+
+ protected override string CodeActionTitle => LayoutResources.SA1506CodeFix;
+
+ protected override async Task FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray diagnostics)
+ => await GetTransformedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
}
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1507CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1507CodeFixProvider.cs
index 7c892ef63..142faed69 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1507CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1507CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1508CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1508CodeFixProvider.cs
index 771c5e653..81b56580d 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1508CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1508CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1509CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1509CodeFixProvider.cs
index 92cf21af6..26e937214 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1509CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1509CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
@@ -29,7 +31,7 @@ internal class SA1509CodeFixProvider : CodeFixProvider
///
public override FixAllProvider GetFixAllProvider()
{
- return CustomFixAllProviders.BatchFixer;
+ return FixAll.Instance;
}
///
@@ -40,7 +42,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
LayoutResources.SA1509CodeFix,
- token => this.GetTransformedDocumentAsync(context.Document, diagnostic, token),
+ token => GetTransformedDocumentAsync(context.Document, diagnostic, token),
nameof(SA1509CodeFixProvider)),
diagnostic);
}
@@ -48,25 +50,33 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
return SpecializedTasks.CompletedTask;
}
- private async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
+ private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
- var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
-
- var openBrace = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
- var leadingTrivia = openBrace.LeadingTrivia;
+ var newRoot = await GetTransformedDocumentAsync(document, ImmutableArray.Create(diagnostic), cancellationToken).ConfigureAwait(false);
+ return document.WithSyntaxRoot(newRoot);
+ }
- var newTriviaList = SyntaxFactory.TriviaList();
+ private static async Task GetTransformedDocumentAsync(Document document, ImmutableArray diagnostics, CancellationToken cancellationToken)
+ {
+ var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
+ return syntaxRoot.ReplaceTokens(
+ diagnostics.Select(diagnostic => syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start)),
+ (originalToken, rewrittenToken) =>
+ {
+ var openBrace = rewrittenToken;
+ var leadingTrivia = openBrace.LeadingTrivia;
- var previousEmptyLines = this.GetPreviousEmptyLines(openBrace).ToList();
- newTriviaList = newTriviaList.AddRange(leadingTrivia.Except(previousEmptyLines));
+ var newTriviaList = SyntaxFactory.TriviaList();
- var newOpenBrace = openBrace.WithLeadingTrivia(newTriviaList);
- var newSyntaxRoot = syntaxRoot.ReplaceToken(openBrace, newOpenBrace);
+ var previousEmptyLines = GetPreviousEmptyLines(openBrace).ToList();
+ newTriviaList = newTriviaList.AddRange(leadingTrivia.Except(previousEmptyLines));
- return document.WithSyntaxRoot(newSyntaxRoot);
+ var newOpenBrace = openBrace.WithLeadingTrivia(newTriviaList);
+ return newOpenBrace;
+ });
}
- private IEnumerable GetPreviousEmptyLines(SyntaxToken openBrace)
+ private static IEnumerable GetPreviousEmptyLines(SyntaxToken openBrace)
{
var result = new List();
@@ -92,5 +102,16 @@ private IEnumerable GetPreviousEmptyLines(SyntaxToken openBrace)
return result;
}
+
+ private class FixAll : DocumentBasedFixAllProvider
+ {
+ public static FixAllProvider Instance { get; } =
+ new FixAll();
+
+ protected override string CodeActionTitle => LayoutResources.SA1509CodeFix;
+
+ protected override async Task FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray diagnostics)
+ => await GetTransformedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
+ }
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1510CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1510CodeFixProvider.cs
index aa404b655..c265f1f63 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1510CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1510CodeFixProvider.cs
@@ -1,10 +1,13 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
using System.Collections.Immutable;
using System.Composition;
+ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
@@ -26,7 +29,7 @@ internal class SA1510CodeFixProvider : CodeFixProvider
///
public override FixAllProvider GetFixAllProvider()
{
- return CustomFixAllProviders.BatchFixer;
+ return FixAll.Instance;
}
///
@@ -46,13 +49,31 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
}
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
+ {
+ var newRoot = await GetTransformedDocumentAsync(document, ImmutableArray.Create(diagnostic), cancellationToken).ConfigureAwait(false);
+ return document.WithSyntaxRoot(newRoot);
+ }
+
+ private static async Task GetTransformedDocumentAsync(Document document, ImmutableArray diagnostics, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
+ return syntaxRoot.ReplaceTokens(
+ diagnostics.Select(diagnostic => syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start)),
+ (originalToken, rewrittenToken) =>
+ {
+ return rewrittenToken.WithoutLeadingBlankLines();
+ });
+ }
+
+ private class FixAll : DocumentBasedFixAllProvider
+ {
+ public static FixAllProvider Instance { get; } =
+ new FixAll();
- var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
+ protected override string CodeActionTitle => LayoutResources.SA1510CodeFix;
- var newSyntaxRoot = syntaxRoot.ReplaceToken(token, token.WithoutLeadingBlankLines());
- return document.WithSyntaxRoot(newSyntaxRoot);
+ protected override async Task FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray diagnostics)
+ => await GetTransformedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
}
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1511CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1511CodeFixProvider.cs
index e3ab05c10..0a67ff2c7 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1511CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1511CodeFixProvider.cs
@@ -1,10 +1,13 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
using System.Collections.Immutable;
using System.Composition;
+ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
@@ -26,7 +29,7 @@ internal class SA1511CodeFixProvider : CodeFixProvider
///
public override FixAllProvider GetFixAllProvider()
{
- return CustomFixAllProviders.BatchFixer;
+ return FixAll.Instance;
}
///
@@ -46,13 +49,31 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
}
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
+ {
+ var newRoot = await GetTransformedDocumentAsync(document, ImmutableArray.Create(diagnostic), cancellationToken).ConfigureAwait(false);
+ return document.WithSyntaxRoot(newRoot);
+ }
+
+ private static async Task GetTransformedDocumentAsync(Document document, ImmutableArray diagnostics, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
+ return syntaxRoot.ReplaceTokens(
+ diagnostics.Select(diagnostic => syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start)),
+ (originalToken, rewrittenToken) =>
+ {
+ return rewrittenToken.WithoutLeadingBlankLines();
+ });
+ }
+
+ private class FixAll : DocumentBasedFixAllProvider
+ {
+ public static FixAllProvider Instance { get; } =
+ new FixAll();
- var whileToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
+ protected override string CodeActionTitle => LayoutResources.SA1511CodeFix;
- var newSyntaxRoot = syntaxRoot.ReplaceToken(whileToken, whileToken.WithoutLeadingBlankLines());
- return document.WithSyntaxRoot(newSyntaxRoot);
+ protected override async Task FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray diagnostics)
+ => await GetTransformedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
}
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1512CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1512CodeFixProvider.cs
index 4737a3ce1..edbcea566 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1512CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1512CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1513CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1513CodeFixProvider.cs
index 80e81fd91..bea1d10e9 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1513CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1513CodeFixProvider.cs
@@ -1,10 +1,13 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
using System.Collections.Immutable;
using System.Composition;
+ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
@@ -30,7 +33,7 @@ internal class SA1513CodeFixProvider : CodeFixProvider
///
public override FixAllProvider GetFixAllProvider()
{
- return CustomFixAllProviders.BatchFixer;
+ return FixAll.Instance;
}
///
@@ -51,15 +54,32 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
- var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.End);
+ var newRoot = await GetTransformedDocumentAsync(document, ImmutableArray.Create(diagnostic), cancellationToken).ConfigureAwait(false);
+ return document.WithSyntaxRoot(newRoot);
+ }
+
+ private static async Task GetTransformedDocumentAsync(Document document, ImmutableArray diagnostics, CancellationToken cancellationToken)
+ {
+ var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
+ return root.ReplaceTokens(
+ diagnostics.Select(diagnostic => root.FindToken(diagnostic.Location.SourceSpan.End)),
+ (originalToken, rewrittenToken) =>
+ {
+ var endOfLineTrivia = rewrittenToken.GetPrecedingEndOfLineTrivia();
+ var newTrivia = rewrittenToken.LeadingTrivia.Insert(0, endOfLineTrivia);
+ return rewrittenToken.WithLeadingTrivia(newTrivia);
+ });
+ }
+
+ private class FixAll : DocumentBasedFixAllProvider
+ {
+ public static FixAllProvider Instance { get; } =
+ new FixAll();
- var newTrivia = token.LeadingTrivia.Insert(0, SyntaxFactory.CarriageReturnLineFeed);
- var newToken = token.WithLeadingTrivia(newTrivia);
- var newSyntaxRoot = syntaxRoot.ReplaceToken(token, newToken);
- var newDocument = document.WithSyntaxRoot(newSyntaxRoot);
+ protected override string CodeActionTitle => LayoutResources.SA1513CodeFix;
- return newDocument;
+ protected override async Task FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray diagnostics)
+ => await GetTransformedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
}
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1514CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1514CodeFixProvider.cs
index a50f9c3e8..80d604ffe 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1514CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1514CodeFixProvider.cs
@@ -1,10 +1,13 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
using System.Collections.Immutable;
using System.Composition;
+ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
@@ -27,7 +30,7 @@ internal class SA1514CodeFixProvider : CodeFixProvider
///
public override FixAllProvider GetFixAllProvider()
{
- return CustomFixAllProviders.BatchFixer;
+ return FixAll.Instance;
}
///
@@ -47,23 +50,44 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
}
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
+ {
+ var newRoot = await GetTransformedDocumentAsync(document, ImmutableArray.Create(diagnostic), cancellationToken).ConfigureAwait(false);
+ return document.WithSyntaxRoot(newRoot);
+ }
+
+ private static async Task GetTransformedDocumentAsync(Document document, ImmutableArray diagnostics, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var documentationHeader = syntaxRoot.FindTrivia(diagnostic.Location.SourceSpan.Start);
- var triviaList = documentationHeader.Token.LeadingTrivia;
- var documentationHeaderIndex = triviaList.IndexOf(documentationHeader);
+ var documentationHeaders = diagnostics.Select(diagnostic => syntaxRoot.FindTrivia(diagnostic.Location.SourceSpan.Start)).ToArray();
+ return syntaxRoot.ReplaceTokens(
+ documentationHeaders.Select(header => header.Token),
+ (originalToken, rewrittenToken) =>
+ {
+ var triviaList = rewrittenToken.LeadingTrivia;
+ var documentationHeaderIndex = originalToken.LeadingTrivia.IndexOf(originalToken.LeadingTrivia.First(documentationHeaders.Contains));
- // Keep any leading whitespace with the documentation header
- var index = documentationHeaderIndex - 1;
- while ((index >= 0) && triviaList[index].IsKind(SyntaxKind.WhitespaceTrivia))
- {
- index--;
- }
+ // Keep any leading whitespace with the documentation header
+ var index = documentationHeaderIndex - 1;
+ while ((index >= 0) && triviaList[index].IsKind(SyntaxKind.WhitespaceTrivia))
+ {
+ index--;
+ }
+
+ var newLeadingTrivia = rewrittenToken.LeadingTrivia.Insert(index + 1, SyntaxFactory.CarriageReturnLineFeed);
+ return rewrittenToken.WithLeadingTrivia(newLeadingTrivia);
+ });
+ }
+
+ private class FixAll : DocumentBasedFixAllProvider
+ {
+ public static FixAllProvider Instance { get; } =
+ new FixAll();
+
+ protected override string CodeActionTitle => LayoutResources.SA1514CodeFix;
- var newLeadingTrivia = documentationHeader.Token.LeadingTrivia.Insert(index + 1, SyntaxFactory.CarriageReturnLineFeed);
- var newSyntaxRoot = syntaxRoot.ReplaceToken(documentationHeader.Token, documentationHeader.Token.WithLeadingTrivia(newLeadingTrivia));
- return document.WithSyntaxRoot(newSyntaxRoot);
+ protected override async Task FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray diagnostics)
+ => await GetTransformedDocumentAsync(document, diagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
}
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1515CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1515CodeFixProvider.cs
index 633dee914..d4d914a32 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1515CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1515CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1516CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1516CodeFixProvider.cs
index bb87c6d4e..b8d7940fa 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1516CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1516CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
@@ -78,6 +80,9 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
private static Task GetTransformedDocumentAsync(Document document, SyntaxNode syntaxRoot, Diagnostic diagnostic, bool insertBlankLine, CancellationToken cancellationToken)
{
+ // Currently unused
+ _ = cancellationToken;
+
var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
node = GetRelevantNode(node);
@@ -142,6 +147,16 @@ private static SyntaxNode GetRelevantNode(SyntaxNode innerNode)
return currentNode;
}
+ if (currentNode is AttributeListSyntax)
+ {
+ return currentNode;
+ }
+
+ if (currentNode is ExternAliasDirectiveSyntax)
+ {
+ return currentNode;
+ }
+
currentNode = currentNode.Parent;
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1517CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1517CodeFixProvider.cs
index c53240cce..9f13ba80c 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1517CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1517CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1518CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1518CodeFixProvider.cs
index 59f3d40f7..6ad31f1db 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1518CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1518CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.LayoutRules
{
@@ -32,9 +34,10 @@ public override FixAllProvider GetFixAllProvider()
}
///
- public override Task RegisterCodeFixesAsync(CodeFixContext context)
+ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
- var settings = SettingsHelper.GetStyleCopSettings(context.Document.Project.AnalyzerOptions, context.CancellationToken);
+ var syntaxTree = await context.Document.GetSyntaxTreeAsync(context.CancellationToken).ConfigureAwait(false);
+ var settings = SettingsHelper.GetStyleCopSettings(context.Document.Project.AnalyzerOptions, syntaxTree, context.CancellationToken);
foreach (var diagnostic in context.Diagnostics)
{
context.RegisterCodeFix(
@@ -44,8 +47,6 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
nameof(SA1518CodeFixProvider)),
diagnostic);
}
-
- return SpecializedTasks.CompletedTask;
}
///
@@ -78,7 +79,8 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
return null;
}
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
+ var syntaxTree = await document.GetSyntaxTreeAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxTree, fixAllContext.CancellationToken);
Document updatedDocument = await FixEndOfFileAsync(document, diagnostics[0], settings.LayoutRules.NewlineAtEndOfFile, fixAllContext.CancellationToken).ConfigureAwait(false);
return await updatedDocument.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1119CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1119CodeFixProvider.cs
index f5959c1a0..0c2034f1c 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1119CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1119CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
@@ -74,7 +76,8 @@ private static SyntaxNode GetReplacement(ParenthesizedExpressionSyntax oldNode)
if (!leadingTrivia.Any())
{
var previousToken = oldNode.OpenParenToken.GetPreviousToken();
- if (TriviaHelper.IndexOfTrailingWhitespace(previousToken.TrailingTrivia) == -1)
+ if (!(previousToken.IsKind(SyntaxKind.OpenParenToken) || previousToken.IsKind(SyntaxKind.CloseParenToken))
+ && (TriviaHelper.IndexOfTrailingWhitespace(previousToken.TrailingTrivia) == -1))
{
leadingTrivia = SyntaxFactory.TriviaList(SyntaxFactory.Space);
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1400CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1400CodeFixProvider.cs
index b09b6561e..eb895617d 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1400CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1400CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
@@ -13,6 +15,7 @@ namespace StyleCop.Analyzers.MaintainabilityRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
///
/// Implements a code fix for .
@@ -82,6 +85,11 @@ private static Task GetTransformedDocumentAsync(Document document, Syn
updatedDeclarationNode = HandleStructDeclaration((StructDeclarationSyntax)declarationNode);
break;
+ case SyntaxKindEx.RecordDeclaration:
+ case SyntaxKindEx.RecordStructDeclaration:
+ updatedDeclarationNode = HandleRecordDeclaration((RecordDeclarationSyntaxWrapper)declarationNode);
+ break;
+
case SyntaxKind.DelegateDeclaration:
updatedDeclarationNode = HandleDelegateDeclaration((DelegateDeclarationSyntax)declarationNode);
break;
@@ -194,6 +202,23 @@ private static SyntaxNode HandleStructDeclaration(StructDeclarationSyntax node)
.WithoutFormatting();
}
+ private static SyntaxNode HandleRecordDeclaration(RecordDeclarationSyntaxWrapper node)
+ {
+ SyntaxToken triviaToken = node.Keyword;
+ if (triviaToken.IsMissing)
+ {
+ return null;
+ }
+
+ SyntaxKind defaultVisibility = IsNestedType(node) ? SyntaxKind.PrivateKeyword : SyntaxKind.InternalKeyword;
+ SyntaxTokenList modifiers = DeclarationModifiersHelper.AddModifier(node.Modifiers, ref triviaToken, defaultVisibility);
+ return node
+ .WithKeyword(triviaToken)
+ .WithModifiers(modifiers)
+ .SyntaxNode
+ .WithoutFormatting();
+ }
+
private static SyntaxNode HandleDelegateDeclaration(DelegateDeclarationSyntax node)
{
SyntaxToken triviaToken = node.DelegateKeyword;
@@ -355,6 +380,8 @@ private static SyntaxNode FindParentDeclarationNode(SyntaxNode node)
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.StructDeclaration:
+ case SyntaxKindEx.RecordDeclaration:
+ case SyntaxKindEx.RecordStructDeclaration:
case SyntaxKind.DelegateDeclaration:
case SyntaxKind.EventDeclaration:
case SyntaxKind.EventFieldDeclaration:
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1402CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1402CodeFixProvider.cs
index 9eb16a898..2f1d0740e 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1402CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1402CodeFixProvider.cs
@@ -1,8 +1,11 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
+ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
@@ -14,6 +17,7 @@ namespace StyleCop.Analyzers.MaintainabilityRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
///
/// Implements a code fix for .
@@ -64,7 +68,7 @@ private static async Task GetTransformedSolutionAsync(Document documen
DocumentId extractedDocumentId = DocumentId.CreateNewId(document.Project.Id);
string suffix;
FileNameHelpers.GetFileNameAndSuffix(document.Name, out suffix);
- var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(cancellationToken);
+ var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(root.SyntaxTree, cancellationToken);
string extractedDocumentName = FileNameHelpers.GetConventionalFileName(memberDeclarationSyntax, settings.DocumentationRules.FileNamingConvention) + suffix;
List nodesToRemoveFromExtracted = new List();
@@ -86,9 +90,15 @@ private static async Task GetTransformedSolutionAsync(Document documen
case SyntaxKind.InterfaceDeclaration:
case SyntaxKind.EnumDeclaration:
case SyntaxKind.DelegateDeclaration:
+ case SyntaxKindEx.RecordDeclaration:
+ case SyntaxKindEx.RecordStructDeclaration:
nodesToRemoveFromExtracted.Add(child);
break;
+ case SyntaxKindEx.FileScopedNamespaceDeclaration:
+ // Only one file-scoped namespace is allowed per syntax tree
+ throw new InvalidOperationException("This location is not reachable");
+
default:
break;
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1404CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1404CodeFixProvider.cs
index 1b85242ad..eda639640 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1404CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1404CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
@@ -50,7 +52,8 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
CodeAction.Create(
MaintainabilityResources.SA1404CodeFix,
token => AddJustificationToAttributeAsync(context.Document, root, attribute),
- nameof(SA1404CodeFixProvider) + "-Add"), diagnostic);
+ nameof(SA1404CodeFixProvider) + "-Add"),
+ diagnostic);
return;
}
@@ -60,7 +63,8 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
CodeAction.Create(
MaintainabilityResources.SA1404CodeFix,
token => UpdateValueOfArgumentAsync(context.Document, root, argument),
- nameof(SA1404CodeFixProvider) + "-Update"), diagnostic);
+ nameof(SA1404CodeFixProvider) + "-Update"),
+ diagnostic);
return;
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1407SA1408CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1407SA1408CodeFixProvider.cs
index 508be3b72..f4dd7a710 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1407SA1408CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1407SA1408CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1407SA1408FixAllProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1407SA1408FixAllProvider.cs
index 816d1923b..79a549386 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1407SA1408FixAllProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1407SA1408FixAllProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
@@ -37,14 +39,14 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
nodes.Add(node);
}
- return root.ReplaceNodes(nodes, (originalNode, rewrittenNode) => AddParentheses(originalNode, rewrittenNode));
+ return root.ReplaceNodes(nodes, (originalNode, rewrittenNode) => AddParentheses(rewrittenNode));
}
- private static SyntaxNode AddParentheses(SyntaxNode originalNode, SyntaxNode rewrittenNode)
+ private static SyntaxNode AddParentheses(SyntaxNode node)
{
- if (!(rewrittenNode is BinaryExpressionSyntax syntax))
+ if (!(node is BinaryExpressionSyntax syntax))
{
- return rewrittenNode;
+ return node;
}
BinaryExpressionSyntax trimmedSyntax = syntax.WithoutTrivia();
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1410SA1411CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1410SA1411CodeFixProvider.cs
index 5d4581233..44baa92dd 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1410SA1411CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1410SA1411CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1412CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1412CodeFixProvider.cs
index 2b63d8a02..e27a9499c 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1412CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1412CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1412FixAllProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1412FixAllProvider.cs
index 1383aab17..7acba25b7 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1412FixAllProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1412FixAllProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1413CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1413CodeFixProvider.cs
index d77894a99..c1bafdacf 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1413CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/MaintainabilityRules/SA1413CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.MaintainabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToLowerCaseCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToLowerCaseCodeFixProvider.cs
index ce4b87bf3..8356c0a84 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToLowerCaseCodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToLowerCaseCodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.NamingRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToUpperCaseCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToUpperCaseCodeFixProvider.cs
index 64da69570..e7581fc74 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToUpperCaseCodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/RenameToUpperCaseCodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.NamingRules
{
@@ -14,6 +16,7 @@ namespace StyleCop.Analyzers.NamingRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
///
/// Implements a code fix for all analyzers that require a symbol to be upper case.
@@ -56,14 +59,20 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var token = root.FindToken(diagnostic.Location.SourceSpan.Start);
var tokenText = token.ValueText.TrimStart('_');
+ if (tokenText == string.Empty)
+ {
+ // Skip this one, since we can't create a new identifier from this
+ continue;
+ }
+
var baseName = char.ToUpper(tokenText[0]) + tokenText.Substring(1);
var newName = baseName;
var memberSyntax = RenameHelper.GetParentDeclaration(token);
- if (memberSyntax is NamespaceDeclarationSyntax)
+ if (BaseNamespaceDeclarationSyntaxWrapper.IsInstance(memberSyntax))
{
// namespaces are not symbols. So we are just renaming the namespace
- Func> renameNamespace = cancellationToken =>
+ Task RenameNamespace(CancellationToken cancellationToken)
{
IdentifierNameSyntax identifierSyntax = (IdentifierNameSyntax)token.Parent;
@@ -71,12 +80,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
var newRoot = root.ReplaceNode(identifierSyntax, newIdentifierSyntax);
return Task.FromResult(context.Document.WithSyntaxRoot(newRoot));
- };
+ }
context.RegisterCodeFix(
CodeAction.Create(
string.Format(NamingResources.RenameToCodeFix, newName),
- renameNamespace,
+ (Func>)RenameNamespace,
nameof(RenameToUpperCaseCodeFixProvider) + "_" + diagnostic.Id),
diagnostic);
}
@@ -96,7 +105,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
&& !await RenameHelper.IsValidNewMemberNameAsync(semanticModel, declaredSymbol, newName, context.CancellationToken).ConfigureAwait(false))
{
usedSuffix = true;
- newName = newName + Suffix;
+ newName += Suffix;
}
int index = 0;
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1302CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1302CodeFixProvider.cs
index bb8851555..e5cbfe84b 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1302CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1302CodeFixProvider.cs
@@ -1,11 +1,12 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.NamingRules
{
using System.Collections.Immutable;
using System.Composition;
- using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
@@ -28,6 +29,13 @@ internal class SA1302CodeFixProvider : CodeFixProvider
public override ImmutableArray FixableDiagnosticIds { get; } =
ImmutableArray.Create(SA1302InterfaceNamesMustBeginWithI.DiagnosticId);
+ ///
+ public override FixAllProvider GetFixAllProvider()
+ {
+ // Fix All is not yet supported
+ return null;
+ }
+
///
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1308CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1308CodeFixProvider.cs
index bbb090443..8b1992b4e 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1308CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1308CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.NamingRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1309CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1309CodeFixProvider.cs
index d25a67ba7..834134dfc 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1309CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1309CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.NamingRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1310CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1310CodeFixProvider.cs
index 64c32a6e7..f22d8a6ee 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1310CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1310CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.NamingRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1314CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1314CodeFixProvider.cs
index db5f324c7..63c2fa507 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1314CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1314CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.NamingRules
{
@@ -26,6 +28,13 @@ internal class SA1314CodeFixProvider : CodeFixProvider
public override ImmutableArray FixableDiagnosticIds { get; } =
ImmutableArray.Create(SA1314TypeParameterNamesMustBeginWithT.DiagnosticId);
+ ///
+ public override FixAllProvider GetFixAllProvider()
+ {
+ // Fix All is not yet supported
+ return null;
+ }
+
///
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1316CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1316CodeFixProvider.cs
new file mode 100644
index 000000000..b6377ef4a
--- /dev/null
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SA1316CodeFixProvider.cs
@@ -0,0 +1,65 @@
+// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
+
+namespace StyleCop.Analyzers.NamingRules
+{
+ using System.Collections.Immutable;
+ using System.Composition;
+ using System.Threading;
+ using System.Threading.Tasks;
+ using Microsoft.CodeAnalysis;
+ using Microsoft.CodeAnalysis.CodeActions;
+ using Microsoft.CodeAnalysis.CodeFixes;
+ using Microsoft.CodeAnalysis.CSharp;
+ using StyleCop.Analyzers.Helpers;
+
+ ///
+ /// Implements a code fix for .
+ ///
+ [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SA1316CodeFixProvider))]
+ [Shared]
+ internal class SA1316CodeFixProvider : CodeFixProvider
+ {
+ ///
+ public override ImmutableArray FixableDiagnosticIds { get; } =
+ ImmutableArray.Create(SA1316TupleElementNamesShouldUseCorrectCasing.DiagnosticId);
+
+ ///
+ public override FixAllProvider GetFixAllProvider()
+ {
+ // Fix All is not yet supported
+ return null;
+ }
+
+ ///
+ public override Task RegisterCodeFixesAsync(CodeFixContext context)
+ {
+ foreach (var diagnostic in context.Diagnostics)
+ {
+ if (diagnostic.Properties.TryGetValue(SA1316TupleElementNamesShouldUseCorrectCasing.ExpectedTupleElementNameKey, out string fixedTupleElementName))
+ {
+ context.RegisterCodeFix(
+ CodeAction.Create(
+ NamingResources.SA1316CodeFix,
+ cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, fixedTupleElementName, cancellationToken),
+ nameof(SA1316CodeFixProvider)),
+ diagnostic);
+ }
+ }
+
+ return SpecializedTasks.CompletedTask;
+ }
+
+ private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, string fixedTupleElementName, CancellationToken cancellationToken)
+ {
+ var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
+
+ var identifierToken = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
+
+ var newSyntaxRoot = syntaxRoot.ReplaceToken(identifierToken, SyntaxFactory.Identifier(fixedTupleElementName).WithTriviaFrom(identifierToken));
+ return document.WithSyntaxRoot(newSyntaxRoot);
+ }
+ }
+}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SX1309CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SX1309CodeFixProvider.cs
index 97b2f4445..8e0563f3b 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SX1309CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/NamingRules/SX1309CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.NamingRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs
index f1285613b..808f1bd2a 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/ElementOrderCodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.OrderingRules
{
@@ -15,6 +17,7 @@ namespace StyleCop.Analyzers.OrderingRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.Settings.ObjectModel;
///
@@ -57,9 +60,9 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
- var elementOrder = settings.OrderingRules.ElementOrder;
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
+ var elementOrder = settings.OrderingRules.ElementOrder;
var memberDeclaration = syntaxRoot.FindNode(diagnostic.Location.SourceSpan).FirstAncestorOrSelf();
if (memberDeclaration == null)
@@ -82,9 +85,9 @@ private static SyntaxNode UpdateSyntaxRoot(MemberDeclarationSyntax memberDeclara
return HandleTypeDeclaration(memberToMove, (TypeDeclarationSyntax)parentDeclaration, elementOrder, syntaxRoot, indentationSettings);
}
- if (parentDeclaration is NamespaceDeclarationSyntax)
+ if (BaseNamespaceDeclarationSyntaxWrapper.IsInstance(parentDeclaration))
{
- return HandleNamespaceDeclaration(memberToMove, (NamespaceDeclarationSyntax)parentDeclaration, elementOrder, syntaxRoot, indentationSettings);
+ return HandleBaseNamespaceDeclaration(memberToMove, (BaseNamespaceDeclarationSyntaxWrapper)parentDeclaration, elementOrder, syntaxRoot, indentationSettings);
}
if (parentDeclaration is CompilationUnitSyntax)
@@ -105,7 +108,7 @@ private static SyntaxNode HandleCompilationUnitDeclaration(MemberOrderHelper mem
return OrderMember(memberOrder, compilationUnitDeclaration.Members, elementOrder, syntaxRoot, indentationSettings);
}
- private static SyntaxNode HandleNamespaceDeclaration(MemberOrderHelper memberOrder, NamespaceDeclarationSyntax namespaceDeclaration, ImmutableArray elementOrder, SyntaxNode syntaxRoot, IndentationSettings indentationSettings)
+ private static SyntaxNode HandleBaseNamespaceDeclaration(MemberOrderHelper memberOrder, BaseNamespaceDeclarationSyntaxWrapper namespaceDeclaration, ImmutableArray elementOrder, SyntaxNode syntaxRoot, IndentationSettings indentationSettings)
{
return OrderMember(memberOrder, namespaceDeclaration.Members, elementOrder, syntaxRoot, indentationSettings);
}
@@ -113,7 +116,7 @@ private static SyntaxNode HandleNamespaceDeclaration(MemberOrderHelper memberOrd
private static SyntaxNode OrderMember(MemberOrderHelper memberOrder, SyntaxList members, ImmutableArray elementOrder, SyntaxNode syntaxRoot, IndentationSettings indentationSettings)
{
var memberIndex = members.IndexOf(memberOrder.Member);
- MemberOrderHelper target = default(MemberOrderHelper);
+ MemberOrderHelper target = default;
for (var i = memberIndex - 1; i >= 0; --i)
{
@@ -263,9 +266,9 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
return null;
}
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
+ var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
var elementOrder = settings.OrderingRules.ElementOrder;
- var syntaxRoot = await document.GetSyntaxRootAsync().ConfigureAwait(false);
var trackedDiagnosticMembers = new List();
foreach (var diagnostic in diagnostics)
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1205CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1205CodeFixProvider.cs
index 094811b21..f58727a68 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1205CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1205CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.OrderingRules
{
@@ -13,6 +15,7 @@ namespace StyleCop.Analyzers.OrderingRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
///
/// Implements code fixes for .
@@ -25,6 +28,7 @@ internal class SA1205CodeFixProvider : CodeFixProvider
private static readonly ImmutableArray InternalAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.InternalKeyword);
private static readonly ImmutableArray ProtectedAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.ProtectedKeyword);
private static readonly ImmutableArray ProtectedOrInternalAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.ProtectedKeyword, SyntaxKind.InternalKeyword);
+ private static readonly ImmutableArray ProtectedAndInternalAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.PrivateKeyword, SyntaxKind.ProtectedKeyword);
private static readonly ImmutableArray PrivateAccessibilityKeywords = ImmutableArray.Create(SyntaxKind.PrivateKeyword);
private static readonly ImmutableArray UnexpectedAccessibilityKeywords = ImmutableArray.Create();
@@ -88,6 +92,8 @@ private static ImmutableArray GetMissingAccessModifiers(Accessibilit
return ProtectedAccessibilityKeywords;
case Accessibility.ProtectedOrInternal:
return ProtectedOrInternalAccessibilityKeywords;
+ case Accessibility.ProtectedAndInternal:
+ return ProtectedAndInternalAccessibilityKeywords;
case Accessibility.Private:
return PrivateAccessibilityKeywords;
default:
@@ -108,6 +114,9 @@ private static TypeDeclarationSyntax ReplaceModifiers(TypeDeclarationSyntax node
return ((InterfaceDeclarationSyntax)node).WithModifiers(modifiers);
case SyntaxKind.StructDeclaration:
return ((StructDeclarationSyntax)node).WithModifiers(modifiers);
+ case SyntaxKindEx.RecordDeclaration:
+ case SyntaxKindEx.RecordStructDeclaration:
+ return ((RecordDeclarationSyntaxWrapper)node).WithModifiers(modifiers);
}
return node;
@@ -125,6 +134,9 @@ private static TypeDeclarationSyntax ReplaceKeyword(TypeDeclarationSyntax node,
return ((InterfaceDeclarationSyntax)node).WithKeyword(keyword);
case SyntaxKind.StructDeclaration:
return ((StructDeclarationSyntax)node).WithKeyword(keyword);
+ case SyntaxKindEx.RecordDeclaration:
+ case SyntaxKindEx.RecordStructDeclaration:
+ return ((RecordDeclarationSyntaxWrapper)node).WithKeyword(keyword);
}
return node;
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1206CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1206CodeFixProvider.cs
index 04b749dd1..e3a9a2b8c 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1206CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1206CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.OrderingRules
{
@@ -95,7 +97,8 @@ private static SyntaxTokenList FullySortModifiers(SyntaxTokenList modifiers)
return AdjustTrivia(
accessModifiers
.Concat(staticModifiers)
- .Concat(otherModifiers), modifiers);
+ .Concat(otherModifiers),
+ modifiers);
}
///
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1207CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1207CodeFixProvider.cs
index 9e0a427e7..9f01e64a0 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1207CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1207CodeFixProvider.cs
@@ -1,10 +1,13 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.OrderingRules
{
using System.Collections.Immutable;
using System.Composition;
+ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
@@ -59,21 +62,23 @@ private static async Task GetTransformedDocumentAsync(Document documen
return document;
}
- var newDeclarationNode = originalDeclarationNode.ReplaceTokens(childTokens, ComputeReplacementToken);
+ bool hasInternalKeyword = childTokens.Any(token => token.IsKind(SyntaxKind.InternalKeyword));
+ var newDeclarationNode = originalDeclarationNode.ReplaceTokens(childTokens, (originalToken, rewrittenToken) => ComputeReplacementToken(originalToken, rewrittenToken, hasInternalKeyword));
var newSyntaxRoot = syntaxRoot.ReplaceNode(originalDeclarationNode, newDeclarationNode);
return document.WithSyntaxRoot(newSyntaxRoot);
}
- private static SyntaxToken ComputeReplacementToken(SyntaxToken originalToken, SyntaxToken rewrittenToken)
+ private static SyntaxToken ComputeReplacementToken(SyntaxToken originalToken, SyntaxToken rewrittenToken, bool hasInternalKeyword)
{
- if (originalToken.IsKind(SyntaxKind.InternalKeyword))
+ if (originalToken.IsKind(SyntaxKind.InternalKeyword)
+ || originalToken.IsKind(SyntaxKind.PrivateKeyword))
{
return SyntaxFactory.Token(SyntaxKind.ProtectedKeyword).WithTriviaFrom(rewrittenToken);
}
else if (originalToken.IsKind(SyntaxKind.ProtectedKeyword))
{
- return SyntaxFactory.Token(SyntaxKind.InternalKeyword).WithTriviaFrom(rewrittenToken);
+ return SyntaxFactory.Token(hasInternalKeyword ? SyntaxKind.InternalKeyword : SyntaxKind.PrivateKeyword).WithTriviaFrom(rewrittenToken);
}
else
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1212SA1213CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1212SA1213CodeFixProvider.cs
index b69d89af7..c7df8d124 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1212SA1213CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/SA1212SA1213CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.OrderingRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.SourceMap.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.SourceMap.cs
index 3acb210e0..364680b6b 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.SourceMap.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.SourceMap.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.OrderingRules
{
@@ -10,6 +12,7 @@ namespace StyleCop.Analyzers.OrderingRules
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
+ using StyleCop.Analyzers.Lightup;
///
/// Implements a code fix for all misaligned using statements.
@@ -123,12 +126,12 @@ internal TreeTextSpan GetContainingSpan(SyntaxNode node)
private static void ProcessNodeMembers(TreeTextSpan.Builder builder, SyntaxList members)
{
- foreach (var namespaceDeclaration in members.OfType())
+ foreach (var namespaceDeclaration in members.Where(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member)))
{
var childBuilder = builder.AddChild(namespaceDeclaration.FullSpan.Start);
childBuilder.SetEnd(namespaceDeclaration.FullSpan.End);
- ProcessNodeMembers(childBuilder, namespaceDeclaration.Members);
+ ProcessNodeMembers(childBuilder, ((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Members);
}
}
@@ -146,7 +149,7 @@ private static void BuildDirectiveTriviaMaps(CompilationUnitSyntax compilationUn
switch (directiveTrivia.Kind())
{
case SyntaxKind.IfDirectiveTrivia:
- AddNewDirectiveTriviaSpan(conditionalBuilder, conditionalStack, directiveTrivia);
+ AddNewDirectiveTriviaSpan(conditionalStack, directiveTrivia);
break;
case SyntaxKind.ElifDirectiveTrivia:
@@ -154,19 +157,19 @@ private static void BuildDirectiveTriviaMaps(CompilationUnitSyntax compilationUn
var previousSpan = conditionalStack.Pop();
previousSpan.SetEnd(directiveTrivia.FullSpan.Start);
- AddNewDirectiveTriviaSpan(conditionalBuilder, conditionalStack, directiveTrivia);
+ AddNewDirectiveTriviaSpan(conditionalStack, directiveTrivia);
break;
case SyntaxKind.EndIfDirectiveTrivia:
- CloseDirectiveTriviaSpan(conditionalBuilder, conditionalStack, directiveTrivia);
+ CloseDirectiveTriviaSpan(conditionalStack, directiveTrivia);
break;
case SyntaxKind.RegionDirectiveTrivia:
- AddNewDirectiveTriviaSpan(regionBuilder, regionStack, directiveTrivia);
+ AddNewDirectiveTriviaSpan(regionStack, directiveTrivia);
break;
case SyntaxKind.EndRegionDirectiveTrivia:
- CloseDirectiveTriviaSpan(regionBuilder, regionStack, directiveTrivia);
+ CloseDirectiveTriviaSpan(regionStack, directiveTrivia);
break;
case SyntaxKind.PragmaWarningDirectiveTrivia:
@@ -192,14 +195,14 @@ private static TreeTextSpan.Builder SetupBuilder(CompilationUnitSyntax compilati
return rootBuilder;
}
- private static void AddNewDirectiveTriviaSpan(TreeTextSpan.Builder spanBuilder, Stack spanStack, DirectiveTriviaSyntax directiveTrivia)
+ private static void AddNewDirectiveTriviaSpan(Stack spanStack, DirectiveTriviaSyntax directiveTrivia)
{
var parent = spanStack.Peek();
var newDirectiveSpan = parent.AddChild(directiveTrivia.FullSpan.Start);
spanStack.Push(newDirectiveSpan);
}
- private static void CloseDirectiveTriviaSpan(TreeTextSpan.Builder spanBuilder, Stack spanStack, DirectiveTriviaSyntax directiveTrivia)
+ private static void CloseDirectiveTriviaSpan(Stack spanStack, DirectiveTriviaSyntax directiveTrivia)
{
var previousSpan = spanStack.Pop();
previousSpan.SetEnd(directiveTrivia.FullSpan.End);
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.TreeTextSpan.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.TreeTextSpan.cs
index 37c5634dc..8abd78c18 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.TreeTextSpan.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.TreeTextSpan.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.OrderingRules
{
@@ -158,7 +160,7 @@ internal TreeTextSpan GetContainingSpan(TextSpan textSpan)
internal class Builder
{
private readonly List children = new List();
- private int start;
+ private readonly int start;
private int end = int.MaxValue;
///
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.UsingsSorter.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.UsingsSorter.cs
index 4d0b96f79..d2d2e865c 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.UsingsSorter.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.UsingsSorter.cs
@@ -1,18 +1,20 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.OrderingRules
{
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
- using System.Globalization;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.Settings.ObjectModel;
///
@@ -35,6 +37,7 @@ private class UsingsSorter
private readonly Dictionary> systemUsings = new Dictionary>();
private readonly Dictionary> namespaceUsings = new Dictionary>();
private readonly Dictionary> aliases = new Dictionary>();
+ private readonly Dictionary> systemStaticImports = new Dictionary>();
private readonly Dictionary> staticImports = new Dictionary>();
public UsingsSorter(StyleCopSettings settings, SemanticModel semanticModel, CompilationUnitSyntax compilationUnit, ImmutableArray fileHeader)
@@ -76,6 +79,11 @@ public List GetContainedUsings(TreeTextSpan directiveSpan)
result.AddRange(usingsList);
}
+ if (this.systemStaticImports.TryGetValue(directiveSpan, out usingsList))
+ {
+ result.AddRange(usingsList);
+ }
+
if (this.staticImports.TryGetValue(directiveSpan, out usingsList))
{
result.AddRange(usingsList);
@@ -84,13 +92,14 @@ public List GetContainedUsings(TreeTextSpan directiveSpan)
return result;
}
- public SyntaxList GenerateGroupedUsings(TreeTextSpan directiveSpan, string indentation, bool withTrailingBlankLine, bool qualifyNames)
+ public SyntaxList GenerateGroupedUsings(TreeTextSpan directiveSpan, string indentation, bool withLeadingBlankLine, bool withTrailingBlankLine, bool qualifyNames)
{
var usingList = new List();
List triviaToMove = new List();
usingList.AddRange(this.GenerateUsings(this.systemUsings, directiveSpan, indentation, triviaToMove, qualifyNames));
usingList.AddRange(this.GenerateUsings(this.namespaceUsings, directiveSpan, indentation, triviaToMove, qualifyNames));
+ usingList.AddRange(this.GenerateUsings(this.systemStaticImports, directiveSpan, indentation, triviaToMove, qualifyNames));
usingList.AddRange(this.GenerateUsings(this.staticImports, directiveSpan, indentation, triviaToMove, qualifyNames));
usingList.AddRange(this.GenerateUsings(this.aliases, directiveSpan, indentation, triviaToMove, qualifyNames));
@@ -100,6 +109,12 @@ public SyntaxList GenerateGroupedUsings(TreeTextSpan direc
usingList[0] = usingList[0].WithLeadingTrivia(newLeadingTrivia);
}
+ if (withLeadingBlankLine && usingList.Count > 0)
+ {
+ var firstUsing = usingList[0];
+ usingList[0] = firstUsing.WithLeadingTrivia(firstUsing.GetLeadingTrivia().Insert(0, SyntaxFactory.CarriageReturnLineFeed));
+ }
+
if (withTrailingBlankLine && (usingList.Count > 0))
{
var lastUsing = usingList[usingList.Count - 1];
@@ -109,13 +124,14 @@ public SyntaxList GenerateGroupedUsings(TreeTextSpan direc
return SyntaxFactory.List(usingList);
}
- public SyntaxList GenerateGroupedUsings(List usingsList, string indentation, bool withTrailingBlankLine, bool qualifyNames)
+ public SyntaxList GenerateGroupedUsings(List usingsList, string indentation, bool withLeadingBlankLine, bool withTrailingBlankLine, bool qualifyNames)
{
var usingList = new List();
List triviaToMove = new List();
usingList.AddRange(this.GenerateUsings(this.systemUsings, usingsList, indentation, triviaToMove, qualifyNames));
usingList.AddRange(this.GenerateUsings(this.namespaceUsings, usingsList, indentation, triviaToMove, qualifyNames));
+ usingList.AddRange(this.GenerateUsings(this.systemStaticImports, usingsList, indentation, triviaToMove, qualifyNames));
usingList.AddRange(this.GenerateUsings(this.staticImports, usingsList, indentation, triviaToMove, qualifyNames));
usingList.AddRange(this.GenerateUsings(this.aliases, usingsList, indentation, triviaToMove, qualifyNames));
@@ -125,6 +141,12 @@ public SyntaxList GenerateGroupedUsings(List 0)
+ {
+ var firstUsing = usingList[0];
+ usingList[0] = firstUsing.WithLeadingTrivia(firstUsing.GetLeadingTrivia().Insert(0, SyntaxFactory.CarriageReturnLineFeed));
+ }
+
if (withTrailingBlankLine && (usingList.Count > 0))
{
var lastUsing = usingList[usingList.Count - 1];
@@ -357,15 +379,19 @@ private UsingDirectiveSyntax QualifyUsingDirective(UsingDirectiveSyntax usingDir
rewrittenName = replacement.WithTriviaFrom(originalName);
break;
}
- else if (symbol is INamedTypeSymbol)
+ else if (symbol is INamedTypeSymbol namedTypeSymbol)
{
// TODO: Preserve inner trivia
// TODO: simplify after qualification
string fullName;
- if (SpecialTypeHelper.IsPredefinedType(((INamedTypeSymbol)symbol).OriginalDefinition.SpecialType))
+ if (SpecialTypeHelper.IsPredefinedType(namedTypeSymbol.OriginalDefinition.SpecialType))
{
fullName = "global::System." + symbol.Name;
}
+ else if (namedTypeSymbol.IsTupleType())
+ {
+ fullName = namedTypeSymbol.TupleUnderlyingTypeOrSelf().ToFullyQualifiedValueTupleDisplayString();
+ }
else
{
fullName = symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
@@ -419,22 +445,35 @@ private int CompareUsings(UsingDirectiveSyntax left, UsingDirectiveSyntax right)
return NameSyntaxHelpers.Compare(left.Name, right.Name);
}
+ private bool IsSeparatedStaticSystemUsing(UsingDirectiveSyntax syntax)
+ {
+ if (!this.separateSystemDirectives)
+ {
+ return false;
+ }
+
+ return this.StartsWithSystemUsingDirectiveIdentifier(syntax.Name);
+ }
+
private bool IsSeparatedSystemUsing(UsingDirectiveSyntax syntax)
{
if (!this.separateSystemDirectives
- || (syntax.Alias != null)
- || syntax.StaticKeyword.IsKind(SyntaxKind.StaticKeyword)
|| syntax.HasNamespaceAliasQualifier())
{
return false;
}
- if (!(this.semanticModel.GetSymbolInfo(syntax.Name).Symbol is INamespaceSymbol namespaceSymbol))
+ return this.StartsWithSystemUsingDirectiveIdentifier(syntax.Name);
+ }
+
+ private bool StartsWithSystemUsingDirectiveIdentifier(NameSyntax name)
+ {
+ if (!(this.semanticModel.GetSymbolInfo(name).Symbol is INamespaceOrTypeSymbol namespaceOrTypeSymbol))
{
return false;
}
- var namespaceTypeName = namespaceSymbol.ToDisplayString(FullNamespaceDisplayFormat);
+ var namespaceTypeName = namespaceOrTypeSymbol.ToDisplayString(FullNamespaceDisplayFormat);
var firstPart = namespaceTypeName.Split('.')[0];
return string.Equals(SystemUsingDirectiveIdentifier, firstPart, StringComparison.Ordinal);
@@ -442,10 +481,10 @@ private bool IsSeparatedSystemUsing(UsingDirectiveSyntax syntax)
private void ProcessMembers(SyntaxList members)
{
- foreach (var namespaceDeclaration in members.OfType())
+ foreach (var namespaceDeclaration in members.Where(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member)))
{
- this.ProcessUsingDirectives(namespaceDeclaration.Usings);
- this.ProcessMembers(namespaceDeclaration.Members);
+ this.ProcessUsingDirectives(((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Usings);
+ this.ProcessMembers(((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Members);
}
}
@@ -459,9 +498,16 @@ private void ProcessUsingDirectives(SyntaxList usingDirect
{
this.AddUsingDirective(this.aliases, usingDirective, containingSpan);
}
- else if (!usingDirective.StaticKeyword.IsKind(SyntaxKind.None))
+ else if (usingDirective.StaticKeyword.IsKind(SyntaxKind.StaticKeyword))
{
- this.AddUsingDirective(this.staticImports, usingDirective, containingSpan);
+ if (this.IsSeparatedStaticSystemUsing(usingDirective))
+ {
+ this.AddUsingDirective(this.systemStaticImports, usingDirective, containingSpan);
+ }
+ else
+ {
+ this.AddUsingDirective(this.staticImports, usingDirective, containingSpan);
+ }
}
else if (this.IsSeparatedSystemUsing(usingDirective))
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs
index b824d3ce9..93366215b 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.OrderingRules
{
@@ -16,6 +18,7 @@ namespace StyleCop.Analyzers.OrderingRules
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
using StyleCop.Analyzers.Settings.ObjectModel;
///
@@ -29,7 +32,9 @@ internal sealed partial class UsingCodeFixProvider : CodeFixProvider
private static readonly List EmptyUsingsList = new List();
private static readonly SyntaxAnnotation UsingCodeFixAnnotation = new SyntaxAnnotation(nameof(UsingCodeFixAnnotation));
- private static readonly SymbolDisplayFormat FullNamespaceDisplayFormat = SymbolDisplayFormat.FullyQualifiedFormat.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);
+ private static readonly SymbolDisplayFormat FullNamespaceDisplayFormat = SymbolDisplayFormat.FullyQualifiedFormat
+ .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)
+ .WithMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers);
///
public override ImmutableArray FixableDiagnosticIds { get; } =
@@ -78,8 +83,8 @@ private static async Task GetTransformedDocumentAsync(Document documen
var fileHeader = GetFileHeader(syntaxRoot);
var compilationUnit = (CompilationUnitSyntax)syntaxRoot;
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, semanticModel.SyntaxTree, cancellationToken);
var usingDirectivesPlacement = forcePreservePlacement ? UsingDirectivesPlacement.Preserve : DeterminePlacement(compilationUnit, settings);
var usingsHelper = new UsingsSorter(settings, semanticModel, compilationUnit, fileHeader);
@@ -132,9 +137,14 @@ private static string DetermineIndentation(CompilationUnitSyntax compilationUnit
if (usingDirectivesPlacement == UsingDirectivesPlacement.InsideNamespace)
{
- var rootNamespace = compilationUnit.Members.OfType().First();
+ var rootNamespace = compilationUnit.Members.First(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member));
var indentationLevel = IndentationHelper.GetIndentationSteps(indentationSettings, rootNamespace);
- usingsIndentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationLevel + 1);
+ if (!rootNamespace.IsKind(SyntaxKindEx.FileScopedNamespaceDeclaration))
+ {
+ indentationLevel++;
+ }
+
+ usingsIndentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationLevel);
}
else
{
@@ -183,9 +193,9 @@ private static int CountNamespaces(SyntaxList members)
{
var result = 0;
- foreach (var namespaceDeclaration in members.OfType())
+ foreach (var namespaceDeclaration in members.Where(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member)))
{
- result += 1 + CountNamespaces(namespaceDeclaration.Members);
+ result += 1 + CountNamespaces(((BaseNamespaceDeclarationSyntaxWrapper)namespaceDeclaration).Members);
}
return result;
@@ -217,8 +227,9 @@ private static void BuildReplaceMapForNamespaces(UsingsSorter usingsHelper, Dict
}
var indentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationSteps);
+ var withLeadingBlankLine = usingList[0].Parent.IsKind(SyntaxKindEx.FileScopedNamespaceDeclaration);
- var modifiedUsings = usingsHelper.GenerateGroupedUsings(usingList, indentation, false, qualifyNames);
+ var modifiedUsings = usingsHelper.GenerateGroupedUsings(usingList, indentation, withLeadingBlankLine, withTrailingBlankLine: false, qualifyNames);
for (var i = 0; i < usingList.Count; i++)
{
@@ -246,7 +257,7 @@ private static void BuildReplaceMapForConditionalDirectives(UsingsSorter usingsH
var indentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationSteps);
- var modifiedUsings = usingsHelper.GenerateGroupedUsings(childSpan, indentation, false, qualifyNames: false);
+ var modifiedUsings = usingsHelper.GenerateGroupedUsings(childSpan, indentation, false, false, qualifyNames: false);
for (var i = 0; i < originalUsings.Count; i++)
{
@@ -265,10 +276,11 @@ private static int CompareSpanStart(UsingDirectiveSyntax left, UsingDirectiveSyn
private static SyntaxNode AddUsingsToNamespace(SyntaxNode newSyntaxRoot, UsingsSorter usingsHelper, string usingsIndentation, bool hasConditionalDirectives)
{
- var rootNamespace = ((CompilationUnitSyntax)newSyntaxRoot).Members.OfType().First();
+ var rootNamespace = (BaseNamespaceDeclarationSyntaxWrapper)((CompilationUnitSyntax)newSyntaxRoot).Members.First(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member));
+ var withLeadingBlankLine = rootNamespace.SyntaxNode.IsKind(SyntaxKindEx.FileScopedNamespaceDeclaration);
var withTrailingBlankLine = hasConditionalDirectives || rootNamespace.Members.Any() || rootNamespace.Externs.Any();
- var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withTrailingBlankLine, qualifyNames: false);
+ var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withLeadingBlankLine, withTrailingBlankLine, qualifyNames: false);
groupedUsings = groupedUsings.AddRange(rootNamespace.Usings);
var newRootNamespace = rootNamespace.WithUsings(groupedUsings);
@@ -282,7 +294,7 @@ private static SyntaxNode AddUsingsToCompilationRoot(SyntaxNode newSyntaxRoot, U
var newCompilationUnit = (CompilationUnitSyntax)newSyntaxRoot;
var withTrailingBlankLine = hasConditionalDirectives || newCompilationUnit.AttributeLists.Any() || newCompilationUnit.Members.Any() || newCompilationUnit.Externs.Any();
- var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withTrailingBlankLine, qualifyNames: true);
+ var groupedUsings = usingsHelper.GenerateGroupedUsings(TreeTextSpan.Empty, usingsIndentation, withLeadingBlankLine: false, withTrailingBlankLine, qualifyNames: true);
groupedUsings = groupedUsings.AddRange(newCompilationUnit.Usings);
newSyntaxRoot = newCompilationUnit.WithUsings(groupedUsings);
@@ -478,7 +490,7 @@ public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
{
if (this.fileHeader.Contains(trivia))
{
- return default(SyntaxTrivia);
+ return default;
}
return base.VisitTrivia(trivia);
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Properties/AssemblyInfo.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Properties/AssemblyInfo.cs
index b8f32f289..05d80f9a6 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Properties/AssemblyInfo.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Properties/AssemblyInfo.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
using System;
using System.Runtime.CompilerServices;
@@ -14,4 +16,9 @@
[assembly: InternalsVisibleTo("StyleCop.Analyzers.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c36d40d996fcc95fb6a89754728616758f459026e31478ce93633b3e27a4af416f103aa3d7a9e7998f829f8715cc1240d30724fd662042550fa71357b19562622424267e9e4640c403edbe64709a9ca5918128a9b9020b0db6e770d0dd1eac888869c23a835b74bde00e171984b1d1c24636cf030f0b23106e73035a2be145a6")]
[assembly: InternalsVisibleTo("StyleCop.Analyzers.Test.CSharp7, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c36d40d996fcc95fb6a89754728616758f459026e31478ce93633b3e27a4af416f103aa3d7a9e7998f829f8715cc1240d30724fd662042550fa71357b19562622424267e9e4640c403edbe64709a9ca5918128a9b9020b0db6e770d0dd1eac888869c23a835b74bde00e171984b1d1c24636cf030f0b23106e73035a2be145a6")]
+[assembly: InternalsVisibleTo("StyleCop.Analyzers.Test.CSharp8, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c36d40d996fcc95fb6a89754728616758f459026e31478ce93633b3e27a4af416f103aa3d7a9e7998f829f8715cc1240d30724fd662042550fa71357b19562622424267e9e4640c403edbe64709a9ca5918128a9b9020b0db6e770d0dd1eac888869c23a835b74bde00e171984b1d1c24636cf030f0b23106e73035a2be145a6")]
+[assembly: InternalsVisibleTo("StyleCop.Analyzers.Test.CSharp9, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c36d40d996fcc95fb6a89754728616758f459026e31478ce93633b3e27a4af416f103aa3d7a9e7998f829f8715cc1240d30724fd662042550fa71357b19562622424267e9e4640c403edbe64709a9ca5918128a9b9020b0db6e770d0dd1eac888869c23a835b74bde00e171984b1d1c24636cf030f0b23106e73035a2be145a6")]
+[assembly: InternalsVisibleTo("StyleCop.Analyzers.Test.CSharp10, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c36d40d996fcc95fb6a89754728616758f459026e31478ce93633b3e27a4af416f103aa3d7a9e7998f829f8715cc1240d30724fd662042550fa71357b19562622424267e9e4640c403edbe64709a9ca5918128a9b9020b0db6e770d0dd1eac888869c23a835b74bde00e171984b1d1c24636cf030f0b23106e73035a2be145a6")]
+[assembly: InternalsVisibleTo("StyleCop.Analyzers.Test.CSharp11, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c36d40d996fcc95fb6a89754728616758f459026e31478ce93633b3e27a4af416f103aa3d7a9e7998f829f8715cc1240d30724fd662042550fa71357b19562622424267e9e4640c403edbe64709a9ca5918128a9b9020b0db6e770d0dd1eac888869c23a835b74bde00e171984b1d1c24636cf030f0b23106e73035a2be145a6")]
+[assembly: InternalsVisibleTo("StyleCop.Analyzers.Test.CSharp12, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c36d40d996fcc95fb6a89754728616758f459026e31478ce93633b3e27a4af416f103aa3d7a9e7998f829f8715cc1240d30724fd662042550fa71357b19562622424267e9e4640c403edbe64709a9ca5918128a9b9020b0db6e770d0dd1eac888869c23a835b74bde00e171984b1d1c24636cf030f0b23106e73035a2be145a6")]
[assembly: InternalsVisibleTo("StyleCopTester, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c36d40d996fcc95fb6a89754728616758f459026e31478ce93633b3e27a4af416f103aa3d7a9e7998f829f8715cc1240d30724fd662042550fa71357b19562622424267e9e4640c403edbe64709a9ca5918128a9b9020b0db6e770d0dd1eac888869c23a835b74bde00e171984b1d1c24636cf030f0b23106e73035a2be145a6")]
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/PublicAPI.Shipped.txt b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/PublicAPI.Shipped.txt
new file mode 100644
index 000000000..e69de29bb
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/PublicAPI.Unshipped.txt b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/PublicAPI.Unshipped.txt
new file mode 100644
index 000000000..e69de29bb
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/IndentationCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/IndentationCodeFixProvider.cs
index 04c28e100..1569b8dbd 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/IndentationCodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/IndentationCodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -61,14 +63,14 @@ private static bool TryGetTextChange(Diagnostic diagnostic, SyntaxNode syntaxRoo
string replacement;
if (!diagnostic.Properties.TryGetValue(SA1137ElementsShouldHaveTheSameIndentation.ExpectedIndentationKey, out replacement))
{
- textChange = default(TextChange);
+ textChange = default;
return false;
}
var trivia = syntaxRoot.FindTrivia(diagnostic.Location.SourceSpan.Start);
TextSpan originalSpan;
- if (trivia == default(SyntaxTrivia))
+ if (trivia == default)
{
// The warning was reported on a token because the line is not indented
originalSpan = new TextSpan(diagnostic.Location.SourceSpan.Start, 0);
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/RemoveRegionCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/RemoveRegionCodeFixProvider.cs
index 975a7599f..a3b374185 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/RemoveRegionCodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/RemoveRegionCodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/RemoveRegionFixAllProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/RemoveRegionFixAllProvider.cs
index b56c7eeed..314b84cfe 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/RemoveRegionFixAllProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/RemoveRegionFixAllProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1100CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1100CodeFixProvider.cs
index d375eab7e..65fbdb130 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1100CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1100CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1101CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1101CodeFixProvider.cs
index 5bec2f707..5e445f418 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1101CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1101CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1102CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1102CodeFixProvider.cs
index b0c9a0745..bece491ab 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1102CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1102CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -52,7 +54,7 @@ private static async Task GetTransformedDocumentAsync(Document documen
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var indentationTrivia = QueryIndentationHelpers.GetQueryIndentationTrivia(settings.Indentation, token);
var precedingToken = token.GetPreviousToken();
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1103CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1103CodeFixProvider.cs
index b58e1c6aa..dc634ec81 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1103CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1103CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -118,7 +120,7 @@ private static async Task GetTransformedDocumentForMultipleLinesAsync(
var nodeList = CreateQueryNodeList(queryExpression);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var indentationTrivia = QueryIndentationHelpers.GetQueryIndentationTrivia(settings.Indentation, queryExpression);
for (var i = 1; i < nodeList.Length; i++)
@@ -153,7 +155,11 @@ private static ImmutableArray CreateQueryNodeList(QueryExpressionSyn
private static void ProcessQueryBody(QueryBodySyntax body, List queryNodes)
{
queryNodes.AddRange(body.Clauses);
- queryNodes.Add(body.SelectOrGroup);
+
+ if (!body.SelectOrGroup.IsMissing)
+ {
+ queryNodes.Add(body.SelectOrGroup);
+ }
if (body.Continuation != null)
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1104SA1105CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1104SA1105CodeFixProvider.cs
index d97b58526..fd8ffb8c4 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1104SA1105CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1104SA1105CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -54,7 +56,7 @@ private static async Task GetTransformedDocumentAsync(Document documen
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var token = syntaxRoot.FindToken(diagnostic.Location.SourceSpan.Start);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var indentationTrivia = QueryIndentationHelpers.GetQueryIndentationTrivia(settings.Indentation, token);
var precedingToken = token.GetPreviousToken();
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1106CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1106CodeFixProvider.cs
index e86943d4c..ddce6d839 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1106CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1106CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1107CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1107CodeFixProvider.cs
index f690931f4..228cd2243 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1107CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1107CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1107FixAllProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1107FixAllProvider.cs
index 3c0ad9082..55b4f9f33 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1107FixAllProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1107FixAllProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1116CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1116CodeFixProvider.cs
index 6ebfb1d46..9bafce580 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1116CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1116CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -70,7 +72,7 @@ private static async Task GetTransformedDocumentAsync(Document documen
}
}
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, tree, cancellationToken);
SyntaxTriviaList newTrivia =
SyntaxFactory.TriviaList(
SyntaxFactory.CarriageReturnLineFeed,
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1120CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1120CodeFixProvider.cs
index 05c2d0c52..c7b7bccaf 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1120CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1120CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -80,7 +82,7 @@ private static async Task GetTransformedDocumentAsync(Document documen
}
// Replace all roots with an empty node
- var newRoot = root.ReplaceTrivia(triviaToRemove, (original, rewritten) => default(SyntaxTrivia));
+ var newRoot = root.ReplaceTrivia(triviaToRemove, (original, rewritten) => default);
Document updatedDocument = document.WithSyntaxRoot(newRoot);
return updatedDocument;
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1121CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1121CodeFixProvider.cs
index 098204171..fbe74b25a 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1121CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1121CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1122CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1122CodeFixProvider.cs
index 5a994141b..09be377f3 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1122CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1122CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1127CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1127CodeFixProvider.cs
index 43fbf9193..c0ea7a3f7 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1127CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1127CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -60,7 +62,7 @@ private static async Task GetTransformedDocumentAsync(Document documen
var afterEndToken = endToken.GetNextToken();
var parentIndentation = GetParentIndentation(whereToken);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var indentationTrivia = SyntaxFactory.Whitespace(parentIndentation + IndentationHelper.GenerateIndentationString(settings.Indentation, 1));
var replaceMap = new Dictionary()
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1128CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1128CodeFixProvider.cs
index 328889413..09b4230f7 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1128CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1128CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -55,7 +57,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var newLine = FormattingHelper.GetNewLineTrivia(document);
var constructorInitializer = (ConstructorInitializerSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
@@ -108,7 +110,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
}
var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
var newLine = FormattingHelper.GetNewLineTrivia(document);
var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan).Parent);
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1129CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1129CodeFixProvider.cs
index c93affbe8..fea369877 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1129CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1129CodeFixProvider.cs
@@ -1,11 +1,14 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
using System;
using System.Collections.Immutable;
using System.Composition;
+ using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -14,7 +17,9 @@ namespace StyleCop.Analyzers.ReadabilityRules
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
+ using Microsoft.CodeAnalysis.Editing;
using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
///
/// Implements a code fix for .
@@ -53,44 +58,80 @@ private static async Task GetTransformedDocumentAsync(Document documen
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var newExpression = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
- var newSyntaxRoot = syntaxRoot.ReplaceNode(newExpression, GetReplacementNode(newExpression, semanticModel, cancellationToken));
+ var newSyntaxRoot = syntaxRoot.ReplaceNode(newExpression, GetReplacementNode(document.Project, newExpression, semanticModel, cancellationToken));
return document.WithSyntaxRoot(newSyntaxRoot);
}
- private static SyntaxNode GetReplacementNode(SyntaxNode node, SemanticModel semanticModel, CancellationToken cancellationToken)
+ private static SyntaxNode GetReplacementNode(Project project, SyntaxNode node, SemanticModel semanticModel, CancellationToken cancellationToken)
{
- var newExpression = (ObjectCreationExpressionSyntax)node;
+ var newExpression = (BaseObjectCreationExpressionSyntaxWrapper)node;
+
+ var symbolInfo = semanticModel.GetSymbolInfo(newExpression, cancellationToken);
+ var namedTypeSymbol = (symbolInfo.Symbol as IMethodSymbol)?.ContainingType;
- var symbolInfo = semanticModel.GetSymbolInfo(newExpression.Type, cancellationToken);
- var namedTypeSymbol = symbolInfo.Symbol as INamedTypeSymbol;
+ var type = GetOrCreateTypeSyntax(project, newExpression, namedTypeSymbol);
- SyntaxNode replacement = null;
- string memberName = null;
+ SyntaxNode replacement;
- if (IsType(namedTypeSymbol))
+ if (IsType(namedTypeSymbol)
+ || namedTypeSymbol?.SpecialType == SpecialType.System_IntPtr
+ || namedTypeSymbol?.SpecialType == SpecialType.System_UIntPtr
+ || IsType(namedTypeSymbol))
{
if (IsDefaultParameterValue(newExpression))
{
- replacement = SyntaxFactory.DefaultExpression(newExpression.Type);
+ replacement = SyntaxFactory.DefaultExpression(type);
}
else
{
- replacement = ConstructMemberAccessSyntax(newExpression.Type, nameof(CancellationToken.None));
+ string fieldName;
+ if (IsType(namedTypeSymbol))
+ {
+ fieldName = nameof(CancellationToken.None);
+ }
+ else if (namedTypeSymbol.SpecialType == SpecialType.System_IntPtr)
+ {
+ fieldName = nameof(IntPtr.Zero);
+ }
+ else if (namedTypeSymbol.SpecialType == SpecialType.System_UIntPtr)
+ {
+ fieldName = nameof(IntPtr.Zero);
+ }
+ else
+ {
+ Debug.Assert(IsType(namedTypeSymbol), "Assertion failed: IsType(namedTypeSymbol)");
+ fieldName = nameof(Guid.Empty);
+ }
+
+ replacement = ConstructMemberAccessSyntax(type, fieldName);
}
}
- else if (IsEnumWithDefaultMember(namedTypeSymbol, out memberName))
+ else if (IsEnumWithDefaultMember(namedTypeSymbol, out string memberName))
{
- replacement = ConstructMemberAccessSyntax(newExpression.Type, memberName);
+ replacement = ConstructMemberAccessSyntax(type, memberName);
}
else
{
- replacement = SyntaxFactory.DefaultExpression(newExpression.Type);
+ replacement = SyntaxFactory.DefaultExpression(type);
}
return replacement
- .WithLeadingTrivia(newExpression.GetLeadingTrivia())
- .WithTrailingTrivia(newExpression.GetTrailingTrivia());
+ .WithLeadingTrivia(newExpression.SyntaxNode.GetLeadingTrivia())
+ .WithTrailingTrivia(newExpression.SyntaxNode.GetTrailingTrivia());
+ }
+
+ private static TypeSyntax GetOrCreateTypeSyntax(Project project, BaseObjectCreationExpressionSyntaxWrapper baseObjectCreationExpression, INamedTypeSymbol constructedType)
+ {
+ if (baseObjectCreationExpression.SyntaxNode is ObjectCreationExpressionSyntax objectCreationExpressionSyntax)
+ {
+ return objectCreationExpressionSyntax.Type;
+ }
+ else
+ {
+ SyntaxGenerator generator = SyntaxGenerator.GetGenerator(project);
+ return (TypeSyntax)generator.TypeExpression(constructedType);
+ }
}
///
@@ -124,9 +165,9 @@ private static bool IsType(INamedTypeSymbol namedTypeSymbol)
return true;
}
- private static bool IsDefaultParameterValue(ObjectCreationExpressionSyntax expression)
+ private static bool IsDefaultParameterValue(BaseObjectCreationExpressionSyntaxWrapper expression)
{
- if (expression.Parent.Parent is ParameterSyntax parameterSyntax)
+ if (expression.SyntaxNode.Parent.Parent is ParameterSyntax parameterSyntax)
{
return parameterSyntax.Parent.Parent is BaseMethodDeclarationSyntax;
}
@@ -173,6 +214,11 @@ private static bool IsEnumWithDefaultMember(INamedTypeSymbol namedTypeSymbol, ou
/// A new member access expression.
private static SyntaxNode ConstructMemberAccessSyntax(TypeSyntax typeSyntax, string memberName)
{
+ // NOTE: This creates the correct source code when applied, but these are not necessarily the syntax
+ // nodes that the compiler would create from that source code. For example, the type syntax can
+ // contain QualifiedName nodes, whereas the compiler would have created SimpleMemberAccessExpression instead.
+ // This means that the validation that happens in the tests need to be turned off for some tests.
+ // We could have transformed the nodes to match, but we keep the code simple instead.
return SyntaxFactory.MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
typeSyntax,
@@ -199,7 +245,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true));
- return syntaxRoot.ReplaceNodes(nodes, (originalNode, rewrittenNode) => GetReplacementNode(rewrittenNode, semanticModel, fixAllContext.CancellationToken));
+ return syntaxRoot.ReplaceNodes(nodes, (originalNode, rewrittenNode) => GetReplacementNode(document.Project, rewrittenNode, semanticModel, fixAllContext.CancellationToken));
}
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1130CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1130CodeFixProvider.cs
index 9d66d8cc4..616797082 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1130CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1130CodeFixProvider.cs
@@ -1,9 +1,10 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
- using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
@@ -71,11 +72,12 @@ private static async Task CanFixAsync(CodeFixContext context, Diagnostic d
private static SyntaxNode ReplaceWithLambda(SemanticModel semanticModel, AnonymousMethodExpressionSyntax anonymousMethod)
{
var parameterList = anonymousMethod.ParameterList;
- SyntaxNode lambdaExpression;
+ ExpressionSyntax lambdaExpression;
+ SyntaxToken arrowToken;
if (parameterList == null)
{
- ImmutableArray argumentList = default(ImmutableArray);
+ ImmutableArray argumentList = ImmutableArray.Empty;
switch (anonymousMethod.Parent.Kind())
{
@@ -89,15 +91,38 @@ private static SyntaxNode ReplaceWithLambda(SemanticModel semanticModel, Anonymo
case SyntaxKind.AddAssignmentExpression:
case SyntaxKind.SubtractAssignmentExpression:
- var list = GetAssignmentArgumentList(semanticModel, anonymousMethod);
+ {
+ var list = GetAssignmentArgumentList(semanticModel, anonymousMethod);
+ if (list == null)
+ {
+ return null;
+ }
+
+ argumentList = list.Value;
+ break;
+ }
- if (list == null)
+ case SyntaxKind.ArrowExpressionClause:
+ case SyntaxKind.ReturnStatement:
+ argumentList = GetMemberReturnTypeArgumentList(semanticModel, anonymousMethod);
+ if (argumentList.IsEmpty)
{
return null;
}
- argumentList = list.Value;
break;
+
+ case SyntaxKind.CastExpression:
+ {
+ var list = GetCastTypeArgumentList(semanticModel, anonymousMethod);
+ if (list == null)
+ {
+ return null;
+ }
+
+ argumentList = list.Value;
+ break;
+ }
}
List parameters = GenerateUniqueParameterNames(semanticModel, anonymousMethod, argumentList);
@@ -107,12 +132,17 @@ private static SyntaxNode ReplaceWithLambda(SemanticModel semanticModel, Anonymo
: SyntaxFactory.SeparatedList();
parameterList = SyntaxFactory.ParameterList(newList)
- .WithLeadingTrivia(anonymousMethod.DelegateKeyword.LeadingTrivia)
+ .WithLeadingTrivia(anonymousMethod.DelegateKeyword.LeadingTrivia);
+
+ arrowToken = SyntaxFactory.Token(SyntaxKind.EqualsGreaterThanToken)
.WithTrailingTrivia(anonymousMethod.DelegateKeyword.TrailingTrivia);
}
else
{
parameterList = parameterList.WithLeadingTrivia(anonymousMethod.DelegateKeyword.TrailingTrivia);
+
+ arrowToken = SyntaxFactory.Token(SyntaxKind.EqualsGreaterThanToken)
+ .WithTrailingTrivia(SyntaxFactory.ElasticSpace);
}
foreach (var parameter in parameterList.Parameters)
@@ -123,9 +153,6 @@ private static SyntaxNode ReplaceWithLambda(SemanticModel semanticModel, Anonymo
}
}
- var arrowToken = SyntaxFactory.Token(SyntaxKind.EqualsGreaterThanToken)
- .WithTrailingTrivia(SyntaxFactory.ElasticSpace);
-
if (parameterList.Parameters.Count == 1)
{
var parameterSyntax = RemoveType(parameterList.Parameters[0]);
@@ -151,6 +178,13 @@ private static SyntaxNode ReplaceWithLambda(SemanticModel semanticModel, Anonymo
lambdaExpression = SyntaxFactory.ParenthesizedLambdaExpression(anonymousMethod.AsyncKeyword, parameterListSyntax, arrowToken, anonymousMethod.Body);
}
+ if (anonymousMethod.Parent.IsKind(SyntaxKind.CastExpression))
+ {
+ // In this case, the lambda needs enclosing parenthesis to be syntactically correct
+ lambdaExpression = SyntaxFactory.ParenthesizedExpression(lambdaExpression);
+ }
+
+ // TODO: No tests require this annotation. Can it be removed?
return lambdaExpression
.WithAdditionalAnnotations(Formatter.Annotation);
}
@@ -162,7 +196,7 @@ private static ImmutableArray GetMethodInvocationArgumentList(SemanticMo
var originalInvocableExpression = argumentListSyntax.Parent;
var originalSymbolInfo = semanticModel.GetSymbolInfo(originalInvocableExpression);
- var argumentIndex = argumentListSyntax.Arguments.IndexOf(argumentSyntax);
+ var argumentIndex = SA1130UseLambdaSyntax.FindParameterIndex(originalSymbolInfo, argumentSyntax, argumentListSyntax);
var parameterList = SA1130UseLambdaSyntax.GetDelegateParameterList(originalSymbolInfo.Symbol, argumentIndex);
return parameterList.Parameters.Select(p => p.Identifier.ToString()).ToImmutableArray();
}
@@ -193,6 +227,27 @@ private static ImmutableArray GetEqualsArgumentList(SemanticModel semant
return namedTypeSymbol.DelegateInvokeMethod.Parameters.Select(ps => ps.Name).ToImmutableArray();
}
+ private static ImmutableArray GetMemberReturnTypeArgumentList(SemanticModel semanticModel, AnonymousMethodExpressionSyntax anonymousMethod)
+ {
+ var enclosingSymbol = semanticModel.GetEnclosingSymbol(anonymousMethod.Parent.SpanStart);
+ return !(((IMethodSymbol)enclosingSymbol).ReturnType is INamedTypeSymbol returnType) ? ImmutableArray.Empty : returnType.DelegateInvokeMethod.Parameters.Select(ps => ps.Name).ToImmutableArray();
+ }
+
+ private static ImmutableArray? GetCastTypeArgumentList(SemanticModel semanticModel, AnonymousMethodExpressionSyntax anonymousMethod)
+ {
+ var castExpression = (CastExpressionSyntax)anonymousMethod.Parent;
+
+ var symbol = semanticModel.GetSymbolInfo(castExpression.Type);
+ var namedTypeSymbol = symbol.Symbol as INamedTypeSymbol;
+ var parameters = namedTypeSymbol?.DelegateInvokeMethod?.Parameters;
+ if (parameters == null)
+ {
+ return null;
+ }
+
+ return parameters.Value.Select(ps => ps.Name).ToImmutableArray();
+ }
+
private static List GenerateUniqueParameterNames(SemanticModel semanticModel, AnonymousMethodExpressionSyntax anonymousMethod, ImmutableArray argumentNames)
{
var parameters = new List();
@@ -286,7 +341,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
return rewrittenNode;
}
- return newNode;
+ return newNode.WithoutFormatting();
});
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1131CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1131CodeFixProvider.cs
index 49b694e1c..02d9bc576 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1131CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1131CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -64,33 +66,48 @@ private static async Task GetTransformedDocumentAsync(Document documen
private static BinaryExpressionSyntax TransformExpression(BinaryExpressionSyntax binaryExpression)
{
+ // NOTE: This code also changes the syntax node kind, besides the operator token. The modified source code would
+ // have been the same without this, but we do this to make tests pass with the default CodeActionValidationMode.
var newLeft = binaryExpression.Right.WithTriviaFrom(binaryExpression.Left);
var newRight = binaryExpression.Left.WithTriviaFrom(binaryExpression.Right);
- return binaryExpression.WithLeft(newLeft).WithRight(newRight).WithOperatorToken(GetCorrectOperatorToken(binaryExpression.OperatorToken));
+ GetReplacementInfo(binaryExpression.OperatorToken, out var newOperatorToken, out var newNodeKind);
+ return SyntaxFactory.BinaryExpression(newNodeKind, newLeft, newOperatorToken, newRight);
}
- private static SyntaxToken GetCorrectOperatorToken(SyntaxToken operatorToken)
+ private static void GetReplacementInfo(SyntaxToken operatorToken, out SyntaxToken newToken, out SyntaxKind newNodeKind)
{
switch (operatorToken.Kind())
{
case SyntaxKind.EqualsEqualsToken:
case SyntaxKind.ExclamationEqualsToken:
- return operatorToken;
+ newToken = operatorToken;
+ newNodeKind = operatorToken.Parent.Kind();
+ break;
case SyntaxKind.GreaterThanToken:
- return SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.LessThanToken, operatorToken.TrailingTrivia);
+ newToken = SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.LessThanToken, operatorToken.TrailingTrivia);
+ newNodeKind = SyntaxKind.LessThanExpression;
+ break;
case SyntaxKind.GreaterThanEqualsToken:
- return SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.LessThanEqualsToken, operatorToken.TrailingTrivia);
+ newToken = SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.LessThanEqualsToken, operatorToken.TrailingTrivia);
+ newNodeKind = SyntaxKind.LessThanOrEqualExpression;
+ break;
case SyntaxKind.LessThanToken:
- return SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.GreaterThanToken, operatorToken.TrailingTrivia);
+ newToken = SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.GreaterThanToken, operatorToken.TrailingTrivia);
+ newNodeKind = SyntaxKind.GreaterThanExpression;
+ break;
case SyntaxKind.LessThanEqualsToken:
- return SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.GreaterThanEqualsToken, operatorToken.TrailingTrivia);
+ newToken = SyntaxFactory.Token(operatorToken.LeadingTrivia, SyntaxKind.GreaterThanEqualsToken, operatorToken.TrailingTrivia);
+ newNodeKind = SyntaxKind.GreaterThanOrEqualExpression;
+ break;
default:
- return SyntaxFactory.Token(SyntaxKind.None);
+ newToken = SyntaxFactory.Token(SyntaxKind.None);
+ newNodeKind = (SyntaxKind)operatorToken.Parent.RawKind;
+ break;
}
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1132CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1132CodeFixProvider.cs
index e68a38149..8dcdee5b2 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1132CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1132CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs
index 3c6ad17b6..ebb9a6a56 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1133CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -59,7 +61,7 @@ private static async Task GetTransformedDocumentAsync(Document documen
var nodeInSourceSpan = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
AttributeListSyntax attributeList = nodeInSourceSpan.FirstAncestorOrSelf();
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var indentationSteps = IndentationHelper.GetIndentationSteps(settings.Indentation, attributeList);
var indentationTrivia = IndentationHelper.GenerateWhitespaceTrivia(settings.Indentation, indentationSteps);
@@ -111,10 +113,12 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
return null;
}
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
- var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true).FirstAncestorOrSelf());
+ // 🐉 Need to eagerly evaluate this with ToList() to ensure nodes are not garbage collected between the
+ // call to TrackNodes and subsequent enumeration.
+ var nodes = diagnostics.Select(diagnostic => syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true).FirstAncestorOrSelf()).ToList();
var newRoot = syntaxRoot.TrackNodes(nodes);
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs
index daf349312..5fb8ea893 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1134CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -34,25 +36,29 @@ public override FixAllProvider GetFixAllProvider()
}
///
- public override Task RegisterCodeFixesAsync(CodeFixContext context)
+ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
+ var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
+
foreach (var diagnostic in context.Diagnostics)
{
- context.RegisterCodeFix(
- CodeAction.Create(
- ReadabilityResources.SA1134CodeFix,
- cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
- nameof(SA1134CodeFixProvider)),
- diagnostic);
+ // Do not offer the code fix if the error is found at an invalid node (like IncompleteMemberSyntax)
+ if (syntaxRoot.FindNode(diagnostic.Location.SourceSpan) is AttributeListSyntax)
+ {
+ context.RegisterCodeFix(
+ CodeAction.Create(
+ ReadabilityResources.SA1134CodeFix,
+ cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
+ nameof(SA1134CodeFixProvider)),
+ diagnostic);
+ }
}
-
- return SpecializedTasks.CompletedTask;
}
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var tokensToReplace = new Dictionary();
AddTokensToReplaceToMap(tokensToReplace, syntaxRoot, diagnostic, settings);
@@ -109,7 +115,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
}
var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, fixAllContext.CancellationToken);
var tokensToReplace = new Dictionary();
foreach (var diagnostic in diagnostics)
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1135CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1135CodeFixProvider.cs
index 07936bee3..7b6128988 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1135CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1135CodeFixProvider.cs
@@ -1,8 +1,11 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
+ using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
@@ -63,7 +66,76 @@ private static async Task GetTransformedDocumentAsync(Document documen
private static SyntaxNode GetReplacementNode(SemanticModel semanticModel, UsingDirectiveSyntax node, CancellationToken cancellationToken)
{
SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(node.Name, cancellationToken);
- return node.WithName(SyntaxFactory.ParseName(symbolInfo.Symbol.ToString()));
+ var symbolNameSyntax = SyntaxFactory.ParseName(symbolInfo.Symbol.ToQualifiedString(node.Name));
+
+ var newName = GetReplacementName(symbolNameSyntax, node.Name);
+ return node.WithName((NameSyntax)newName);
+ }
+
+ private static TypeSyntax GetReplacementName(TypeSyntax symbolNameSyntax, TypeSyntax nameSyntax)
+ {
+ switch (nameSyntax.Kind())
+ {
+ case SyntaxKind.GenericName:
+ return GetReplacementGenericName(symbolNameSyntax, (GenericNameSyntax)nameSyntax);
+
+ case SyntaxKind.QualifiedName:
+ return GetReplacementQualifiedName((QualifiedNameSyntax)symbolNameSyntax, (QualifiedNameSyntax)nameSyntax);
+
+ default:
+ return symbolNameSyntax;
+ }
+ }
+
+ private static NameSyntax GetReplacementGenericName(TypeSyntax symbolNameSyntax, GenericNameSyntax genericNameSyntax)
+ {
+ var symbolQualifiedNameSyntax = symbolNameSyntax as QualifiedNameSyntax;
+ var symbolGenericNameSyntax = (GenericNameSyntax)(symbolQualifiedNameSyntax?.Right ?? symbolNameSyntax);
+
+ TypeArgumentListSyntax newTypeArgumentList = GetReplacementTypeArgumentList(symbolGenericNameSyntax, genericNameSyntax);
+
+ if (symbolQualifiedNameSyntax != null)
+ {
+ var newRightPart = ((GenericNameSyntax)symbolQualifiedNameSyntax.Right).WithTypeArgumentList(newTypeArgumentList);
+ return symbolQualifiedNameSyntax.WithRight(newRightPart);
+ }
+
+ return genericNameSyntax.WithTypeArgumentList(newTypeArgumentList);
+ }
+
+ private static TypeArgumentListSyntax GetReplacementTypeArgumentList(GenericNameSyntax symbolGenericNameSyntax, GenericNameSyntax genericNameSyntax)
+ {
+ var replacements = new Dictionary();
+ for (var i = 0; i < genericNameSyntax.TypeArgumentList.Arguments.Count; i++)
+ {
+ var argument = genericNameSyntax.TypeArgumentList.Arguments[i];
+
+ if (!argument.IsKind(SyntaxKind.PredefinedType))
+ {
+ var symbolArgument = symbolGenericNameSyntax.TypeArgumentList.Arguments[i];
+
+ var replacementArgument = GetReplacementName(symbolArgument, argument)
+ .WithLeadingTrivia(argument.GetLeadingTrivia())
+ .WithTrailingTrivia(argument.GetTrailingTrivia());
+
+ replacements.Add(argument, replacementArgument);
+ }
+ }
+
+ var newTypeArgumentList = genericNameSyntax.TypeArgumentList.ReplaceNodes(replacements.Keys, (original, maybeRewritten) => replacements[original]);
+ return newTypeArgumentList;
+ }
+
+ private static NameSyntax GetReplacementQualifiedName(QualifiedNameSyntax symbolNameSyntax, QualifiedNameSyntax nameSyntax)
+ {
+ if (nameSyntax.Right.IsKind(SyntaxKind.GenericName))
+ {
+ return GetReplacementGenericName(symbolNameSyntax, (GenericNameSyntax)nameSyntax.Right);
+ }
+ else
+ {
+ return symbolNameSyntax;
+ }
}
private class FixAll : DocumentBasedFixAllProvider
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1136CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1136CodeFixProvider.cs
index d03cbd837..114d839a1 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1136CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1136CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
@@ -51,7 +53,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxRoot.SyntaxTree, cancellationToken);
var enumMemberDeclaration = (EnumMemberDeclarationSyntax)syntaxRoot.FindNode(diagnostic.Location.SourceSpan);
var enumDeclaration = (EnumDeclarationSyntax)enumMemberDeclaration.Parent;
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1139CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1139CodeFixProvider.cs
index c7f8c39a0..0a1ceb8c0 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1139CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1139CodeFixProvider.cs
@@ -1,9 +1,10 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
- using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
@@ -14,7 +15,6 @@ namespace StyleCop.Analyzers.ReadabilityRules
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
- using Microsoft.CodeAnalysis.Text;
using StyleCop.Analyzers.Helpers;
///
@@ -44,10 +44,13 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
return SpecializedTasks.CompletedTask;
}
+ ///
+ public override FixAllProvider GetFixAllProvider()
+ => CustomFixAllProviders.BatchFixer;
+
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
- var oldSemanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
if (!(syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true) is CastExpressionSyntax node))
{
@@ -68,9 +71,18 @@ private static SyntaxNode GenerateReplacementNode(CastExpressionSyntax node)
(LiteralExpressionSyntax)plusMinusSyntax.Operand.WalkDownParentheses();
var typeToken = node.Type.GetFirstToken();
var replacementLiteral = literalExpressionSyntax.WithLiteralSuffix(typeToken.Kind());
+
+ var newLeadingTrivia = SyntaxFactory.TriviaList(node.GetLeadingTrivia().Concat(node.CloseParenToken.TrailingTrivia).Concat(node.Expression.GetLeadingTrivia()))
+ .WithoutLeadingWhitespace()
+ .WithoutTrailingWhitespace();
+
+ if (newLeadingTrivia.Count != 0)
+ {
+ newLeadingTrivia = newLeadingTrivia.Add(SyntaxFactory.Space);
+ }
+
var replacementNode = node.Expression.ReplaceNode(literalExpressionSyntax, replacementLiteral)
- .WithLeadingTrivia(node.GetLeadingTrivia().Concat(node.CloseParenToken.TrailingTrivia).Concat(node.Expression.GetLeadingTrivia()))
- .WithTrailingTrivia(node.Expression.GetTrailingTrivia().Concat(node.GetTrailingTrivia()));
+ .WithLeadingTrivia(newLeadingTrivia);
return replacementNode;
}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1141CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1141CodeFixProvider.cs
new file mode 100644
index 000000000..9159a72ed
--- /dev/null
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1141CodeFixProvider.cs
@@ -0,0 +1,178 @@
+// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
+
+namespace StyleCop.Analyzers.ReadabilityRules
+{
+ using System;
+ using System.Collections.Immutable;
+ using System.Composition;
+ using System.Threading;
+ using System.Threading.Tasks;
+ using Microsoft.CodeAnalysis;
+ using Microsoft.CodeAnalysis.CodeActions;
+ using Microsoft.CodeAnalysis.CodeFixes;
+ using Microsoft.CodeAnalysis.CSharp;
+ using Microsoft.CodeAnalysis.CSharp.Syntax;
+ using Microsoft.CodeAnalysis.Simplification;
+ using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
+
+ [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SA1141CodeFixProvider))]
+ [Shared]
+ internal class SA1141CodeFixProvider : CodeFixProvider
+ {
+ ///
+ public override ImmutableArray FixableDiagnosticIds { get; } =
+ ImmutableArray.Create(SA1141UseTupleSyntax.DiagnosticId);
+
+ ///
+ public override FixAllProvider GetFixAllProvider()
+ {
+ // Fix All is not yet supported
+ return null;
+ }
+
+ ///
+ public override Task RegisterCodeFixesAsync(CodeFixContext context)
+ {
+ foreach (var diagnostic in context.Diagnostics)
+ {
+ context.RegisterCodeFix(
+ CodeAction.Create(
+ ReadabilityResources.SA1141CodeFix,
+ cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
+ nameof(SA1141CodeFixProvider)),
+ diagnostic);
+ }
+
+ return SpecializedTasks.CompletedTask;
+ }
+
+ private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
+ {
+ var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
+ var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
+
+ var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
+ if (node.IsKind(SyntaxKind.SimpleMemberAccessExpression))
+ {
+ // get the invocation node when processing ValueTuple.Create, as that needs to be replaced.
+ node = node.Parent;
+ }
+
+ var newNode = GetReplacementNode(semanticModel, node);
+
+ // doing our own formatting, as the default formatting for operators is incompatible with StyleCop.Analyzers.
+ var separatorRewriter = new SeparatorRewriter();
+ newNode = separatorRewriter.Visit(newNode);
+
+ switch (node.Parent.Kind())
+ {
+ case SyntaxKind.MethodDeclaration:
+ case SyntaxKind.Parameter:
+ case SyntaxKind.PropertyDeclaration:
+ case SyntaxKind.IndexerDeclaration:
+ case SyntaxKind.DelegateDeclaration:
+ newNode = newNode.WithTrailingTrivia(SyntaxFactory.Space);
+ break;
+ }
+
+ var newSyntaxRoot = syntaxRoot.ReplaceNode(node, newNode.WithAdditionalAnnotations(Simplifier.Annotation));
+ return document.WithSyntaxRoot(newSyntaxRoot.WithoutFormatting());
+ }
+
+ private static SyntaxNode GetReplacementNode(SemanticModel semanticModel, SyntaxNode node)
+ {
+ switch (node)
+ {
+ case QualifiedNameSyntax qualifiedNameSyntax:
+ return TransformGenericNameToTuple(semanticModel, (GenericNameSyntax)qualifiedNameSyntax.Right);
+
+ case GenericNameSyntax genericNameSyntax:
+ return TransformGenericNameToTuple(semanticModel, genericNameSyntax);
+
+ case ObjectCreationExpressionSyntax objectCreationExpression:
+ return TransformArgumentListToTuple(semanticModel, objectCreationExpression.ArgumentList.Arguments);
+
+ case InvocationExpressionSyntax invocationExpressionSyntax:
+ return TransformArgumentListToTuple(semanticModel, invocationExpressionSyntax.ArgumentList.Arguments);
+
+ default:
+ return node;
+ }
+ }
+
+ private static SyntaxNode TransformGenericNameToTuple(SemanticModel semanticModel, GenericNameSyntax genericName)
+ {
+ var implementationType = typeof(SeparatedSyntaxListWrapper<>.AutoWrapSeparatedSyntaxList<>).MakeGenericType(typeof(TupleElementSyntaxWrapper), SyntaxWrapperHelper.GetWrappedType(typeof(TupleElementSyntaxWrapper)));
+ var tupleElements = (SeparatedSyntaxListWrapper)Activator.CreateInstance(implementationType);
+
+ foreach (var typeArgument in genericName.TypeArgumentList.Arguments)
+ {
+ if (IsValueTuple(semanticModel, typeArgument))
+ {
+ var tupleTypeSyntax = (TypeSyntax)GetReplacementNode(semanticModel, typeArgument);
+ tupleElements = tupleElements.Add(SyntaxFactoryEx.TupleElement(tupleTypeSyntax));
+ }
+ else
+ {
+ tupleElements = tupleElements.Add(SyntaxFactoryEx.TupleElement(typeArgument));
+ }
+ }
+
+ return SyntaxFactoryEx.TupleType(tupleElements);
+ }
+
+ private static SyntaxNode TransformArgumentListToTuple(SemanticModel semanticModel, SeparatedSyntaxList arguments)
+ {
+ SeparatedSyntaxList processedArguments = default;
+
+ for (var i = 0; i < arguments.Count; i++)
+ {
+ var argument = arguments[i];
+
+ var argumentTypeInfo = semanticModel.GetTypeInfo(argument.Expression);
+ if (!Equals(argumentTypeInfo.Type, argumentTypeInfo.ConvertedType))
+ {
+ var expectedType = SyntaxFactory.ParseTypeName(argumentTypeInfo.ConvertedType.ToDisplayString());
+ argument = argument.WithExpression(SyntaxFactory.CastExpression(expectedType, argument.Expression));
+ }
+
+ processedArguments = processedArguments.Add(argument);
+ }
+
+ return SyntaxFactoryEx.TupleExpression(processedArguments);
+ }
+
+ private static bool IsValueTuple(SemanticModel semanticModel, TypeSyntax typeSyntax)
+ {
+ if (typeSyntax.IsKind(SyntaxKindEx.TupleType))
+ {
+ return false;
+ }
+
+ var symbolInfo = semanticModel.GetSymbolInfo(typeSyntax);
+ return (symbolInfo.Symbol is ITypeSymbol typeSymbol) && typeSymbol.IsTupleType();
+ }
+
+ private class SeparatorRewriter : CSharpSyntaxRewriter
+ {
+ public SeparatorRewriter()
+ : base(false)
+ {
+ }
+
+ public override SyntaxToken VisitToken(SyntaxToken token)
+ {
+ if (token.IsKind(SyntaxKind.CommaToken))
+ {
+ token = token.WithTrailingTrivia(SyntaxFactory.Space);
+ }
+
+ return base.VisitToken(token);
+ }
+ }
+ }
+}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1142CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1142CodeFixProvider.cs
new file mode 100644
index 000000000..4e9b2a2d4
--- /dev/null
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SA1142CodeFixProvider.cs
@@ -0,0 +1,92 @@
+// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
+
+namespace StyleCop.Analyzers.ReadabilityRules
+{
+ using System.Collections.Generic;
+ 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 Microsoft.CodeAnalysis.CSharp;
+ using StyleCop.Analyzers.Helpers;
+ using StyleCop.Analyzers.Lightup;
+
+ [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(SA1142CodeFixProvider))]
+ [Shared]
+ internal class SA1142CodeFixProvider : CodeFixProvider
+ {
+ ///
+ public override ImmutableArray FixableDiagnosticIds { get; } =
+ ImmutableArray.Create(SA1142ReferToTupleElementsByName.DiagnosticId);
+
+ public override FixAllProvider GetFixAllProvider() => FixAll.Instance;
+
+ ///
+ public override Task RegisterCodeFixesAsync(CodeFixContext context)
+ {
+ foreach (var diagnostic in context.Diagnostics)
+ {
+ context.RegisterCodeFix(
+ CodeAction.Create(
+ ReadabilityResources.SA1142CodeFix,
+ cancellationToken => GetTransformedDocumentAsync(context.Document, diagnostic, cancellationToken),
+ nameof(SA1141CodeFixProvider)),
+ diagnostic);
+ }
+
+ return SpecializedTasks.CompletedTask;
+ }
+
+ private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
+ {
+ var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
+ var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
+
+ var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
+ var replacementNode = GetReplacementNode(semanticModel, node);
+
+ var newSyntaxRoot = syntaxRoot.ReplaceNode(node, replacementNode);
+ return document.WithSyntaxRoot(newSyntaxRoot);
+ }
+
+ private static SyntaxNode GetReplacementNode(SemanticModel semanticModel, SyntaxNode fieldName)
+ {
+ var fieldSymbol = (IFieldSymbol)semanticModel.GetSymbolInfo(fieldName.Parent).Symbol;
+ var fieldNameSymbol = fieldSymbol.ContainingType.GetMembers().OfType().Single(fs => !Equals(fs, fieldSymbol) && Equals(fs.CorrespondingTupleField(), fieldSymbol));
+
+ return SyntaxFactory.IdentifierName(fieldNameSymbol.Name).WithTriviaFrom(fieldName);
+ }
+
+ private class FixAll : DocumentBasedFixAllProvider
+ {
+ public static FixAllProvider Instance { get; } = new FixAll();
+
+ ///
+ protected override string CodeActionTitle => ReadabilityResources.SA1142CodeFix;
+
+ ///
+ protected override async Task FixAllInDocumentAsync(FixAllContext fixAllContext, Document document, ImmutableArray diagnostics)
+ {
+ var syntaxRoot = await document.GetSyntaxRootAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
+ var semanticModel = await document.GetSemanticModelAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
+
+ var replaceMap = new Dictionary();
+
+ foreach (var diagnostic in diagnostics)
+ {
+ var node = syntaxRoot.FindNode(diagnostic.Location.SourceSpan, getInnermostNodeForTie: true);
+ replaceMap[node] = GetReplacementNode(semanticModel, node);
+ }
+
+ return syntaxRoot.ReplaceNodes(replaceMap.Keys, (original, rewritten) => replaceMap[original]);
+ }
+ }
+ }
+}
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SX1101CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SX1101CodeFixProvider.cs
index 7a763c89f..a8ba58718 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SX1101CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/ReadabilityRules/SX1101CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.ReadabilityRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Settings/SettingsFileCodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Settings/SettingsFileCodeFixProvider.cs
index 5225e02c3..b1a91252a 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Settings/SettingsFileCodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Settings/SettingsFileCodeFixProvider.cs
@@ -1,9 +1,10 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.Settings
{
- using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
@@ -79,7 +80,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(
CodeAction.Create(
SettingsResources.SettingsFileCodeFix,
- cancellationToken => GetTransformedSolutionAsync(context.Document, diagnostic, cancellationToken),
+ cancellationToken => GetTransformedSolutionAsync(context.Document, cancellationToken),
nameof(SettingsFileCodeFixProvider)),
diagnostic);
}
@@ -94,8 +95,11 @@ public override FixAllProvider GetFixAllProvider()
return null;
}
- private static Task GetTransformedSolutionAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
+ private static Task GetTransformedSolutionAsync(Document document, CancellationToken cancellationToken)
{
+ // Currently unused
+ _ = cancellationToken;
+
var project = document.Project;
var solution = project.Solution;
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1003CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1003CodeFixProvider.cs
index 0f31834ac..c0d3ecd73 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1003CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1003CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.SpacingRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1004CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1004CodeFixProvider.cs
index 216c93f65..237353f97 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1004CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1004CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.SpacingRules
{
@@ -55,10 +57,10 @@ private static async Task GetTransformedDocumentAsync(Document documen
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
- return document.WithText(text.WithChanges(GetTextChange(root, text, diagnostic)));
+ return document.WithText(text.WithChanges(GetTextChange(root, diagnostic)));
}
- private static TextChange GetTextChange(SyntaxNode root, SourceText sourceText, Diagnostic diagnostic)
+ private static TextChange GetTextChange(SyntaxNode root, Diagnostic diagnostic)
{
var token = root.FindToken(diagnostic.Location.SourceSpan.Start, findInsideTrivia: true);
switch (token.Kind())
@@ -93,7 +95,7 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
List changes = new List();
foreach (var diagnostic in diagnostics)
{
- changes.Add(GetTextChange(root, text, diagnostic));
+ changes.Add(GetTextChange(root, diagnostic));
}
changes.Sort((left, right) => left.Span.Start.CompareTo(right.Span.Start));
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1005CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1005CodeFixProvider.cs
index 3741358fa..a75d85f8f 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1005CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1005CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.SpacingRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1018CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1018CodeFixProvider.cs
index e9338a319..c77796c34 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1018CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1018CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.SpacingRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1025CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1025CodeFixProvider.cs
index 5d8434230..6c12e8bdb 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1025CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1025CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.SpacingRules
{
diff --git a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1027CodeFixProvider.cs b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1027CodeFixProvider.cs
index 4429ddf55..8eacf22e4 100644
--- a/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1027CodeFixProvider.cs
+++ b/StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/SpacingRules/SA1027CodeFixProvider.cs
@@ -1,5 +1,7 @@
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
-// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
+// Licensed under the MIT License. See LICENSE in the project root for license information.
+
+#nullable disable
namespace StyleCop.Analyzers.SpacingRules
{
@@ -52,7 +54,8 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
private static async Task GetTransformedDocumentAsync(Document document, Diagnostic diagnostic, CancellationToken cancellationToken)
{
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, cancellationToken);
+ var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, syntaxTree, cancellationToken);
SourceText sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
return document.WithText(sourceText.WithChanges(FixDiagnostic(settings.Indentation, sourceText, diagnostic)));
}
@@ -86,7 +89,7 @@ private static TextChange FixDiagnostic(IndentationSettings indentationSettings,
{
if (useTabs)
{
- replacement.Length = replacement.Length - spaceCount;
+ replacement.Length -= spaceCount;
replacement.Append('\t');
spaceCount = 0;
}
@@ -113,7 +116,7 @@ private static TextChange FixDiagnostic(IndentationSettings indentationSettings,
if (offsetWithinTabColumn == 0)
{
// We reached a tab stop.
- replacement.Length = replacement.Length - spaceCount;
+ replacement.Length -= spaceCount;
replacement.Append('\t');
spaceCount = 0;
}
@@ -157,7 +160,8 @@ protected override async Task FixAllInDocumentAsync(FixAllContext fi
return null;
}
- var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, fixAllContext.CancellationToken);
+ SyntaxTree tree = await document.GetSyntaxTreeAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
+ var settings = SettingsHelper.GetStyleCopSettings(document.Project.AnalyzerOptions, tree, fixAllContext.CancellationToken);
SourceText sourceText = await document.GetTextAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
List