Skip to content

Commit

Permalink
Correctly handle properties in spanForElement, prepare for release
Browse files Browse the repository at this point in the history
Fixes #379
  • Loading branch information
kevmoo committed Oct 3, 2018
1 parent b9d92c2 commit 5d70d11
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
4 changes: 4 additions & 0 deletions source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.1+2

* Correctly handle properties in `spanForElement`.

## 0.9.1+1

* Allow `package:build` version 1.0.0.
Expand Down
12 changes: 12 additions & 0 deletions source_gen/lib/src/span_for_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,17 @@ SourceSpan spanForElement(Element element, [SourceFile file]) {
}
file = SourceFile.fromString(contents.data, url: url);
}
if (element.nameOffset < 0) {
if (element is PropertyInducingElement) {
if (element.getter != null) {
return spanForElement(element.getter);
}

if (element.setter != null) {
return spanForElement(element.setter);
}
}
}

return file.span(element.nameOffset, element.nameOffset + element.nameLength);
}
2 changes: 1 addition & 1 deletion source_gen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: source_gen
version: 0.9.1+1
version: 0.9.1+2
author: Dart Team <[email protected]>
description: Automated source code generation for Dart.
homepage: https://github.com/dart-lang/source_gen
Expand Down
65 changes: 56 additions & 9 deletions source_gen/test/span_for_element_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,66 @@ void main() {

setUpAll(() async {
library = await resolveSource(r'''
library test_lib;
library test_lib;
abstract class Example implements List {}
''', (resolver) => resolver.findLibraryByName('test_lib'),
abstract class Example implements List {
List<A> get getter => null;
set setter(int value) {}
int field;
int get fieldProp => field;
set fieldProp(int value) {
field = value;
}
}
''', (resolver) => resolver.findLibraryByName('test_lib'),
inputId: AssetId('test_lib', 'lib/test_lib.dart'));
});

test('should highlight the use of "class Example"', () {
test('should highlight the use of "class Example"', () async {
expect(
spanForElement(library.getType('Example')).message('Here it is'), r'''
line 3, column 16 of package:test_lib/test_lib.dart: Here it is
abstract class Example implements List {
^^^^^^^''');
});

test('should correctly highlight getter', () async {
expect(
spanForElement(library.getType('Example').getField('getter'))
.message('Here it is'),
r'''
line 4, column 15 of package:test_lib/test_lib.dart: Here it is
List<A> get getter => null;
^^^^^^''');
});

test('should correctly highlight setter', () async {
expect(
spanForElement(library.getType('Example').getField('setter'))
.message('Here it is'),
r'''
line 5, column 7 of package:test_lib/test_lib.dart: Here it is
set setter(int value) {}
^^^^^^''');
});

test('should correctly highlight field', () async {
expect(
spanForElement(library.getType('Example').getField('field'))
.message('Here it is'),
r'''
line 6, column 7 of package:test_lib/test_lib.dart: Here it is
int field;
^^^^^''');
});

test('highlight getter with getter/setter property', () async {
expect(
spanForElement(library.getType('Example')).message('Here it is'),
''
'line 3, column 22 of package:test_lib/test_lib.dart: Here it is\n'
' abstract class Example implements List {}\n'
' ^^^^^^^');
spanForElement(library.getType('Example').getField('fieldProp'))
.message('Here it is'),
r'''
line 7, column 11 of package:test_lib/test_lib.dart: Here it is
int get fieldProp => field;
^^^^^^^^^''');
});
}

0 comments on commit 5d70d11

Please sign in to comment.