-
Notifications
You must be signed in to change notification settings - Fork 15
/
MixedNamesCheck.java
185 lines (162 loc) · 7.4 KB
/
MixedNamesCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Sonar Delphi Plugin
* Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo
* Author(s):
* Przemyslaw Kociolek ([email protected])
* Michal Wojcik ([email protected])
* Fabricio Colombo ([email protected])
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package au.com.integradev.delphi.checks;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.sonar.check.Rule;
import org.sonar.plugins.communitydelphi.api.ast.ArgumentListNode;
import org.sonar.plugins.communitydelphi.api.ast.AttributeNode;
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
import org.sonar.plugins.communitydelphi.api.ast.NameReferenceNode;
import org.sonar.plugins.communitydelphi.api.ast.UnitImportNode;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheck;
import org.sonar.plugins.communitydelphi.api.check.DelphiCheckContext;
import org.sonar.plugins.communitydelphi.api.reporting.QuickFix;
import org.sonar.plugins.communitydelphi.api.reporting.QuickFixEdit;
import org.sonar.plugins.communitydelphi.api.symbol.NameOccurrence;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.NameDeclaration;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.RoutineNameDeclaration;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.UnitImportNameDeclaration;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.UnitNameDeclaration;
import org.sonar.plugins.communitydelphi.api.symbol.declaration.VariableNameDeclaration;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
@DeprecatedRuleKey(ruleKey = "MixedNamesRule", repositoryKey = "delph")
@Rule(key = "MixedNames")
public class MixedNamesCheck extends DelphiCheck {
private static final String MESSAGE = "Avoid mixing names (found: \"%s\" expected: \"%s\").";
private static final String QUICK_FIX_MESSAGE = "Correct to \"%s\"";
@Override
public DelphiCheckContext visit(NameReferenceNode reference, DelphiCheckContext context) {
NameDeclaration declaration = reference.getNameDeclaration();
NameOccurrence occurrence = reference.getNameOccurrence();
if (declaration != null) {
if (declaration instanceof UnitImportNameDeclaration) {
// Checks the occurrence against the original unit declaration instead of the import
// declaration
checkUnitReference(
reference.getIdentifier(),
context,
occurrence.getImage(),
(UnitImportNameDeclaration) declaration);
} else if (!isSpecialCase(declaration, occurrence)) {
String actual = occurrence.getImage();
String expected = declaration.getImage();
if (!actual.equals(expected)) {
DelphiNode location = reference.getIdentifier();
context
.newIssue()
.onNode(location)
.withMessage(String.format(MESSAGE, actual, expected))
.withQuickFixes(
QuickFix.newFix(QUICK_FIX_MESSAGE, expected)
.withEdit(QuickFixEdit.replace(location, expected)))
.report();
}
}
}
return super.visit(reference, context);
}
@Override
public DelphiCheckContext visit(UnitImportNode importNode, DelphiCheckContext context) {
UnitImportNameDeclaration declaration = importNode.getImportNameDeclaration();
DelphiNode location = importNode.getNameNode();
String importName = declaration.fullyQualifiedName();
checkUnitReference(location, context, importName, declaration);
return super.visit(importNode, context);
}
@Override
public DelphiCheckContext visit(AttributeNode attributeNode, DelphiCheckContext context) {
List<NameReferenceNode> nameReferences = attributeNode.getNameReference().flatten();
for (int i = 0; i + 1 < nameReferences.size(); i++) {
context = super.visit(nameReferences.get(i), context);
}
NameReferenceNode lastNameReference = nameReferences.get(nameReferences.size() - 1);
NameOccurrence occurrence = lastNameReference.getNameOccurrence();
if (occurrence != null) {
NameDeclaration declaration = occurrence.getNameDeclaration();
if (declaration != null && !isSelf(declaration)) {
String actual = occurrence.getImage();
String expected = declaration.getImage();
if (actual.length() != expected.length()) {
expected = StringUtils.removeEndIgnoreCase(expected, "Attribute");
}
if (!actual.equals(expected)) {
DelphiNode location = lastNameReference.getIdentifier();
context
.newIssue()
.onNode(location)
.withMessage(String.format(MESSAGE, actual, expected))
.withQuickFixes(
QuickFix.newFix(QUICK_FIX_MESSAGE, expected)
.withEdit(QuickFixEdit.replace(location, expected)))
.report();
}
}
}
ArgumentListNode argumentListNode = attributeNode.getArgumentList();
if (argumentListNode != null) {
return super.visit(argumentListNode, context);
} else {
return context;
}
}
private static boolean isSpecialCase(NameDeclaration declaration, NameOccurrence occurrence) {
if (!(declaration instanceof RoutineNameDeclaration)) {
return false;
}
switch (((RoutineNameDeclaration) declaration).fullyQualifiedName()) {
case "System.WriteLn":
return occurrence.getImage().equals("Writeln");
case "System.ReadLn":
return occurrence.getImage().equals("Readln");
default:
return false;
}
}
private static boolean isSelf(NameDeclaration declaration) {
return declaration instanceof VariableNameDeclaration
&& ((VariableNameDeclaration) declaration).isSelf();
}
private void checkUnitReference(
DelphiNode location,
DelphiCheckContext context,
String importName,
UnitImportNameDeclaration importDeclaration) {
UnitNameDeclaration originalDeclaration = importDeclaration.getOriginalDeclaration();
if (originalDeclaration != null) {
String unitName = originalDeclaration.fullyQualifiedName();
// Only add violations on import names that are not aliases and do not match the original case
if (StringUtils.endsWithIgnoreCase(unitName, importName) && !unitName.endsWith(importName)) {
String matchingSegment = unitName.substring(unitName.length() - importName.length());
context
.newIssue()
.onNode(location)
.withMessage(String.format(MESSAGE, importName, matchingSegment))
.withQuickFixes(
QuickFix.newFix(QUICK_FIX_MESSAGE, matchingSegment)
.withEdit(QuickFixEdit.replace(location, matchingSegment)))
.report();
}
}
}
}