-
Notifications
You must be signed in to change notification settings - Fork 0
Analyzers #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Analyzers #1
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1f05437
Add ktsu.Sdk.Analyzers project with initial analyzer implementation a…
matt-edmondson 4ca45a2
[major] Add Roslyn analyzers to enforce SDK requirements and remove U…
matt-edmondson 6490f66
Fix solution directory discovery logic for great-grandparent level
matt-edmondson 75aa17b
Enhance MissingStandardPackagesAnalyzer to validate additional packag…
matt-edmondson 32c7b17
Fix attribute syntax for InternalsVisibleTo in code fix provider
matt-edmondson b6da61b
Update Sdk.targets to replace {version} placeholder with actual SDK v…
matt-edmondson 5a4391e
Update SetPackageReferenceProperties target to run before GenerateMSB…
matt-edmondson f17ff44
Fix erroneous spaces in solution discovery
matt-edmondson c5fe90d
Refactor RequiresSystemMemory method to simplify null checks on targe…
matt-edmondson e26e1f1
Enhance assembly name check to support strong-named assemblies in Mis…
matt-edmondson fd19ff6
Add PolyGuard and PolyNullability properties for non-test projects
matt-edmondson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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
114 changes: 114 additions & 0 deletions
114
Sdk.Analyzers/AddInternalsVisibleToAttributeCodeFixProvider.cs
This file contains hidden or 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,114 @@ | ||
| // Copyright (c) ktsu.dev | ||
| // All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| namespace ktsu.Sdk.Analyzers; | ||
|
|
||
| using System.Collections.Immutable; | ||
| using System.Composition; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CodeActions; | ||
| using Microsoft.CodeAnalysis.CodeFixes; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| /// <summary> | ||
| /// Code fix provider that adds InternalsVisibleToAttribute to expose internals to test projects | ||
| /// </summary> | ||
| [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AddInternalsVisibleToAttributeCodeFixProvider))] | ||
| [Shared] | ||
| public class AddInternalsVisibleToAttributeCodeFixProvider : CodeFixProvider | ||
| { | ||
|
|
||
| /// <inheritdoc/> | ||
| public override ImmutableArray<string> FixableDiagnosticIds => [MissingInternalsVisibleToAttributeAnalyzer.DiagnosticId]; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override FixAllProvider GetFixAllProvider() => WellKnownFixAllProviders.BatchFixer; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
| { | ||
| SyntaxNode? root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); | ||
| if (root is null) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Diagnostic diagnostic = context.Diagnostics.First(); | ||
|
|
||
| context.RegisterCodeFix( | ||
| CodeAction.Create( | ||
| title: "Add [assembly: InternalsVisibleTo(...)]", | ||
| createChangedDocument: ct => AddInternalsVisibleToAttributeAsync(context.Document, diagnostic, ct), | ||
| equivalenceKey: nameof(AddInternalsVisibleToAttributeCodeFixProvider)), | ||
| diagnostic); | ||
| } | ||
|
|
||
| private static async Task<Document> AddInternalsVisibleToAttributeAsync( | ||
| Document document, | ||
| Diagnostic diagnostic, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| SyntaxNode? root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); | ||
| if (root is not CompilationUnitSyntax compilationUnit) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| // Get test project namespace from analyzer config options | ||
|
|
||
| AnalyzerConfigOptions options = document.Project.AnalyzerOptions.AnalyzerConfigOptionsProvider.GlobalOptions; | ||
| if (!options.TryGetValue("build_property.TestProjectNamespace", out string? testNamespace) || string.IsNullOrWhiteSpace(testNamespace)) | ||
| { | ||
| return document; | ||
| } | ||
|
|
||
| // Check if using directive already exists | ||
|
|
||
| bool hasUsing = compilationUnit.Usings.Any(u => | ||
| u.Name?.ToString() == "System.Runtime.CompilerServices"); | ||
|
|
||
| // Create the using directive if needed | ||
|
|
||
| SyntaxList<UsingDirectiveSyntax> newUsings = compilationUnit.Usings; | ||
| if (!hasUsing) | ||
| { | ||
| UsingDirectiveSyntax usingDirective = SyntaxFactory.UsingDirective( | ||
| SyntaxFactory.ParseName("System.Runtime.CompilerServices")) | ||
| .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed); | ||
| newUsings = newUsings.Add(usingDirective); | ||
| } | ||
|
|
||
| // Create the InternalsVisibleTo attribute | ||
|
|
||
| AttributeArgumentSyntax attributeArgument = SyntaxFactory.AttributeArgument( | ||
| SyntaxFactory.LiteralExpression( | ||
| SyntaxKind.StringLiteralExpression, | ||
| SyntaxFactory.Literal(testNamespace))); | ||
|
|
||
| AttributeSyntax attribute = SyntaxFactory.Attribute( | ||
| SyntaxFactory.ParseName("System.Runtime.CompilerServices.InternalsVisibleTo"), | ||
| SyntaxFactory.AttributeArgumentList( | ||
| SyntaxFactory.SingletonSeparatedList(attributeArgument))); | ||
|
|
||
| AttributeListSyntax attributeList = SyntaxFactory.AttributeList( | ||
| SyntaxFactory.AttributeTargetSpecifier(SyntaxFactory.Token(SyntaxKind.AssemblyKeyword)), | ||
| SyntaxFactory.SingletonSeparatedList(attribute)) | ||
| .WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed); | ||
|
|
||
| // Add the attribute to the compilation unit | ||
|
|
||
| CompilationUnitSyntax newCompilationUnit = compilationUnit | ||
| .WithUsings(newUsings) | ||
| .AddAttributeLists(attributeList) | ||
| .WithLeadingTrivia(compilationUnit.GetLeadingTrivia()) | ||
| .WithTrailingTrivia(compilationUnit.GetTrailingTrivia()); | ||
|
|
||
| return document.WithSyntaxRoot(newCompilationUnit); | ||
| } | ||
| } | ||
This file contains hidden or 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,12 @@ | ||
| ; Shipped analyzer releases | ||
| ; https://github.com/dotnet/roslyn/blob/main/src/RoslynAnalyzers/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
|
||
| ## Release {version} | ||
|
|
||
| ### New Rules | ||
|
|
||
| Rule ID | Category | Severity | Notes | ||
| --------|----------|----------|------- | ||
| KTSU0001 | ktsu.Sdk | Error | Missing required package reference | ||
| KTSU0002 | ktsu.Sdk | Error | Missing InternalsVisibleTo attribute for test project | ||
|
|
This file contains hidden or 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,7 @@ | ||
| ; Unshipped analyzer releases | ||
| ; https://github.com/dotnet/roslyn/blob/main/src/RoslynAnalyzers/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
|
||
| ### New Rules | ||
|
|
||
| Rule ID | Category | Severity | Notes | ||
| --------|----------|----------|------- |
This file contains hidden or 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 @@ | ||
| // Copyright (c) ktsu.dev | ||
| // All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| namespace ktsu.Sdk.Analyzers; | ||
|
|
||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
|
|
||
| /// <summary> | ||
| /// Base class for all ktsu.Sdk analyzers | ||
| /// </summary> | ||
| public abstract class KtsuAnalyzerBase : DiagnosticAnalyzer | ||
| { | ||
|
|
||
| /// <summary> | ||
| /// Category for ktsu.Sdk analyzers | ||
| /// </summary> | ||
| protected const string Category = "ktsu.Sdk"; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.