From a55699375e6e7867e57c64b3c537a69a58f20194 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Tue, 20 Feb 2024 09:46:45 -0800 Subject: [PATCH 1/3] Do not crash in the presense of macro applications. * Dispose of an AnalysisContextCollectionImpl. * Before getting class hierarchy, navigate to possible augmentation declaration. * Do not attempt to access source code of augmentation elements. --- lib/src/model/method.dart | 3 ++- lib/src/model/package_builder.dart | 5 +++-- lib/src/model/source_code_mixin.dart | 18 +++++++++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/src/model/method.dart b/lib/src/model/method.dart index 2f0cc5d0a0..006aaa8660 100644 --- a/lib/src/model/method.dart +++ b/lib/src/model/method.dart @@ -113,7 +113,8 @@ class Method extends ModelElement return null; } var parent = element.enclosingElement as InterfaceElement; - for (var t in parent.allSupertypes) { + var parentDeclaration = parent.augmented?.declaration ?? parent; + for (var t in parentDeclaration.allSupertypes) { Element? e = t.getMethod(element.name); if (e != null) { assert( diff --git a/lib/src/model/package_builder.dart b/lib/src/model/package_builder.dart index 6096b071a2..3e2df5857b 100644 --- a/lib/src/model/package_builder.dart +++ b/lib/src/model/package_builder.dart @@ -136,8 +136,7 @@ class PubPackageBuilder implements PackageBuilder { late final Map> _packageMap; - late final AnalysisContextCollection _contextCollection = - AnalysisContextCollectionImpl( + late final _contextCollection = AnalysisContextCollectionImpl( includedPaths: [_config.inputDir], // TODO(jcollins-g): should we pass excluded directories here instead of // handling it ourselves? @@ -464,6 +463,8 @@ class PubPackageBuilder implements PackageBuilder { specialFiles.difference(files), addingSpecials: true, ); + // Shutdown macro support. + await _contextCollection.dispose(); } /// Throws an exception if any configured-to-be-included files were not found diff --git a/lib/src/model/source_code_mixin.dart b/lib/src/model/source_code_mixin.dart index eafd92bfeb..5c35745867 100644 --- a/lib/src/model/source_code_mixin.dart +++ b/lib/src/model/source_code_mixin.dart @@ -13,7 +13,8 @@ mixin SourceCode implements Documentable { Element? get element; - bool get hasSourceCode => config.includeSource && sourceCode.isNotEmpty; + bool get hasSourceCode => + config.includeSource && !element.isAugmentation && sourceCode.isNotEmpty; Library? get library; @@ -22,3 +23,18 @@ mixin SourceCode implements Documentable { return modelNode == null ? '' : modelNode.sourceCode; } } + +extension on Element? { + /// Whether `this` is an agumentation method or property. + /// + /// This property should only be referenced for elements whose source code we + /// may wish to refer to. + bool get isAugmentation { + final self = this; + return switch (self) { + ExecutableElement() => self.augmentationTarget != null, + PropertyInducingElement() => self.augmentationTarget != null, + _ => false, + }; + } +} From fccf00370eb73780d6f0ad7a3ec765c4b89ddf6d Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Tue, 20 Feb 2024 17:38:52 -0800 Subject: [PATCH 2/3] unused-import --- lib/src/model/package_builder.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/model/package_builder.dart b/lib/src/model/package_builder.dart index 3e2df5857b..eb259f4ba4 100644 --- a/lib/src/model/package_builder.dart +++ b/lib/src/model/package_builder.dart @@ -4,7 +4,6 @@ import 'dart:async'; -import 'package:analyzer/dart/analysis/analysis_context_collection.dart'; import 'package:analyzer/dart/analysis/context_root.dart'; import 'package:analyzer/dart/analysis/results.dart'; import 'package:analyzer/dart/ast/ast.dart'; From 5e181869519a4869c4f4e7696d24999b69a17d34 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Wed, 21 Feb 2024 10:02:55 -0800 Subject: [PATCH 3/3] typo --- lib/src/model/source_code_mixin.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/model/source_code_mixin.dart b/lib/src/model/source_code_mixin.dart index 5c35745867..79505d4f11 100644 --- a/lib/src/model/source_code_mixin.dart +++ b/lib/src/model/source_code_mixin.dart @@ -25,7 +25,7 @@ mixin SourceCode implements Documentable { } extension on Element? { - /// Whether `this` is an agumentation method or property. + /// Whether `this` is an augmentation method or property. /// /// This property should only be referenced for elements whose source code we /// may wish to refer to.