Skip to content

Commit 1c2c293

Browse files
committed
Merge remote-tracking branch 'refs/remotes/DotNetAnalyzers/master'
Conflicts: StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1649FileNameMustMatchTypeName.cs
2 parents b59d5e8 + c3c1e4d commit 1c2c293

14 files changed

Lines changed: 295 additions & 65 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
6666
private static async Task<Document> GetTransformedDocumentAsync(Document document, CancellationToken cancellationToken)
6767
{
6868
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
69-
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings();
69+
var settings = document.Project.AnalyzerOptions.GetStyleCopSettings(cancellationToken);
7070

7171
var fileHeader = FileHeaderHelpers.ParseFileHeader(root);
7272
SyntaxNode newSyntaxRoot;

StyleCop.Analyzers/StyleCop.Analyzers.Test/OrderingRules/SA1202UnitTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,43 @@ public string
955955
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
956956
}
957957

958+
/// <summary>
959+
/// Verifies that the analyzer will properly handle incomplete members.
960+
/// </summary>
961+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
962+
[Fact]
963+
public async Task Issue1507RegressionAsync()
964+
{
965+
string testCode = @"public class OuterType
966+
{
967+
private string Test;
968+
private string
969+
public string
970+
}
971+
";
972+
973+
// We don't care about the syntax errors.
974+
var expected = new[]
975+
{
976+
new DiagnosticResult
977+
{
978+
Id = "CS1585",
979+
Message = "Member modifier 'public' must precede the member type and name",
980+
Severity = DiagnosticSeverity.Error,
981+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 5, 5) }
982+
},
983+
new DiagnosticResult
984+
{
985+
Id = "CS1519",
986+
Message = "Invalid token '}' in class, struct, or interface member declaration",
987+
Severity = DiagnosticSeverity.Error,
988+
Locations = new[] { new DiagnosticResultLocation("Test0.cs", 6, 1) }
989+
}
990+
};
991+
992+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
993+
}
994+
958995
/// <inheritdoc/>
959996
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
960997
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/Settings/SettingsUnitTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class SettingsUnitTests
2121
[Fact]
2222
public void VerifySettingsDefaults()
2323
{
24-
var styleCopSettings = SettingsHelper.GetStyleCopSettings(default(SyntaxTreeAnalysisContext));
24+
var styleCopSettings = SettingsHelper.GetStyleCopSettings(default(SyntaxTreeAnalysisContext), CancellationToken.None);
2525

2626
Assert.Equal("PlaceholderCompany", styleCopSettings.DocumentationRules.CompanyName);
2727
Assert.Equal("Copyright (c) PlaceholderCompany. All rights reserved.", styleCopSettings.DocumentationRules.CopyrightText);
@@ -57,7 +57,7 @@ public async Task VerifySettingsAreReadCorrectlyAsync()
5757
";
5858
var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false);
5959

60-
var styleCopSettings = context.GetStyleCopSettings();
60+
var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None);
6161

6262
Assert.Equal("TestCompany", styleCopSettings.DocumentationRules.CompanyName);
6363
Assert.Equal("Custom copyright text.", styleCopSettings.DocumentationRules.CopyrightText);
@@ -83,7 +83,7 @@ public async Task VerifySettingsWillUseCompanyNameInDefaultCopyrightTextAsync()
8383
";
8484
var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false);
8585

86-
var styleCopSettings = context.GetStyleCopSettings();
86+
var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None);
8787

8888
Assert.Equal("TestCompany", styleCopSettings.DocumentationRules.CompanyName);
8989
Assert.Equal("Copyright (c) TestCompany. All rights reserved.", styleCopSettings.DocumentationRules.CopyrightText);
@@ -103,7 +103,7 @@ public async Task VerifyCircularReferenceBehaviorAsync()
103103
";
104104
var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false);
105105

106-
var styleCopSettings = context.GetStyleCopSettings();
106+
var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None);
107107

