Skip to content

Commit 69ac3b4

Browse files
Merge pull request #165 from Workiva/fixed_constructor_references
FEDX-2227: Fixed logic for constructor references
2 parents 1cd0c7c + 69bb98d commit 69ac3b4

File tree

8 files changed

+90
-36
lines changed

8 files changed

+90
-36
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ gen-proto:
2121
protoc --dart_out=. ./lib/src/gen/scip.proto
2222

2323
print:
24-
scip print ./index.scip
24+
scip print ./index.scip
25+
26+
print-ast:
27+
dart run ./tool/ast_printer.dart ./snapshots/input/staging-project

lib/src/symbol_generator.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,37 @@ class SymbolGenerator {
4444
} else if (node is SimpleIdentifier) {
4545
var element = node.staticElement;
4646

47+
// A SimpleIdentifier with a direct parent of a ConstructorDeclaration
48+
// is the reference to the class itself. Skip this declaration
49+
if (node.parent is ConstructorDeclaration) {
50+
return null;
51+
}
52+
53+
// if we're nested under a ConstructorName identifier, use the constructor
54+
// as the element to annotate instead of the reference to the Class
55+
final parentConstructor = node.thisOrAncestorOfType<ConstructorName>();
56+
if (parentConstructor != null) {
57+
// ConstructorNames can also include an import PrefixIdentifier: `math.Rectangle()`
58+
// both 'math' and 'Rectangle' are SimpleIdentifiers. We only want the constructor
59+
// element for 'Rectangle' in this case
60+
final parentPrefixIdentifier =
61+
node.thisOrAncestorOfType<PrefixedIdentifier>();
62+
if (parentPrefixIdentifier?.prefix == node) return element;
63+
64+
// Constructors can be named: `Foo.bar()`, both `Foo` and `bar` are SimpleIdentifiers
65+
// When the constructor is named, 'bar' is the constructor reference and `Foo` should
66+
// reference the class
67+
if (parentConstructor.name == node) {
68+
return parentConstructor.staticElement;
69+
} else if (parentConstructor.name != null) {
70+
return element;
71+
}
72+
73+
// Otherwise, constructor is just `Foo()`, so simply return the
74+
// constructor's element
75+
return parentConstructor.staticElement;
76+
}
77+
4778
// Both `.loadLibrary()`, and `.call()` are synthetic functions that
4879
// have no definition. These should therefore should not be indexed.
4980
if (element is FunctionElement && element.isSynthetic) {

snapshots/input/basic-project/lib/more.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class Animal with SleepMixin {
3636
}
3737
}
3838

39+
factory Animal.cat() => Animal('Timmy', type: AnimalType.cat);
40+
3941
void makeSound() {
4042
soundMaker?.call();
4143
}
@@ -54,16 +56,19 @@ void main() {
5456
List<int> numbers = [1, 2, 3, 4, 5];
5557
int sum = calculateSum(numbers);
5658

57-
Animal cat = Animal('Kitty', type: AnimalType.cat);
59+
Animal bird = Animal('Kitty', type: AnimalType.bird);
5860
Animal dog = Animal('Buddy', type: AnimalType.dog);
61+
Animal cat = Animal.cat();
5962

60-
cat.makeSound();
61-
cat.sleep();
63+
bird.makeSound();
64+
bird.sleep();
6265

6366
dog.makeSound();
6467
dog.sleep();
6568

66-
print(cat);
69+
cat.makeSound();
70+
71+
print(bird);
6772
print(dog);
6873
print('The sum of $numbers is $sum');
6974

snapshots/input/basic-project/lib/relationships.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ class Dog extends Animal with SwimAction {
2626
String get someGetter => 'value';
2727

2828
@override
29-
set someSetter(String v) {};
29+
set someSetter(String v) => print(v);
3030
}

snapshots/output/basic-project/lib/main.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
// ^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`int.dart`/int#
9696
// ^^^^^ definition local 5
9797
Foo(1, value: true, value2: '');
98-
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#
98+
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#`<constructor>`().
9999
// ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#`<constructor>`().(value)
100100
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#`<constructor>`().(value2)
101101
}

snapshots/output/basic-project/lib/more.dart

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
Animal(this.name, {required this.type}) {
4242
// ^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#`<constructor>`().
43-
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#
4443
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#name.
4544
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#type.
4645
// ^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#`<constructor>`().(type)
@@ -77,6 +76,13 @@
7776
}
7877
}
7978

