Skip to content

Commit

Permalink
[element model] migrate element_visitors_test
Browse files Browse the repository at this point in the history
Change-Id: I5eb336b52380de3ad49d0dd9bced1f0dc7fc1cb8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403720
Commit-Queue: Phil Quitslund <[email protected]>
Auto-Submit: Phil Quitslund <[email protected]>
Commit-Queue: Konstantin Shcheglov <[email protected]>
Reviewed-by: Konstantin Shcheglov <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Jan 8, 2025
1 parent 31d8adf commit edde039
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
1 change: 0 additions & 1 deletion pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ lib/src/services/refactoring/legacy/rename.dart
lib/src/services/refactoring/legacy/rename_import.dart
lib/src/services/search/element_visitors.dart
test/services/refactoring/legacy/abstract_rename.dart
test/services/search/element_visitors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/visitor.dart';
import 'package:analyzer/dart/element/visitor2.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';

/// Return the [Element] that is either [root], or one of its direct or
/// indirect children, and has the given [nameOffset].
Expand All @@ -22,6 +23,11 @@ Element? findElementByNameOffset(Element? root, int nameOffset) {
return null;
}

/// Return the [Element2] that is either [root], or one of its direct or
/// indirect children, and has the given [nameOffset].
Element2? findElementByNameOffset2(Element2? root, int nameOffset) =>
findElementByNameOffset(root.asElement, nameOffset).asElement2;

/// Uses [processor] to visit all of the children of [element].
/// If [processor] returns `true`, then children of a child are visited too.
void visitChildren(Element element, BoolElementProcessor processor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analysis_server/src/services/search/element_visitors.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

Expand All @@ -18,7 +17,7 @@ void main() {

@reflectiveTest
class FindElementByNameOffsetTest extends AbstractSingleUnitTest {
Element get testUnitElement => testLibraryElement.asElement;
Element2 get testUnitElement => testLibraryElement;

Future<void> test_class() async {
await resolveTestCode(r'''
Expand All @@ -43,13 +42,13 @@ void bbb() {}
class AAA {}
class BBB {}
''');
expect(findElementByNameOffset(null, 0), isNull);
expect(findElementByNameOffset2(null, 0), isNull);

expect(findElementByNameOffset(testUnitElement, 0), isNull);
expect(findElementByNameOffset(testUnitElement, 1), isNull);
expect(findElementByNameOffset2(testUnitElement, 0), isNull);
expect(findElementByNameOffset2(testUnitElement, 1), isNull);

expect(findElementByNameOffset(testUnitElement, 5), isNull);
expect(findElementByNameOffset(testUnitElement, 7), isNull);
expect(findElementByNameOffset2(testUnitElement, 5), isNull);
expect(findElementByNameOffset2(testUnitElement, 7), isNull);
}

Future<void> test_topLevelVariable() async {
Expand All @@ -63,8 +62,8 @@ int? ccc;
}

void _assertElement(int nameOffset, ElementKind kind, String name) {
var element = findElementByNameOffset(testUnitElement, nameOffset)!;
var element = findElementByNameOffset2(testUnitElement, nameOffset)!;
expect(element.kind, kind);
expect(element.name, name);
expect(element.name3, name);
}
}

0 comments on commit edde039

Please sign in to comment.