Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with location calculation in enum fields #2660

Merged
merged 4 commits into from May 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/src/generator/templates.runtime_renderers.dart
Expand Up @@ -3794,6 +3794,19 @@ class _Renderer_ContainerAccessor extends RendererBase<ContainerAccessor> {
..._Renderer_Accessor.propertyMap<CT_>(),
..._Renderer_ContainerMember.propertyMap<CT_>(),
..._Renderer_Inheritable.propertyMap<CT_>(),
'characterLocation': Property(
getValue: (CT_ c) => c.characterLocation,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(
c, remainingNames, 'CharacterLocation'),
isNullValue: (CT_ c) => c.characterLocation == null,
renderValue:
(CT_ c, RendererBase<CT_> r, List<MustachioNode> ast) {
return renderSimple(c.characterLocation, ast, r.template,
parent: r);
},
),
'enclosingElement': Property(
getValue: (CT_ c) => c.enclosingElement,
renderVariable:
Expand Down
14 changes: 14 additions & 0 deletions lib/src/model/accessor.dart
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/source/line_info.dart';
import 'package:analyzer/src/dart/element/member.dart' show ExecutableMember;
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/comment_referable.dart';
Expand Down Expand Up @@ -161,6 +162,19 @@ class ContainerAccessor extends Accessor with ContainerMember, Inheritable {
return accessor;
}

/// The index and values fields are never declared, and must be special cased.
bool get _isEnumSynthetic =>
enclosingCombo is EnumField && (name == 'index' || name == 'values');

@override
CharacterLocation get characterLocation {
if (_isEnumSynthetic) return enclosingElement.characterLocation;
// TODO(jcollins-g): Remove the enclosingCombo case below once
// https://github.com/dart-lang/sdk/issues/46154 is fixed.
if (enclosingCombo is EnumField) return enclosingCombo.characterLocation;
return super.characterLocation;
}

ModelElement _enclosingElement;
bool _isInherited = false;

Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Expand Up @@ -7,7 +7,7 @@ environment:
sdk: '>=2.11.99 <3.0.0'

dependencies:
analyzer: ^1.5.0
analyzer: ^1.7.1
args: ^2.0.0
charcode: ^1.2.0
collection: ^1.2.0
Expand All @@ -17,7 +17,7 @@ dependencies:
html: ^0.15.0
logging: ^1.0.0
markdown: ^4.0.0
meta: ^1.2.4
meta: ^1.3.0
package_config: ^2.0.0
path: ^1.3.0
pub_semver: ^2.0.0
Expand Down
34 changes: 34 additions & 0 deletions test/end2end/model_test.dart
Expand Up @@ -6,6 +6,7 @@ library dartdoc.model_test;

import 'dart:io';

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

test('always has a valid location', () {
void expectValidLocation(CharacterLocation location) {
expect(location.lineNumber, greaterThanOrEqualTo(0));
expect(location.columnNumber, greaterThanOrEqualTo(0));
}

;
var simpleProperty =
fakeLibrary.properties.firstWhere((p) => p.name == 'simpleProperty');
expectValidLocation(simpleProperty.getter.characterLocation);
expectValidLocation(simpleProperty.setter.characterLocation);
expectValidLocation(onlyGetterGetter.characterLocation);
expectValidLocation(onlySetterSetter.characterLocation);

Iterable<Accessor> _expandAccessors(Field f) sync* {
if (f.hasGetter) yield f.getter;
if (f.hasSetter) yield f.setter;
}

// classB has a variety of inherited and partially overridden fields.
// All should have valid locations on their accessors.
for (var a in classB.allFields.expand(_expandAccessors)) {
expectValidLocation(a.characterLocation);
}

// Enums also have fields and have historically had problems.
var macrosFromAccessors =
fakeLibrary.enums.firstWhere((e) => e.name == 'MacrosFromAccessors');
for (var a in macrosFromAccessors.allFields.expand(_expandAccessors)) {
expectValidLocation(a.characterLocation);
}
});

test('are available on top-level variables', () {
expect(onlyGetterGetter.name, equals('justGetter'));
expect(onlyGetterSetter, isNull);
Expand Down
2 changes: 1 addition & 1 deletion test/mustachio/aot_compiler_builder_test.dart
Expand Up @@ -2,7 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

@Timeout.factor(2)
@Timeout.factor(4)
import 'dart:convert';
import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
Expand Down