|
| 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.OrderingRules |
| 5 | +{ |
| 6 | + using System; |
| 7 | + using System.Collections.Immutable; |
| 8 | + using Microsoft.CodeAnalysis; |
| 9 | + using Microsoft.CodeAnalysis.CSharp; |
| 10 | + using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 11 | + using Microsoft.CodeAnalysis.Diagnostics; |
| 12 | + using StyleCop.Analyzers.Helpers; |
| 13 | + using StyleCop.Analyzers.Settings.ObjectModel; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// An readonly element is positioned beneath a non-readonly element. |
| 17 | + /// </summary> |
| 18 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 19 | + internal class SA1214ReadonlyElementsMustAppearBeforeNonReadonlyElements : DiagnosticAnalyzer |
| 20 | + { |
| 21 | + /// <summary> |
| 22 | + /// The ID for diagnostics produced by the |
| 23 | + /// <see cref="SA1214ReadonlyElementsMustAppearBeforeNonReadonlyElements"/> analyzer. |
| 24 | + /// </summary> |
| 25 | + public const string DiagnosticId = "SA1214"; |
| 26 | + private const string Title = "Readonly elements must appear before non-readonly elements"; |
| 27 | + private const string MessageFormat = "Readonly fields must appear before non-readonly fields."; |
| 28 | + private const string Description = "A readonly field is positioned beneath a non-readonly field."; |
| 29 | + private const string HelpLink = "https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1214.md"; |
| 30 | + |
| 31 | + private static readonly DiagnosticDescriptor Descriptor = |
| 32 | + new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, AnalyzerCategory.OrderingRules, DiagnosticSeverity.Warning, AnalyzerConstants.EnabledByDefault, Description, HelpLink); |
| 33 | + |
| 34 | + private static readonly ImmutableArray<SyntaxKind> TypeDeclarationKinds = |
| 35 | + ImmutableArray.Create(SyntaxKind.ClassDeclaration, SyntaxKind.StructDeclaration); |
| 36 | + |
| 37 | + private static readonly Action<CompilationStartAnalysisContext> CompilationStartAction = HandleCompilationStart; |
| 38 | + private static readonly Action<SyntaxNodeAnalysisContext, StyleCopSettings> TypeDeclarationAction = HandleTypeDeclaration; |
| 39 | + |
| 40 | + /// <inheritdoc/> |
| 41 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = |
| 42 | + ImmutableArray.Create(Descriptor); |
| 43 | + |
| 44 | + /// <inheritdoc/> |
| 45 | + public override void Initialize(AnalysisContext context) |
| 46 | + { |
| 47 | + context.RegisterCompilationStartAction(CompilationStartAction); |
| 48 | + } |
| 49 | + |
| 50 | + private static void HandleCompilationStart(CompilationStartAnalysisContext context) |
| 51 | + { |
| 52 | + context.RegisterSyntaxNodeActionHonorExclusions(TypeDeclarationAction, TypeDeclarationKinds); |
| 53 | + } |
| 54 | + |
| 55 | + private static void HandleTypeDeclaration(SyntaxNodeAnalysisContext context, StyleCopSettings settings) |
| 56 | + { |
| 57 | + var elementOrder = settings.OrderingRules.ElementOrder; |
| 58 | + int readonlyIndex = elementOrder.IndexOf(OrderingTrait.Readonly); |
| 59 | + if (readonlyIndex < 0) |
| 60 | + { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + var typeDeclaration = (TypeDeclarationSyntax)context.Node; |
| 65 | + |
| 66 | + FieldDeclarationSyntax previousMember = null; |
| 67 | + var previousFieldConst = true; |
| 68 | + var previousFieldStatic = false; |
| 69 | + var previousFieldReadonly = false; |
| 70 | + var previousAccessLevel = AccessLevel.NotSpecified; |
| 71 | + foreach (var member in typeDeclaration.Members) |
| 72 | + { |
| 73 | + FieldDeclarationSyntax field = member as FieldDeclarationSyntax; |
| 74 | + if (field == null) |
| 75 | + { |
| 76 | + previousMember = null; |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + var modifiers = member.GetModifiers(); |
| 81 | + var currentAccessLevel = MemberOrderHelper.GetAccessLevelForOrdering(member, modifiers); |
| 82 | + bool currentFieldConst = modifiers.Any(SyntaxKind.ConstKeyword); |
| 83 | + bool currentFieldStatic = modifiers.Any(SyntaxKind.StaticKeyword); |
| 84 | + bool currentFieldReadonly = modifiers.Any(SyntaxKind.ReadOnlyKeyword); |
| 85 | + if (previousMember == null) |
| 86 | + { |
| 87 | + previousMember = field; |
| 88 | + previousFieldConst = currentFieldConst; |
| 89 | + previousFieldStatic = currentFieldStatic; |
| 90 | + previousFieldReadonly = currentFieldReadonly; |
| 91 | + previousAccessLevel = currentAccessLevel; |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + bool compareReadonly = true; |
| 96 | + for (int j = 0; j < readonlyIndex; j++) |
| 97 | + { |
| 98 | + switch (elementOrder[j]) |
| 99 | + { |
| 100 | + case OrderingTrait.Kind: |
| 101 | + // This analyzer only ever looks at sequences of fields. |
| 102 | + continue; |
| 103 | + |
| 104 | + case OrderingTrait.Accessibility: |
| 105 | + if (previousAccessLevel != currentAccessLevel) |
| 106 | + { |
| 107 | + compareReadonly = false; |
| 108 | + } |
| 109 | + |
| 110 | + continue; |
| 111 | + |
| 112 | + case OrderingTrait.Constant: |
| 113 | + if (previousFieldConst || currentFieldConst) |
| 114 | + { |
| 115 | + compareReadonly = false; |
| 116 | + } |
| 117 | + |
| 118 | + continue; |
| 119 | + |
| 120 | + case OrderingTrait.Static: |
| 121 | + if (previousFieldStatic != currentFieldStatic) |
| 122 | + { |
| 123 | + compareReadonly = false; |
| 124 | + } |
| 125 | + |
| 126 | + continue; |
| 127 | + |
| 128 | + case OrderingTrait.Readonly: |
| 129 | + default: |
| 130 | + continue; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (compareReadonly) |
| 135 | + { |
| 136 | + if ((currentFieldReadonly || currentFieldConst) && !previousFieldReadonly && !previousFieldConst) |
| 137 | + { |
| 138 | + context.ReportDiagnostic(Diagnostic.Create(Descriptor, NamedTypeHelpers.GetNameOrIdentifierLocation(member), AccessLevelHelper.GetName(currentAccessLevel))); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + previousMember = field; |
| 143 | + previousFieldConst = currentFieldConst; |
| 144 | + previousFieldStatic = currentFieldStatic; |
| 145 | + previousFieldReadonly = currentFieldReadonly; |
| 146 | + previousAccessLevel = currentAccessLevel; |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments