Skip to content

Commit d67a7d8

Browse files
srawlinsCommit Queue
authored andcommitted
linter: Remove unused ast utilities
Change-Id: I4a436a77a69cc4a78ceb1ca27b95a81ab55dc868 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/428422 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]>
1 parent 3cfaff7 commit d67a7d8

File tree

2 files changed

+12
-87
lines changed

2 files changed

+12
-87
lines changed

pkg/linter/lib/src/ast.dart

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ import 'package:analyzer/src/workspace/workspace.dart' // ignore: implementation
1818
import 'package:path/path.dart' as path;
1919

2020
import '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].
2628
List<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) =>
131133
bool 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.
140137
bool isHashCode(ClassMember element) => _hasFieldOrMethod(element, 'hashCode');
141138

142139
/// Returns `true` if this element is an `index` method or field declaration.
143140
bool 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.
270237
bool 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.
276240
File? 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-
302260
bool _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-
365313
int? _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.
414368
typedef ElementProcessor = bool Function(Element element);

pkg/linter/test/rule_test.dart

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,12 @@
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:linter/src/ast.dart';
65
import 'package:test/test.dart';
76

87
import 'util/test_utils.dart';
98

109
void main() {
1110
group('rule tests', () {
1211
setUp(setUpSharedTestEnvironment);
13-
defineRuleUnitTests();
14-
});
15-
}
16-
17-
void defineRuleUnitTests() {
18-
group('names', () {
19-
group('keywords', () {
20-
var good = ['class', 'if', 'assert', 'catch', 'import'];
21-
testEach(good, isKeyWord, isTrue);
22-
var bad = ['_class', 'iff', 'assert_', 'Catch'];
23-
testEach(bad, isKeyWord, isFalse);
24-
});
25-
group('identifiers', () {
26-
var good = [
27-
'foo',
28-
'_if',
29-
'_',
30-
'f2',
31-
'fooBar',
32-
'foo_bar',
33-
'\$foo',
34-
'foo\$Bar',
35-
'foo\$',
36-
];
37-
testEach(good, isValidDartIdentifier, isTrue);
38-
var bad = ['if', '42', '3', '2f'];
39-
testEach(bad, isValidDartIdentifier, isFalse);
40-
});
4112
});
4213
}

0 commit comments

Comments
 (0)