Skip to content

Commit 9d89c34

Browse files
authored
Fix problem with location calculation in enum fields (#2660)
* Make character locations work for enum accessors * rebuild and adjust meta dep * moar timeouts * even more timeouts?
1 parent 18e648a commit 9d89c34

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

lib/src/generator/templates.runtime_renderers.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3794,6 +3794,19 @@ class _Renderer_ContainerAccessor extends RendererBase<ContainerAccessor> {
37943794
..._Renderer_Accessor.propertyMap<CT_>(),
37953795
..._Renderer_ContainerMember.propertyMap<CT_>(),
37963796
..._Renderer_Inheritable.propertyMap<CT_>(),
3797+
'characterLocation': Property(
3798+
getValue: (CT_ c) => c.characterLocation,
3799+
renderVariable: (CT_ c, Property<CT_> self,
3800+
List<String> remainingNames) =>
3801+
self.renderSimpleVariable(
3802+
c, remainingNames, 'CharacterLocation'),
3803+
isNullValue: (CT_ c) => c.characterLocation == null,
3804+
renderValue:
3805+
(CT_ c, RendererBase<CT_> r, List<MustachioNode> ast) {
3806+
return renderSimple(c.characterLocation, ast, r.template,
3807+
parent: r);
3808+
},
3809+
),
37973810
'enclosingElement': Property(
37983811
getValue: (CT_ c) => c.enclosingElement,
37993812
renderVariable:

lib/src/model/accessor.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/element/element.dart';
6+
import 'package:analyzer/source/line_info.dart';
67
import 'package:analyzer/src/dart/element/member.dart' show ExecutableMember;
78
import 'package:dartdoc/src/element_type.dart';
89
import 'package:dartdoc/src/model/comment_referable.dart';
@@ -161,6 +162,19 @@ class ContainerAccessor extends Accessor with ContainerMember, Inheritable {
161162
return accessor;
162163
}
163164

165+
/// The index and values fields are never declared, and must be special cased.
166+
bool get _isEnumSynthetic =>
167+
enclosingCombo is EnumField && (name == 'index' || name == 'values');
168+
169+
@override
170+
CharacterLocation get characterLocation {
171+
if (_isEnumSynthetic) return enclosingElement.characterLocation;
172+
// TODO(jcollins-g): Remove the enclosingCombo case below once
173+
// https://github.com/dart-lang/sdk/issues/46154 is fixed.
174+
if (enclosingCombo is EnumField) return enclosingCombo.characterLocation;
175+
return super.characterLocation;
176+
}
177+
164178
ModelElement _enclosingElement;
165179
bool _isInherited = false;
166180

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ environment:
77
sdk: '>=2.11.99 <3.0.0'
88

99
dependencies:
10-
analyzer: ^1.5.0
10+
analyzer: ^1.7.1
1111
args: ^2.0.0
1212
charcode: ^1.2.0
1313
collection: ^1.2.0
@@ -17,7 +17,7 @@ dependencies:
1717
html: ^0.15.0
1818
logging: ^1.0.0
1919
markdown: ^4.0.0
20-
meta: ^1.2.4
20+
meta: ^1.3.0
2121
package_config: ^2.0.0
2222
path: ^1.3.0
2323
pub_semver: ^2.0.0

test/end2end/model_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ library dartdoc.model_test;
66

77
import 'dart:io';
88

9+
import 'package:analyzer/source/line_info.dart';
910
import 'package:async/async.dart';
1011
import 'package:dartdoc/src/element_type.dart';
1112
import 'package:dartdoc/src/model/feature.dart';
@@ -3347,6 +3348,39 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
33473348
classB = exLibrary.classes.singleWhere((c) => c.name == 'B');
33483349
});
33493350

3351+
test('always has a valid location', () {
3352+
void expectValidLocation(CharacterLocation location) {
3353+
expect(location.lineNumber, greaterThanOrEqualTo(0));
3354+
expect(location.columnNumber, greaterThanOrEqualTo(0));
3355+
}
3356+
3357+
;
3358+
var simpleProperty =
3359+
fakeLibrary.properties.firstWhere((p) => p.name == 'simpleProperty');
3360+
expectValidLocation(simpleProperty.getter.characterLocation);
3361+
expectValidLocation(simpleProperty.setter.characterLocation);
3362+
expectValidLocation(onlyGetterGetter.characterLocation);
3363+
expectValidLocation(onlySetterSetter.characterLocation);
3364+
3365+
Iterable<Accessor> _expandAccessors(Field f) sync* {
3366+
if (f.hasGetter) yield f.getter;
3367+
if (f.hasSetter) yield f.setter;
3368+
}
3369+
3370+
// classB has a variety of inherited and partially overridden fields.
3371+
// All should have valid locations on their accessors.
3372+
for (var a in classB.allFields.expand(_expandAccessors)) {
3373+
expectValidLocation(a.characterLocation);
3374+
}
3375+
3376+
// Enums also have fields and have historically had problems.
3377+
var macrosFromAccessors =
3378+
fakeLibrary.enums.firstWhere((e) => e.name == 'MacrosFromAccessors');
3379+
for (var a in macrosFromAccessors.allFields.expand(_expandAccessors)) {
3380+
expectValidLocation(a.characterLocation);
3381+
}
3382+
});
3383+
33503384
test('are available on top-level variables', () {
33513385
expect(onlyGetterGetter.name, equals('justGetter'));
33523386
expect(onlyGetterSetter, isNull);

test/mustachio/aot_compiler_builder_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
@Timeout.factor(2)
5+
@Timeout.factor(4)
66
import 'dart:convert';
77
import 'package:analyzer/dart/element/element.dart';
88
import 'package:build/build.dart';
@@ -77,7 +77,7 @@ import 'package:mustachio/annotations.dart';
7777
expect(
7878
renderersLibrary.getTopLevelFunction('_renderFoo_partial_foo_header_0'),
7979
isNotNull);
80-
});
80+
}, timeout: Timeout.factor(2));
8181

8282
test('builds a public API render function', () async {
8383
writer = InMemoryAssetWriter();

0 commit comments

Comments
 (0)