108108
Assert.Equal("[CircularReference]", styleCopSettings.DocumentationRules.CopyrightText);
109109
}
@@ -122,7 +122,7 @@ public async Task VerifyInvalidReferenceBehaviorAsync()
122122
";
123123
var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false);
124124

125-
var styleCopSettings = context.GetStyleCopSettings();
125+
var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None);
126126

127127
Assert.Equal("[InvalidReference]", styleCopSettings.DocumentationRules.CopyrightText);
128128
}
@@ -133,7 +133,7 @@ public async Task VerifyInvalidJsonBehaviorAsync()
133133
var settings = @"This is not a JSON file.";
134134
var context = await CreateAnalysisContextAsync(settings).ConfigureAwait(false);
135135

136-
var styleCopSettings = context.GetStyleCopSettings();
136+
var styleCopSettings = context.GetStyleCopSettings(CancellationToken.None);
137137

138138
// The result is the same as the default settings.
139139
Assert.Equal("PlaceholderCompany", styleCopSettings.DocumentationRules.CompanyName);

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1004UnitTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,58 @@ private void Method3()
166166
await this.VerifyCSharpDiagnosticAsync(testCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
167167
}
168168

169+
/// <summary>
170+
/// Verifies that a multi-line documentation comment without leading spaces is handled correctly.
171+
/// </summary>
172+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
173+
[Fact]
174+
public async Task TestMultilineDocumentationCommentWithoutLeadingSpacesAsync()
175+
{
176+
string testCode = @"
177+
public class TypeName
178+
{
179+
/**
180+
*<summary>
181+
* The summary text.
182+
*</summary>
183+
* <param name=""x"">The document root.</param>
184+
* <param name=""y"">The XML header token.</param>
185+
*/
186+
private void Method1(int x, int y)
187+
{
188+
}
189+
}
190+
";
191+
192+
string fixedCode = @"
193+
public class TypeName
194+
{
195+
/**
196+
* <summary>
197+
* The summary text.
198+
* </summary>
199+
* <param name=""x"">The document root.</param>
200+
* <param name=""y"">The XML header token.</param>
201+
*/
202+
private void Method1(int x, int y)
203+
{
204+
}
205+
}
206+
";
207+
208+
DiagnosticResult[] expected =
209+
{
210+
this.CSharpDiagnostic().WithLocation(5, 7),
211+
this.CSharpDiagnostic().WithLocation(7, 7),
212+
this.CSharpDiagnostic().WithLocation(8, 7),
213+
this.CSharpDiagnostic().WithLocation(9, 7)
214+
};
215+
216+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
217+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
218+
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
219+
}
220+
169221
/// <inheritdoc/>
170222
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
171223
{

StyleCop.Analyzers/StyleCop.Analyzers.Test/SpacingRules/SA1028UnitTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,51 @@ public async Task TrailingWhitespaceAfterTextTokenAsync()
329329
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
330330
}
331331

332+
/// <summary>
333+
/// Verifies that trailing whitespace after a multi-line documentation comment is handled properly.
334+
/// This is a regression test for #821
335+
/// </summary>
336+
/// <returns>A <see cref="Task"/> representing the asynchronous unit test.</returns>
337+
[Fact]
338+
public async Task VerifyTrailingWhitespaceInsideMultiLineXmlDocumentationCommentAsync()
339+
{
340+
string testCode = new StringBuilder()
341+
.AppendLine("/**")
342+
.AppendLine(" * <summary> ")
343+
.AppendLine(" * Some description ")
344+
.AppendLine(" * </summary> ")
345+
.AppendLine(" */")
346+
.AppendLine("class Foo { }")
347+
.ToString();
348+
349+
string fixedCode = new StringBuilder()
350+
.AppendLine("/**")
351+
.AppendLine(" * <summary>")
352+
.AppendLine(" * Some description")
353+
.AppendLine(" * </summary>")
354+
.AppendLine(" */")
355+
.AppendLine("class Foo { }")
356+
.ToString();
357+
358+
DiagnosticResult[] expected =
359+
{
360+
this.CSharpDiagnostic().WithLocation(2, 13),
361+
this.CSharpDiagnostic().WithLocation(3, 20),
362+
this.CSharpDiagnostic().WithLocation(4, 14)
363+
};
364+
365+
await this.VerifyCSharpDiagnosticAsync(testCode, expected, CancellationToken.None).ConfigureAwait(false);
366+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
367+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
368+
}
369+
370+
/// <inheritdoc/>
332371
protected override IEnumerable<DiagnosticAnalyzer> GetCSharpDiagnosticAnalyzers()
333372
{
334373
yield return new SA1028CodeMustNotContainTrailingWhitespace();
335374
}
336375

