Skip to content

Commit 6fc1427

Browse files
srawlinsCommit Queue
authored andcommitted
linter: Remove unused utils
`_reservedWords` and `isReservedWord` were only used in `utils.dart`, so are moved over there. The following were unused (except in their own unit tests), and are removed: `_lowerCaseUnderScore`, `_pubspec`, `hasLeadingUnderscore`, `isIdentifier`, `isLowerCaseUnderScore`, `isPubspecFileName`, `CamelCaseString`. I touched up doc comments as I went. Change-Id: Ie03f9328a25e3147bd9c320cf80841ba56da7abd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/444161 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 60a4ec2 commit 6fc1427

File tree

5 files changed

+29
-147
lines changed

5 files changed

+29
-147
lines changed

pkg/linter/lib/src/ast.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ import 'package:analyzer/file_system/file_system.dart';
1515
import 'package:analyzer/workspace/workspace.dart';
1616
import 'package:path/path.dart' as path;
1717

18-
final Set<String> _reservedWords = {
19-
for (var entry in Keyword.keywords.entries)
20-
if (entry.value.isReservedWord) entry.key,
21-
};
22-
2318
/// Returns direct children of [parent].
2419
List<Element> getChildren(Element parent, [String? name]) {
2520
var children = <Element>[];
@@ -155,9 +150,6 @@ bool isInPublicDir(CompilationUnit node, WorkspacePackage? package) {
155150
cuPath == linkHookFile;
156151
}
157152

158-
/// Check if the given word is a Dart reserved word.
159-
bool isReservedWord(String word) => _reservedWords.contains(word);
160-
161153
/// Returns `true` if the given method [declaration] is a "simple getter".
162154
///
163155
/// A simple getter takes one of these basic forms:

pkg/linter/lib/src/utils.dart

Lines changed: 29 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/dart/ast/token.dart';
56
import 'package:analyzer/src/lint/io.dart'; // ignore: implementation_imports
67

7-
import 'ast.dart';
8-
import 'util/ascii_utils.dart';
8+
// A camel case string here is defined as:
9+
// * An arbitrary number of optional leading `_`s or `$`s,
10+
// * followed by an upper-case letter, `$` or `?`,
11+
// * followed by any number of letters, digits, `?` or `$`s.
12+
//
13+
// This ensures that the text contains a `$`, `?` or upper-case letter
14+
// before any lower-case letter or digit, and no letters or `?`s before an
15+
// `_`.
16+
final _camelCasePattern = RegExp(r'^_*(?:\$+_+)*[$?A-Z][$?a-zA-Z\d]*$');
917

1018
// An identifier here is defined as:
1119
// * A sequence of `_`, `$`, letters or digits,
@@ -24,13 +32,6 @@ final _lowerCamelCase = RegExp(
2432
r'^_*[?$a-z][a-z\d?$]*(?:(?:[A-Z]|_\d)[a-z\d?$]*)*_?$',
2533
);
2634

27-
// A lower-case underscore (snake-case) is here defined as:
28-
// * A sequence of lower-case letters, digits and underscores,
29-
// * starting with a lower-case letter, and
30-
// * with no two adjacent underscores,
31-
// * and not ending in an underscore.
32-
final _lowerCaseUnderScore = RegExp(r'^[a-z](?:_?[a-z\d])*$');
33-
3435
@Deprecated('Prefer: ascii_utils.isValidFileName')
3536
final _lowerCaseUnderScoreWithDots = RegExp(
3637
r'^_?[_a-z\d]*(?:\.[a-z][_a-z\d]*)*$',
@@ -45,7 +46,10 @@ final _lowerCaseUnderScoreWithLeadingUnderscores = RegExp(
4546
r'^_*[a-z](?:_?[a-z\d])*$',
4647
);
4748

48-
final _pubspec = RegExp(r'^_?pubspec\.yaml$');
49+
final Set<String> _reservedWords = {
50+
for (var entry in Keyword.keywords.entries)
51+
if (entry.value.isReservedWord) entry.key,
52+
};
4953

5054
// A library prefix here is defined as:
5155
// * An optional leading `?`,
@@ -54,82 +58,46 @@ final _pubspec = RegExp(r'^_?pubspec\.yaml$');
5458
// * followed by any number of lower-case letters, digits and underscores.
5559
final _validLibraryPrefix = RegExp(r'^\$?_*[a-z][_a-z\d]*$');
5660

57-
/// Returns `true` if the given [name] has a leading `_`.
58-
@Deprecated('Prefer: ascii_utils String extension `hasLeadingUnderscore`')
59-
bool hasLeadingUnderscore(String name) => name.hasLeadingUnderscore;
61+
/// Whether this [string] is formatted in `CamelCase`.
62+
bool isCamelCase(String string) => _isCamelCase(string);
6063

61-
/// Check if this [string] is formatted in `CamelCase`.
62-
bool isCamelCase(String string) => CamelCaseString.isCamelCase(string);
63-
64-
/// Returns `true` if this [fileName] is a Dart file.
64+
/// Whether this [fileName] is a Dart file.
6565
bool isDartFileName(String fileName) => fileName.endsWith('.dart');
6666

67-
/// Returns `true` if this [name] is a legal Dart identifier.
68-
bool isIdentifier(String name) => _identifier.hasMatch(name);
69-
70-
/// Returns `true` if this [id] is `lowerCamelCase`.
67+
/// Whether [id] is `lowerCamelCase`.
7168
bool isLowerCamelCase(String id) =>
7269
id.length == 1 && isUpperCase(id.codeUnitAt(0)) ||
7370
id == '_' ||
7471
_lowerCamelCase.hasMatch(id);
7572

76-
/// Returns `true` if this [id] is `lower_camel_case_with_underscores`.
77-
bool isLowerCaseUnderScore(String id) => _lowerCaseUnderScore.hasMatch(id);
78-
79-
/// Returns `true` if this [id] is `lower_camel_case_with_underscores_or.dots`.
73+
/// Whether this [id] is `lower_camel_case_with_underscores_or.dots`.
8074
bool isLowerCaseUnderScoreWithDots(String id) =>
8175
// ignore: deprecated_member_use_from_same_package
8276
_lowerCaseUnderScoreWithDots.hasMatch(id);
8377

84-
/// Returns `true` if this [fileName] is a Pubspec file.
85-
bool isPubspecFileName(String fileName) => _pubspec.hasMatch(fileName);
86-
87-
/// Returns `true` if the given code unit [c] is upper case.
78+
/// Whether the given code unit [c] is upper case.
8879
bool isUpperCase(int c) => c >= 0x40 && c <= 0x5A;
8980

90-
/// Returns true if this [libraryPrefix] is valid.
81+
/// Whether this [libraryPrefix] is valid.
9182
bool isValidLibraryPrefix(String libraryPrefix) =>
9283
_validLibraryPrefix.hasMatch(libraryPrefix);
9384

94-
/// Returns true if this [id] is a valid package name.
85+
/// Whether this [id] is a valid package name.
9586
bool isValidPackageName(String id) =>
9687
_lowerCaseUnderScoreWithLeadingUnderscores.hasMatch(id) &&
97-
isIdentifier(id) &&
98-
!isReservedWord(id);
88+
_isIdentifier(id) &&
89+
!_isReservedWord(id);
9990

10091
/// Write the given [object] to the console.
10192
/// Uses the shared [outSink] for redirecting in tests.
10293
void printToConsole(Object? object) {
10394
outSink.writeln(object);
10495
}
10596

106-
class CamelCaseString {
107-
static final _camelCaseMatcher = RegExp(r'[A-Z][a-z]*');
108-
109-
// A camel case string here is defined as:
110-
// * An arbitrary number of optional leading `_`s or `$`s,
111-
// * followed by an upper-case letter, `$` or `?`,
112-
// * followed by any number of letters, digits, `?` or `$`s.
113-
//
114-
// This ensures that the text contains a `$`, `?` or upper-case letter
115-
// before any lower-case letter or digit, and no letters or `?`s before an
116-
// `_`.
117-
static final _camelCaseTester = RegExp(r'^_*(?:\$+_+)*[$?A-Z][$?a-zA-Z\d]*$');
118-
119-
final String value;
120-
CamelCaseString(this.value) {
121-
if (!isCamelCase(value)) {
122-
throw ArgumentError.value(value, 'value', '$value is not CamelCase');
123-
}
124-
}
97+
bool _isCamelCase(String name) => _camelCasePattern.hasMatch(name);
12598

126-
String get humanized => _humanize(value);
99+
/// Whether this [name] is a legal Dart identifier.
100+
bool _isIdentifier(String name) => _identifier.hasMatch(name);
127101

128-
@override
129-
String toString() => value;
130-
131-
static bool isCamelCase(String name) => _camelCaseTester.hasMatch(name);
132-
133-
static String _humanize(String camelCase) =>
134-
_camelCaseMatcher.allMatches(camelCase).map((m) => m[0]).join(' ');
135-
}
102+
/// Whether the given word is a Dart reserved word.
103+
bool _isReservedWord(String word) => _reservedWords.contains(word);

pkg/linter/test/all.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:analyzer/src/lint/io.dart';
66

77
import 'ascii_utils_test.dart' as ascii_utils;
88
import 'doc_test.dart' as doc;
9-
import 'engine_test.dart' as engine;
109
import 'formatter_test.dart' as formatter;
1110
import 'integration_test.dart' as integration;
1211
import 'lint_code_test.dart' as lint_code;
@@ -31,7 +30,6 @@ void main() {
3130

3231
ascii_utils.main();
3332
doc.main();
34-
engine.main();
3533
formatter.main();
3634
integration.main();
3735
lint_code.main();

pkg/linter/test/engine_test.dart

Lines changed: 0 additions & 37 deletions
This file was deleted.

pkg/linter/test/utils_test.dart

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ void main() {
4040
);
4141
});
4242

43-
group('pubspec', () {
44-
testEach(['pubspec.yaml', '_pubspec.yaml'], isPubspecFileName, isTrue);
45-
testEach(['__pubspec.yaml', 'foo.yaml'], isPubspecFileName, isFalse);
46-
});
47-
4843
group('camel case', () {
4944
group('upper', () {
5045
var good = [
@@ -65,21 +60,6 @@ void main() {
6560
var bad = ['fooBar', 'foo', 'f', '_f', 'F_B'];
6661
testEach(bad, isCamelCase, isFalse);
6762
});
68-
69-
group('CamelCaseString', () {
70-
test('invalid creation', () {
71-
expect(
72-
() => CamelCaseString('invalid'),
73-
throwsA(TypeMatcher<ArgumentError>()),
74-
);
75-
});
76-
test('toString', () {
77-
expect(CamelCaseString('CamelCase').toString(), 'CamelCase');
78-
});
79-
test('humanize', () {
80-
expect(CamelCaseString('CamelCase').humanized, 'Camel Case');
81-
});
82-
});
8363
});
8464

8565
group('library prefixes', () {
@@ -124,25 +104,6 @@ void main() {
124104
testEach(bad, isValidLibraryPrefix, isFalse);
125105
});
126106

127-
group('lower_case_underscores', () {
128-
var good = ['foo_bar', 'foo', 'foo_bar_baz', 'p', 'p1', 'p21', 'p1ll0'];
129-
testEach(good, isLowerCaseUnderScore, isTrue);
130-
131-
var bad = [
132-
'Foo',
133-
'fooBar',
134-
'foo_Bar',
135-
'foo_',
136-
'_f',
137-
'F_B',
138-
'JS',
139-
'JSON',
140-
'1',
141-
'1b',
142-
];
143-
testEach(bad, isLowerCaseUnderScore, isFalse);
144-
});
145-
146107
group('isLowerCaseUnderScoreWithDots', () {
147108
var good = [
148109
'bwu_server.shared.datastore.some_file',

0 commit comments

Comments
 (0)