Skip to content

Commit 7f711ee

Browse files
committed
Merge pull request #1567 from sharwell/header-fixes
Additional file header code fixes
2 parents f197f29 + 6f1012a commit 7f711ee

7 files changed

Lines changed: 121 additions & 11 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1634UnitTests.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,27 @@ public async Task TestFileHeaderWithMissingCopyrightTagAsync()
2828
// </author>
2929
// <summary>This is a test file.</summary>
3030
31+
namespace Bar
32+
{
33+
}
34+
";
35+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
36+
// Copyright (c) FooCorp. All rights reserved.
37+
// </copyright>
38+
// <author>
39+
// John Doe
40+
// </author>
41+
// <summary>This is a test file.</summary>
42+
3143
namespace Bar
3244
{
3345
}
3446
";
3547

3648
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
3749
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
50+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
51+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3852
}
3953

4054
/// <summary>
@@ -68,13 +82,23 @@ public async Task TestValidFileHeaderWithShorthandCopyrightAsync()
6882
{
6983
var testCode = @"// <copyright file=""Test0.cs"" company=""FooCorp""/>
7084
85+
namespace Bar
86+
{
87+
}
88+
";
89+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
90+
// Copyright (c) FooCorp. All rights reserved.
91+
// </copyright>
92+
7193
namespace Bar
7294
{
7395
}
7496
";
7597

7698
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1635Descriptor).WithLocation(1, 4);
7799
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
100+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
101+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
78102
}
79103

80104
/// <summary>
@@ -88,13 +112,26 @@ public async Task TestInvalidFileHeaderWithCopyrightInWrongCaseAsync()
88112
// Copyright (c) FooCorp. All rights reserved.
89113
// </Copyright>
90114
115+
namespace Bar
116+
{
117+
}
118+
";
119+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
120+
// Copyright (c) FooCorp. All rights reserved.
121+
// </copyright>
122+
// <Copyright file=""Test0.cs"" company=""FooCorp"">
123+
// Copyright (c) FooCorp. All rights reserved.
124+
// </Copyright>
125+
91126
namespace Bar
92127
{
93128
}
94129
";
95130

96131
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
97132
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
133+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
134+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
98135
}
99136

100137
/// <summary>
@@ -162,7 +199,7 @@ namespace Bar
162199
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 5);
163200
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
164201
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
165-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
202+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
166203
}
167204

168205
/// <summary>
@@ -197,7 +234,7 @@ namespace Bar
197234
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
198235
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
199236
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
200-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
237+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
201238
}
202239

203240
/// <summary>
@@ -234,7 +271,7 @@ namespace Bar
234271
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
235272
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
236273
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
237-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
274+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
238275
}
239276

240277
/// <summary>
@@ -271,7 +308,7 @@ namespace Bar
271308
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
272309
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
273310
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
274-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
311+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
275312
}
276313

277314
/// <summary>
@@ -316,7 +353,7 @@ namespace Bar
316353
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1634Descriptor).WithLocation(1, 1);
317354
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
318355
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
319-
await this.VerifyCSharpFixAsync(testCode, fixedCode).ConfigureAwait(false);
356+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
320357
}
321358

322359
protected override CodeFixProvider GetCSharpCodeFixProvider()

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1637UnitTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,28 @@ public async Task TestCopyrightElementWithoutFileAttributeAsync()
2525
// Copyright (c) FooCorp. All rights reserved.
2626
// </copyright>
2727
28+
namespace Bar
29+
{
30+
}
31+
";
32+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
33+
// Copyright (c) FooCorp. All rights reserved.
34+
// </copyright>
35+
2836
namespace Bar
2937
{
3038
}
3139
";
3240

3341
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1637Descriptor).WithLocation(1, 4);
3442
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
43+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3545
}
3646

3747
protected override CodeFixProvider GetCSharpCodeFixProvider()
3848
{
39-
throw new System.NotImplementedException();
49+
return new FileHeaderCodeFixProvider();
4050
}
4151
}
4252
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1638UnitTests.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,28 @@ public async Task TestCopyrightElementWithMismatchingFileAttributeAsync()
2525
// Copyright (c) FooCorp. All rights reserved.
2626
// </copyright>
2727
28+
namespace Bar
29+
{
30+
}
31+
";
32+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
33+
// Copyright (c) FooCorp. All rights reserved.
34+
// </copyright>
35+
2836
namespace Bar
2937
{
3038
}
3139
";
3240

3341
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1638Descriptor).WithLocation(1, 4);
3442
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
43+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3545
}
3646

3747
protected override CodeFixProvider GetCSharpCodeFixProvider()
3848
{
39-
throw new System.NotImplementedException();
49+
return new FileHeaderCodeFixProvider();
4050
}
4151
}
4252
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1640UnitTests.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ public async Task TestCopyrightElementWithoutCompanyAttributeAsync()
2525
// Copyright (c) FooCorp. All rights reserved.
2626
// </copyright>
2727
28+
namespace Bar
29+
{
30+
}
31+
";
32+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
33+
// Copyright (c) FooCorp. All rights reserved.
34+
// </copyright>
35+
2836
namespace Bar
2937
{
3038
}
3139
";
3240

3341
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1640Descriptor).WithLocation(1, 4);
3442
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
43+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3545
}
3646

3747
/// <summary>
@@ -45,13 +55,23 @@ public async Task TestCopyrightElementWithEmptyCompanyAttributeAsync()
4555
// Copyright (c) FooCorp. All rights reserved.
4656
// </copyright>
4757
58+
namespace Bar
59+
{
60+
}
61+
";
62+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
63+
// Copyright (c) FooCorp. All rights reserved.
64+
// </copyright>
65+
4866
namespace Bar
4967
{
5068
}
5169
";
5270

5371
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1640Descriptor).WithLocation(1, 4);
5472
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
73+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
74+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
5575
}
5676

5777
/// <summary>
@@ -65,18 +85,28 @@ public async Task TestCopyrightElementWithWhitespaceOnlyCompanyAttributeAsync()
6585
// Copyright (c) FooCorp. All rights reserved.
6686
// </copyright>
6787
88+
namespace Bar
89+
{
90+
}
91+
";
92+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
93+
// Copyright (c) FooCorp. All rights reserved.
94+
// </copyright>
95+
6896
namespace Bar
6997
{
7098
}
7199
";
72100

73101
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1640Descriptor).WithLocation(1, 4);
74102
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
103+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
104+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
75105
}
76106

77107
protected override CodeFixProvider GetCSharpCodeFixProvider()
78108
{
79-
throw new System.NotImplementedException();
109+
return new FileHeaderCodeFixProvider();
80110
}
81111
}
82112
}

StyleCop.Analyzers/StyleCop.Analyzers.Test/DocumentationRules/SA1641UnitTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ public async Task TestCopyrightElementWithWrongCompanyAttributeAsync()
2525
// Copyright (c) FooCorp. All rights reserved.
2626
// </copyright>
2727
28+
namespace Bar
29+
{
30+
}
31+
";
32+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
33+
// Copyright (c) FooCorp. All rights reserved.
34+
// </copyright>
35+
2836
namespace Bar
2937
{
3038
}
3139
";
3240

3341
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1641Descriptor).WithLocation(1, 4);
3442
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
43+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
44+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
3545
}
3646

3747
/// <summary>
@@ -45,18 +55,28 @@ public async Task TestCopyrightElementWithInvalidCaseCompanyAttributeAsync()
4555
// Copyright (c) FooCorp. All rights reserved.
4656
// </copyright>
4757
58+
namespace Bar
59+
{
60+
}
61+
";
62+
var fixedCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
63+
// Copyright (c) FooCorp. All rights reserved.
64+
// </copyright>
65+
4866
namespace Bar
4967
{
5068
}
5169
";
5270

5371
var expectedDiagnostic = this.CSharpDiagnostic(FileHeaderAnalyzers.SA1641Descriptor).WithLocation(1, 4);
5472
await this.VerifyCSharpDiagnosticAsync(testCode, expectedDiagnostic, CancellationToken.None).ConfigureAwait(false);
73+
await this.VerifyCSharpDiagnosticAsync(fixedCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
74+
await this.VerifyCSharpFixAsync(testCode, fixedCode, cancellationToken: CancellationToken.None).ConfigureAwait(false);
5575
}
5676

5777
protected override CodeFixProvider GetCSharpCodeFixProvider()
5878
{
59-
throw new System.NotImplementedException();
79+
return new FileHeaderCodeFixProvider();
6080
}
6181
}
6282
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ namespace StyleCop.Analyzers.DocumentationRules
2424
/// <seealso href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1639.md">SA1639 File header must have summary</seealso>
2525
/// <seealso href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1640.md">SA1640 File header must have valid company text</seealso>
2626
/// <seealso href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1641.md">SA1641 File header company name text must match</seealso>
27-
/// <seealso href="https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1649.md">SA1649 File header file name documentation must match type name</seealso>
2827
[DiagnosticAnalyzer(LanguageNames.CSharp)]
2928
internal class FileHeaderAnalyzers : DiagnosticAnalyzer
3029
{

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ internal class FileHeaderCodeFixProvider : CodeFixProvider
3535
FileHeaderAnalyzers.SA1633DescriptorMissing.Id,
3636
FileHeaderAnalyzers.SA1634Descriptor.Id,
3737
FileHeaderAnalyzers.SA1635Descriptor.Id,
38-
FileHeaderAnalyzers.SA1636Descriptor.Id);
38+
FileHeaderAnalyzers.SA1636Descriptor.Id,
39+
FileHeaderAnalyzers.SA1637Descriptor.Id,
40+
FileHeaderAnalyzers.SA1638Descriptor.Id,
41+
FileHeaderAnalyzers.SA1640Descriptor.Id,
42+
FileHeaderAnalyzers.SA1641Descriptor.Id);
3943

4044
/// <inheritdoc/>
4145
public override FixAllProvider GetFixAllProvider()

0 commit comments

Comments
 (0)