Skip to content

Commit

Permalink
Fix enum links for exported enums (#3957)
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins authored Jan 6, 2025
1 parent c8dd09c commit 591173b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
6 changes: 3 additions & 3 deletions lib/src/model/enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ class EnumField extends Field {
return canonicalModelElement?.href;
}
assert(canonicalEnclosingContainer == enclosingElement);
// TODO(jcollins-g): EnumField should not depend on enclosingElement, but
// we sort of have to while we are half-converted to [FileStructure].
return '${package.baseHref}${enclosingElement.library.dirName}/${enclosingElement.fileName}';
assert(canonicalLibrary != null);
return '${package.baseHref}${canonicalLibrary!.dirName}/'
'${enclosingElement.fileName}';
}

@override
Expand Down
18 changes: 18 additions & 0 deletions test/enums_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import 'dartdoc_test_base.dart';
import 'src/test_descriptor_utils.dart' as d;
import 'src/utils.dart';

void main() {
Expand Down Expand Up @@ -637,6 +638,23 @@ enum E {
expect(oneValue.constantValue, equals(oneValue.renderedName));
}

void test_value_linksToItsAnchor_inExportedLib() async {
var library = (await bootPackageFromFiles([
d.file('lib/src/library.dart', '''
enum E { one, two three }
'''),
d.file('lib/enums.dart', '''
export 'src/library.dart';
'''),
]))
.libraries
.named(libraryName);
var oneValue =
library.enums.named('E').publicEnumValues.named('one') as EnumField;
expect(oneValue.linkedName, '<a href="$linkPrefix/E.html#one">one</a>');
expect(oneValue.constantValue, equals(oneValue.renderedName));
}

void test_values_haveIndices() async {
var library = await bootPackageWithLibrary('enum E { one, two, three }');
var oneValue =
Expand Down
11 changes: 10 additions & 1 deletion test/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,16 @@ bool get classModifiersAllowed =>
.allows(platformVersion);

extension ModelElementIterableExtension<T extends ModelElement> on Iterable<T> {
T named(String name) => singleWhere((e) => e.name == name);
T named(String name) {
var elements = where((e) => e.name == name).toList();
if (elements.isEmpty) {
throw StateError("No $T elements named '$name'");
}
if (elements.length > 1) {
throw StateError("Too many $T elements named '$name': $elements");
}
return elements.single;
}

T displayNamed(String displayName) =>
singleWhere((e) => e.displayName == displayName);
Expand Down

0 comments on commit 591173b

Please sign in to comment.