-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
DiagnosticSuppressor
to suppress the IDE0002 Style Hint in the …
…IDE (#9310) Context: #8381 Add a `DiagnosticSuppressor` to "turn off" the `IDE0002` style diagnostic message which incorrectly tells users to use the `_Microsoft.Android.Resource.Designer.ResourceConstant` type directly. This can cause allot of annoyance with our users because it appears on EVERY single usage of `Resource.*`. So you end up with what looks like code spagetti. So we need to start shipping an `Analyzer` assembly along with the `Ref` framework pack. This is the place these things need to go. Unfortunately it means that the older frameworks will not get this analyzer. Only the current one. On the packaging side, the Analyzer assembly has to go in a `analyzers/dotnet/<language>` folder in the .Ref Nuget Package. There also needs to be an entry in the `FrameworkList.xml` file which has a `Type="Analyzer" ` and a `Language="cs"`. This allows the IDE's to pickup the code. We can ship both regular Analyzers and the DiagnosticSuppressors in the same assembly. So we can extend this with more if needed. How this works is we use Rosyln to look for the IDE0002 diagnsotic message, we then check the code and see if it is refering to a `_Microsoft.Android.Resource.Designer.*` derived class. If it is , we add a suppression. This will stop the hint appearing in the IDE. I have tested this on VS in devbox and it appears to work . Also we generate a Resource Designer assembly and an Intermediate source file at build time. Both of these contain classes which should have the `GeneratedCode` Attribute. So lets add it. The version will be the same as the build assembly used to generate it.
- Loading branch information
1 parent
27b5d2e
commit b3079db
Showing
11 changed files
with
139 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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
19 changes: 19 additions & 0 deletions
19
src/Microsoft.Android.Sdk.Analysis/Microsoft.Android.Sdk.Analysis.csproj
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,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\..\Configuration.props" /> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> | ||
<OutputPath>$(MicrosoftAndroidSdkAnalysisOutDir)</OutputPath> | ||
<IsRoslynComponent>true</IsRoslynComponent> | ||
<LangVersion>latest</LangVersion> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
<AssemblyOriginatorKeyFile>..\..\product.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" PrivateAssets="all" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" /> | ||
</ItemGroup> | ||
</Project> |
71 changes: 71 additions & 0 deletions
71
src/Microsoft.Android.Sdk.Analysis/ResourceDesignerDiagnosticSuppressor.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,71 @@ | ||
using System.Linq; | ||
using System.Collections.Immutable; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class ResourceDesignerDiagnosticSuppressor : DiagnosticSuppressor | ||
{ | ||
private const string DesignerNamespace = "_Microsoft.Android.Resource.Designer"; | ||
private static readonly SuppressionDescriptor Rule = new( | ||
"XAD0001", | ||
"IDE0002", | ||
"The Resource Designer class should not be simplified." | ||
); | ||
|
||
public override ImmutableArray<SuppressionDescriptor> SupportedSuppressions | ||
=> ImmutableArray.Create(Rule); | ||
|
||
public override void ReportSuppressions(SuppressionAnalysisContext context) | ||
{ | ||
foreach (var diagnostic in context.ReportedDiagnostics) | ||
{ | ||
if (diagnostic.Id != Rule.SuppressedDiagnosticId) | ||
continue; | ||
Location location = diagnostic.Location; | ||
SyntaxTree syntaxTree = location.SourceTree; | ||
if (syntaxTree is null) | ||
continue; | ||
|
||
SyntaxNode root = syntaxTree.GetRoot(context.CancellationToken); | ||
SyntaxNode syntaxNode = root.FindNode(location.SourceSpan) | ||
.DescendantNodesAndSelf() | ||
.FirstOrDefault (); | ||
|
||
if (syntaxNode is null) | ||
continue; | ||
|
||
SemanticModel model = context.GetSemanticModel(syntaxTree); | ||
ISymbol typeSymbol = model.GetSymbolInfo (syntaxNode).Symbol; | ||
if (typeSymbol is not INamedTypeSymbol namedTypeSymbol) | ||
continue; | ||
|
||
if (IsResourceDesignerDerivedType(namedTypeSymbol)) | ||
{ | ||
Suppression suppression = Suppression.Create(Rule, diagnostic); | ||
context.ReportSuppression(suppression); | ||
} | ||
} | ||
} | ||
|
||
private static bool IsResourceDesignerDerivedType(INamedTypeSymbol typeSymbol) | ||
{ | ||
return IsDerivedFrom(typeSymbol, DesignerNamespace); | ||
} | ||
|
||
private static bool IsDerivedFrom(INamedTypeSymbol typeSymbol, string baseClassName) | ||
{ | ||
while (typeSymbol != null) | ||
{ | ||
if (typeSymbol.ToDisplayString().StartsWith(baseClassName)) | ||
{ | ||
return true; | ||
} | ||
typeSymbol = typeSymbol.BaseType; | ||
} | ||
return false; | ||
} | ||
} |
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