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 name_validator; | 5 library name_validator; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 | 8 |
9 import '../entrypoint.dart'; | 9 import '../entrypoint.dart'; |
10 import '../io.dart'; | 10 import '../io.dart'; |
(...skipping 27 matching lines...) Expand all Loading... |
38 var libName = path.basenameWithoutExtension(file); | 38 var libName = path.basenameWithoutExtension(file); |
39 _checkName(libName, 'The name of "$file", "$libName",'); | 39 _checkName(libName, 'The name of "$file", "$libName",'); |
40 } | 40 } |
41 }); | 41 }); |
42 } | 42 } |
43 | 43 |
44 void _checkName(String name, String description) { | 44 void _checkName(String name, String description) { |
45 if (name == "") { | 45 if (name == "") { |
46 errors.add("$description may not be empty."); | 46 errors.add("$description may not be empty."); |
47 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { | 47 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { |
48 errors.add("$description must be a valid Dart identifier: it may only " | 48 errors.add("$description may only contain letters, numbers, and " |
49 "contain letters, numbers, and underscores."); | 49 "underscores.\n" |
| 50 "Using a valid Dart identifier makes the name usable in Dart code."); |
50 } else if (!new RegExp(r"^[a-zA-Z]").hasMatch(name)) { | 51 } else if (!new RegExp(r"^[a-zA-Z]").hasMatch(name)) { |
51 errors.add("$description must be a valid Dart identifier: it must begin " | 52 errors.add("$description must begin with letter.\n" |
52 "with a letter."); | 53 "Using a valid Dart identifier makes the name usable in Dart code."); |
53 } else if (_RESERVED_WORDS.contains(name.toLowerCase())) { | 54 } else if (_RESERVED_WORDS.contains(name.toLowerCase())) { |
54 errors.add("$description must be a valid Dart identifier: it may not be " | 55 errors.add("$description may not be a reserved word in Dart.\n" |
55 "a reserved word in Dart."); | 56 "Using a valid Dart identifier makes the name usable in Dart code."); |
56 } else if (new RegExp(r"[A-Z]").hasMatch(name)) { | 57 } else if (new RegExp(r"[A-Z]").hasMatch(name)) { |
57 warnings.add('$description should be lower-case. Maybe use ' | 58 warnings.add('$description should be lower-case. Maybe use ' |
58 '"${_unCamelCase(name)}"?'); | 59 '"${_unCamelCase(name)}"?'); |
59 } | 60 } |
60 } | 61 } |
61 | 62 |
62 String _unCamelCase(String source) { | 63 String _unCamelCase(String source) { |
63 var builder = new StringBuffer(); | 64 var builder = new StringBuffer(); |
64 var lastMatchEnd = 0; | 65 var lastMatchEnd = 0; |
65 for (var match in new RegExp(r"[a-z]([A-Z])").allMatches(source)) { | 66 for (var match in new RegExp(r"[a-z]([A-Z])").allMatches(source)) { |
66 builder | 67 builder |
67 ..add(source.substring(lastMatchEnd, match.start + 1)) | 68 ..add(source.substring(lastMatchEnd, match.start + 1)) |
68 ..add("_") | 69 ..add("_") |
69 ..add(match.group(1).toLowerCase()); | 70 ..add(match.group(1).toLowerCase()); |
70 lastMatchEnd = match.end; | 71 lastMatchEnd = match.end; |
71 } | 72 } |
72 builder.add(source.substring(lastMatchEnd)); | 73 builder.add(source.substring(lastMatchEnd)); |
73 return builder.toString().toLowerCase(); | 74 return builder.toString().toLowerCase(); |
74 } | 75 } |
75 } | 76 } |
OLD | NEW |