| OLD | NEW |
| 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 * ERROR 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 * ERROR 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.error("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) { |
| 36 return status; | 36 return status; |
| 37 } | 37 } |
| 38 // is private, OK | 38 // is private, OK |
| 39 int startIndex = 0; | 39 int startIndex = 0; |
| 40 if (name.codeUnitAt(0) == CHAR_UNDERSCORE) { | 40 if (name.codeUnitAt(0) == CHAR_UNDERSCORE) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 * ERROR if the name is illegal. |
| 116 */ | 116 */ |
| 117 RefactoringStatus validateLibraryName(String name) { | 117 RefactoringStatus validateLibraryName(String name) { |
| 118 // null | 118 // null |
| 119 if (name == null) { | 119 if (name == null) { |
| 120 return new RefactoringStatus.error("Library name must not be null."); | 120 return new RefactoringStatus.fatal("Library name must not be null."); |
| 121 } | 121 } |
| 122 // blank | 122 // blank |
| 123 if (isBlank(name)) { | 123 if (isBlank(name)) { |
| 124 return new RefactoringStatus.error("Library name must not be blank."); | 124 return new RefactoringStatus.fatal("Library name must not be blank."); |
| 125 } | 125 } |
| 126 // check identifiers | 126 // check identifiers |
| 127 List<String> identifiers = name.split('.'); | 127 List<String> identifiers = name.split('.'); |
| 128 for (String identifier in identifiers) { | 128 for (String identifier in identifiers) { |
| 129 RefactoringStatus status = | 129 RefactoringStatus status = |
| 130 _validateIdentifier( | 130 _validateIdentifier( |
| 131 identifier, | 131 identifier, |
| 132 "Library name identifier", | 132 "Library name identifier", |
| 133 "a lowercase letter or underscore"); | 133 "a lowercase letter or underscore"); |
| 134 if (!status.isOK) { | 134 if (!status.isOK) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 // has leading/trailing spaces | 183 // has leading/trailing spaces |
| 184 String trimmed = identifier.trim(); | 184 String trimmed = identifier.trim(); |
| 185 if (identifier != trimmed) { | 185 if (identifier != trimmed) { |
| 186 String message = "$desc must not start or end with a blank."; | 186 String message = "$desc must not start or end with a blank."; |
| 187 return new RefactoringStatus.error(message); | 187 return new RefactoringStatus.error(message); |
| 188 } | 188 } |
| 189 // empty | 189 // empty |
| 190 int length = identifier.length; | 190 int length = identifier.length; |
| 191 if (length == 0) { | 191 if (length == 0) { |
| 192 String message = "$desc must not be empty."; | 192 String message = "$desc must not be empty."; |
| 193 return new RefactoringStatus.error(message); | 193 return new RefactoringStatus.fatal(message); |
| 194 } | 194 } |
| 195 int currentChar = identifier.codeUnitAt(0); | 195 int currentChar = identifier.codeUnitAt(0); |
| 196 if (!isLetter(currentChar) && | 196 if (!isLetter(currentChar) && |
| 197 currentChar != CHAR_UNDERSCORE && | 197 currentChar != CHAR_UNDERSCORE && |
| 198 currentChar != CHAR_DOLLAR) { | 198 currentChar != CHAR_DOLLAR) { |
| 199 String message = "$desc must begin with $beginDesc."; | 199 String message = "$desc must begin with $beginDesc."; |
| 200 return new RefactoringStatus.error(message); | 200 return new RefactoringStatus.error(message); |
| 201 } | 201 } |
| 202 for (int i = 1; i < length; i++) { | 202 for (int i = 1; i < length; i++) { |
| 203 currentChar = identifier.codeUnitAt(i); | 203 currentChar = identifier.codeUnitAt(i); |
| 204 if (!isLetterOrDigit(currentChar) && | 204 if (!isLetterOrDigit(currentChar) && |
| 205 currentChar != CHAR_UNDERSCORE && | 205 currentChar != CHAR_UNDERSCORE && |
| 206 currentChar != CHAR_DOLLAR) { | 206 currentChar != CHAR_DOLLAR) { |
| 207 String charStr = new String.fromCharCode(currentChar); | 207 String charStr = new String.fromCharCode(currentChar); |
| 208 String message = "$desc must not contain '$charStr'."; | 208 String message = "$desc must not contain '$charStr'."; |
| 209 return new RefactoringStatus.error(message); | 209 return new RefactoringStatus.error(message); |
| 210 } | 210 } |
| 211 } | 211 } |
| 212 return new RefactoringStatus(); | 212 return new RefactoringStatus(); |
| 213 } | 213 } |
| 214 | 214 |
| 215 /** | 215 /** |
| 216 * Validates [identifier], should be lower camel case. | 216 * Validates [identifier], should be lower camel case. |
| 217 */ | 217 */ |
| 218 RefactoringStatus _validateLowerCamelCase(String identifier, String desc) { | 218 RefactoringStatus _validateLowerCamelCase(String identifier, String desc) { |
| 219 desc += ' name'; | 219 desc += ' name'; |
| 220 // null | 220 // null |
| 221 if (identifier == null) { | 221 if (identifier == null) { |
| 222 String message = "$desc must not be null."; | 222 String message = "$desc must not be null."; |
| 223 return new RefactoringStatus.error(message); | 223 return new RefactoringStatus.fatal(message); |
| 224 } | 224 } |
| 225 // is not identifier | 225 // is not identifier |
| 226 RefactoringStatus status = | 226 RefactoringStatus status = |
| 227 _validateIdentifier(identifier, desc, "a lowercase letter or underscore"); | 227 _validateIdentifier(identifier, desc, "a lowercase letter or underscore"); |
| 228 if (!status.isOK) { | 228 if (!status.isOK) { |
| 229 return status; | 229 return status; |
| 230 } | 230 } |
| 231 // is private, OK | 231 // is private, OK |
| 232 if (identifier.codeUnitAt(0) == CHAR_UNDERSCORE) { | 232 if (identifier.codeUnitAt(0) == CHAR_UNDERSCORE) { |
| 233 return new RefactoringStatus(); | 233 return new RefactoringStatus(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 246 } | 246 } |
| 247 | 247 |
| 248 /** | 248 /** |
| 249 * Validate the given identifier, which should be upper camel case. | 249 * Validate the given identifier, which should be upper camel case. |
| 250 */ | 250 */ |
| 251 RefactoringStatus _validateUpperCamelCase(String identifier, String desc) { | 251 RefactoringStatus _validateUpperCamelCase(String identifier, String desc) { |
| 252 desc += ' name'; | 252 desc += ' name'; |
| 253 // null | 253 // null |
| 254 if (identifier == null) { | 254 if (identifier == null) { |
| 255 String message = "$desc must not be null."; | 255 String message = "$desc must not be null."; |
| 256 return new RefactoringStatus.error(message); | 256 return new RefactoringStatus.fatal(message); |
| 257 } | 257 } |
| 258 // is not identifier | 258 // is not identifier |
| 259 RefactoringStatus status = | 259 RefactoringStatus status = |
| 260 _validateIdentifier(identifier, desc, "an uppercase letter or underscore")
; | 260 _validateIdentifier(identifier, desc, "an uppercase letter or underscore")
; |
| 261 if (!status.isOK) { | 261 if (!status.isOK) { |
| 262 return status; | 262 return status; |
| 263 } | 263 } |
| 264 // is private, OK | 264 // is private, OK |
| 265 if (identifier.codeUnitAt(0) == CHAR_UNDERSCORE) { | 265 if (identifier.codeUnitAt(0) == CHAR_UNDERSCORE) { |
| 266 return new RefactoringStatus(); | 266 return new RefactoringStatus(); |
| 267 } | 267 } |
| 268 // leading $, OK | 268 // leading $, OK |
| 269 if (identifier.codeUnitAt(0) == CHAR_DOLLAR) { | 269 if (identifier.codeUnitAt(0) == CHAR_DOLLAR) { |
| 270 return new RefactoringStatus(); | 270 return new RefactoringStatus(); |
| 271 } | 271 } |
| 272 // does not start with upper case | 272 // does not start with upper case |
| 273 if (!isUpperCase(identifier.codeUnitAt(0))) { | 273 if (!isUpperCase(identifier.codeUnitAt(0))) { |
| 274 // By convention, class names usually start with an uppercase letter | 274 // By convention, class names usually start with an uppercase letter |
| 275 String message = "$desc should start with an uppercase letter."; | 275 String message = "$desc should start with an uppercase letter."; |
| 276 return new RefactoringStatus.warning(message); | 276 return new RefactoringStatus.warning(message); |
| 277 } | 277 } |
| 278 // OK | 278 // OK |
| 279 return new RefactoringStatus(); | 279 return new RefactoringStatus(); |
| 280 } | 280 } |
| OLD | NEW |