From a2f14090e9f7ee95cfb58c28173541ff0b0009ee Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Fri, 3 Jan 2025 12:35:53 -0800 Subject: [PATCH] Fix enum links for exported enums --- lib/src/model/enum.dart | 6 +++--- test/enums_test.dart | 18 ++++++++++++++++++ test/src/utils.dart | 11 ++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/src/model/enum.dart b/lib/src/model/enum.dart index a7338d3c44..b34dfefe52 100644 --- a/lib/src/model/enum.dart +++ b/lib/src/model/enum.dart @@ -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 diff --git a/test/enums_test.dart b/test/enums_test.dart index ed87599dd8..0c88525247 100644 --- a/test/enums_test.dart +++ b/test/enums_test.dart @@ -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() { @@ -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, 'one'); + expect(oneValue.constantValue, equals(oneValue.renderedName)); + } + void test_values_haveIndices() async { var library = await bootPackageWithLibrary('enum E { one, two, three }'); var oneValue = diff --git a/test/src/utils.dart b/test/src/utils.dart index 99d9a0f1d6..d43db63a8b 100644 --- a/test/src/utils.dart +++ b/test/src/utils.dart @@ -355,7 +355,16 @@ bool get classModifiersAllowed => .allows(platformVersion); extension ModelElementIterableExtension on Iterable { - 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);