| 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 import 'package:analyzer/src/generated/scanner.dart'; |
| 9 | 10 |
| 10 /** | 11 /** |
| 11 * Returns the [RefactoringStatus] with severity: | 12 * Returns the [RefactoringStatus] with severity: |
| 12 * OK if the name is valid; | 13 * OK if the name is valid; |
| 13 * WARNING if the name is discouraged; | 14 * WARNING if the name is discouraged; |
| 14 * FATAL if the name is illegal. | 15 * FATAL if the name is illegal. |
| 15 */ | 16 */ |
| 16 RefactoringStatus validateClassName(String name) { | 17 RefactoringStatus validateClassName(String name) { |
| 17 return _validateUpperCamelCase(name, "Class"); | 18 return _validateUpperCamelCase(name, "Class"); |
| 18 } | 19 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 if (identifier != trimmed) { | 158 if (identifier != trimmed) { |
| 158 String message = "$desc must not start or end with a blank."; | 159 String message = "$desc must not start or end with a blank."; |
| 159 return new RefactoringStatus.fatal(message); | 160 return new RefactoringStatus.fatal(message); |
| 160 } | 161 } |
| 161 // empty | 162 // empty |
| 162 int length = identifier.length; | 163 int length = identifier.length; |
| 163 if (length == 0) { | 164 if (length == 0) { |
| 164 String message = "$desc must not be empty."; | 165 String message = "$desc must not be empty."; |
| 165 return new RefactoringStatus.fatal(message); | 166 return new RefactoringStatus.fatal(message); |
| 166 } | 167 } |
| 168 // keyword |
| 169 { |
| 170 Keyword keyword = Keyword.keywords[identifier]; |
| 171 if (keyword != null && !keyword.isPseudoKeyword) { |
| 172 String message = "$desc must not be a keyword."; |
| 173 return new RefactoringStatus.fatal(message); |
| 174 } |
| 175 } |
| 176 // first character |
| 167 int currentChar = identifier.codeUnitAt(0); | 177 int currentChar = identifier.codeUnitAt(0); |
| 168 if (!isLetter(currentChar) && | 178 if (!isLetter(currentChar) && |
| 169 currentChar != CHAR_UNDERSCORE && | 179 currentChar != CHAR_UNDERSCORE && |
| 170 currentChar != CHAR_DOLLAR) { | 180 currentChar != CHAR_DOLLAR) { |
| 171 String message = "$desc must begin with $beginDesc."; | 181 String message = "$desc must begin with $beginDesc."; |
| 172 return new RefactoringStatus.fatal(message); | 182 return new RefactoringStatus.fatal(message); |
| 173 } | 183 } |
| 184 // other characters |
| 174 for (int i = 1; i < length; i++) { | 185 for (int i = 1; i < length; i++) { |
| 175 currentChar = identifier.codeUnitAt(i); | 186 currentChar = identifier.codeUnitAt(i); |
| 176 if (!isLetterOrDigit(currentChar) && | 187 if (!isLetterOrDigit(currentChar) && |
| 177 currentChar != CHAR_UNDERSCORE && | 188 currentChar != CHAR_UNDERSCORE && |
| 178 currentChar != CHAR_DOLLAR) { | 189 currentChar != CHAR_DOLLAR) { |
| 179 String charStr = new String.fromCharCode(currentChar); | 190 String charStr = new String.fromCharCode(currentChar); |
| 180 String message = "$desc must not contain '$charStr'."; | 191 String message = "$desc must not contain '$charStr'."; |
| 181 return new RefactoringStatus.fatal(message); | 192 return new RefactoringStatus.fatal(message); |
| 182 } | 193 } |
| 183 } | 194 } |
| 195 // OK |
| 184 return new RefactoringStatus(); | 196 return new RefactoringStatus(); |
| 185 } | 197 } |
| 186 | 198 |
| 187 /** | 199 /** |
| 188 * Validates [identifier], should be lower camel case. | 200 * Validates [identifier], should be lower camel case. |
| 189 */ | 201 */ |
| 190 RefactoringStatus _validateLowerCamelCase(String identifier, String desc) { | 202 RefactoringStatus _validateLowerCamelCase(String identifier, String desc) { |
| 191 desc += ' name'; | 203 desc += ' name'; |
| 192 // null | 204 // null |
| 193 if (identifier == null) { | 205 if (identifier == null) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 } | 255 } |
| 244 // does not start with upper case | 256 // does not start with upper case |
| 245 if (!isUpperCase(identifier.codeUnitAt(0))) { | 257 if (!isUpperCase(identifier.codeUnitAt(0))) { |
| 246 // By convention, class names usually start with an uppercase letter | 258 // By convention, class names usually start with an uppercase letter |
| 247 String message = "$desc should start with an uppercase letter."; | 259 String message = "$desc should start with an uppercase letter."; |
| 248 return new RefactoringStatus.warning(message); | 260 return new RefactoringStatus.warning(message); |
| 249 } | 261 } |
| 250 // OK | 262 // OK |
| 251 return new RefactoringStatus(); | 263 return new RefactoringStatus(); |
| 252 } | 264 } |
| OLD | NEW |