| 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:io'; | 8 import 'dart:io'; |
| 8 | 9 |
| 9 import '../../../pkg/path/lib/path.dart' as path; | 10 import '../../../pkg/path/lib/path.dart' as path; |
| 10 import '../entrypoint.dart'; | 11 import '../entrypoint.dart'; |
| 11 import '../io.dart'; | 12 import '../io.dart'; |
| 12 import '../validator.dart'; | 13 import '../validator.dart'; |
| 13 | 14 |
| 14 /// Dart reserved words, from the Dart spec. | 15 /// Dart reserved words, from the Dart spec. |
| 15 final _RESERVED_WORDS = [ | 16 final _RESERVED_WORDS = [ |
| 16 "abstract", "as", "dynamic", "export", "external", "factory", "get", | 17 "abstract", "as", "dynamic", "export", "external", "factory", "get", |
| 17 "implements", "import", "library", "operator", "part", "set", "static", | 18 "implements", "import", "library", "operator", "part", "set", "static", |
| 18 "typedef" | 19 "typedef" |
| 19 ]; | 20 ]; |
| 20 | 21 |
| 21 /// A validator that validates the name of the package and its libraries. | 22 /// A validator that validates the name of the package and its libraries. |
| 22 class NameValidator extends Validator { | 23 class NameValidator extends Validator { |
| 23 NameValidator(Entrypoint entrypoint) | 24 NameValidator(Entrypoint entrypoint) |
| 24 : super(entrypoint); | 25 : super(entrypoint); |
| 25 | 26 |
| 26 Future validate() { | 27 Future validate() { |
| 27 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"'); | 28 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"'); |
| 28 | 29 |
| 29 return _libraries.transform((libraries) { | 30 return _libraries.then((libraries) { |
| 30 for (var library in libraries) { | 31 for (var library in libraries) { |
| 31 var libName = path.basenameWithoutExtension(library); | 32 var libName = path.basenameWithoutExtension(library); |
| 32 _checkName(libName, 'The name of "$library", "$libName",'); | 33 _checkName(libName, 'The name of "$library", "$libName",'); |
| 33 } | 34 } |
| 34 | 35 |
| 35 if (libraries.length == 1) { | 36 if (libraries.length == 1) { |
| 36 var libName = path.basenameWithoutExtension(libraries[0]); | 37 var libName = path.basenameWithoutExtension(libraries[0]); |
| 37 if (libName == entrypoint.root.name) return; | 38 if (libName == entrypoint.root.name) return; |
| 38 warnings.add('The name of "${libraries[0]}", "$libName", should match ' | 39 warnings.add('The name of "${libraries[0]}", "$libName", should match ' |
| 39 'the name of the package, "${entrypoint.root.name}".\n' | 40 'the name of the package, "${entrypoint.root.name}".\n' |
| 40 'This helps users know what library to import.'); | 41 'This helps users know what library to import.'); |
| 41 } | 42 } |
| 42 }); | 43 }); |
| 43 } | 44 } |
| 44 | 45 |
| 45 /// Returns a list of all libraries in the current package as paths relative | 46 /// Returns a list of all libraries in the current package as paths relative |
| 46 /// to the package's root directory. | 47 /// to the package's root directory. |
| 47 Future<List<String>> get _libraries { | 48 Future<List<String>> get _libraries { |
| 48 var libDir = join(entrypoint.root.dir, "lib"); | 49 var libDir = join(entrypoint.root.dir, "lib"); |
| 49 return dirExists(libDir).chain((libDirExists) { | 50 return dirExists(libDir).then((libDirExists) { |
| 50 if (!libDirExists) return new Future.immediate([]); | 51 if (!libDirExists) return new Future.immediate([]); |
| 51 return listDir(libDir, recursive: true); | 52 return listDir(libDir, recursive: true); |
| 52 }).then((files) { | 53 }).then((files) { |
| 53 return files.map((file) => relativeTo(file, dirname(libDir))) | 54 return files |
| 54 .filter((file) { | 55 .mappedBy((file) => relativeTo(file, dirname(libDir))) |
| 55 return !splitPath(file).contains("src") && | 56 .where((file) => !splitPath(file).contains("src") && |
| 56 path.extension(file) == '.dart'; | 57 path.extension(file) == '.dart') |
| 57 }); | 58 .toList(); |
| 58 }); | 59 }); |
| 59 } | 60 } |
| 60 | 61 |
| 61 void _checkName(String name, String description) { | 62 void _checkName(String name, String description) { |
| 62 if (name == "") { | 63 if (name == "") { |
| 63 errors.add("$description may not be empty."); | 64 errors.add("$description may not be empty."); |
| 64 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { | 65 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { |
| 65 errors.add("$description may only contain letters, numbers, and " | 66 errors.add("$description may only contain letters, numbers, and " |
| 66 "underscores.\n" | 67 "underscores.\n" |
| 67 "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."); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 84 builder | 85 builder |
| 85 ..add(source.substring(lastMatchEnd, match.start + 1)) | 86 ..add(source.substring(lastMatchEnd, match.start + 1)) |
| 86 ..add("_") | 87 ..add("_") |
| 87 ..add(match.group(1).toLowerCase()); | 88 ..add(match.group(1).toLowerCase()); |
| 88 lastMatchEnd = match.end; | 89 lastMatchEnd = match.end; |
| 89 } | 90 } |
| 90 builder.add(source.substring(lastMatchEnd)); | 91 builder.add(source.substring(lastMatchEnd)); |
| 91 return builder.toString().toLowerCase(); | 92 return builder.toString().toLowerCase(); |
| 92 } | 93 } |
| 93 } | 94 } |
| OLD | NEW |