Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: pkg/analysis_server/lib/src/services/refactoring/naming_conventions.dart

Issue 585073002: Issue 18296. Harden the name checks. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/extract_method_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library services.src.refactoring.naming_conventions; 5 library services.src.refactoring.naming_conventions;
6 6
7 import 'package:analysis_server/src/services/correction/status.dart'; 7 import 'package:analysis_server/src/services/correction/status.dart';
8 import 'package:analysis_server/src/services/correction/strings.dart'; 8 import 'package:analysis_server/src/services/correction/strings.dart';
9 9
10 10
11 /** 11 /**
12 * Returns the [RefactoringStatus] with severity: 12 * Returns the [RefactoringStatus] with severity:
13 * OK if the name is valid; 13 * OK if the name is valid;
14 * WARNING if the name is discouraged; 14 * WARNING if the name is discouraged;
15 * ERROR if the name is illegal. 15 * FATAL if the name is illegal.
16 */ 16 */
17 RefactoringStatus validateClassName(String name) { 17 RefactoringStatus validateClassName(String name) {
18 return _validateUpperCamelCase(name, "Class"); 18 return _validateUpperCamelCase(name, "Class");
19 } 19 }
20 20
21 /** 21 /**
22 * Returns the [RefactoringStatus] with severity: 22 * Returns the [RefactoringStatus] with severity:
23 * OK if the name is valid; 23 * OK if the name is valid;
24 * WARNING if the name is discouraged; 24 * WARNING if the name is discouraged;
25 * ERROR if the name is illegal. 25 * FATAL if the name is illegal.
26 */ 26 */
27 RefactoringStatus validateConstantName(String name) { 27 RefactoringStatus validateConstantName(String name) {
28 // null 28 // null
29 if (name == null) { 29 if (name == null) {
30 return new RefactoringStatus.fatal("Constant name must not be null."); 30 return new RefactoringStatus.fatal("Constant name must not be null.");
31 } 31 }
32 // is not identifier 32 // is not identifier
33 RefactoringStatus status = 33 RefactoringStatus status =
34 _validateIdentifier(name, "Constant name", 'an uppercase letter or undersc ore'); 34 _validateIdentifier(name, "Constant name", 'an uppercase letter or undersc ore');
35 if (!status.isOK) { 35 if (!status.isOK) {
(...skipping 13 matching lines...) Expand all
49 } 49 }
50 } 50 }
51 // OK 51 // OK
52 return new RefactoringStatus(); 52 return new RefactoringStatus();
53 } 53 }
54 54
55 /** 55 /**
56 * Returns the [RefactoringStatus] with severity: 56 * Returns the [RefactoringStatus] with severity:
57 * OK if the name is valid; 57 * OK if the name is valid;
58 * WARNING if the name is discouraged; 58 * WARNING if the name is discouraged;
59 * ERROR if the name is illegal. 59 * FATAL if the name is illegal.
60 */ 60 */
61 RefactoringStatus validateConstructorName(String name) { 61 RefactoringStatus validateConstructorName(String name) {
62 if (name != null && name.isEmpty) { 62 if (name != null && name.isEmpty) {
63 return new RefactoringStatus(); 63 return new RefactoringStatus();
64 } 64 }
65 return _validateLowerCamelCase(name, "Constructor"); 65 return _validateLowerCamelCase(name, "Constructor");
66 } 66 }
67 67
68 /** 68 /**
69 * Returns the [RefactoringStatus] with severity: 69 * Returns the [RefactoringStatus] with severity:
70 * OK if the name is valid; 70 * OK if the name is valid;
71 * WARNING if the name is discouraged; 71 * WARNING if the name is discouraged;
72 * ERROR if the name is illegal. 72 * FATAL if the name is illegal.
73 */ 73 */
74 RefactoringStatus validateFieldName(String name) { 74 RefactoringStatus validateFieldName(String name) {
75 return _validateLowerCamelCase(name, "Field"); 75 return _validateLowerCamelCase(name, "Field");
76 } 76 }
77 77
78 /** 78 /**
79 * Returns the [RefactoringStatus] with severity: 79 * Returns the [RefactoringStatus] with severity:
80 * OK if the name is valid; 80 * OK if the name is valid;
81 * WARNING if the name is discouraged; 81 * WARNING if the name is discouraged;
82 * ERROR if the name is illegal. 82 * FATAL if the name is illegal.
83 */ 83 */
84 RefactoringStatus validateFunctionName(String name) { 84 RefactoringStatus validateFunctionName(String name) {
85 return _validateLowerCamelCase(name, "Function"); 85 return _validateLowerCamelCase(name, "Function");
86 } 86 }
87 87
88 /** 88 /**
89 * Returns the [RefactoringStatus] with severity: 89 * Returns the [RefactoringStatus] with severity:
90 * OK if the name is valid; 90 * OK if the name is valid;
91 * WARNING if the name is discouraged; 91 * WARNING if the name is discouraged;
92 * ERROR if the name is illegal. 92 * FATAL if the name is illegal.
93 */ 93 */
94 RefactoringStatus validateFunctionTypeAliasName(String name) { 94 RefactoringStatus validateFunctionTypeAliasName(String name) {
95 return _validateUpperCamelCase(name, "Function type alias"); 95 return _validateUpperCamelCase(name, "Function type alias");
96 } 96 }
97 97
98 /** 98 /**
99 * Returns the [RefactoringStatus] with severity: 99 * Returns the [RefactoringStatus] with severity:
100 * OK if the name is valid; 100 * OK if the name is valid;
101 * WARNING if the name is discouraged; 101 * WARNING if the name is discouraged;
102 * ERROR if the name is illegal. 102 * FATAL if the name is illegal.
103 */ 103 */
104 RefactoringStatus validateImportPrefixName(String name) { 104 RefactoringStatus validateImportPrefixName(String name) {
105 if (name != null && name.isEmpty) { 105 if (name != null && name.isEmpty) {
106 return new RefactoringStatus(); 106 return new RefactoringStatus();
107 } 107 }
108 return _validateLowerCamelCase(name, "Import prefix"); 108 return _validateLowerCamelCase(name, "Import prefix");
109 } 109 }
110 110
111 /** 111 /**
112 * Returns the [RefactoringStatus] with severity: 112 * Returns the [RefactoringStatus] with severity:
113 * OK if the name is valid; 113 * OK if the name is valid;
114 * WARNING if the name is discouraged; 114 * WARNING if the name is discouraged;
115 * ERROR if the name is illegal. 115 * FATAL if the name is illegal.
116 */ 116 */
117 RefactoringStatus validateLabelName(String name) { 117 RefactoringStatus validateLabelName(String name) {
118 return _validateLowerCamelCase(name, "Label"); 118 return _validateLowerCamelCase(name, "Label");
119 } 119 }
120 120
121 /** 121 /**
122 * Returns the [RefactoringStatus] with severity: 122 * Returns the [RefactoringStatus] with severity:
123 * OK if the name is valid; 123 * OK if the name is valid;
124 * WARNING if the name is discouraged; 124 * WARNING if the name is discouraged;
125 * ERROR if the name is illegal. 125 * FATAL if the name is illegal.
126 */ 126 */
127 RefactoringStatus validateLibraryName(String name) { 127 RefactoringStatus validateLibraryName(String name) {
128 // null 128 // null
129 if (name == null) { 129 if (name == null) {
130 return new RefactoringStatus.fatal("Library name must not be null."); 130 return new RefactoringStatus.fatal("Library name must not be null.");
131 } 131 }
132 // blank 132 // blank
133 if (isBlank(name)) { 133 if (isBlank(name)) {
134 return new RefactoringStatus.fatal("Library name must not be blank."); 134 return new RefactoringStatus.fatal("Library name must not be blank.");
135 } 135 }
(...skipping 19 matching lines...) Expand all
155 } 155 }
156 } 156 }
157 // OK 157 // OK
158 return new RefactoringStatus(); 158 return new RefactoringStatus();
159 } 159 }
160 160
161 /** 161 /**
162 * Returns the [RefactoringStatus] with severity: 162 * Returns the [RefactoringStatus] with severity:
163 * OK if the name is valid; 163 * OK if the name is valid;
164 * WARNING if the name is discouraged; 164 * WARNING if the name is discouraged;
165 * ERROR if the name is illegal. 165 * FATAL if the name is illegal.
166 */ 166 */
167 RefactoringStatus validateMethodName(String name) { 167 RefactoringStatus validateMethodName(String name) {
168 return _validateLowerCamelCase(name, "Method"); 168 return _validateLowerCamelCase(name, "Method");
169 } 169 }
170 170
171 /** 171 /**
172 * Returns the [RefactoringStatus] with severity: 172 * Returns the [RefactoringStatus] with severity:
173 * OK if the name is valid; 173 * OK if the name is valid;
174 * WARNING if the name is discouraged; 174 * WARNING if the name is discouraged;
175 * ERROR if the name is illegal. 175 * FATAL if the name is illegal.
176 */ 176 */
177 RefactoringStatus validateParameterName(String name) { 177 RefactoringStatus validateParameterName(String name) {
178 return _validateLowerCamelCase(name, "Parameter"); 178 return _validateLowerCamelCase(name, "Parameter");
179 } 179 }
180 180
181 /** 181 /**
182 * Returns the [RefactoringStatus] with severity: 182 * Returns the [RefactoringStatus] with severity:
183 * OK if the name is valid; 183 * OK if the name is valid;
184 * WARNING if the name is discouraged; 184 * WARNING if the name is discouraged;
185 * ERROR if the name is illegal. 185 * FATAL if the name is illegal.
186 */ 186 */
187 RefactoringStatus validateVariableName(String name) { 187 RefactoringStatus validateVariableName(String name) {
188 return _validateLowerCamelCase(name, "Variable"); 188 return _validateLowerCamelCase(name, "Variable");
189 } 189 }
190 190
191 RefactoringStatus _validateIdentifier(String identifier, String desc, 191 RefactoringStatus _validateIdentifier(String identifier, String desc,
192 String beginDesc) { 192 String beginDesc) {
193 // has leading/trailing spaces 193 // has leading/trailing spaces
194 String trimmed = identifier.trim(); 194 String trimmed = identifier.trim();
195 if (identifier != trimmed) { 195 if (identifier != trimmed) {
196 String message = "$desc must not start or end with a blank."; 196 String message = "$desc must not start or end with a blank.";
197 return new RefactoringStatus.error(message); 197 return new RefactoringStatus.fatal(message);
198 } 198 }
199 // empty 199 // empty
200 int length = identifier.length; 200 int length = identifier.length;
201 if (length == 0) { 201 if (length == 0) {
202 String message = "$desc must not be empty."; 202 String message = "$desc must not be empty.";
203 return new RefactoringStatus.fatal(message); 203 return new RefactoringStatus.fatal(message);
204 } 204 }
205 int currentChar = identifier.codeUnitAt(0); 205 int currentChar = identifier.codeUnitAt(0);
206 if (!isLetter(currentChar) && 206 if (!isLetter(currentChar) &&
207 currentChar != CHAR_UNDERSCORE && 207 currentChar != CHAR_UNDERSCORE &&
208 currentChar != CHAR_DOLLAR) { 208 currentChar != CHAR_DOLLAR) {
209 String message = "$desc must begin with $beginDesc."; 209 String message = "$desc must begin with $beginDesc.";
210 return new RefactoringStatus.error(message); 210 return new RefactoringStatus.fatal(message);
211 } 211 }
212 for (int i = 1; i < length; i++) { 212 for (int i = 1; i < length; i++) {
213 currentChar = identifier.codeUnitAt(i); 213 currentChar = identifier.codeUnitAt(i);
214 if (!isLetterOrDigit(currentChar) && 214 if (!isLetterOrDigit(currentChar) &&
215 currentChar != CHAR_UNDERSCORE && 215 currentChar != CHAR_UNDERSCORE &&
216 currentChar != CHAR_DOLLAR) { 216 currentChar != CHAR_DOLLAR) {
217 String charStr = new String.fromCharCode(currentChar); 217 String charStr = new String.fromCharCode(currentChar);
218 String message = "$desc must not contain '$charStr'."; 218 String message = "$desc must not contain '$charStr'.";
219 return new RefactoringStatus.error(message); 219 return new RefactoringStatus.fatal(message);
220 } 220 }
221 } 221 }
222 return new RefactoringStatus(); 222 return new RefactoringStatus();
223 } 223 }
224 224
225 /** 225 /**
226 * Validates [identifier], should be lower camel case. 226 * Validates [identifier], should be lower camel case.
227 */ 227 */
228 RefactoringStatus _validateLowerCamelCase(String identifier, String desc) { 228 RefactoringStatus _validateLowerCamelCase(String identifier, String desc) {
229 desc += ' name'; 229 desc += ' name';
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 } 281 }
282 // does not start with upper case 282 // does not start with upper case
283 if (!isUpperCase(identifier.codeUnitAt(0))) { 283 if (!isUpperCase(identifier.codeUnitAt(0))) {
284 // By convention, class names usually start with an uppercase letter 284 // By convention, class names usually start with an uppercase letter
285 String message = "$desc should start with an uppercase letter."; 285 String message = "$desc should start with an uppercase letter.";
286 return new RefactoringStatus.warning(message); 286 return new RefactoringStatus.warning(message);
287 } 287 }
288 // OK 288 // OK
289 return new RefactoringStatus(); 289 return new RefactoringStatus();
290 } 290 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/test/services/refactoring/extract_method_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698