OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 pub.validator.name; | 5 library pub.validator.name; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:path/path.dart' as path; | 9 import 'package:path/path.dart' as path; |
10 | 10 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 void _checkName(String name, String description, {bool isPackage}) { | 63 void _checkName(String name, String description, {bool isPackage}) { |
64 // Packages names are more stringent than libraries. | 64 // Packages names are more stringent than libraries. |
65 var messages = isPackage ? errors : warnings; | 65 var messages = isPackage ? errors : warnings; |
66 | 66 |
67 if (name == "") { | 67 if (name == "") { |
68 errors.add("$description may not be empty."); | 68 errors.add("$description may not be empty."); |
69 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { | 69 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { |
70 messages.add("$description may only contain letters, numbers, and " | 70 messages.add("$description may only contain letters, numbers, and " |
71 "underscores.\n" | 71 "underscores.\n" |
72 "Using a valid Dart identifier makes the name usable in Dart code."); | 72 "Using a valid Dart identifier makes the name usable in Dart code."); |
73 } else if (!new RegExp(r"^[a-zA-Z]").hasMatch(name)) { | 73 } else if (!new RegExp(r"^[a-zA-Z_]").hasMatch(name)) { |
74 messages.add("$description must begin with letter.\n" | 74 messages.add("$description must begin with a letter or underscore.\n" |
75 "Using a valid Dart identifier makes the name usable in Dart code."); | 75 "Using a valid Dart identifier makes the name usable in Dart code."); |
76 } else if (_RESERVED_WORDS.contains(name.toLowerCase())) { | 76 } else if (_RESERVED_WORDS.contains(name.toLowerCase())) { |
77 messages.add("$description may not be a reserved word in Dart.\n" | 77 messages.add("$description may not be a reserved word in Dart.\n" |
78 "Using a valid Dart identifier makes the name usable in Dart code."); | 78 "Using a valid Dart identifier makes the name usable in Dart code."); |
79 } else if (new RegExp(r"[A-Z]").hasMatch(name)) { | 79 } else if (new RegExp(r"[A-Z]").hasMatch(name)) { |
80 warnings.add('$description should be lower-case. Maybe use ' | 80 warnings.add('$description should be lower-case. Maybe use ' |
81 '"${_unCamelCase(name)}"?'); | 81 '"${_unCamelCase(name)}"?'); |
82 } | 82 } |
83 } | 83 } |
84 | 84 |
85 String _unCamelCase(String source) { | 85 String _unCamelCase(String source) { |
86 var builder = new StringBuffer(); | 86 var builder = new StringBuffer(); |
87 var lastMatchEnd = 0; | 87 var lastMatchEnd = 0; |
88 for (var match in new RegExp(r"[a-z]([A-Z])").allMatches(source)) { | 88 for (var match in new RegExp(r"[a-z]([A-Z])").allMatches(source)) { |
89 builder | 89 builder |
90 ..write(source.substring(lastMatchEnd, match.start + 1)) | 90 ..write(source.substring(lastMatchEnd, match.start + 1)) |
91 ..write("_") | 91 ..write("_") |
92 ..write(match.group(1).toLowerCase()); | 92 ..write(match.group(1).toLowerCase()); |
93 lastMatchEnd = match.end; | 93 lastMatchEnd = match.end; |
94 } | 94 } |
95 builder.write(source.substring(lastMatchEnd)); | 95 builder.write(source.substring(lastMatchEnd)); |
96 return builder.toString().toLowerCase(); | 96 return builder.toString().toLowerCase(); |
97 } | 97 } |
98 } | 98 } |
OLD | NEW |