Skip to content

Commit

Permalink
fixed named constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewnitschke-wk committed Dec 19, 2024
1 parent d29d685 commit f003e51
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions lib/src/symbol_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,23 @@ class SymbolGenerator {
// as the element to annotate instead of the reference to the Class
final parentConstructor = node.thisOrAncestorOfType<ConstructorName>();
if (parentConstructor != null) {
// ConstructorNames can also include a PrefixIdentifier: `math.Rectangle()`
// both 'math' and 'Rectangle' are simple identifiers. We only want
// the constructor element fo 'Rectangle' in this case

// ConstructorNames can also include an import PrefixIdentifier: `math.Rectangle()`
// both 'math' and 'Rectangle' are SimpleIdentifiers. We only want the constructor
// element fo 'Rectangle' in this case
final parentPrefixIdentifier = node.thisOrAncestorOfType<PrefixedIdentifier>();
if (parentPrefixIdentifier != null) {
if (parentPrefixIdentifier.identifier == node) {
return parentConstructor.staticElement;
}
if (parentPrefixIdentifier?.prefix == node) return element;

// Constructors can be named: `Foo.bar()`, both `Foo` and `bar` are SimpleIdentifiers
// When the constructor is named, 'bar' is the constructor reference and `Foo` should
// reference the class
if (parentConstructor.name == node) {
return parentConstructor.staticElement;
} if (parentConstructor.name != null) {
return element;
}

// Otherwise, element is just `Rectangle()`, so simply return the
// Otherwise, constructor is just `Foo()`, so simply return the
// constructor's element
return parentConstructor.staticElement;
}
Expand Down
2 changes: 1 addition & 1 deletion snapshots/output/basic-project/lib/more.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
Animal cat = Animal.cat();
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#
// ^^^ definition local 7
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#cat().
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#cat().

bird.makeSound();
Expand Down

0 comments on commit f003e51

Please sign in to comment.