79+
factory Animal.cat() => Animal('Timmy', type: AnimalType.cat);
80+
// ^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#cat().
81+
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#`<constructor>`().
82+
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#`<constructor>`().(type)
83+
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#
84+
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#cat.
85+
8086
void makeSound() {
8187
// ^^^^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#makeSound().
8288
soundMaker?.call();
@@ -122,27 +128,32 @@
122128
// ^^^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/calculateSum().
123129
// ^^^^^^^ reference local 3
124130

125-
Animal cat = Animal('Kitty', type: AnimalType.cat);
131+
Animal bird = Animal('Kitty', type: AnimalType.bird);
126132
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#
127-
// ^^^ definition local 5
128-
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#
129-
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#`<constructor>`().(type)
130-
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#
131-
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#cat.
133+
// ^^^^ definition local 5
134+
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#`<constructor>`().
135+
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#`<constructor>`().(type)
136+
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#
137+
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#bird.
132138
Animal dog = Animal('Buddy', type: AnimalType.dog);
133139
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#
134140
// ^^^ definition local 6
135-
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#
141+
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#`<constructor>`().
136142
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#`<constructor>`().(type)
137143
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#
138144
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/AnimalType#dog.
145+
Animal cat = Animal.cat();
146+
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#
147+
// ^^^ definition local 7
148+
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#
149+
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#cat().
139150

140-
cat.makeSound();
141-
// ^^^ reference local 5
142-
// ^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#makeSound().
143-
cat.sleep();
144-
// ^^^ reference local 5
145-
// ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/SleepMixin#sleep().
151+
bird.makeSound();
152+
// ^^^^ reference local 5
153+
// ^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#makeSound().
154+
bird.sleep();
155+
// ^^^^ reference local 5
156+
// ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/SleepMixin#sleep().
146157

147158
dog.makeSound();
148159
// ^^^ reference local 6
@@ -151,9 +162,13 @@
151162
// ^^^ reference local 6
152163
// ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/SleepMixin#sleep().
153164

154-
print(cat);
165+
cat.makeSound();
166+
// ^^^ reference local 7
167+
// ^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/Animal#makeSound().
168+
169+
print(bird);
155170
// ^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`print.dart`/print().
156-
// ^^^ reference local 5
171+
// ^^^^ reference local 5
157172
print(dog);
158173
// ^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`print.dart`/print().
159174
// ^^^ reference local 6
@@ -165,23 +180,23 @@
165180
print(math.Rectangle(1,2,3,4));
166181
// ^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`print.dart`/print().
167182
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/`more.dart`/math.
168-
// ^^^^^^^^^ reference scip-dart pub dart:math 2.19.0 dart:math/`rectangle.dart`/Rectangle#
183+
// ^^^^^^^^^ reference scip-dart pub dart:math 2.19.0 dart:math/`rectangle.dart`/Rectangle#`<constructor>`().
169184

170185
[1,2].reduce((a, b) => a + b);
171186
// ^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`iterable.dart`/Iterable#reduce().
172-
// ^ definition local 7
173-
// ^ definition local 8
174-
// ^ reference local 7
175-
// ^ reference local 8
187+
// ^ definition local 8
188+
// ^ definition local 9
189+
// ^ reference local 8
190+
// ^ reference local 9
176191
}
177192

178193
void test(String Function(int) p) {}
179194
// ^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/test().
180195
// ^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`string.dart`/String#
181196
// ^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`int.dart`/int#
182-
// ^ definition local 9
197+
// ^ definition local 10
183198
void deepTest(String Function(void Function(String test)) p) {}
184199
// ^^^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/`more.dart`/deepTest().
185200
// ^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`string.dart`/String#
186201
// ^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`string.dart`/String#
187-
// ^ definition local 10
202+
// ^ definition local 11

snapshots/output/basic-project/lib/other.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// ^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#value3.
1919
Foo(
2020
// ^^^ definition scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#`<constructor>`().
21-
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#
2221
this._far, {
2322
// ^^^^ reference local 0
2423
required this.value,
@@ -44,7 +43,6 @@
4443
// ^^^^^^^^^^ definition local 1
4544
Bar(this._someValue);
4645
// ^^^ definition scip-dart pub dart_test 1.0.0 lib/`other.dart`/Bar#`<constructor>`().
47-
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Bar#
4846
// ^^^^^^^^^^ reference local 1
4947

5048
void someMethod() {
@@ -64,20 +62,20 @@
6462
// ^^^^ reference scip-dart pub dart:async 2.19.0 dart:async/`future.dart`/Future#then().
6563
// ^ definition local 2
6664
Bar('a').someMethod.call()
67-
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Bar#
65+
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Bar#`<constructor>`().
6866
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Bar#someMethod().
6967
});
7068

7169
Foo(1, value: true, value2: 'asdf')..value = false;
72-
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#
70+
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#`<constructor>`().
7371
// ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#`<constructor>`().(value)
7472
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#`<constructor>`().(value2)
7573
// ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#value.
7674

7775
final someStr = 'someStr';
7876
// ^^^^^^^ definition local 3
7977
Foo(2, value: false, value2: 'some Val!')
80-
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#
78+
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#`<constructor>`().
8179
// ^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#`<constructor>`().(value)
8280
// ^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/`other.dart`/Foo#`<constructor>`().(value2)
8381
..value = true

snapshots/output/basic-project/lib/relationships.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@
6767

6868
@override
6969
// ^^^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`annotations.dart`/override.
70-
set someSetter(String v) {};
70+
set someSetter(String v) => print(v);
7171
// ^^^^^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/`relationships.dart`/Dog#`<set>someSetter`.
7272
// relationship scip-dart pub dart_test 1.0.0 lib/`relationships.dart`/Mammal#`<set>someSetter`. implementation reference
7373
// ^^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`string.dart`/String#
7474
// ^ definition local 1
75+
// ^^^^^ reference scip-dart pub dart:core 2.19.0 dart:core/`print.dart`/print().
76+
// ^ reference local 1
7577
}

0 commit comments

Comments
 (0)