~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/InconsistentNamingIssue.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
ļ»æ// 
2
 
// InconsistentNamingIssue.cs
3
 
//  
4
 
// Author:
5
 
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
6
 
// 
7
 
// Copyright (c) 2012 Xamarin <http://xamarin.com>
8
 
// 
9
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
 
// of this software and associated documentation files (the "Software"), to deal
11
 
// in the Software without restriction, including without limitation the rights
12
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 
// copies of the Software, and to permit persons to whom the Software is
14
 
// furnished to do so, subject to the following conditions:
15
 
// 
16
 
// The above copyright notice and this permission notice shall be included in
17
 
// all copies or substantial portions of the Software.
18
 
// 
19
 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
 
// THE SOFTWARE.
26
 
using System;
27
 
using System.Collections.Generic;
28
 
using System.Linq;
29
 
using ICSharpCode.NRefactory.Semantics;
30
 
using ICSharpCode.NRefactory.TypeSystem;
31
 
 
32
 
namespace ICSharpCode.NRefactory.CSharp.Refactoring
33
 
{
34
 
        [IssueDescription("Inconsistent Naming",
35
 
               Description = "Name doesn't match the defined style for this entity.",
36
 
               Category = IssueCategories.ConstraintViolations,
37
 
               Severity = Severity.Warning)]
38
 
        public class InconsistentNamingIssue : ICodeIssueProvider
39
 
        {
40
 
                public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
41
 
                {
42
 
                        var visitor = new GatherVisitor(context, this);
43
 
                        context.RootNode.AcceptVisitor(visitor);
44
 
                        return visitor.FoundIssues;
45
 
                }
46
 
 
47
 
                class GatherVisitor : GatherVisitorBase
48
 
                {
49
 
                        readonly InconsistentNamingIssue inspector;
50
 
                        readonly NamingConventionService service;
51
 
 
52
 
                        public GatherVisitor (BaseRefactoringContext ctx, InconsistentNamingIssue inspector) : base (ctx)
53
 
                        {
54
 
                                this.inspector = inspector;
55
 
                                service = (NamingConventionService)ctx.GetService (typeof (NamingConventionService));
56
 
                        }
57
 
 
58
 
                        void CheckName(AstNode node, AffectedEntity entity, Identifier identifier, Modifiers accessibilty)
59
 
                        {
60
 
                                ResolveResult resolveResult = null;
61
 
                                if (node != null) {
62
 
                                        resolveResult = ctx.Resolve(node);
63
 
                                }
64
 
                                if (resolveResult is TypeResolveResult) {
65
 
                                        var type = ((TypeResolveResult)resolveResult).Type;
66
 
                                        if (type.DirectBaseTypes.Any(t => t.FullName == "System.Attribute")) {
67
 
                                                if (CheckNamedResolveResult(resolveResult, AffectedEntity.CustomAttributes, identifier, accessibilty)) {
68
 
                                                        return;
69
 
                                                }
70
 
                                        } else if (type.DirectBaseTypes.Any(t => t.FullName == "System.EventArgs")) {
71
 
                                                if (CheckNamedResolveResult(resolveResult, AffectedEntity.CustomEventArgs, identifier, accessibilty)) {
72
 
                                                        return;
73
 
                                                }
74
 
                                        } else if (type.DirectBaseTypes.Any(t => t.FullName == "System.Exception")) {
75
 
                                                if (CheckNamedResolveResult(resolveResult, AffectedEntity.CustomExceptions, identifier, accessibilty)) {
76
 
                                                        return;
77
 
                                                }
78
 
                                        }
79
 
 
80
 
                                        var typeDef = type.GetDefinition();
81
 
                                        if (typeDef != null && typeDef.Attributes.Any(attr => attr.AttributeType.FullName == "NUnit.Framework.TestFixtureAttribute")) {
82
 
                                                if (CheckNamedResolveResult(resolveResult, AffectedEntity.TestType, identifier, accessibilty)) {
83
 
                                                        return;
84
 
                                                }
85
 
                                        }
86
 
                                } else if (resolveResult is MemberResolveResult) {
87
 
                                        var member = ((MemberResolveResult)resolveResult).Member;
88
 
                                        if (member.EntityType == EntityType.Method && member.Attributes.Any(attr => attr.AttributeType.FullName == "NUnit.Framework.TestAttribute")) {
89
 
                                                if (CheckNamedResolveResult(resolveResult, AffectedEntity.TestMethod, identifier, accessibilty)) {
90
 
                                                        return;
91
 
                                                }
92
 
                                        }
93
 
                                }
94
 
                                CheckNamedResolveResult(resolveResult, entity, identifier, accessibilty);
95
 
                        }
96
 
 
97
 
                        bool CheckNamedResolveResult(ResolveResult resolveResult, AffectedEntity entity, Identifier identifier, Modifiers accessibilty)
98
 
                        {
99
 
                                bool wasHandled = false;
100
 
                                foreach (var rule in service.Rules) {
101
 
                                        if (!rule.AffectedEntity.HasFlag(entity)) {
102
 
                                                continue;
103
 
                                        }
104
 
                                        if (!rule.VisibilityMask.HasFlag(accessibilty)) {
105
 
                                                continue;
106
 
                                        }
107
 
                                        if (!rule.IncludeInstanceMembers || !rule.IncludeStaticEntities) {
108
 
                                                IEntity typeSystemEntity = null;
109
 
                                                if (resolveResult is MemberResolveResult) {
110
 
                                                        typeSystemEntity = ((MemberResolveResult)resolveResult).Member;
111
 
                                                } else if (resolveResult is TypeResolveResult) { 
112
 
                                                        typeSystemEntity = ((TypeResolveResult)resolveResult).Type.GetDefinition();
113
 
                                                }
114
 
                                                if (!rule.IncludeInstanceMembers) {
115
 
                                                        if (typeSystemEntity == null || !typeSystemEntity.IsStatic) {
116
 
                                                                continue;
117
 
                                                        }
118
 
                                                }
119
 
                                                if (!rule.IncludeStaticEntities) {
120
 
                                                        if (typeSystemEntity == null || typeSystemEntity.IsStatic) {
121
 
                                                                continue;
122
 
                                                        }
123
 
                                                }
124
 
                                        }
125
 
                                        wasHandled = true;
126
 
                                        if (!rule.IsValid(identifier.Name)) {
127
 
                                                IList<string> suggestedNames;
128
 
                                                var msg = rule.GetErrorMessage(ctx, identifier.Name, out suggestedNames);
129
 
                                                var actions = new List<CodeAction>(suggestedNames.Select(n => new CodeAction(string.Format(ctx.TranslateString("Rename to '{0}'"), n), (Script script) => {
130
 
                                                                if (resolveResult is MemberResolveResult) {
131
 
                                                                        script.Rename(((MemberResolveResult)resolveResult).Member, n);
132
 
                                                                } else if (resolveResult is TypeResolveResult) {
133
 
                                                                        var def = ((TypeResolveResult)resolveResult).Type.GetDefinition();
134
 
                                                                        if (def != null) {
135
 
                                                                                script.Rename(def, n);
136
 
                                                                        } else {
137
 
                                                                                script.RenameTypeParameter(((TypeResolveResult)resolveResult).Type, n);
138
 
                                                                        }
139
 
                                                                } else if (resolveResult is LocalResolveResult) {
140
 
                                                                        script.Rename(((LocalResolveResult)resolveResult).Variable, n);
141
 
                                                                } else { 
142
 
                                                                        script.Replace(identifier, Identifier.Create(n));
143
 
                                                                }
144
 
                                                        }
145
 
                                                )));
146
 
 
147
 
                                                if (resolveResult is MemberResolveResult || resolveResult is TypeResolveResult || resolveResult is LocalResolveResult) {
148
 
                                                        actions.Add(new CodeAction(string.Format(ctx.TranslateString("Rename '{0}'..."), identifier.Name), (Script script) => {
149
 
                                                                if (resolveResult is MemberResolveResult) {
150
 
                                                                        script.Rename(((MemberResolveResult)resolveResult).Member);
151
 
                                                                } else if (resolveResult is TypeResolveResult) {
152
 
                                                                        var def = ((TypeResolveResult)resolveResult).Type.GetDefinition();
153
 
                                                                        if (def != null) {
154
 
                                                                                script.Rename(def);
155
 
                                                                        } else {
156
 
                                                                                script.RenameTypeParameter(((TypeResolveResult)resolveResult).Type);
157
 
                                                                        }
158
 
                                                                } else if (resolveResult is LocalResolveResult) {
159
 
                                                                        script.Rename(((LocalResolveResult)resolveResult).Variable);
160
 
                                                                }
161
 
                                                        }));
162
 
                                                }
163
 
 
164
 
                                                AddIssue(identifier, msg, actions);
165
 
                                        }
166
 
                                }
167
 
                                return wasHandled;
168
 
                        }
169
 
 
170
 
                        public override void VisitNamespaceDeclaration(NamespaceDeclaration namespaceDeclaration)
171
 
                        {
172
 
                                base.VisitNamespaceDeclaration(namespaceDeclaration);
173
 
                                foreach (var id in namespaceDeclaration.Identifiers) {
174
 
                                        CheckName(null, AffectedEntity.Namespace, id, Modifiers.None);
175
 
                                }
176
 
                        }
177
 
 
178
 
                        Modifiers GetAccessibiltiy(EntityDeclaration decl, Modifiers defaultModifier)
179
 
                        {
180
 
                                var accessibility = (decl.Modifiers & Modifiers.VisibilityMask);
181
 
                                if (accessibility == Modifiers.None) {
182
 
                                        return defaultModifier;
183
 
                                }
184
 
                                return accessibility;
185
 
                        }
186
 
 
187
 
                        public override void VisitTypeDeclaration(TypeDeclaration typeDeclaration)
188
 
                        {
189
 
                                base.VisitTypeDeclaration(typeDeclaration);
190
 
                                AffectedEntity entity;
191
 
                                switch (typeDeclaration.ClassType) {
192
 
                                        case ClassType.Class:
193
 
                                                entity = AffectedEntity.Class;
194
 
                                                break;
195
 
                                        case ClassType.Struct:
196
 
                                                entity = AffectedEntity.Struct;
197
 
                                                break;
198
 
                                        case ClassType.Interface:
199
 
                                                entity = AffectedEntity.Interface;
200
 
                                                break;
201
 
                                        case ClassType.Enum:
202
 
                                                entity = AffectedEntity.Enum;
203
 
                                                break;
204
 
                                        default:
205
 
                                                throw new System.ArgumentOutOfRangeException();
206
 
                                }
207
 
                                CheckName(typeDeclaration, entity, typeDeclaration.NameToken, GetAccessibiltiy(typeDeclaration, typeDeclaration.Parent is TypeDeclaration ? Modifiers.Private : Modifiers.Internal));
208
 
                        }
209
 
 
210
 
                        public override void VisitDelegateDeclaration(DelegateDeclaration delegateDeclaration)
211
 
                        {
212
 
                                base.VisitDelegateDeclaration(delegateDeclaration);
213
 
                                CheckName(delegateDeclaration, AffectedEntity.Delegate, delegateDeclaration.NameToken, GetAccessibiltiy(delegateDeclaration, delegateDeclaration.Parent is TypeDeclaration ? Modifiers.Private : Modifiers.Internal));
214
 
                        }
215
 
 
216
 
                        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
217
 
                        {
218
 
                                base.VisitPropertyDeclaration(propertyDeclaration);
219
 
                                CheckName(propertyDeclaration, AffectedEntity.Property, propertyDeclaration.NameToken, GetAccessibiltiy(propertyDeclaration, Modifiers.Private));
220
 
                        }
221
 
 
222
 
                        public override void VisitMethodDeclaration(MethodDeclaration methodDeclaration)
223
 
                        {
224
 
                                base.VisitMethodDeclaration(methodDeclaration);
225
 
 
226
 
                                CheckName(methodDeclaration, methodDeclaration.Modifiers.HasFlag(Modifiers.Async) ? AffectedEntity.AsyncMethod : AffectedEntity.Method, methodDeclaration.NameToken, GetAccessibiltiy(methodDeclaration, Modifiers.Private));
227
 
                        }
228
 
 
229
 
                        public override void VisitFieldDeclaration(FieldDeclaration fieldDeclaration)
230
 
                        {
231
 
                                base.VisitFieldDeclaration(fieldDeclaration);
232
 
                                var entity = AffectedEntity.Field;
233
 
                                if (fieldDeclaration.Modifiers.HasFlag(Modifiers.Const)) {
234
 
                                        entity = AffectedEntity.ConstantField;
235
 
                                } else if (fieldDeclaration.Modifiers.HasFlag(Modifiers.Readonly)) {
236
 
                                        entity = AffectedEntity.ReadonlyField;
237
 
                                }
238
 
                                foreach (var init in fieldDeclaration.Variables) {
239
 
                                        CheckName(init, entity, init.NameToken, GetAccessibiltiy(fieldDeclaration, Modifiers.Private));
240
 
                                }
241
 
                        }
242
 
 
243
 
                        public override void VisitFixedFieldDeclaration(FixedFieldDeclaration fixedFieldDeclaration)
244
 
                        {
245
 
                                base.VisitFixedFieldDeclaration(fixedFieldDeclaration);
246
 
                                var entity = AffectedEntity.Field;
247
 
                                if (fixedFieldDeclaration.Modifiers.HasFlag(Modifiers.Const)) {
248
 
                                        entity = AffectedEntity.ConstantField;
249
 
                                } else if (fixedFieldDeclaration.Modifiers.HasFlag(Modifiers.Readonly)) {
250
 
                                        entity = AffectedEntity.ReadonlyField;
251
 
                                }
252
 
                                CheckName(fixedFieldDeclaration, entity, fixedFieldDeclaration.NameToken, GetAccessibiltiy(fixedFieldDeclaration, Modifiers.Private));
253
 
                        }
254
 
 
255
 
                        public override void VisitEventDeclaration(EventDeclaration eventDeclaration)
256
 
                        {
257
 
                                base.VisitEventDeclaration(eventDeclaration);
258
 
                                foreach (var init in eventDeclaration.Variables) {
259
 
                                        CheckName(init, AffectedEntity.Event, init.NameToken, GetAccessibiltiy(eventDeclaration, Modifiers.Private));
260
 
                                }
261
 
                        }
262
 
 
263
 
                        public override void VisitCustomEventDeclaration(CustomEventDeclaration eventDeclaration)
264
 
                        {
265
 
                                base.VisitCustomEventDeclaration(eventDeclaration);
266
 
                                CheckName(eventDeclaration, AffectedEntity.Event, eventDeclaration.NameToken, GetAccessibiltiy(eventDeclaration, Modifiers.Private));
267
 
                        }
268
 
 
269
 
                        public override void VisitEnumMemberDeclaration(EnumMemberDeclaration enumMemberDeclaration)
270
 
                        {
271
 
                                base.VisitEnumMemberDeclaration(enumMemberDeclaration);
272
 
                                CheckName(enumMemberDeclaration, AffectedEntity.EnumMember, enumMemberDeclaration.NameToken, GetAccessibiltiy(enumMemberDeclaration, Modifiers.Private));
273
 
                        }
274
 
 
275
 
                        public override void VisitParameterDeclaration(ParameterDeclaration parameterDeclaration)
276
 
                        {
277
 
                                base.VisitParameterDeclaration(parameterDeclaration);
278
 
                                CheckName(parameterDeclaration, parameterDeclaration.Parent is LambdaExpression ? AffectedEntity.LambdaParameter : AffectedEntity.Parameter, parameterDeclaration.NameToken, Modifiers.None);
279
 
                        }
280
 
 
281
 
                        public override void VisitTypeParameterDeclaration(TypeParameterDeclaration typeParameterDeclaration)
282
 
                        {
283
 
                                base.VisitTypeParameterDeclaration(typeParameterDeclaration);
284
 
                                CheckName(typeParameterDeclaration, AffectedEntity.TypeParameter, typeParameterDeclaration.NameToken, Modifiers.None);
285
 
                        }
286
 
 
287
 
                        public override void VisitVariableDeclarationStatement(VariableDeclarationStatement variableDeclarationStatement)
288
 
                        {
289
 
                                base.VisitVariableDeclarationStatement(variableDeclarationStatement);
290
 
                                var entity = variableDeclarationStatement.Modifiers.HasFlag(Modifiers.Const) ? AffectedEntity.LocalConstant : AffectedEntity.LocalVariable;
291
 
                                foreach (var init in variableDeclarationStatement.Variables) {
292
 
                                        CheckName(init, entity, init.NameToken, Modifiers.None);
293
 
                                }
294
 
                        }
295
 
 
296
 
                        public override void VisitLabelStatement(LabelStatement labelStatement)
297
 
                        {
298
 
                                base.VisitLabelStatement(labelStatement);
299
 
                                CheckName(null, AffectedEntity.Label, labelStatement.LabelToken, Modifiers.None);
300
 
                        }
301
 
                }
302
 
 
303
 
        }
304
 
}
305