Skip to content

Commit 436d870

Browse files
committed
Update FileHeaderAnalyzers to use the new cached settings support
1 parent 5b8372b commit 436d870

1 file changed

Lines changed: 21 additions & 31 deletions

File tree

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

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace StyleCop.Analyzers.DocumentationRules
66
using System;
77
using System.Collections.Immutable;
88
using System.IO;
9-
using System.Threading;
109
using System.Xml.Linq;
1110
using Microsoft.CodeAnalysis;
1211
using Microsoft.CodeAnalysis.Diagnostics;
@@ -182,22 +181,13 @@ private static void HandleCompilationStart(CompilationStartAnalysisContext conte
182181
// Disabling SA1633 will disable all other header related diagnostics.
183182
if (!compilation.IsAnalyzerSuppressed(SA1633Identifier))
184183
{
185-
Analyzer analyzer = new Analyzer(context.Options, context.CancellationToken);
186-
context.RegisterSyntaxTreeActionHonorExclusions(ctx => analyzer.HandleSyntaxTreeAction(ctx, compilation));
184+
context.RegisterSyntaxTreeActionHonorExclusions((ctx, settings) => Analyzer.HandleSyntaxTree(ctx, settings, compilation));
187185
}
188186
}
189187

190-
private sealed class Analyzer
188+
private static class Analyzer
191189
{
192-
private readonly DocumentationSettings documentationSettings;
193-
194-
public Analyzer(AnalyzerOptions options, CancellationToken cancellationToken)
195-
{
196-
StyleCopSettings settings = options.GetStyleCopSettings(cancellationToken);
197-
this.documentationSettings = settings.DocumentationRules;
198-
}
199-
200-
public void HandleSyntaxTreeAction(SyntaxTreeAnalysisContext context, Compilation compilation)
190+
public static void HandleSyntaxTree(SyntaxTreeAnalysisContext context, StyleCopSettings settings, Compilation compilation)
201191
{
202192
var root = context.Tree.GetRoot(context.CancellationToken);
203193

@@ -207,7 +197,7 @@ public void HandleSyntaxTreeAction(SyntaxTreeAnalysisContext context, Compilatio
207197
return;
208198
}
209199

210-
if (this.documentationSettings.XmlHeader)
200+
if (settings.DocumentationRules.XmlHeader)
211201
{
212202
var fileHeader = FileHeaderHelpers.ParseXmlFileHeader(root);
213203
if (fileHeader.IsMissing)
@@ -224,12 +214,12 @@ public void HandleSyntaxTreeAction(SyntaxTreeAnalysisContext context, Compilatio
224214

225215
if (!compilation.IsAnalyzerSuppressed(SA1634Identifier))
226216
{
227-
this.CheckCopyrightHeader(context, compilation, fileHeader);
217+
CheckCopyrightHeader(context, settings.DocumentationRules, compilation, fileHeader);
228218
}
229219

230220
if (!compilation.IsAnalyzerSuppressed(SA1639Identifier))
231221
{
232-
this.CheckSummaryHeader(context, compilation, fileHeader);
222+
CheckSummaryHeader(context, compilation, fileHeader);
233223
}
234224
}
235225
else
@@ -254,7 +244,7 @@ public void HandleSyntaxTreeAction(SyntaxTreeAnalysisContext context, Compilatio
254244
return;
255245
}
256246

257-
if (!this.CompareCopyrightText(fileHeader.CopyrightText))
247+
if (!CompareCopyrightText(settings.DocumentationRules, fileHeader.CopyrightText))
258248
{
259249
context.ReportDiagnostic(Diagnostic.Create(SA1636Descriptor, fileHeader.GetLocation(context.Tree)));
260250
return;
@@ -263,7 +253,7 @@ public void HandleSyntaxTreeAction(SyntaxTreeAnalysisContext context, Compilatio
263253
}
264254
}
265255

266-
private void CheckCopyrightHeader(SyntaxTreeAnalysisContext context, Compilation compilation, XmlFileHeader fileHeader)
256+
private static void CheckCopyrightHeader(SyntaxTreeAnalysisContext context, DocumentationSettings documentationSettings, Compilation compilation, XmlFileHeader fileHeader)
267257
{
268258
var copyrightElement = fileHeader.GetElement("copyright");
269259
if (copyrightElement == null)
@@ -274,21 +264,21 @@ private void CheckCopyrightHeader(SyntaxTreeAnalysisContext context, Compilation
274264

275265
if (!compilation.IsAnalyzerSuppressed(SA1637Identifier))
276266
{
277-
this.CheckFile(context, compilation, fileHeader, copyrightElement);
267+
CheckFile(context, compilation, fileHeader, copyrightElement);
278268
}
279269

280270
if (!compilation.IsAnalyzerSuppressed(SA1640Identifier))
281271
{
282-
this.CheckCompanyName(context, compilation, fileHeader, copyrightElement);
272+
CheckCompanyName(context, documentationSettings, compilation, fileHeader, copyrightElement);
283273
}
284274

285275
if (!compilation.IsAnalyzerSuppressed(SA1635Identifier))
286276
{
287-
this.CheckCopyrightText(context, compilation, fileHeader, copyrightElement);
277+
CheckCopyrightText(context, documentationSettings, compilation, fileHeader, copyrightElement);
288278
}
289279
}
290280

291-
private void CheckFile(SyntaxTreeAnalysisContext context, Compilation compilation, XmlFileHeader fileHeader, XElement copyrightElement)
281+
private static void CheckFile(SyntaxTreeAnalysisContext context, Compilation compilation, XmlFileHeader fileHeader, XElement copyrightElement)
292282
{
293283
var fileAttribute = copyrightElement.Attribute("file");
294284
if (fileAttribute == null)
@@ -311,7 +301,7 @@ private void CheckFile(SyntaxTreeAnalysisContext context, Compilation compilatio
311301
}
312302
}
313303

314-
private void CheckCopyrightText(SyntaxTreeAnalysisContext context, Compilation compilation, XmlFileHeader fileHeader, XElement copyrightElement)
304+
private static void CheckCopyrightText(SyntaxTreeAnalysisContext context, DocumentationSettings documentationSettings, Compilation compilation, XmlFileHeader fileHeader, XElement copyrightElement)
315305
{
316306
var copyrightText = copyrightElement.Value;
317307
if (string.IsNullOrWhiteSpace(copyrightText))
@@ -326,22 +316,22 @@ private void CheckCopyrightText(SyntaxTreeAnalysisContext context, Compilation c
326316
return;
327317
}
328318

329-
var settingsCopyrightText = this.documentationSettings.CopyrightText;
319+
var settingsCopyrightText = documentationSettings.CopyrightText;
330320
if (string.Equals(settingsCopyrightText, DocumentationSettings.DefaultCopyrightText, StringComparison.OrdinalIgnoreCase))
331321
{
332322
// The copyright text is meaningless until the company name is configured by the user.
333323
return;
334324
}
335325

336326
// trim any leading / trailing new line or whitespace characters (those are a result of the XML formatting)
337-
if (!this.CompareCopyrightText(copyrightText.Trim('\r', '\n', ' ', '\t')))
327+
if (!CompareCopyrightText(documentationSettings, copyrightText.Trim('\r', '\n', ' ', '\t')))
338328
{
339329
var location = fileHeader.GetElementLocation(context.Tree, copyrightElement);
340330
context.ReportDiagnostic(Diagnostic.Create(SA1636Descriptor, location));
341331
}
342332
}
343333

344-
private void CheckCompanyName(SyntaxTreeAnalysisContext context, Compilation compilation, XmlFileHeader fileHeader, XElement copyrightElement)
334+
private static void CheckCompanyName(SyntaxTreeAnalysisContext context, DocumentationSettings documentationSettings, Compilation compilation, XmlFileHeader fileHeader, XElement copyrightElement)
345335
{
346336
var companyName = copyrightElement.Attribute("company")?.Value;
347337
if (string.IsNullOrWhiteSpace(companyName))
@@ -356,20 +346,20 @@ private void CheckCompanyName(SyntaxTreeAnalysisContext context, Compilation com
356346
return;
357347
}
358348

359-
if (string.Equals(this.documentationSettings.CompanyName, DocumentationSettings.DefaultCompanyName, StringComparison.OrdinalIgnoreCase))
349+
if (string.Equals(documentationSettings.CompanyName, DocumentationSettings.DefaultCompanyName, StringComparison.OrdinalIgnoreCase))
360350
{
361351
// The company name is meaningless until configured by the user.
362352
return;
363353
}
364354

365-
if (string.CompareOrdinal(companyName, this.documentationSettings.CompanyName) != 0)
355+
if (string.CompareOrdinal(companyName, documentationSettings.CompanyName) != 0)
366356
{
367357
var location = fileHeader.GetElementLocation(context.Tree, copyrightElement);
368358
context.ReportDiagnostic(Diagnostic.Create(SA1641Descriptor, location));
369359
}
370360
}
371361

372-
private void CheckSummaryHeader(SyntaxTreeAnalysisContext context, Compilation compilation, XmlFileHeader fileHeader)
362+
private static void CheckSummaryHeader(SyntaxTreeAnalysisContext context, Compilation compilation, XmlFileHeader fileHeader)
373363
{
374364
var summaryElement = fileHeader.GetElement("summary");
375365
if (summaryElement == null)
@@ -385,10 +375,10 @@ private void CheckSummaryHeader(SyntaxTreeAnalysisContext context, Compilation c
385375
}
386376
}
387377

388-
private bool CompareCopyrightText(string copyrightText)
378+
private static bool CompareCopyrightText(DocumentationSettings documentationSettings, string copyrightText)
389379
{
390380
// make sure that both \n and \r\n are accepted from the settings.
391-
var reformattedCopyrightTextParts = this.documentationSettings.CopyrightText.Replace("\r\n", "\n").Split('\n');
381+
var reformattedCopyrightTextParts = documentationSettings.CopyrightText.Replace("\r\n", "\n").Split('\n');
392382
var fileHeaderCopyrightTextParts = copyrightText.Replace("\r\n", "\n").Split('\n');
393383

394384
if (reformattedCopyrightTextParts.Length != fileHeaderCopyrightTextParts.Length)

0 commit comments

Comments
 (0)