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' ;
56import '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' )
3536final _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.
5559final _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.
6565bool 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` .
7168bool 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` .
8074bool 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.
8879bool isUpperCase (int c) => c >= 0x40 && c <= 0x5A ;
8980
90- /// Returns true if this [libraryPrefix] is valid.
81+ /// Whether this [libraryPrefix] is valid.
9182bool 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.
9586bool 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.
10293void 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);
0 commit comments