diff --git a/lib/src/scip_visitor.dart b/lib/src/scip_visitor.dart index eb65dff2..97df531e 100644 --- a/lib/src/scip_visitor.dart +++ b/lib/src/scip_visitor.dart @@ -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 diff --git a/snapshots/input/basic-project/lib/other.dart b/snapshots/input/basic-project/lib/other.dart index f25236b1..d7acee00 100644 --- a/snapshots/input/basic-project/lib/other.dart +++ b/snapshots/input/basic-project/lib/other.dart @@ -2,3 +2,13 @@ class Foo { int _far; Foo(this._far); } + +class Bar { + String _someValue; + Bar(this._someValue); + + void someMethod() { + _someValue = 'asdf'; + print(_someValue); + } +} \ No newline at end of file diff --git a/snapshots/output/basic-project/lib/more.dart b/snapshots/output/basic-project/lib/more.dart index 7d67319b..94af943f 100755 --- a/snapshots/output/basic-project/lib/more.dart +++ b/snapshots/output/basic-project/lib/more.dart @@ -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(). } } diff --git a/snapshots/output/basic-project/lib/other.dart b/snapshots/output/basic-project/lib/other.dart index bc000eb3..08522d3a 100755 --- a/snapshots/output/basic-project/lib/other.dart +++ b/snapshots/output/basic-project/lib/other.dart @@ -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#(). +// 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 + } + }