-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from marcominerva/develop
Optimize code
- Loading branch information
Showing
5 changed files
with
110 additions
and
20 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
16 changes: 16 additions & 0 deletions
16
src/MinimalHelpers.Routing.Analyzers/CompilationExtensions.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,16 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
|
||
namespace MinimalHelpers.Routing.Analyzers; | ||
|
||
internal static class CompilationExtensions | ||
{ | ||
/// <summary> | ||
/// Checks whether a given compilation (assumed to be for C#) is using at least a given language version. | ||
/// </summary> | ||
/// <param name="compilation">The <see cref="Compilation"/> to consider for analysis.</param> | ||
/// <param name="languageVersion">The minimum language version to check.</param> | ||
/// <returns>Whether <paramref name="compilation"/> is using at least the specified language version.</returns> | ||
public static bool HasLanguageVersionAtLeastEqualTo(this Compilation compilation, LanguageVersion languageVersion) | ||
=> ((CSharpCompilation)compilation).LanguageVersion >= languageVersion; | ||
} |
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
63 changes: 63 additions & 0 deletions
63
src/MinimalHelpers.Routing.Analyzers/TypeDeclarationSyntaxExtensions.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,63 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace MinimalHelpers.Routing.Analyzers; | ||
|
||
/// <summary> | ||
/// Extension methods for the <see cref="SyntaxNode"/> type. | ||
/// </summary> | ||
internal static class TypeDeclarationSyntaxExtensions | ||
{ | ||
/// <summary> | ||
/// Checks whether a given <see cref="TypeDeclarationSyntax"/> has or could possibly have any base types, using only syntax. | ||
/// </summary> | ||
/// <param name="typeDeclaration">The input <see cref="TypeDeclarationSyntax"/> instance to check.</param> | ||
/// <returns>Whether <paramref name="typeDeclaration"/> has or could possibly have any base types.</returns> | ||
public static bool HasOrPotentiallyHasBaseTypes(this TypeDeclarationSyntax typeDeclaration) | ||
{ | ||
// If the base types list is not empty, the type can definitely has implemented interfaces | ||
if (typeDeclaration.BaseList is { Types.Count: > 0 }) | ||
{ | ||
return true; | ||
} | ||
|
||
// If the base types list is empty, check if the type is partial. If it is, it means | ||
// that there could be another partial declaration with a non-empty base types list. | ||
foreach (var modifier in typeDeclaration.Modifiers) | ||
{ | ||
if (modifier.IsKind(SyntaxKind.PartialKeyword)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Checks whether a given <see cref="TypeDeclarationSyntax"/> has or could possibly have any attributes, using only syntax. | ||
/// </summary> | ||
/// <param name="typeDeclaration">The input <see cref="TypeDeclarationSyntax"/> instance to check.</param> | ||
/// <returns>Whether <paramref name="typeDeclaration"/> has or could possibly have any attributes.</returns> | ||
public static bool HasOrPotentiallyHasAttributes(this TypeDeclarationSyntax typeDeclaration) | ||
{ | ||
// If the type has any attributes lists, then clearly it can have attributes | ||
if (typeDeclaration.AttributeLists.Count > 0) | ||
{ | ||
return true; | ||
} | ||
|
||
// If the declaration has no attribute lists, check if the type is partial. If it is, it means | ||
// that there could be another partial declaration with some attribute lists over them. | ||
foreach (var modifier in typeDeclaration.Modifiers) | ||
{ | ||
if (modifier.IsKind(SyntaxKind.PartialKeyword)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |