33// BSD-style license that can be found in the LICENSE file.
44
55import 'package:analyzer/dart/element/element.dart' ;
6+ import 'package:analyzer/dart/element/element2.dart' ;
67import 'package:analyzer/src/dart/element/element.dart' ;
78import 'package:analyzer/src/dart/element/member.dart' ;
89import 'package:analyzer/src/dart/element/type.dart' ;
@@ -12,12 +13,10 @@ import 'package:analyzer/src/utilities/extensions/element.dart';
1213import 'package:analyzer/src/utilities/extensions/string.dart' ;
1314
1415class AugmentedClassDeclarationBuilder
15- extends AugmentedInstanceDeclarationBuilder {
16- final ClassElementImpl firstFragment;
17-
16+ extends AugmentedInstanceDeclarationBuilder <ClassElementImpl > {
1817 AugmentedClassDeclarationBuilder ({
18+ required super .firstFragment,
1919 required MaybeAugmentedClassElementMixin element,
20- required this .firstFragment,
2120 }) {
2221 firstFragment.augmentedInternal = element;
2322 addFields (firstFragment.fields);
@@ -27,6 +26,9 @@ class AugmentedClassDeclarationBuilder
2726 }
2827
2928 void augment (ClassElementImpl fragment) {
29+ lastFragment.augmentation = fragment;
30+ lastFragment = fragment;
31+
3032 var element = _ensureAugmented (firstFragment);
3133 fragment.augmentedInternal = firstFragment.augmentedInternal;
3234
@@ -39,21 +41,19 @@ class AugmentedClassDeclarationBuilder
3941}
4042
4143class AugmentedEnumDeclarationBuilder
42- extends AugmentedInstanceDeclarationBuilder {
43- final EnumElementImpl declaration;
44-
44+ extends AugmentedInstanceDeclarationBuilder <EnumElementImpl > {
4545 AugmentedEnumDeclarationBuilder ({
46- required this .declaration ,
46+ required super .firstFragment ,
4747 }) {
48- addFields (declaration .fields);
49- addConstructors (declaration .constructors);
50- addAccessors (declaration .accessors);
51- addMethods (declaration .methods);
48+ addFields (firstFragment .fields);
49+ addConstructors (firstFragment .constructors);
50+ addAccessors (firstFragment .accessors);
51+ addMethods (firstFragment .methods);
5252 }
5353
5454 void augment (EnumElementImpl element) {
55- var augmented = _ensureAugmented (declaration );
56- element.augmentedInternal = declaration .augmentedInternal;
55+ var augmented = _ensureAugmented (firstFragment );
56+ element.augmentedInternal = firstFragment .augmentedInternal;
5757
5858 addFields (element.fields);
5959 addConstructors (element.constructors);
@@ -64,20 +64,18 @@ class AugmentedEnumDeclarationBuilder
6464}
6565
6666class AugmentedExtensionDeclarationBuilder
67- extends AugmentedInstanceDeclarationBuilder {
68- final ExtensionElementImpl declaration;
69-
67+ extends AugmentedInstanceDeclarationBuilder <ExtensionElementImpl > {
7068 AugmentedExtensionDeclarationBuilder ({
71- required this .declaration ,
69+ required super .firstFragment ,
7270 }) {
73- addFields (declaration .fields);
74- addAccessors (declaration .accessors);
75- addMethods (declaration .methods);
71+ addFields (firstFragment .fields);
72+ addAccessors (firstFragment .accessors);
73+ addMethods (firstFragment .methods);
7674 }
7775
7876 void augment (ExtensionElementImpl element) {
79- var augmented = _ensureAugmented (declaration );
80- element.augmentedInternal = declaration .augmentedInternal;
77+ var augmented = _ensureAugmented (firstFragment );
78+ element.augmentedInternal = firstFragment .augmentedInternal;
8179
8280 addFields (element.fields);
8381 addAccessors (element.accessors);
@@ -87,21 +85,19 @@ class AugmentedExtensionDeclarationBuilder
8785}
8886
8987class AugmentedExtensionTypeDeclarationBuilder
90- extends AugmentedInstanceDeclarationBuilder {
91- final ExtensionTypeElementImpl declaration;
92-
88+ extends AugmentedInstanceDeclarationBuilder <ExtensionTypeElementImpl > {
9389 AugmentedExtensionTypeDeclarationBuilder ({
94- required this .declaration ,
90+ required super .firstFragment ,
9591 }) {
96- addFields (declaration .fields);
97- addConstructors (declaration .constructors);
98- addAccessors (declaration .accessors);
99- addMethods (declaration .methods);
92+ addFields (firstFragment .fields);
93+ addConstructors (firstFragment .constructors);
94+ addAccessors (firstFragment .accessors);
95+ addMethods (firstFragment .methods);
10096 }
10197
10298 void augment (ExtensionTypeElementImpl element) {
103- var augmented = _ensureAugmented (declaration );
104- element.augmentedInternal = declaration .augmentedInternal;
99+ var augmented = _ensureAugmented (firstFragment );
100+ element.augmentedInternal = firstFragment .augmentedInternal;
105101
106102 addFields (element.fields);
107103 addConstructors (element.constructors);
@@ -111,13 +107,18 @@ class AugmentedExtensionTypeDeclarationBuilder
111107 }
112108}
113109
114- abstract class AugmentedInstanceDeclarationBuilder {
110+ abstract class AugmentedInstanceDeclarationBuilder <
111+ F extends InstanceElementImpl > extends FragmentedElementBuilder <F > {
115112 final Map <String , FieldElementImpl > fields = {};
116113 final Map <String , ConstructorElementImpl > constructors = {};
117114 final Map <String , PropertyAccessorElementImpl > getters = {};
118115 final Map <String , PropertyAccessorElementImpl > setters = {};
119116 final Map <String , MethodElementImpl > methods = {};
120117
118+ AugmentedInstanceDeclarationBuilder ({
119+ required super .firstFragment,
120+ });
121+
121122 void addAccessors (List <PropertyAccessorElementImpl > elements) {
122123 for (var element in elements) {
123124 var name = element.name;
@@ -319,20 +320,18 @@ abstract class AugmentedInstanceDeclarationBuilder {
319320}
320321
321322class AugmentedMixinDeclarationBuilder
322- extends AugmentedInstanceDeclarationBuilder {
323- final MixinElementImpl declaration;
324-
323+ extends AugmentedInstanceDeclarationBuilder <MixinElementImpl > {
325324 AugmentedMixinDeclarationBuilder ({
326- required this .declaration ,
325+ required super .firstFragment ,
327326 }) {
328- addFields (declaration .fields);
329- addAccessors (declaration .accessors);
330- addMethods (declaration .methods);
327+ addFields (firstFragment .fields);
328+ addAccessors (firstFragment .accessors);
329+ addMethods (firstFragment .methods);
331330 }
332331
333332 void augment (MixinElementImpl element) {
334- var augmented = _ensureAugmented (declaration );
335- element.augmentedInternal = declaration .augmentedInternal;
333+ var augmented = _ensureAugmented (firstFragment );
334+ element.augmentedInternal = firstFragment .augmentedInternal;
336335
337336 addFields (element.fields);
338337 addAccessors (element.accessors);
@@ -402,6 +401,32 @@ class AugmentedTopVariablesBuilder {
402401 }
403402}
404403
404+ /// A builder for top-level fragmented elements, e.g. classes.
405+ class FragmentedElementBuilder <F extends Fragment > {
406+ final F firstFragment;
407+ F lastFragment;
408+
409+ FragmentedElementBuilder ({
410+ required this .firstFragment,
411+ }) : lastFragment = firstFragment;
412+
413+ /// If [fragment] is an augmentation, set its previous fragment to
414+ /// [lastFragment] .
415+ ///
416+ /// We invoke this method on any [FragmentedElementBuilder] associated with
417+ /// the name of [fragment] , even if it is not a correct builder for this
418+ /// [fragment] . So, the [lastFragment] might have a wrong type, but we still
419+ /// want to remember it for generating the corresponding diagnostic.
420+ void setPreviousFor (AugmentableElement fragment) {
421+ if (fragment.isAugmentation) {
422+ // TODO(scheglov): hopefully the type check can be removed in the future.
423+ if (lastFragment case ElementImpl lastFragment) {
424+ fragment.augmentationTargetAny = lastFragment;
425+ }
426+ }
427+ }
428+ }
429+
405430extension < T extends ExecutableElement > on List <T > {
406431 Iterable <T > get notAugmented {
407432 return where ((e) => e.augmentation == null );
0 commit comments