-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RequireNamedArgumentsAnalyzer: add support for exemptions (#961)
ExemptSymbolsBuilder is a helper for building sets of exempt symbols without bespoke logic per-analyzer. The AdditionalFile format is similar to Roslyn's BannedApiAnalyzer: https://github.com/dotnet/roslyn-analyzers/blob/3211f48253bc18560156d90dc5e710d35f7d03fa/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/BannedApiAnalyzers.Help.md In this case the optional [;Description Text] is excluded from the format, as exemptions do not lead to user-facing messages. Comments are supported for developers to leave notes justifying the exemption.
- Loading branch information
Showing
3 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
src/D2L.CodeStyle.Analyzers/Helpers/ExemptSymbolsBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace D2L.CodeStyle.Analyzers.Helpers; | ||
|
||
internal sealed class ExemptSymbolsBuilder { | ||
|
||
private readonly Compilation m_compilation; | ||
private readonly AnalyzerOptions m_analyzerOptions; | ||
private readonly CancellationToken m_cancellationToken; | ||
|
||
private readonly HashSet<ISymbol> m_exemptions = new( SymbolEqualityComparer.Default ); | ||
private bool m_built = false; | ||
|
||
[System.Diagnostics.CodeAnalysis.SuppressMessage( | ||
"MicrosoftCodeAnalysisPerformance", | ||
"RS1012:Start action has no registered actions", | ||
Justification = "Not an analyzer" | ||
)] | ||
public ExemptSymbolsBuilder( | ||
CompilationStartAnalysisContext context | ||
) { | ||
m_compilation = context.Compilation; | ||
m_analyzerOptions = context.Options; | ||
m_cancellationToken = context.CancellationToken; | ||
} | ||
|
||
public HashSet<ISymbol> Build() { | ||
ThrowIfBuilt(); | ||
|
||
m_built = true; | ||
return m_exemptions; | ||
} | ||
|
||
/// <summary> | ||
/// Loads exemptions from AdditionalFiles matching "<paramref name="fileNameBase"/>.txt" and "<paramref name="fileNameBase"/>.*.txt". | ||
/// Each file should contain a series of lines, each starting with a DocumentationCommentDelcarationId, with an optional trailing comment (//). | ||
/// </summary> | ||
public ExemptSymbolsBuilder AddFromAdditionalFiles( | ||
string fileNameBase | ||
) { | ||
ThrowIfBuilt(); | ||
|
||
fileNameBase += "."; | ||
|
||
foreach( AdditionalText file in m_analyzerOptions.AdditionalFiles ) { | ||
string fileName = Path.GetFileName( file.Path ); | ||
if( fileName is null ) { | ||
continue; | ||
} | ||
|
||
bool fileNameMatch = fileName.StartsWith( fileNameBase, StringComparison.Ordinal ) && fileName.EndsWith( ".txt", StringComparison.Ordinal ); | ||
if( !fileNameMatch ) { | ||
continue; | ||
} | ||
|
||
SourceText? sourceText = file.GetText( m_cancellationToken ); | ||
if( sourceText is null ) { | ||
continue; | ||
} | ||
|
||
foreach( TextLine line in sourceText.Lines ) { | ||
ReadOnlySpan<char> text = line.ToString().AsSpan(); | ||
|
||
int commentIndex = text.IndexOf( "//".AsSpan(), StringComparison.Ordinal ); | ||
if( commentIndex != -1 ) { | ||
text = text.Slice( 0, commentIndex ); | ||
} | ||
|
||
text = text.TrimEnd(); | ||
|
||
if( text.Length > 0 ) { | ||
AddFromDocumentationCommentId( text.ToString() ); | ||
} | ||
} | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public ExemptSymbolsBuilder AddFromDocumentationCommentId( string id ) { | ||
ThrowIfBuilt(); | ||
|
||
ImmutableArray<ISymbol> symbols = DocumentationCommentId.GetSymbolsForDeclarationId( id, m_compilation ); | ||
foreach( var symbol in symbols ) { | ||
m_exemptions.Add( symbol ); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
private void ThrowIfBuilt() { | ||
if( m_built ) { | ||
throw new InvalidOperationException( "Builder instance has already been used" ); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters