-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b1f39d
commit e01bfa2
Showing
5 changed files
with
149 additions
and
90 deletions.
There are no files selected for viewing
39 changes: 5 additions & 34 deletions
39
...alyzers/CSharp/Microsoft.CodeQuality.Analyzers/Maintainability/CSharpMakeTypesInternal.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 |
---|---|---|
@@ -1,49 +1,20 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using Analyzer.Utilities.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeQuality.Analyzers.Maintainability; | ||
|
||
namespace Microsoft.CodeQuality.CSharp.Analyzers.Maintainability | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class CSharpMakeTypesInternal : MakeTypesInternal<SyntaxKind> | ||
public sealed class CSharpMakeTypesInternal : MakeTypesInternal | ||
{ | ||
protected override ImmutableArray<SyntaxKind> TypeKinds { get; } = | ||
ImmutableArray.Create(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.RecordDeclaration); | ||
|
||
protected override SyntaxKind EnumKind { get; } = SyntaxKind.EnumDeclaration; | ||
|
||
protected override ImmutableArray<SyntaxKind> DelegateKinds { get; } = ImmutableArray.Create(SyntaxKind.DelegateDeclaration); | ||
|
||
protected override void AnalyzeTypeDeclaration(SyntaxNodeAnalysisContext context) | ||
{ | ||
var type = (TypeDeclarationSyntax)context.Node; | ||
ReportIfPublic(context, type.Modifiers, type.Identifier); | ||
} | ||
|
||
protected override void AnalyzeEnumDeclaration(SyntaxNodeAnalysisContext context) | ||
{ | ||
var @enum = (EnumDeclarationSyntax)context.Node; | ||
ReportIfPublic(context, @enum.Modifiers, @enum.Identifier); | ||
} | ||
|
||
protected override void AnalyzeDelegateDeclaration(SyntaxNodeAnalysisContext context) | ||
{ | ||
var @delegate = (DelegateDeclarationSyntax)context.Node; | ||
ReportIfPublic(context, @delegate.Modifiers, @delegate.Identifier); | ||
} | ||
|
||
private static void ReportIfPublic(SyntaxNodeAnalysisContext context, SyntaxTokenList modifiers, SyntaxToken identifier) | ||
protected override SyntaxToken? GetIdentifier(SyntaxNode type) | ||
{ | ||
if (modifiers.Any(SyntaxKind.PublicKeyword)) | ||
{ | ||
context.ReportDiagnostic(identifier.CreateDiagnostic(Rule)); | ||
} | ||
return (type as TypeDeclarationSyntax)?.Identifier | ||
?? (type as EnumDeclarationSyntax)?.Identifier | ||
?? (type as DelegateDeclarationSyntax)?.Identifier; | ||
} | ||
} | ||
} |
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
41 changes: 16 additions & 25 deletions
41
...ers/VisualBasic/Microsoft.CodeQuality.Analyzers/Maintainability/BasicMakeTypesInternal.vb
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 |
---|---|---|
@@ -1,41 +1,32 @@ | ||
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
Imports System.Collections.Immutable | ||
Imports Analyzer.Utilities.Extensions | ||
Imports Microsoft.CodeAnalysis | ||
Imports Microsoft.CodeAnalysis.Diagnostics | ||
Imports Microsoft.CodeAnalysis.VisualBasic | ||
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax | ||
Imports Microsoft.CodeQuality.Analyzers.Maintainability | ||
|
||
Namespace Microsoft.CodeQuality.VisualBasic.Analyzers.Maintainability | ||
<DiagnosticAnalyzer(LanguageNames.VisualBasic)> | ||
Public NotInheritable Class BasicMakeTypesInternal | ||
Inherits MakeTypesInternal(Of SyntaxKind) | ||
Inherits MakeTypesInternal | ||
|
||
Protected Overrides ReadOnly Property TypeKinds As ImmutableArray(Of SyntaxKind) = ImmutableArray.Create(SyntaxKind.ClassStatement, SyntaxKind.StructureStatement, SyntaxKind.InterfaceStatement) | ||
Protected Overrides ReadOnly Property EnumKind As SyntaxKind = SyntaxKind.EnumStatement | ||
Protected Overrides ReadOnly Property DelegateKinds As ImmutableArray(Of SyntaxKind) = ImmutableArray.Create(SyntaxKind.DelegateFunctionStatement, SyntaxKind.DelegateSubStatement) | ||
|
||
Protected Overrides Sub AnalyzeTypeDeclaration(context As SyntaxNodeAnalysisContext) | ||
Dim type = DirectCast(context.Node, TypeStatementSyntax) | ||
ReportIfPublic(context, type.Modifiers, type.Identifier) | ||
End Sub | ||
|
||
Protected Overrides Sub AnalyzeEnumDeclaration(context As SyntaxNodeAnalysisContext) | ||
Dim enumStatement = DirectCast(context.Node, EnumStatementSyntax) | ||
ReportIfPublic(context, enumStatement.Modifiers, enumStatement.Identifier) | ||
End Sub | ||
Protected Overrides Function GetIdentifier(type As SyntaxNode) As SyntaxToken? | ||
Dim typeStatement = TryCast(type, TypeStatementSyntax) | ||
If typeStatement IsNot Nothing Then | ||
Return typeStatement.Identifier | ||
End If | ||
|
||
Protected Overrides Sub AnalyzeDelegateDeclaration(context As SyntaxNodeAnalysisContext) | ||
Dim delegateStatement = DirectCast(context.Node, DelegateStatementSyntax) | ||
ReportIfPublic(context, delegateStatement.Modifiers, delegateStatement.Identifier) | ||
End Sub | ||
Dim enumStatement = TryCast(type, EnumStatementSyntax) | ||
If enumStatement IsNot Nothing Then | ||
Return enumStatement.Identifier | ||
End If | ||
|
||
Private Shared Sub ReportIfPublic(context As SyntaxNodeAnalysisContext, modifiers As SyntaxTokenList, identifier As SyntaxToken) | ||
If modifiers.Any(SyntaxKind.PublicKeyword) Then | ||
context.ReportDiagnostic(identifier.CreateDiagnostic(Rule)) | ||
Dim delegateStatement = TryCast(type, DelegateStatementSyntax) | ||
If delegateStatement IsNot Nothing Then | ||
Return delegateStatement.Identifier | ||
End If | ||
End Sub | ||
|
||
Return Nothing | ||
End Function | ||
End Class | ||
End Namespace |