Skip to content

Commit 52f965e

Browse files
srawlinsCommit Queue
authored andcommitted
Analyzer: Remove outdated NodeLocator class
Change-Id: If8cff437d35452af5a19b4d414352228f899a94f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/423216 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent befa614 commit 52f965e

File tree

11 files changed

+30
-234
lines changed

11 files changed

+30
-234
lines changed

pkg/analysis_server/lib/src/services/refactoring/legacy/extract_local.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl
5656
String? stringLiteralPart;
5757
final List<SourceRange> occurrences = <SourceRange>[];
5858
final Map<Element2, int> elementIds = <Element2, int>{};
59-
Set<String> excludedVariableNames = <String>{};
59+
Set<String> _excludedVariableNames = <String>{};
6060

6161
ExtractLocalRefactoringImpl(
6262
this.resolveResult,
@@ -125,7 +125,7 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl
125125
_prepareOccurrences();
126126
_prepareOffsetsLengths();
127127
// names
128-
excludedVariableNames = unit.findPossibleLocalVariableConflicts(
128+
_excludedVariableNames = unit.findPossibleLocalVariableConflicts(
129129
selectionOffset,
130130
);
131131
_prepareNames();
@@ -137,7 +137,7 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl
137137
RefactoringStatus checkName() {
138138
var result = RefactoringStatus();
139139
result.addStatus(validateVariableName(name));
140-
if (excludedVariableNames.contains(name)) {
140+
if (_excludedVariableNames.contains(name)) {
141141
result.addError(
142142
format("The name '{0}' is already used in the scope.", name),
143143
);
@@ -530,15 +530,15 @@ class ExtractLocalRefactoringImpl extends RefactoringImpl
530530
names.addAll(
531531
getVariableNameSuggestionsForText(
532532
stringLiteralPart,
533-
excludedVariableNames,
533+
_excludedVariableNames,
534534
),
535535
);
536536
} else if (singleExpression != null) {
537537
names.addAll(
538538
getVariableNameSuggestionsForExpression(
539539
singleExpression.staticType,
540540
singleExpression,
541-
excludedVariableNames,
541+
_excludedVariableNames,
542542
),
543543
);
544544
}

pkg/analysis_server/lib/src/utilities/extensions/ast.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import 'package:analyzer/dart/element/element2.dart';
1111
import 'package:analyzer/source/source.dart';
1212
import 'package:analyzer/src/dart/ast/element_locator.dart';
1313
import 'package:analyzer/src/dart/ast/token.dart';
14-
import 'package:analyzer/src/dart/ast/utilities.dart';
1514
import 'package:analyzer/src/utilities/extensions/ast.dart';
1615
import 'package:analyzer/src/utilities/extensions/collection.dart';
1716
import 'package:analyzer/src/utilities/extensions/element.dart';
17+
import 'package:analyzer/utilities/extensions/ast.dart';
1818

1919
class ThrowStatement {
2020
final ExpressionStatement statement;
@@ -282,15 +282,15 @@ extension CompilationUnitExtension on CompilationUnit {
282282
/// Returns names of elements that might conflict with a new local variable
283283
/// declared at [offset].
284284
Set<String> findPossibleLocalVariableConflicts(int offset) {
285-
var conflicts = <String>{};
286-
var enclosingNode = NodeLocator(offset).searchWithin(this)!;
285+
var enclosingNode = nodeCovering(offset: offset)!;
287286
var enclosingBlock = enclosingNode.thisOrAncestorOfType<Block>();
288-
if (enclosingBlock != null) {
289-
var visitor = _ReferencedUnprefixedNamesCollector();
290-
enclosingBlock.accept(visitor);
291-
return visitor.names;
287+
if (enclosingBlock == null) {
288+
return {};
292289
}
293-
return conflicts;
290+
291+
var visitor = _ReferencedUnprefixedNamesCollector();
292+
enclosingBlock.accept(visitor);
293+
return visitor.names;
294294
}
295295
}
296296

pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'package:analyzer/src/dart/analysis/file_state.dart' as file_state;
1414
import 'package:analyzer/src/dart/analysis/file_state.dart';
1515
import 'package:analyzer/src/dart/analysis/testing_data.dart';
1616
import 'package:analyzer/src/dart/ast/ast.dart';
17-
import 'package:analyzer/src/dart/ast/utilities.dart';
1817
import 'package:analyzer/src/dart/constant/compute.dart';
1918
import 'package:analyzer/src/dart/constant/constant_verifier.dart';
2019
import 'package:analyzer/src/dart/constant/evaluation.dart';
@@ -54,6 +53,7 @@ import 'package:analyzer/src/util/performance/operation_performance.dart';
5453
import 'package:analyzer/src/utilities/extensions/version.dart';
5554
import 'package:analyzer/src/workspace/pub.dart';
5655
import 'package:analyzer/src/workspace/workspace.dart';
56+
import 'package:analyzer/utilities/extensions/ast.dart';
5757
import 'package:collection/collection.dart';
5858

5959
class AnalysisForCompletionResult {
@@ -158,9 +158,7 @@ class LibraryAnalyzer {
158158
);
159159
});
160160
var parsedUnit = fileAnalysis.unit;
161-
162-
var node = NodeLocator(offset).searchWithin(parsedUnit);
163-
161+
var node = parsedUnit.nodeCovering(offset: offset);
164162
var errorListener = RecordingErrorListener();
165163

166164
return performance.run('resolve', (performance) {

pkg/analyzer/lib/src/dart/analysis/search.dart

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import 'package:analyzer/src/dart/analysis/index.dart';
1414
import 'package:analyzer/src/dart/analysis/results.dart';
1515
import 'package:analyzer/src/dart/ast/ast.dart';
1616
import 'package:analyzer/src/dart/ast/extensions.dart';
17-
import 'package:analyzer/src/dart/ast/utilities.dart';
1817
import 'package:analyzer/src/dart/element/element.dart';
1918
import 'package:analyzer/src/summary/idl.dart';
2019
import 'package:analyzer/src/util/performance/operation_performance.dart';
2120
import 'package:analyzer/src/utilities/cancellation.dart';
2221
import 'package:analyzer/src/utilities/fuzzy_matcher.dart';
22+
import 'package:analyzer/utilities/extensions/ast.dart';
2323
import 'package:collection/collection.dart';
2424

2525
Fragment _getEnclosingFragment(
@@ -857,9 +857,7 @@ class Search {
857857
}
858858
var unit = unitResult.unit;
859859

860-
// Prepare the node.
861-
var node =
862-
NodeLocator(element.firstFragment.nameOffset2!).searchWithin(unit);
860+
var node = unit.nodeCovering(offset: element.firstFragment.nameOffset2!);
863861
if (node == null) {
864862
return const <SearchResult>[];
865863
}

pkg/analyzer/lib/src/dart/ast/utilities.dart

Lines changed: 0 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -15,153 +15,6 @@ import 'package:analyzer/src/generated/engine.dart' show AnalysisEngine;
1515

1616
export 'package:analyzer/src/dart/ast/constant_evaluator.dart';
1717

18-
/// An object used to locate the [AstNode] associated with a source range, given
19-
/// the AST structure built from the source. More specifically, they will return
20-
/// the [AstNode] with the shortest length whose source range completely
21-
/// encompasses the specified range with some exceptions:
22-
///
23-
/// - Offsets that fall between the name and type/formal parameter list of a
24-
/// declaration will return the declaration node and not the parameter list
25-
/// node.
26-
class NodeLocator extends UnifyingAstVisitor<void> {
27-
/// The start offset of the range used to identify the node.
28-
final int _startOffset;
29-
30-
/// The end offset of the range used to identify the node.
31-
final int _endOffset;
32-
33-
/// The element that was found that corresponds to the given source range, or
34-
/// `null` if there is no such element.
35-
AstNode? _foundNode;
36-
37-
/// Initialize a newly created locator to locate an [AstNode] by locating the
38-
/// node within an AST structure that corresponds to the given range of
39-
/// characters (between the [startOffset] and [endOffset] in the source.
40-
NodeLocator(int startOffset, [int? endOffset])
41-
: _startOffset = startOffset,
42-
_endOffset = endOffset ?? startOffset;
43-
44-
/// Return the node that was found that corresponds to the given source range
45-
/// or `null` if there is no such node.
46-
AstNode? get foundNode => _foundNode;
47-
48-
/// Search within the given AST [node] for an identifier representing an
49-
/// element in the specified source range. Return the element that was found,
50-
/// or `null` if no element was found.
51-
AstNode? searchWithin(AstNode? node) {
52-
if (node == null) {
53-
return null;
54-
}
55-
try {
56-
node.accept(this);
57-
} catch (exception, stackTrace) {
58-
// TODO(39284): should this exception be silent?
59-
AnalysisEngine.instance.instrumentationService.logException(
60-
SilentException(
61-
"Unable to locate element at offset ($_startOffset - $_endOffset)",
62-
exception,
63-
stackTrace));
64-
return null;
65-
}
66-
67-
return _foundNode;
68-
}
69-
70-
@override
71-
void visitClassDeclaration(ClassDeclaration node) {
72-
// Names do not have AstNodes but offsets at the end should be treated as
73-
// part of the declaration (not parameter list).
74-
if (_startOffset == _endOffset && _startOffset == node.name.end) {
75-
_foundNode = node;
76-
return;
77-
}
78-
79-
super.visitClassDeclaration(node);
80-
}
81-
82-
@override
83-
void visitConstructorDeclaration(ConstructorDeclaration node) {
84-
// Names do not have AstNodes but offsets at the end should be treated as
85-
// part of the declaration (not parameter list).
86-
if (_startOffset == _endOffset &&
87-
_startOffset == (node.name ?? node.returnType).end) {
88-
_foundNode = node;
89-
return;
90-
}
91-
92-
super.visitConstructorDeclaration(node);
93-
}
94-
95-
@override
96-
void visitFunctionDeclaration(FunctionDeclaration node) {
97-
// Names do not have AstNodes but offsets at the end should be treated as
98-
// part of the declaration (not parameter list).
99-
if (_startOffset == _endOffset && _startOffset == node.name.end) {
100-
_foundNode = node;
101-
return;
102-
}
103-
104-
super.visitFunctionDeclaration(node);
105-
}
106-
107-
@override
108-
void visitMethodDeclaration(MethodDeclaration node) {
109-
// Names do not have AstNodes but offsets at the end should be treated as
110-
// part of the declaration (not parameter list).
111-
if (_startOffset == _endOffset && _startOffset == node.name.end) {
112-
_foundNode = node;
113-
return;
114-
}
115-
116-
super.visitMethodDeclaration(node);
117-
}
118-
119-
@override
120-
void visitNode(AstNode node) {
121-
// Don't visit a new tree if the result has been already found.
122-
if (_foundNode != null) {
123-
return;
124-
}
125-
// Check whether the current node covers the selection.
126-
Token beginToken = node.beginToken;
127-
Token endToken = node.endToken;
128-
// Don't include synthetic tokens.
129-
while (endToken != beginToken) {
130-
// Fasta scanner reports unterminated string literal errors
131-
// and generates a synthetic string token with non-zero length.
132-
// Because of this, check for length > 0 rather than !isSynthetic.
133-
if (endToken.isEof || endToken.length > 0) {
134-
break;
135-
}
136-
endToken = endToken.previous!;
137-
}
138-
int end = endToken.end;
139-
int start = node.offset;
140-
if (end < _startOffset || start > _endOffset) {
141-
return;
142-
}
143-
// Check children.
144-
try {
145-
node.visitChildren(this);
146-
} catch (exception, stackTrace) {
147-
// Ignore the exception and proceed in order to visit the rest of the
148-
// structure.
149-
// TODO(39284): should this exception be silent?
150-
AnalysisEngine.instance.instrumentationService.logException(
151-
SilentException("Exception caught while traversing an AST structure.",
152-
exception, stackTrace));
153-
}
154-
// Found a child.
155-
if (_foundNode != null) {
156-
return;
157-
}
158-
// Check this node.
159-
if (start <= _startOffset && _endOffset <= end) {
160-
_foundNode = node;
161-
}
162-
}
163-
}
164-
16518
/// An object used to locate the [AstNode] associated with a source range.
16619
/// More specifically, they will return the deepest [AstNode] which completely
16720
/// encompasses the specified range with some exceptions:

pkg/analyzer/lib/src/error/unicode_text_verifier.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/error/listener.dart';
7-
import 'package:analyzer/src/dart/ast/utilities.dart';
87
import 'package:analyzer/src/error/codes.dart';
8+
import 'package:analyzer/utilities/extensions/ast.dart';
99

1010
/// A verifier that checks for unsafe Unicode text.
1111
/// See: https://nvd.nist.gov/vuln/detail/CVE-2021-22567
@@ -20,8 +20,7 @@ class UnicodeTextVerifier {
2020
if (0x202a <= codeUnit &&
2121
codeUnit <= 0x2069 &&
2222
(codeUnit <= 0x202e || 0x2066 <= codeUnit)) {
23-
// This uses an AST visitor; consider a more direct approach.
24-
var node = NodeLocator(offset).searchWithin(unit);
23+
var node = unit.nodeCovering(offset: offset);
2524
// If it's not in a string literal, we assume we're in a comment.
2625
// This can potentially over-report on syntactically incorrect sources
2726
// (where Unicode is outside a string or comment).

pkg/analyzer/test/src/dart/ast/ast_test.dart

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

55
import 'package:analyzer/src/dart/ast/ast.dart';
6-
import 'package:analyzer/src/dart/ast/utilities.dart';
6+
import 'package:analyzer/utilities/extensions/ast.dart';
77
import 'package:test/test.dart';
88
import 'package:test_reflective_loader/test_reflective_loader.dart';
99

@@ -108,8 +108,7 @@ class ExpressionImplTest extends ParserTestCase {
108108
assertInContext(String snippet, bool isInContext) {
109109
int index = testSource.indexOf(snippet);
110110
expect(index >= 0, isTrue);
111-
NodeLocator visitor = NodeLocator(index);
112-
var node = visitor.searchWithin(testUnit) as AstNodeImpl;
111+
var node = testUnit.nodeCovering(offset: index)! as AstNodeImpl;
113112
expect(node, TypeMatcher<ExpressionImpl>());
114113
expect((node as ExpressionImpl).inConstantContext,
115114
isInContext ? isTrue : isFalse);

pkg/analyzer/test/src/dart/ast/utilities_test.dart

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import '../../../util/ast_type_matchers.dart';
1212

1313
main() {
1414
defineReflectiveSuite(() {
15-
defineReflectiveTests(NodeLocatorTest);
1615
defineReflectiveTests(NodeLocator2Test);
1716
});
1817
}
@@ -64,56 +63,6 @@ class NodeLocator2Test extends _SharedNodeLocatorTests {
6463
}
6564
}
6665

67-
@reflectiveTest
68-
class NodeLocatorTest extends _SharedNodeLocatorTests {
69-
@override
70-
AstNode? locate(
71-
CompilationUnit unit,
72-
int start, [
73-
int? end,
74-
]) {
75-
var locator = NodeLocator(start, end);
76-
var node = locator.searchWithin(unit)!;
77-
expect(locator.foundNode, same(node));
78-
return node;
79-
}
80-
81-
void test_range() {
82-
CompilationUnit unit = parseCompilationUnit("library myLib;");
83-
var node = _assertLocate(unit, 4, 10);
84-
expect(node, isLibraryDirective);
85-
}
86-
87-
void test_searchWithin_null() {
88-
NodeLocator locator = NodeLocator(0, 0);
89-
expect(locator.searchWithin(null), isNull);
90-
}
91-
92-
void test_searchWithin_offset() {
93-
CompilationUnit unit = parseCompilationUnit("library myLib;");
94-
var node = _assertLocate(unit, 10, 10);
95-
expect(node, isSimpleIdentifier);
96-
}
97-
98-
void test_searchWithin_offsetAfterNode() {
99-
CompilationUnit unit = parseCompilationUnit(r'''
100-
class A {}
101-
class B {}''');
102-
NodeLocator locator = NodeLocator(1024, 1024);
103-
var node = locator.searchWithin(unit.declarations[0]);
104-
expect(node, isNull);
105-
}
106-
107-
void test_searchWithin_offsetBeforeNode() {
108-
CompilationUnit unit = parseCompilationUnit(r'''
109-
class A {}
110-
class B {}''');
111-
NodeLocator locator = NodeLocator(0, 0);
112-
var node = locator.searchWithin(unit.declarations[1]);
113-
expect(node, isNull);
114-
}
115-
}
116-
11766
abstract class _SharedNodeLocatorTests extends ParserTestCase {
11867
AstNode? locate(
11968
CompilationUnit unit,

0 commit comments

Comments
 (0)