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

Improve failure when import cannot be found #4022

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/src/model/prefix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import 'package:dartdoc/src/model/model.dart';
/// Like [Parameter], it doesn't have doc pages, but participates in lookups.
/// Forwards to its referenced library if referred to directly.
class Prefix extends ModelElement with HasNoPage {

@override
final PrefixElement2 element;

Expand All @@ -27,15 +26,21 @@ class Prefix extends ModelElement with HasNoPage {
// TODO(jcollins-g): consider connecting PrefixElement to the imported library
// in analyzer?
late final Library associatedLibrary =
getModelForElement(_getImportedLibraryElement()!) as Library;
getModelForElement(_importedLibraryElement) as Library;

LibraryElement2? _getImportedLibraryElement() {
LibraryElement2 get _importedLibraryElement {
final importLists =
library.element.fragments.map((fragment) => fragment.libraryImports2);
return importLists
var libraryImport = importLists
.expand((import) => import)
.firstWhere((i) => i.prefix2?.element == element)
.importedLibrary2;
.firstWhere((i) => i.prefix2?.element == element);
var importedLibrary = libraryImport.importedLibrary2;
if (importedLibrary == null) {
throw StateError(
'Unexpected null LibraryElement2 for imported library at '
'${libraryImport.uri}');
}
return importedLibrary;
}

@override
Expand Down