Improve error message for invalid characters

The order of checks has been swapped so invlaid characters (which give a more specific message) are returned over those for first letters.

Bug: https://github.com/dart-lang/sdk/issues/32678
Change-Id: I784191ea7a0e072cc8994bbb47b03421e33aa90e
Reviewed-on: https://dart-review.googlesource.com/54244
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Danny Tuppeny <dantup@google.com>
diff --git a/pkg/analysis_server/lib/src/services/refactoring/naming_conventions.dart b/pkg/analysis_server/lib/src/services/refactoring/naming_conventions.dart
index 0e5e556..94bfd73 100644
--- a/pkg/analysis_server/lib/src/services/refactoring/naming_conventions.dart
+++ b/pkg/analysis_server/lib/src/services/refactoring/naming_conventions.dart
@@ -178,17 +178,9 @@
       }
     }
   }
-  // first character
-  int currentChar = identifier.codeUnitAt(0);
-  if (!isLetter(currentChar) &&
-      currentChar != CHAR_UNDERSCORE &&
-      currentChar != CHAR_DOLLAR) {
-    String message = "$desc must begin with $beginDesc.";
-    return new RefactoringStatus.fatal(message);
-  }
-  // other characters
-  for (int i = 1; i < length; i++) {
-    currentChar = identifier.codeUnitAt(i);
+  // invalid characters
+  for (int i = 0; i < length; i++) {
+    int currentChar = identifier.codeUnitAt(i);
     if (!isLetterOrDigit(currentChar) &&
         currentChar != CHAR_UNDERSCORE &&
         currentChar != CHAR_DOLLAR) {
@@ -197,6 +189,14 @@
       return new RefactoringStatus.fatal(message);
     }
   }
+  // first character
+  final int currentChar = identifier.codeUnitAt(0);
+  if (!isLetter(currentChar) &&
+      currentChar != CHAR_UNDERSCORE &&
+      currentChar != CHAR_DOLLAR) {
+    String message = "$desc must begin with $beginDesc.";
+    return new RefactoringStatus.fatal(message);
+  }
   // OK
   return new RefactoringStatus();
 }
diff --git a/pkg/analysis_server/test/services/refactoring/extract_method_test.dart b/pkg/analysis_server/test/services/refactoring/extract_method_test.dart
index 4079387..bcdb836 100644
--- a/pkg/analysis_server/test/services/refactoring/extract_method_test.dart
+++ b/pkg/analysis_server/test/services/refactoring/extract_method_test.dart
@@ -779,6 +779,22 @@
     assertRefactoringStatus(
         refactoring.checkName(), RefactoringProblemSeverity.FATAL,
         expectedMessage: "Method name must not be empty.");
+    // incorrect casing
+    refactoring.name = 'Aaa';
+    assertRefactoringStatus(
+        refactoring.checkName(), RefactoringProblemSeverity.WARNING,
+        expectedMessage: "Method name should start with a lowercase letter.");
+    // starts with digit
+    refactoring.name = '0aa';
+    assertRefactoringStatus(
+        refactoring.checkName(), RefactoringProblemSeverity.FATAL,
+        expectedMessage:
+            "Method name must begin with a lowercase letter or underscore.");
+    // invalid name (quote)
+    refactoring.name = '"';
+    assertRefactoringStatus(
+        refactoring.checkName(), RefactoringProblemSeverity.FATAL,
+        expectedMessage: "Method name must not contain '\"'.");
     // OK
     refactoring.name = 'res';
     assertRefactoringStatusOK(refactoring.checkName());
diff --git a/pkg/analysis_server/test/services/refactoring/naming_conventions_test.dart b/pkg/analysis_server/test/services/refactoring/naming_conventions_test.dart
index fb78873..9c3f157 100644
--- a/pkg/analysis_server/test/services/refactoring/naming_conventions_test.dart
+++ b/pkg/analysis_server/test/services/refactoring/naming_conventions_test.dart
@@ -48,9 +48,14 @@
 
   void test_validateClassName_notIdentifierStart() {
     assertRefactoringStatus(
+        validateClassName("badName"), RefactoringProblemSeverity.WARNING,
+        expectedMessage: "Class name should start with an uppercase letter.");
+  }
+
+  void test_validateClassName_invalidCharacter() {
+    assertRefactoringStatus(
         validateClassName("-NewName"), RefactoringProblemSeverity.FATAL,
-        expectedMessage:
-            "Class name must begin with an uppercase letter or underscore.");
+        expectedMessage: "Class name must not contain '-'.");
   }
 
   void test_validateClassName_null() {
@@ -290,10 +295,16 @@
   }
 
   void test_validateFunctionTypeAliasName_notIdentifierStart() {
-    assertRefactoringStatus(validateFunctionTypeAliasName("-NewName"),
-        RefactoringProblemSeverity.FATAL,
+    assertRefactoringStatus(validateFunctionTypeAliasName("newName"),
+        RefactoringProblemSeverity.WARNING,
         expectedMessage:
-            "Function type alias name must begin with an uppercase letter or underscore.");
+            "Function type alias name should start with an uppercase letter.");
+  }
+
+  void test_validateFunctionTypeAliasName_invalidCharacters() {
+    assertRefactoringStatus(validateFunctionTypeAliasName("New-Name"),
+        RefactoringProblemSeverity.FATAL,
+        expectedMessage: "Function type alias name must not contain \'-\'.");
   }
 
   void test_validateFunctionTypeAliasName_null() {