@@ -18,14 +18,16 @@ import 'package:analyzer/src/workspace/workspace.dart' // ignore: implementation
1818import 'package:path/path.dart' as path;
1919
2020import 'analyzer.dart' ;
21- import 'utils.dart' ;
2221
23- final List <String > reservedWords = _collectReservedWords ();
22+ final Set <String > _reservedWords = {
23+ for (var entry in Keyword .keywords.entries)
24+ if (entry.value.isReservedWord) entry.key,
25+ };
2426
2527/// Returns direct children of [parent] .
2628List <Element > getChildren (Element parent, [String ? name]) {
2729 var children = < Element > [];
28- visitChildren (parent, (Element element) {
30+ _visitChildren (parent, (Element element) {
2931 if (name == null || element.displayName == name) {
3032 children.add (element);
3133 }
@@ -131,27 +133,12 @@ bool hasConstantError(Expression node) =>
131133bool isEquals (ClassMember element) =>
132134 element is MethodDeclaration && element.name.lexeme == '==' ;
133135
134- /// Returns `true` if the keyword associated with this token is `final` or
135- /// `const` .
136- bool isFinalOrConst (Token token) =>
137- isKeyword (token, Keyword .FINAL ) || isKeyword (token, Keyword .CONST );
138-
139136/// Returns `true` if this element is a `hashCode` method or field declaration.
140137bool isHashCode (ClassMember element) => _hasFieldOrMethod (element, 'hashCode' );
141138
142139/// Returns `true` if this element is an `index` method or field declaration.
143140bool isIndex (ClassMember element) => _hasFieldOrMethod (element, 'index' );
144141
145- /// Return true if this compilation unit [node] is declared within the given
146- /// [package] 's `lib/` directory tree.
147- bool isInLibDir (CompilationUnit node, WorkspacePackage ? package) {
148- if (package == null ) return false ;
149- var cuPath = node.declaredFragment? .element.firstFragment.source.fullName;
150- if (cuPath == null ) return false ;
151- var libDir = path.join (package.root, 'lib' );
152- return path.isWithin (libDir, cuPath);
153- }
154-
155142/// Return `true` if this compilation unit [node] is declared within a public
156143/// directory in the given [package] 's directory tree. Public dirs are the
157144/// `lib` and `bin` dirs and the build and link hook file.
@@ -172,25 +159,8 @@ bool isInPublicDir(CompilationUnit node, WorkspacePackage? package) {
172159 cuPath == linkHookFile;
173160}
174161
175- /// Returns `true` if the given [id] is a Dart keyword.
176- bool isKeyWord (String id) => Keyword .keywords.containsKey (id);
177-
178- /// Returns `true` if the keyword associated with the given [token] matches
179- /// [keyword] .
180- bool isKeyword (Token token, Keyword keyword) =>
181- token is KeywordToken && token.keyword == keyword;
182-
183- /// Returns `true` if the given [ClassMember] is a method.
184- bool isMethod (ClassMember m) => m is MethodDeclaration ;
185-
186- /// Returns `true` if the given [ClassMember] is a public method.
187- bool isPublicMethod (ClassMember m) {
188- var declaredElement = m.declaredFragment? .element;
189- return declaredElement != null && isMethod (m) && declaredElement.isPublic;
190- }
191-
192162/// Check if the given word is a Dart reserved word.
193- bool isReservedWord (String word) => reservedWords .contains (word);
163+ bool isReservedWord (String word) => _reservedWords .contains (word);
194164
195165/// Returns `true` if the given method [declaration] is a "simple getter".
196166///
@@ -263,15 +233,9 @@ bool isSimpleSetter(MethodDeclaration setter) {
263233 return false ;
264234}
265235
266- /// Returns `true` if the given [id] is a valid Dart identifier.
267- bool isValidDartIdentifier (String id) => ! isKeyWord (id) && isIdentifier (id);
268-
269236/// Returns `true` if this element is a `values` method or field declaration.
270237bool isValues (ClassMember element) => _hasFieldOrMethod (element, 'values' );
271238
272- /// Returns `true` if the keyword associated with this token is `var` .
273- bool isVar (Token token) => isKeyword (token, Keyword .VAR );
274-
275239/// Return the nearest enclosing pubspec file.
276240File ? locatePubspecFile (CompilationUnit compilationUnit) {
277241 var declaredFragment = compilationUnit.declaredFragment;
@@ -293,12 +257,6 @@ File? locatePubspecFile(CompilationUnit compilationUnit) {
293257 return null ;
294258}
295259
296- /// Uses [processor] to visit all of the children of [element] .
297- /// If [processor] returns `true` , then children of a child are visited too.
298- void visitChildren (Element element, ElementProcessor processor) {
299- element.visitChildren2 (_ElementVisitorAdapter (processor));
300- }
301-
302260bool _checkForSimpleGetter (MethodDeclaration getter, Expression ? expression) {
303261 if (expression is SimpleIdentifier ) {
304262 var staticElement = expression.element;
@@ -352,16 +310,6 @@ bool _checkForSimpleSetter(MethodDeclaration setter, Expression expression) {
352310 return false ;
353311}
354312
355- List <String > _collectReservedWords () {
356- var reserved = < String > [];
357- for (var entry in Keyword .keywords.entries) {
358- if (entry.value.isReservedWord) {
359- reserved.add (entry.key);
360- }
361- }
362- return reserved;
363- }
364-
365313int ? _getIntValue (
366314 Expression expression,
367315 LinterContext ? context, {
@@ -409,6 +357,12 @@ bool _hasFieldOrMethod(ClassMember element, String name) =>
409357 (element is MethodDeclaration && element.name.lexeme == name) ||
410358 (element is FieldDeclaration && getFieldName (element, name) != null );
411359
360+ /// Uses [processor] to visit all of the children of [element] .
361+ /// If [processor] returns `true` , then children of a child are visited too.
362+ void _visitChildren (Element element, ElementProcessor processor) {
363+ element.visitChildren2 (_ElementVisitorAdapter (processor));
364+ }
365+
412366/// An [Element] processor function type.
413367/// If `true` is returned, children of [element] will be visited.
414368typedef ElementProcessor = bool Function (Element element);
0 commit comments