2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
+ import 'package:analyzer/dart/ast/token.dart' ;
5
6
import 'package:analyzer/src/lint/io.dart' ; // ignore: implementation_imports
6
7
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]*$' );
9
17
10
18
// An identifier here is defined as:
11
19
// * A sequence of `_`, `$`, letters or digits,
@@ -24,13 +32,6 @@ final _lowerCamelCase = RegExp(
24
32
r'^_*[?$a-z][a-z\d?$]*(?:(?:[A-Z]|_\d)[a-z\d?$]*)*_?$' ,
25
33
);
26
34
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
-
34
35
@Deprecated ('Prefer: ascii_utils.isValidFileName' )
35
36
final _lowerCaseUnderScoreWithDots = RegExp (
36
37
r'^_?[_a-z\d]*(?:\.[a-z][_a-z\d]*)*$' ,
@@ -45,7 +46,10 @@ final _lowerCaseUnderScoreWithLeadingUnderscores = RegExp(
45
46
r'^_*[a-z](?:_?[a-z\d])*$' ,
46
47
);
47
48
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
+ };
49
53
50
54
// A library prefix here is defined as:
51
55
// * An optional leading `?`,
@@ -54,82 +58,46 @@ final _pubspec = RegExp(r'^_?pubspec\.yaml$');
54
58
// * followed by any number of lower-case letters, digits and underscores.
55
59
final _validLibraryPrefix = RegExp (r'^\$?_*[a-z][_a-z\d]*$' );
56
60
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);
60
63
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.
65
65
bool isDartFileName (String fileName) => fileName.endsWith ('.dart' );
66
66
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` .
71
68
bool isLowerCamelCase (String id) =>
72
69
id.length == 1 && isUpperCase (id.codeUnitAt (0 )) ||
73
70
id == '_' ||
74
71
_lowerCamelCase.hasMatch (id);
75
72
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` .
80
74
bool isLowerCaseUnderScoreWithDots (String id) =>
81
75
// ignore: deprecated_member_use_from_same_package
82
76
_lowerCaseUnderScoreWithDots.hasMatch (id);
83
77
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.
88
79
bool isUpperCase (int c) => c >= 0x40 && c <= 0x5A ;
89
80
90
- /// Returns true if this [libraryPrefix] is valid.
81
+ /// Whether this [libraryPrefix] is valid.
91
82
bool isValidLibraryPrefix (String libraryPrefix) =>
92
83
_validLibraryPrefix.hasMatch (libraryPrefix);
93
84
94
- /// Returns true if this [id] is a valid package name.
85
+ /// Whether this [id] is a valid package name.
95
86
bool isValidPackageName (String id) =>
96
87
_lowerCaseUnderScoreWithLeadingUnderscores.hasMatch (id) &&
97
- isIdentifier (id) &&
98
- ! isReservedWord (id);
88
+ _isIdentifier (id) &&
89
+ ! _isReservedWord (id);
99
90
100
91
/// Write the given [object] to the console.
101
92
/// Uses the shared [outSink] for redirecting in tests.
102
93
void printToConsole (Object ? object) {
103
94
outSink.writeln (object);
104
95
}
105
96
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);
125
98
126
- String get humanized => _humanize (value);
99
+ /// Whether this [name] is a legal Dart identifier.
100
+ bool _isIdentifier (String name) => _identifier.hasMatch (name);
127
101
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);
0 commit comments