Skip to content

Commit bb2b8b2

Browse files
authored
Remove validation for library file paths (#2067)
Fixes #2024 There is nothing that requires a library name to be a valid Dart identifier, and it's common practices to at least have `part of` files which include a `.` in the file name.
1 parent 3c060aa commit bb2b8b2

File tree

2 files changed

+10
-49
lines changed

2 files changed

+10
-49
lines changed

lib/src/validator/name.dart

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,16 @@ import '../entrypoint.dart';
1010
import '../utils.dart';
1111
import '../validator.dart';
1212

13-
/// A validator that validates the name of the package and its libraries.
13+
/// A validator that the name of a package is legal and matches the library name
14+
/// in the case of a single library.
1415
class NameValidator extends Validator {
1516
NameValidator(Entrypoint entrypoint) : super(entrypoint);
1617

1718
Future validate() {
1819
return Future.sync(() {
19-
_checkName(entrypoint.root.name, 'Package name "${entrypoint.root.name}"',
20-
isPackage: true);
20+
_checkName(entrypoint.root.name);
2121

2222
var libraries = _libraries;
23-
for (var library in libraries) {
24-
var libName = path.basenameWithoutExtension(library);
25-
_checkName(libName, 'The name of "$library", "$libName",',
26-
isPackage: false);
27-
}
2823

2924
if (libraries.length == 1) {
3025
var libName = path.basenameWithoutExtension(libraries[0]);
@@ -49,21 +44,19 @@ class NameValidator extends Validator {
4944
.toList();
5045
}
5146

52-
void _checkName(String name, String description, {bool isPackage}) {
53-
// Packages names are more stringent than libraries.
54-
var messages = isPackage ? errors : warnings;
55-
47+
void _checkName(String name) {
48+
final description = 'Package name "$name"';
5649
if (name == "") {
5750
errors.add("$description may not be empty.");
5851
} else if (!RegExp(r"^[a-zA-Z0-9_]*$").hasMatch(name)) {
59-
messages.add("$description may only contain letters, numbers, and "
52+
errors.add("$description may only contain letters, numbers, and "
6053
"underscores.\n"
6154
"Using a valid Dart identifier makes the name usable in Dart code.");
6255
} else if (!RegExp(r"^[a-zA-Z_]").hasMatch(name)) {
63-
messages.add("$description must begin with a letter or underscore.\n"
56+
errors.add("$description must begin with a letter or underscore.\n"
6457
"Using a valid Dart identifier makes the name usable in Dart code.");
6558
} else if (reservedWords.contains(name.toLowerCase())) {
66-
messages.add("$description may not be a reserved word in Dart.\n"
59+
errors.add("$description may not be a reserved word in Dart.\n"
6760
"Using a valid Dart identifier makes the name usable in Dart code.");
6861
} else if (RegExp(r"[A-Z]").hasMatch(name)) {
6962
warnings.add('$description should be lower-case. Maybe use '

test/validator/name_test.dart

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ main() {
2222

2323
test('looks normal', () => expectNoValidationError(name));
2424

25-
test('has a badly-named library in lib/src', () async {
25+
test('has dots in potential library names', () async {
2626
await d.dir(appPath, [
2727
d.libPubspec("test_pkg", "1.0.0"),
2828
d.dir("lib", [
2929
d.file("test_pkg.dart", "int i = 1;"),
30-
d.dir("src", [d.file("8ball.dart", "int j = 2;")])
30+
d.file("test_pkg.g.dart", "int j = 2;")
3131
])
3232
]).create();
3333
expectNoValidationError(name);
@@ -50,38 +50,6 @@ main() {
5050
expectValidationWarning(name);
5151
});
5252

53-
test('has a library name with an invalid character', () async {
54-
await d.dir(appPath, [
55-
d.libPubspec("test_pkg", "1.0.0"),
56-
d.dir("lib", [d.file("test-pkg.dart", "int i = 0;")])
57-
]).create();
58-
expectValidationWarning(name);
59-
});
60-
61-
test('has a library name that begins with a number', () async {
62-
await d.dir(appPath, [
63-
d.libPubspec("test_pkg", "1.0.0"),
64-
d.dir("lib", [d.file("8ball.dart", "int i = 0;")])
65-
]).create();
66-
expectValidationWarning(name);
67-
});
68-
69-
test('has a library name that contains upper-case letters', () async {
70-
await d.dir(appPath, [
71-
d.libPubspec("test_pkg", "1.0.0"),
72-
d.dir("lib", [d.file("TestPkg.dart", "int i = 0;")])
73-
]).create();
74-
expectValidationWarning(name);
75-
});
76-
77-
test('has a library name that is a Dart reserved word', () async {
78-
await d.dir(appPath, [
79-
d.libPubspec("test_pkg", "1.0.0"),
80-
d.dir("lib", [d.file("for.dart", "int i = 0;")])
81-
]).create();
82-
expectValidationWarning(name);
83-
});
84-
8553
test('has a single library named differently than the package', () async {
8654
deleteEntry(path.join(d.sandbox, appPath, "lib", "test_pkg.dart"));
8755
await d.dir(appPath, [

0 commit comments

Comments
 (0)