376+
/// <inheritdoc/>
337377
protected override CodeFixProvider GetCSharpCodeFixProvider()
338378
{
339379
return new SA1028CodeFixProvider();

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/FileHeaderAnalyzers.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.DocumentationRules
66
using System;
77
using System.Collections.Immutable;
88
using System.IO;
9+
using System.Threading;
910
using System.Xml.Linq;
1011
using Microsoft.CodeAnalysis;
1112
using Microsoft.CodeAnalysis.Diagnostics;
@@ -181,7 +182,7 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
181182
// Disabling SA1633 will disable all other header related diagnostics.
182183
if (!compilation.IsAnalyzerSuppressed(SA1633Identifier))
183184
{
184-
Analyzer analyzer = new Analyzer(context.Options);
185+
Analyzer analyzer = new Analyzer(context.Options, context.CancellationToken);
185186
context.RegisterSyntaxTreeActionHonorExclusions(ctx => analyzer.HandleSyntaxTreeAction(ctx, compilation));
186187
}
187188
}
@@ -190,9 +191,9 @@ private sealed class Analyzer
190191
{
191192
private readonly DocumentationSettings documentationSettings;
192193

193-
public Analyzer(AnalyzerOptions options)
194+
public Analyzer(AnalyzerOptions options, CancellationToken cancellationToken)
194195
{
195-
StyleCopSettings settings = options.GetStyleCopSettings();
196+
StyleCopSettings settings = options.GetStyleCopSettings(cancellationToken);
196197
this.documentationSettings = settings.DocumentationRules;
197198
}
198199

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1600ElementsMustBeDocumented.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.DocumentationRules
66
using System;
77
using System.Collections.Immutable;
88
using System.Linq;
9+
using System.Threading;
910
using Helpers;
1011
using Microsoft.CodeAnalysis;
1112
using Microsoft.CodeAnalysis.CSharp;
@@ -58,7 +59,7 @@ public override void Initialize(AnalysisContext context)
5859

5960
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
6061
{
61-
Analyzer analyzer = new Analyzer(context.Options);
62+
Analyzer analyzer = new Analyzer(context.Options, context.CancellationToken);
6263
context.RegisterSyntaxNodeActionHonorExclusions(analyzer.HandleBaseTypeDeclaration, BaseTypeDeclarationKinds);
6364
context.RegisterSyntaxNodeActionHonorExclusions(analyzer.HandleMethodDeclaration, SyntaxKind.MethodDeclaration);
6465
context.RegisterSyntaxNodeActionHonorExclusions(analyzer.HandleConstructorDeclaration, SyntaxKind.ConstructorDeclaration);
@@ -75,9 +76,9 @@ private class Analyzer
7576
{
7677
private readonly DocumentationSettings documentationSettings;
7778

78-
public Analyzer(AnalyzerOptions options)
79+
public Analyzer(AnalyzerOptions options, CancellationToken cancellationToken)
7980
{
80-
StyleCopSettings settings = options.GetStyleCopSettings();
81+
StyleCopSettings settings = options.GetStyleCopSettings(cancellationToken);
8182
this.documentationSettings = settings.DocumentationRules;
8283
}
8384

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1601PartialElementsMustBeDocumented.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace StyleCop.Analyzers.DocumentationRules
66
using System;
77
using System.Collections.Immutable;
88
using System.Linq;
9+
using System.Threading;
910
using Microsoft.CodeAnalysis;
1011
using Microsoft.CodeAnalysis.CSharp;
1112
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -99,7 +100,7 @@ public override void Initialize(AnalysisContext context)
99100

100101
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
101102
{
102-
Analyzer analyzer = new Analyzer(context.Options);
103+
Analyzer analyzer = new Analyzer(context.Options, context.CancellationToken);
103104
context.RegisterSyntaxNodeActionHonorExclusions(analyzer.HandleBaseTypeDeclaration, BaseTypeDeclarationKinds);
104105
context.RegisterSyntaxNodeActionHonorExclusions(analyzer.HandleMethodDeclaration, SyntaxKind.MethodDeclaration);
105106
}
@@ -108,9 +109,9 @@ private class Analyzer
108109
{
109110
private readonly DocumentationSettings documentationSettings;
110111

111-
public Analyzer(AnalyzerOptions options)
112+
public Analyzer(AnalyzerOptions options, CancellationToken cancellationToken)
112113
{
113-
StyleCopSettings settings = options.GetStyleCopSettings();
114+
StyleCopSettings settings = options.GetStyleCopSettings(cancellationToken);
114115
this.documentationSettings = settings.DocumentationRules;
115116
}
116117

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1602EnumerationItemsMustBeDocumented.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace StyleCop.Analyzers.DocumentationRules
55
{
66
using System;
77
using System.Collections.Immutable;
8+
using System.Threading;
89
using Microsoft.CodeAnalysis;
910
using Microsoft.CodeAnalysis.CSharp;
1011
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -66,17 +67,17 @@ public override void Initialize(AnalysisContext context)
6667

6768
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
6869
{
69-
Analyzer analyzer = new Analyzer(context.Options);
70+
Analyzer analyzer = new Analyzer(context.Options, context.CancellationToken);
7071
context.RegisterSyntaxNodeActionHonorExclusions(analyzer.HandleEnumMember, SyntaxKind.EnumMemberDeclaration);
7172
}
7273

7374
private class Analyzer
7475
{
7576
private readonly DocumentationSettings documentationSettings;
7677

77-
public Analyzer(AnalyzerOptions options)
78+
public Analyzer(AnalyzerOptions options, CancellationToken cancellationToken)
7879
{
79-
StyleCopSettings settings = options.GetStyleCopSettings();
80+
StyleCopSettings settings = options.GetStyleCopSettings(cancellationToken);
8081
this.documentationSettings = settings.DocumentationRules;
8182
}
8283

StyleCop.Analyzers/StyleCop.Analyzers/DocumentationRules/SA1649FileNameMustMatchTypeName.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace StyleCop.Analyzers.DocumentationRules
77
using System.Collections.Immutable;
88
using System.IO;
99
using System.Linq;
10+
using System.Threading;
1011
using Helpers;
1112
using Microsoft.CodeAnalysis;
1213
using Microsoft.CodeAnalysis.CSharp;
@@ -55,17 +56,17 @@ public override void Initialize(AnalysisContext context)
5556

5657
private static void HandleCompilationStart(CompilationStartAnalysisContext context)
5758
{
58-
var analyzer = new Analyzer(context.Options);
59+
var analyzer = new Analyzer(context.Options, context.CancellationToken);
5960
context.RegisterSyntaxTreeActionHonorExclusions(analyzer.HandleSyntaxTreeAction);
6061
}
6162

6263
private class Analyzer
6364
{
6465
private readonly FileNamingConvention fileNamingConvention;
6566

66-
public Analyzer(AnalyzerOptions options)
67+
public Analyzer(AnalyzerOptions options, CancellationToken cancellationToken)
6768
{
68-
StyleCopSettings settings = options.GetStyleCopSettings();
69+
StyleCopSettings settings = options.GetStyleCopSettings(cancellationToken);
6970
this.fileNamingConvention = settings.DocumentationRules.FileNamingConvention;
7071
}
7172

0 commit comments

Comments
 (0)