Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(457)

Side by Side Diff: utils/pub/validator/name.dart

Issue 12313014: Make some name validation errors warnings. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Make using a reserved word as a package name an error. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | utils/tests/pub/validator_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 10 matching lines...) Expand all
21 "new", "null", "return", "super", "switch", "this", "throw", "true", "try", 21 "new", "null", "return", "super", "switch", "this", "throw", "true", "try",
22 "var", "void", "while", "with" 22 "var", "void", "while", "with"
23 ]; 23 ];
24 24
25 /// A validator that validates the name of the package and its libraries. 25 /// A validator that validates the name of the package and its libraries.
26 class NameValidator extends Validator { 26 class NameValidator extends Validator {
27 NameValidator(Entrypoint entrypoint) 27 NameValidator(Entrypoint entrypoint)
28 : super(entrypoint); 28 : super(entrypoint);
29 29
30 Future validate() { 30 Future validate() {
31 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"'); 31 _checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"',
32 isPackage: true);
32 33
33 return _libraries.then((libraries) { 34 return _libraries.then((libraries) {
34 for (var library in libraries) { 35 for (var library in libraries) {
35 var libName = path.basenameWithoutExtension(library); 36 var libName = path.basenameWithoutExtension(library);
36 _checkName(libName, 'The name of "$library", "$libName",'); 37 _checkName(libName, 'The name of "$library", "$libName",',
38 isPackage: false);
37 } 39 }
38 40
39 if (libraries.length == 1) { 41 if (libraries.length == 1) {
40 var libName = path.basenameWithoutExtension(libraries[0]); 42 var libName = path.basenameWithoutExtension(libraries[0]);
41 if (libName == entrypoint.root.name) return; 43 if (libName == entrypoint.root.name) return;
42 warnings.add('The name of "${libraries[0]}", "$libName", should match ' 44 warnings.add('The name of "${libraries[0]}", "$libName", should match '
43 'the name of the package, "${entrypoint.root.name}".\n' 45 'the name of the package, "${entrypoint.root.name}".\n'
44 'This helps users know what library to import.'); 46 'This helps users know what library to import.');
45 } 47 }
46 }); 48 });
47 } 49 }
48 50
49 /// Returns a list of all libraries in the current package as paths relative 51 /// Returns a list of all libraries in the current package as paths relative
50 /// to the package's root directory. 52 /// to the package's root directory.
51 Future<List<String>> get _libraries { 53 Future<List<String>> get _libraries {
52 var libDir = path.join(entrypoint.root.dir, "lib"); 54 var libDir = path.join(entrypoint.root.dir, "lib");
53 return defer(() { 55 return defer(() {
54 if (!dirExists(libDir)) return []; 56 if (!dirExists(libDir)) return [];
55 return listDir(libDir, recursive: true); 57 return listDir(libDir, recursive: true);
56 }).then((files) { 58 }).then((files) {
57 return files 59 return files
58 .map((file) => path.relative(file, from: path.dirname(libDir))) 60 .map((file) => path.relative(file, from: path.dirname(libDir)))
59 .where((file) => !path.split(file).contains("src") && 61 .where((file) => !path.split(file).contains("src") &&
60 path.extension(file) == '.dart') 62 path.extension(file) == '.dart')
61 .toList(); 63 .toList();
62 }); 64 });
63 } 65 }
64 66
65 void _checkName(String name, String description) { 67 void _checkName(String name, String description, {bool isPackage}) {
66 if (name == "") { 68 if (name == "") {
67 errors.add("$description may not be empty."); 69 errors.add("$description may not be empty.");
68 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) { 70 } else if (!new RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) {
69 errors.add("$description may only contain letters, numbers, and " 71 warnings.add("$description may only contain letters, numbers, and "
70 "underscores.\n" 72 "underscores.\n"
71 "Using a valid Dart identifier makes the name usable in Dart code."); 73 "Using a valid Dart identifier makes the name usable in Dart code.");
72 } else if (!new RegExp(r"^[a-zA-Z]").hasMatch(name)) { 74 } else if (!new RegExp(r"^[a-zA-Z]").hasMatch(name)) {
73 errors.add("$description must begin with letter.\n" 75 warnings.add("$description must begin with letter.\n"
74 "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.");
75 } else if (_RESERVED_WORDS.contains(name.toLowerCase())) { 77 } else if (_RESERVED_WORDS.contains(name.toLowerCase())) {
76 errors.add("$description may not be a reserved word in Dart.\n" 78 var messages = isPackage ? errors : warnings;
79 messages.add("$description may not be a reserved word in Dart.\n"
77 "Using a valid Dart identifier makes the name usable in Dart code."); 80 "Using a valid Dart identifier makes the name usable in Dart code.");
78 } else if (new RegExp(r"[A-Z]").hasMatch(name)) { 81 } else if (new RegExp(r"[A-Z]").hasMatch(name)) {
79 warnings.add('$description should be lower-case. Maybe use ' 82 warnings.add('$description should be lower-case. Maybe use '
80 '"${_unCamelCase(name)}"?'); 83 '"${_unCamelCase(name)}"?');
81 } 84 }
82 } 85 }
83 86
84 String _unCamelCase(String source) { 87 String _unCamelCase(String source) {
85 var builder = new StringBuffer(); 88 var builder = new StringBuffer();
86 var lastMatchEnd = 0; 89 var lastMatchEnd = 0;
87 for (var match in new RegExp(r"[a-z]([A-Z])").allMatches(source)) { 90 for (var match in new RegExp(r"[a-z]([A-Z])").allMatches(source)) {
88 builder 91 builder
89 ..add(source.substring(lastMatchEnd, match.start + 1)) 92 ..add(source.substring(lastMatchEnd, match.start + 1))
90 ..add("_") 93 ..add("_")
91 ..add(match.group(1).toLowerCase()); 94 ..add(match.group(1).toLowerCase());
92 lastMatchEnd = match.end; 95 lastMatchEnd = match.end;
93 } 96 }
94 builder.add(source.substring(lastMatchEnd)); 97 builder.add(source.substring(lastMatchEnd));
95 return builder.toString().toLowerCase(); 98 return builder.toString().toLowerCase();
96 } 99 }
97 } 100 }
OLDNEW
« no previous file with comments | « no previous file | utils/tests/pub/validator_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698