Skip to content

Commit e944fc7

Browse files
authored
hasLeadingUnderscore extension cleanup (#4274)
1 parent 676c711 commit e944fc7

4 files changed

+9
-5
lines changed

lib/src/rules/no_leading_underscores_for_library_prefixes.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
77

88
import '../analyzer.dart';
9-
import '../utils.dart';
9+
import '../util/ascii_utils.dart';
1010

1111
const _desc = r'Avoid leading underscores for library prefixes.';
1212

@@ -62,7 +62,7 @@ class _Visitor extends SimpleAstVisitor<void> {
6262
return;
6363
}
6464

65-
if (hasLeadingUnderscore(id.name)) {
65+
if (id.name.hasLeadingUnderscore) {
6666
rule.reportLint(id, arguments: [id.name]);
6767
}
6868
}

lib/src/rules/no_leading_underscores_for_local_identifiers.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:analyzer/dart/ast/visitor.dart';
88

99
import '../analyzer.dart';
1010
import '../util/ascii_utils.dart';
11-
import '../utils.dart';
1211

1312
const _desc = r'Avoid leading underscores for local identifiers.';
1413

@@ -87,7 +86,7 @@ class _Visitor extends SimpleAstVisitor<void> {
8786

8887
void checkIdentifier(Token? id) {
8988
if (id == null) return;
90-
if (!hasLeadingUnderscore(id.lexeme)) return;
89+
if (!id.lexeme.hasLeadingUnderscore) return;
9190
if (id.lexeme.isJustUnderscores) return;
9291

9392
rule.reportLintForToken(id, arguments: [id.lexeme]);

lib/src/util/ascii_utils.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ bool isValidDartFileName(String name) {
6060
}
6161

6262
extension StringExtensions on String {
63+
/// Returns `true` if `this` has a leading `_`.
64+
bool get hasLeadingUnderscore => startsWith('_');
65+
6366
/// Returns whether `this` is just underscores.
6467
bool get isJustUnderscores {
6568
if (isEmpty) {

lib/src/utils.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'ast.dart';
6+
import 'util/ascii_utils.dart';
67

78
// An identifier here is defined as:
89
// * A sequence of `_`, `$`, letters or digits,
@@ -49,7 +50,8 @@ final _pubspec = RegExp(r'^_?pubspec\.yaml$');
4950
final _validLibraryPrefix = RegExp(r'^\$?_*[a-z][_a-z\d]*$');
5051

5152
/// Returns `true` if the given [name] has a leading `_`.
52-
bool hasLeadingUnderscore(String name) => name.startsWith('_');
53+
@Deprecated('Prefer: ascii_utils String extension `hasLeadingUnderscore`')
54+
bool hasLeadingUnderscore(String name) => name.hasLeadingUnderscore;
5355

5456
/// Check if this [string] is formatted in `CamelCase`.
5557
bool isCamelCase(String string) => CamelCaseString.isCamelCase(string);

0 commit comments

Comments
 (0)