|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.MaintainabilityRules |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Collections.Generic; |
| 8 | + using System.Collections.Immutable; |
| 9 | + using System.Text; |
| 10 | + using Microsoft.CodeAnalysis; |
| 11 | + using Microsoft.CodeAnalysis.CSharp; |
| 12 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 13 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 14 | + using StyleCop.Analyzers.Helpers; |
| 15 | + using StyleCop.Analyzers.Lightup; |
| 16 | + |
| 17 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 18 | + [NoCodeFix("Cannot generate appropriate names.")] |
| 19 | + public class SA1414TupleTypesInSignaturesShouldHaveElementNames : DiagnosticAnalyzer |
| 20 | + { |
| 21 | + /// <summary> |
| 22 | + /// The ID for diagnostics produced by the <see cref="SA1414TupleTypesInSignaturesShouldHaveElementNames"/> analyzer. |
| 23 | + /// </summary> |
| 24 | + public const string DiagnosticId = "SA1414"; |
| 25 | + |
| 26 | + private static readonly LocalizableString Title = new LocalizableResourceString(nameof(MaintainabilityResources.SA1414Title), MaintainabilityResources.ResourceManager, typeof(MaintainabilityResources)); |
| 27 | + private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(MaintainabilityResources.SA1414MessageFormat), MaintainabilityResources.ResourceManager, typeof(MaintainabilityResources)); |
| 28 | + private static readonly LocalizableString Description = new LocalizableResourceString(nameof(MaintainabilityResources.SA1414Description), MaintainabilityResources.ResourceManager, typeof(MaintainabilityResources)); |
| 29 | + private static readonly string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1141.md"; |
| 30 | + |
| 31 | + private static readonly Action<SyntaxNodeAnalysisContext> MethodDeclarationAction = HandleMethodDeclaration; |
| 32 | + private static readonly Action<SyntaxNodeAnalysisContext> ConstructorDeclarationAction = HandleConstructorDeclaration; |
| 33 | + private static readonly Action<SyntaxNodeAnalysisContext> PropertyDeclarationAction = HandlePropertyDeclaration; |
| 34 | + private static readonly Action<SyntaxNodeAnalysisContext> IndexerDeclarationAction = HandleIndexerDeclaration; |
| 35 | + private static readonly Action<SyntaxNodeAnalysisContext> ConversionOperatorDeclarationAction = HandleConversionOperatorDeclaration; |
| 36 | + private static readonly Action<SyntaxNodeAnalysisContext> DelegateDeclarationAction = HandleDelegateDeclaration; |
| 37 | + |
| 38 | + private static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.ReadabilityRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink); |
| 39 | + |
| 40 | + /// <inheritdoc/> |
| 41 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Descriptor); |
| 42 | + |
| 43 | + /// <inheritdoc/> |
| 44 | + public override void Initialize(AnalysisContext context) |
| 45 | + { |
| 46 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 47 | + context.EnableConcurrentExecution(); |
| 48 | + |
| 49 | + context.RegisterSyntaxNodeAction(MethodDeclarationAction, SyntaxKind.MethodDeclaration); |
| 50 | + context.RegisterSyntaxNodeAction(ConstructorDeclarationAction, SyntaxKind.ConstructorDeclaration); |
| 51 | + context.RegisterSyntaxNodeAction(PropertyDeclarationAction, SyntaxKind.PropertyDeclaration); |
| 52 | + context.RegisterSyntaxNodeAction(IndexerDeclarationAction, SyntaxKind.IndexerDeclaration); |
| 53 | + context.RegisterSyntaxNodeAction(ConversionOperatorDeclarationAction, SyntaxKind.ConversionOperatorDeclaration); |
| 54 | + context.RegisterSyntaxNodeAction(DelegateDeclarationAction, SyntaxKind.DelegateDeclaration); |
| 55 | + } |
| 56 | + |
| 57 | + private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context) |
| 58 | + { |
| 59 | + if (!context.SupportsTuples()) |
| 60 | + { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + var methodDeclaration = (MethodDeclarationSyntax)context.Node; |
| 65 | + |
| 66 | + CheckType(context, methodDeclaration.ReturnType); |
| 67 | + CheckParameterList(context, methodDeclaration.ParameterList); |
| 68 | + } |
| 69 | + |
| 70 | + private static void HandleConstructorDeclaration(SyntaxNodeAnalysisContext context) |
| 71 | + { |
| 72 | + if (!context.SupportsTuples()) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + var constructorDeclaration = (ConstructorDeclarationSyntax)context.Node; |
| 78 | + |
| 79 | + CheckParameterList(context, constructorDeclaration.ParameterList); |
| 80 | + } |
| 81 | + |
| 82 | + private static void HandlePropertyDeclaration(SyntaxNodeAnalysisContext context) |
| 83 | + { |
| 84 | + if (!context.SupportsTuples()) |
| 85 | + { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + var propertyDeclaration = (PropertyDeclarationSyntax)context.Node; |
| 90 | + |
| 91 | + CheckType(context, propertyDeclaration.Type); |
| 92 | + } |
| 93 | + |
| 94 | + private static void HandleIndexerDeclaration(SyntaxNodeAnalysisContext context) |
| 95 | + { |
| 96 | + if (!context.SupportsTuples()) |
| 97 | + { |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + var indexerDeclaration = (IndexerDeclarationSyntax)context.Node; |
| 102 | + |
| 103 | + CheckType(context, indexerDeclaration.Type); |
| 104 | + } |
| 105 | + |
| 106 | + private static void HandleConversionOperatorDeclaration(SyntaxNodeAnalysisContext context) |
| 107 | + { |
| 108 | + if (!context.SupportsTuples()) |
| 109 | + { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + var conversionOperatorDeclarion = (ConversionOperatorDeclarationSyntax)context.Node; |
| 114 | + |
| 115 | + CheckType(context, conversionOperatorDeclarion.Type); |
| 116 | + CheckParameterList(context, conversionOperatorDeclarion.ParameterList); |
| 117 | + } |
| 118 | + |
| 119 | + private static void HandleDelegateDeclaration(SyntaxNodeAnalysisContext context) |
| 120 | + { |
| 121 | + if (!context.SupportsTuples()) |
| 122 | + { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + var delegateDeclarion = (DelegateDeclarationSyntax)context.Node; |
| 127 | + |
| 128 | + CheckType(context, delegateDeclarion.ReturnType); |
| 129 | + CheckParameterList(context, delegateDeclarion.ParameterList); |
| 130 | + } |
| 131 | + |
| 132 | + private static void CheckParameterList(SyntaxNodeAnalysisContext context, ParameterListSyntax parameterList) |
| 133 | + { |
| 134 | + foreach (var parameter in parameterList.Parameters) |
| 135 | + { |
| 136 | + CheckType(context, parameter.Type); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private static void CheckType(SyntaxNodeAnalysisContext context, TypeSyntax typeSyntax) |
| 141 | + { |
| 142 | + switch (typeSyntax.Kind()) |
| 143 | + { |
| 144 | + case SyntaxKindEx.TupleType: |
| 145 | + CheckTupleType(context, (TupleTypeSyntaxWrapper)typeSyntax); |
| 146 | + break; |
| 147 | + |
| 148 | + case SyntaxKind.QualifiedName: |
| 149 | + CheckType(context, ((QualifiedNameSyntax)typeSyntax).Right); |
| 150 | + break; |
| 151 | + |
| 152 | + case SyntaxKind.GenericName: |
| 153 | + CheckGenericName(context, (GenericNameSyntax)typeSyntax); |
| 154 | + break; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private static void CheckTupleType(SyntaxNodeAnalysisContext context, TupleTypeSyntaxWrapper tupleTypeSyntax) |
| 159 | + { |
| 160 | + foreach (var tupleElementSyntax in tupleTypeSyntax.Elements) |
| 161 | + { |
| 162 | + CheckType(context, tupleElementSyntax.Type); |
| 163 | + |
| 164 | + if (tupleElementSyntax.Identifier.IsKind(SyntaxKind.None)) |
| 165 | + { |
| 166 | + var location = tupleElementSyntax.SyntaxNode.GetLocation(); |
| 167 | + context.ReportDiagnostic(Diagnostic.Create(Descriptor, location)); |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private static void CheckGenericName(SyntaxNodeAnalysisContext context, GenericNameSyntax genericNameSyntax) |
| 173 | + { |
| 174 | + foreach (var typeArgument in genericNameSyntax.TypeArgumentList.Arguments) |
| 175 | + { |
| 176 | + CheckType(context, typeArgument); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments