@@ -868,7 +868,6 @@ class FragmentBuilder extends ThrowingAstVisitor<void> {
868868 var holder = _EnclosingContext (
869869 instanceElementBuilder: null ,
870870 fragment: fragment,
871- constFieldsForFinalInstance: true ,
872871 );
873872 _withEnclosing (holder, () {
874873 node.typeParameters? .accept (this );
@@ -1011,7 +1010,6 @@ class FragmentBuilder extends ThrowingAstVisitor<void> {
10111010 var holder = _EnclosingContext (
10121011 instanceElementBuilder: null ,
10131012 fragment: fragment,
1014- constFieldsForFinalInstance: true ,
10151013 );
10161014
10171015 // Build fields for all enum constants.
@@ -1279,24 +1277,6 @@ class FragmentBuilder extends ThrowingAstVisitor<void> {
12791277 name2: _getFragmentName (nameToken),
12801278 nameOffset: nameOffset,
12811279 );
1282- if (variable.initializer case var initializer? ) {
1283- if (node.fields.isConst) {
1284- fragment = FieldFragmentImpl (
1285- name2: _getFragmentName (nameToken),
1286- nameOffset: nameOffset,
1287- )..constantInitializer = initializer;
1288- } else if (_enclosingContext.constFieldsForFinalInstance) {
1289- if (node.fields.isFinal && ! node.isStatic) {
1290- var constElement = FieldFragmentImpl (
1291- name2: _getFragmentName (nameToken),
1292- nameOffset: nameOffset,
1293- )..constantInitializer = initializer;
1294- fragment = constElement;
1295- _libraryBuilder.finalInstanceFields.add (constElement);
1296- }
1297- }
1298- }
1299-
13001280 fragment.nameOffset2 = _getFragmentNameOffset (nameToken);
13011281 fragment.hasInitializer = variable.initializer != null ;
13021282 fragment.isAbstract = node.abstractKeyword != null ;
@@ -1311,6 +1291,15 @@ class FragmentBuilder extends ThrowingAstVisitor<void> {
13111291 _setCodeRange (fragment, variable);
13121292 _setDocumentation (fragment, node);
13131293
1294+ if (variable.initializer case var initializer? ) {
1295+ if (node.fields.isConst) {
1296+ fragment.constantInitializer = initializer;
1297+ } else if (node.fields.isFinal && ! node.isStatic) {
1298+ fragment.constantInitializer = initializer;
1299+ _libraryBuilder.finalInstanceFields.add (fragment);
1300+ }
1301+ }
1302+
13141303 if (node.fields.type == null ) {
13151304 fragment.hasImplicitType = true ;
13161305 }
@@ -2213,10 +2202,6 @@ class _EnclosingContext {
22132202 final List <TypeAliasFragmentImpl > _typeAliases = [];
22142203 final List <TypeParameterFragmentImpl > _typeParameters = [];
22152204
2216- /// A class can have `const` constructors, and if it has we need values
2217- /// of final instance fields.
2218- final bool constFieldsForFinalInstance;
2219-
22202205 /// Not all optional formal parameters can have default values.
22212206 /// For example, formal parameters of methods can, but formal parameters
22222207 /// of function types - not. This flag specifies if we should create
@@ -2226,7 +2211,6 @@ class _EnclosingContext {
22262211 _EnclosingContext ({
22272212 required this .instanceElementBuilder,
22282213 required this .fragment,
2229- this .constFieldsForFinalInstance = false ,
22302214 this .hasDefaultFormalParameters = false ,
22312215 });
22322216
0 commit comments