Skip to content

Commit

Permalink
Augment. Fixes for navigation for constructor annotations.
Browse files Browse the repository at this point in the history
...and for import prefixes in augmentations.

Change-Id: I4c2f358d5bd1819c8691990a0b86a37d04449213
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/360505
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Apr 1, 2024
1 parent 71e5a6b commit aced052
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,44 @@ void f() {
assertHasTarget('foo', targetFile: aFile);
}

Future<void>
test_class_constructor_annotationConstructor_importPrefix() async {
final a = newFile('$testPackageLibPath/a.dart', r'''
class A {
const A();
const A.named();
}
''');

addTestFile('''
library augment 'b.dart';
import 'a.dart' as prefix;
class B {
@prefix.A()
@prefix.A.named()
B().foo();
}
''');

await prepareNavigation();

assertHasRegion('prefix.A()');
assertHasTarget('prefix;');

assertHasRegion('A()');
assertHasTarget('A();', targetFile: a);

assertHasRegion('prefix.A.named()');
assertHasTarget('prefix;');

assertHasRegion('A.named()');
assertHasTarget('named()', targetFile: a);

assertHasRegion('named()');
assertHasTarget('named()', targetFile: a);
}

Future<void> test_class_constructor_named() async {
addTestFile('''
class A {
Expand Down Expand Up @@ -1381,6 +1419,35 @@ library my.lib;
assertHasTargetString('my.lib');
}

Future<void>
test_libraryAugmentation_topLevelFunction_annotationConstructor_importPrefix() async {
final a = newFile('$testPackageLibPath/a.dart', r'''
class A {
const A();
}
''');

newFile('$testPackageLibPath/b.dart', r'''
import augment 'test.dart';
''');

addTestFile('''
library augment 'b.dart';
import 'a.dart' as prefix;
@prefix.A()
external B().foo();
''');

await prepareNavigation();

assertHasRegion('prefix.A()');
assertHasTarget('prefix;');

assertHasRegion('A()');
assertHasTarget('A();', targetFile: a);
}

Future<void> test_multiplyDefinedElement() async {
newFile('$testPackageLibPath/libA.dart', 'library A; int TEST = 1;');
newFile('$testPackageLibPath/libB.dart', 'library B; int TEST = 2;');
Expand Down
5 changes: 3 additions & 2 deletions pkg/analyzer_plugin/lib/utilities/analyzer_converter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,11 @@ extension ElementExtensions on analyzer.Element? {
if (currentElement is analyzer.CompilationUnitElement) {
return currentElement;
}
if (currentElement?.enclosingElement is analyzer.LibraryElement) {
if (currentElement?.enclosingElement
is analyzer.LibraryOrAugmentationElement) {
currentElement = currentElement?.enclosingElement;
}
if (currentElement is analyzer.LibraryElement) {
if (currentElement is analyzer.LibraryOrAugmentationElement) {
return currentElement.definingCompilationUnit;
}
for (;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ class _DartNavigationComputerVisitor extends RecursiveAstVisitor<void> {

@override
void visitConstructorDeclaration(ConstructorDeclaration node) {
node.metadata.accept(this);

// For a default constructor, override the class name to be the declaration
// itself rather than linking to the class.
var nameToken = node.name;
Expand All @@ -302,6 +304,7 @@ class _DartNavigationComputerVisitor extends RecursiveAstVisitor<void> {
node.returnType.accept(this);
computer._addRegionForToken(nameToken, node.declaredElement);
}

node.parameters.accept(this);
node.initializers.accept(this);
node.redirectedConstructor?.accept(this);
Expand Down

0 comments on commit aced052

Please sign in to comment.