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'; |
| 11 import '../path.dart' as path; |
11 import '../validator.dart'; | 12 import '../validator.dart'; |
12 | 13 |
13 /// Dart reserved words, from the Dart spec. | 14 /// Dart reserved words, from the Dart spec. |
14 final _RESERVED_WORDS = [ | 15 final _RESERVED_WORDS = [ |
15 "abstract", "as", "dynamic", "export", "external", "factory", "get", | 16 "abstract", "as", "dynamic", "export", "external", "factory", "get", |
16 "implements", "import", "library", "operator", "part", "set", "static", | 17 "implements", "import", "library", "operator", "part", "set", "static", |
17 "typedef" | 18 "typedef" |
18 ]; | 19 ]; |
19 | 20 |
20 /// A validator that validates the name of the package and its libraries. | 21 /// A validator that validates the name of the package and its libraries. |
21 class NameValidator extends Validator { | 22 class NameValidator extends Validator { |
22 NameValidator(Entrypoint entrypoint) | 23 NameValidator(Entrypoint entrypoint) |
23 : super(entrypoint); | 24 : super(entrypoint); |
24 | 25 |
25 Future validate() { | 26 Future validate() { |
26 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"'); | 27 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"'); |
27 | 28 |
28 var libDir = join(entrypoint.root.dir, "lib"); | 29 var libDir = join(entrypoint.root.dir, "lib"); |
29 return dirExists(libDir).chain((libDirExists) { | 30 return dirExists(libDir).chain((libDirExists) { |
30 if (!libDirExists) return new Future.immediate([]); | 31 if (!libDirExists) return new Future.immediate([]); |
31 return listDir(libDir, recursive: true); | 32 return listDir(libDir, recursive: true); |
32 }).transform((files) { | 33 }).transform((files) { |
33 for (var file in files) { | 34 for (var file in files) { |
34 // TODO(nweiz): Since `file` is absolute, this will break if the package | 35 file = relativeTo(file, libDir); |
35 // itself is in a directory named "src" (issue 7215). | |
36 if (splitPath(file).contains("src")) continue; | 36 if (splitPath(file).contains("src")) continue; |
37 if (new Path(file).extension != 'dart') continue; | 37 if (path.extension(file) != '.dart') continue; |
38 var libName = new Path(basename(file)).filenameWithoutExtension; | 38 var libName = path.filenameWithoutExtension(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 must be a valid Dart identifier: it may only " |
(...skipping 17 matching lines...) Expand all Loading... |
66 builder | 66 builder |
67 ..add(source.substring(lastMatchEnd, match.start + 1)) | 67 ..add(source.substring(lastMatchEnd, match.start + 1)) |
68 ..add("_") | 68 ..add("_") |
69 ..add(match.group(1).toLowerCase()); | 69 ..add(match.group(1).toLowerCase()); |
70 lastMatchEnd = match.end; | 70 lastMatchEnd = match.end; |
71 } | 71 } |
72 builder.add(source.substring(lastMatchEnd)); | 72 builder.add(source.substring(lastMatchEnd)); |
73 return builder.toString().toLowerCase(); | 73 return builder.toString().toLowerCase(); |
74 } | 74 } |
75 } | 75 } |
OLD | NEW |