Skip to content

Commit f41bd3a

Browse files
authored
Improve failure when import cannot be found (#4022)
Fixes #4021 In the sense that the error state should be more visible :D
1 parent d400676 commit f41bd3a

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/src/model/prefix.dart

+11-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import 'package:dartdoc/src/model/model.dart';
1313
/// Like [Parameter], it doesn't have doc pages, but participates in lookups.
1414
/// Forwards to its referenced library if referred to directly.
1515
class Prefix extends ModelElement with HasNoPage {
16-
1716
@override
1817
final PrefixElement2 element;
1918

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

32-
LibraryElement2? _getImportedLibraryElement() {
31+
LibraryElement2 get _importedLibraryElement {
3332
final importLists =
3433
library.element.fragments.map((fragment) => fragment.libraryImports2);
35-
return importLists
34+
var libraryImport = importLists
3635
.expand((import) => import)
37-
.firstWhere((i) => i.prefix2?.element == element)
38-
.importedLibrary2;
36+
.firstWhere((i) => i.prefix2?.element == element);
37+
var importedLibrary = libraryImport.importedLibrary2;
38+
if (importedLibrary == null) {
39+
throw StateError(
40+
'Unexpected null LibraryElement2 for imported library at '
41+
'${libraryImport.uri}');
42+
}
43+
return importedLibrary;
3944
}
4045

4146
@override

0 commit comments

Comments
 (0)