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 |
11 import '../entrypoint.dart'; | 11 import '../entrypoint.dart'; |
| 12 import '../utils.dart'; |
12 import '../validator.dart'; | 13 import '../validator.dart'; |
13 | 14 |
14 /// Dart reserved words, from the Dart spec. | |
15 final _RESERVED_WORDS = [ | |
16 "assert", "break", "case", "catch", "class", "const", "continue", "default", | |
17 "do", "else", "extends", "false", "final", "finally", "for", "if", "in", "is", | |
18 "new", "null", "return", "super", "switch", "this", "throw", "true", "try", | |
19 "var", "void", "while", "with" | |
20 ]; | |
21 | |
22 /// A validator that validates the name of the package and its libraries. | 15 /// A validator that validates the name of the package and its libraries. |
23 class NameValidator extends Validator { | 16 class NameValidator extends Validator { |
24 NameValidator(Entrypoint entrypoint) | 17 NameValidator(Entrypoint entrypoint) |
25 : super(entrypoint); | 18 : super(entrypoint); |
26 | 19 |
27 Future validate() { | 20 Future validate() { |
28 return new Future.sync(() { | 21 return new Future.sync(() { |
29 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"', | 22 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"', |
30 isPackage: true); | 23 isPackage: true); |
31 | 24 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 56 |
64 if (name == "") { | 57 if (name == "") { |
65 errors.add("$description may not be empty."); | 58 errors.add("$description may not be empty."); |
66 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { | 59 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { |
67 messages.add("$description may only contain letters, numbers, and " | 60 messages.add("$description may only contain letters, numbers, and " |
68 "underscores.\n" | 61 "underscores.\n" |
69 "Using a valid Dart identifier makes the name usable in Dart code."); | 62 "Using a valid Dart identifier makes the name usable in Dart code."); |
70 } else if (!new RegExp(r"^[a-zA-Z_]").hasMatch(name)) { | 63 } else if (!new RegExp(r"^[a-zA-Z_]").hasMatch(name)) { |
71 messages.add("$description must begin with a letter or underscore.\n" | 64 messages.add("$description must begin with a letter or underscore.\n" |
72 "Using a valid Dart identifier makes the name usable in Dart code."); | 65 "Using a valid Dart identifier makes the name usable in Dart code."); |
73 } else if (_RESERVED_WORDS.contains(name.toLowerCase())) { | 66 } else if (reservedWords.contains(name.toLowerCase())) { |
74 messages.add("$description may not be a reserved word in Dart.\n" | 67 messages.add("$description may not be a reserved word in Dart.\n" |
75 "Using a valid Dart identifier makes the name usable in Dart code."); | 68 "Using a valid Dart identifier makes the name usable in Dart code."); |
76 } else if (new RegExp(r"[A-Z]").hasMatch(name)) { | 69 } else if (new RegExp(r"[A-Z]").hasMatch(name)) { |
77 warnings.add('$description should be lower-case. Maybe use ' | 70 warnings.add('$description should be lower-case. Maybe use ' |
78 '"${_unCamelCase(name)}"?'); | 71 '"${_unCamelCase(name)}"?'); |
79 } | 72 } |
80 } | 73 } |
81 | 74 |
82 String _unCamelCase(String source) { | 75 String _unCamelCase(String source) { |
83 var builder = new StringBuffer(); | 76 var builder = new StringBuffer(); |
84 var lastMatchEnd = 0; | 77 var lastMatchEnd = 0; |
85 for (var match in new RegExp(r"[a-z]([A-Z])").allMatches(source)) { | 78 for (var match in new RegExp(r"[a-z]([A-Z])").allMatches(source)) { |
86 builder | 79 builder |
87 ..write(source.substring(lastMatchEnd, match.start + 1)) | 80 ..write(source.substring(lastMatchEnd, match.start + 1)) |
88 ..write("_") | 81 ..write("_") |
89 ..write(match.group(1).toLowerCase()); | 82 ..write(match.group(1).toLowerCase()); |
90 lastMatchEnd = match.end; | 83 lastMatchEnd = match.end; |
91 } | 84 } |
92 builder.write(source.substring(lastMatchEnd)); | 85 builder.write(source.substring(lastMatchEnd)); |
93 return builder.toString().toLowerCase(); | 86 return builder.toString().toLowerCase(); |
94 } | 87 } |
95 } | 88 } |
OLD | NEW |