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:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import '../../../pkg/path/lib/path.dart' as path; | 10 import '../../../pkg/path/lib/path.dart' as path; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 }).then((files) { | 58 }).then((files) { |
59 return files | 59 return files |
60 .map((file) => path.relative(file, from: path.dirname(libDir))) | 60 .map((file) => path.relative(file, from: path.dirname(libDir))) |
61 .where((file) => !path.split(file).contains("src") && | 61 .where((file) => !path.split(file).contains("src") && |
62 path.extension(file) == '.dart') | 62 path.extension(file) == '.dart') |
63 .toList(); | 63 .toList(); |
64 }); | 64 }); |
65 } | 65 } |
66 | 66 |
67 void _checkName(String name, String description, {bool isPackage}) { | 67 void _checkName(String name, String description, {bool isPackage}) { |
| 68 // Packages names are more stringent than libraries. |
| 69 var messages = isPackage ? errors : warnings; |
| 70 |
68 if (name == "") { | 71 if (name == "") { |
69 errors.add("$description may not be empty."); | 72 errors.add("$description may not be empty."); |
70 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { | 73 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { |
71 warnings.add("$description may only contain letters, numbers, and " | 74 messages.add("$description may only contain letters, numbers, and " |
72 "underscores.\n" | 75 "underscores.\n" |
73 "Using a valid Dart identifier makes the name usable in Dart code."); | 76 "Using a valid Dart identifier makes the name usable in Dart code."); |
74 } else if (!new RegExp(r"^[a-zA-Z]").hasMatch(name)) { | 77 } else if (!new RegExp(r"^[a-zA-Z]").hasMatch(name)) { |
75 warnings.add("$description must begin with letter.\n" | 78 messages.add("$description must begin with letter.\n" |
76 "Using a valid Dart identifier makes the name usable in Dart code."); | 79 "Using a valid Dart identifier makes the name usable in Dart code."); |
77 } else if (_RESERVED_WORDS.contains(name.toLowerCase())) { | 80 } else if (_RESERVED_WORDS.contains(name.toLowerCase())) { |
78 var messages = isPackage ? errors : warnings; | |
79 messages.add("$description may not be a reserved word in Dart.\n" | 81 messages.add("$description may not be a reserved word in Dart.\n" |
80 "Using a valid Dart identifier makes the name usable in Dart code."); | 82 "Using a valid Dart identifier makes the name usable in Dart code."); |
81 } else if (new RegExp(r"[A-Z]").hasMatch(name)) { | 83 } else if (new RegExp(r"[A-Z]").hasMatch(name)) { |
82 warnings.add('$description should be lower-case. Maybe use ' | 84 warnings.add('$description should be lower-case. Maybe use ' |
83 '"${_unCamelCase(name)}"?'); | 85 '"${_unCamelCase(name)}"?'); |
84 } | 86 } |
85 } | 87 } |
86 | 88 |
87 String _unCamelCase(String source) { | 89 String _unCamelCase(String source) { |
88 var builder = new StringBuffer(); | 90 var builder = new StringBuffer(); |
89 var lastMatchEnd = 0; | 91 var lastMatchEnd = 0; |
90 for (var match in new RegExp(r"[a-z]([A-Z])").allMatches(source)) { | 92 for (var match in new RegExp(r"[a-z]([A-Z])").allMatches(source)) { |
91 builder | 93 builder |
92 ..add(source.substring(lastMatchEnd, match.start + 1)) | 94 ..add(source.substring(lastMatchEnd, match.start + 1)) |
93 ..add("_") | 95 ..add("_") |
94 ..add(match.group(1).toLowerCase()); | 96 ..add(match.group(1).toLowerCase()); |
95 lastMatchEnd = match.end; | 97 lastMatchEnd = match.end; |
96 } | 98 } |
97 builder.add(source.substring(lastMatchEnd)); | 99 builder.add(source.substring(lastMatchEnd)); |
98 return builder.toString().toLowerCase(); | 100 return builder.toString().toLowerCase(); |
99 } | 101 } |
100 } | 102 } |
OLD | NEW |