Skip to content

Commit

Permalink
Merge pull request #32 from Workiva/index_fields
Browse files Browse the repository at this point in the history
FEA-1741: Correctly index fields
  • Loading branch information
rmconsole2-wf authored Apr 17, 2023
2 parents 46af5a9 + 55b7bcc commit 6c7560e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/src/scip_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,22 @@ class ScipVisitor extends GeneralizingAstVisitor {
}

void _visitSimpleIdentifier(SimpleIdentifier node) {
final element = node.staticElement;
var element = node.staticElement;

// [element] for assignment fields is null. If the parent node
// is a `CompoundAssignmentExpression`, we know this node is referring
// to an assignment line. In that case, use the read/write element attached
// to this node instead of the [node]'s element
if (node.parent is CompoundAssignmentExpression) {
final assignmentNode = node.parent as CompoundAssignmentExpression;
element = assignmentNode.readElement ?? assignmentNode.writeElement;
}

// When the identifier is a field, the analyzer creates synthetic getters/
// setters for it. We need to get the backing field.
if (element?.isSynthetic == true && element is PropertyAccessorElement) {
element = element.variable;
}

// element is null if there's nothing really to do for this node. Example: `void`
// TODO: One weird issue found: named parameters of external symbols were element.source
Expand Down
10 changes: 10 additions & 0 deletions snapshots/input/basic-project/lib/other.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@ class Foo {
int _far;
Foo(this._far);
}

class Bar {
String _someValue;
Bar(this._someValue);

void someMethod() {
_someValue = 'asdf';
print(_someValue);
}
}
4 changes: 4 additions & 0 deletions snapshots/output/basic-project/lib/more.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,26 @@
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/AnimalType#
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/AnimalType#cat.
soundMaker = () => print('Meow!');
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/Animal#soundMaker.
// ^^^^^ reference scip-dart pub dart:core 2.18.0 dart:core/print.dart/print().
break;
case AnimalType.dog:
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/AnimalType#
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/AnimalType#dog.
soundMaker = () => print('Woof!');
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/Animal#soundMaker.
// ^^^^^ reference scip-dart pub dart:core 2.18.0 dart:core/print.dart/print().
break;
case AnimalType.bird:
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/AnimalType#
// ^^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/AnimalType#bird.
soundMaker = () => print('Chirp!');
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/Animal#soundMaker.
// ^^^^^ reference scip-dart pub dart:core 2.18.0 dart:core/print.dart/print().
break;
default:
soundMaker = () => print('Unknown animal type');
// ^^^^^^^^^^ reference scip-dart pub dart_test 1.0.0 lib/more.dart/Animal#soundMaker.
// ^^^^^ reference scip-dart pub dart:core 2.18.0 dart:core/print.dart/print().
}
}
Expand Down
25 changes: 25 additions & 0 deletions snapshots/output/basic-project/lib/other.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,28 @@
// documentation ```dart
}

class Bar {
// ^^^ definition scip-dart pub dart_test 1.0.0 lib/other.dart/Bar#
// documentation ```dart
String _someValue;
// ^^^^^^ reference scip-dart pub dart:core 2.18.0 dart:core/string.dart/String#
// ^^^^^^^^^^ definition local 2
// documentation ```dart
Bar(this._someValue);
// ^^^ definition scip-dart pub dart_test 1.0.0 lib/other.dart/Bar#<constructor>().
// documentation ```dart
// ^^^ reference scip-dart pub dart_test 1.0.0 lib/other.dart/Bar#
// ^^^^ reference local 2
// ^^^^^^^^^^ definition local 3
// documentation ```dart

void someMethod() {
// ^^^^^^^^^^ definition scip-dart pub dart_test 1.0.0 lib/other.dart/Bar#someMethod().
// documentation ```dart
_someValue = 'asdf';
// ^^^^^^^^^^ reference local 2
print(_someValue);
// ^^^^^ reference scip-dart pub dart:core 2.18.0 dart:core/print.dart/print().
// ^^^^^^^^^^ reference local 2
}
}

0 comments on commit 6c7560e

Please sign in to comment.