improve formatting of initializer lists and the toJson body

Change-Id: I9363415a32e57a75cc93f2ee3657acfe7103c042
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/365720
Commit-Queue: Jake Macdonald <jakemac@google.com>
Auto-Submit: Jake Macdonald <jakemac@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/_macros/CHANGELOG.md b/pkg/_macros/CHANGELOG.md
index 0b802bc..12a9300 100644
--- a/pkg/_macros/CHANGELOG.md
+++ b/pkg/_macros/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.1.4
+
+- Improve formatting of constructor initializer augmentations.
+
 ## 0.1.3
 
 - Validate parts in `Code.fromParts()`.
diff --git a/pkg/_macros/lib/src/executor/builder_impls.dart b/pkg/_macros/lib/src/executor/builder_impls.dart
index c61db46..eac681f 100644
--- a/pkg/_macros/lib/src/executor/builder_impls.dart
+++ b/pkg/_macros/lib/src/executor/builder_impls.dart
@@ -421,7 +421,6 @@
       throw UnsupportedError(
           'Augmenting existing constructor bodies is not allowed.');
     }
-    body ??= FunctionBodyCode.fromString(';');
     DeclarationCode augmentation = _buildFunctionAugmentation(body, declaration,
         initializers: initializers, docComments: docComments);
     _typeAugmentations.update(
@@ -568,7 +567,7 @@
 /// The [initializers] parameter can only be used if [declaration] is a
 /// constructor.
 DeclarationCode _buildFunctionAugmentation(
-    FunctionBodyCode body, FunctionDeclaration declaration,
+    FunctionBodyCode? body, FunctionDeclaration declaration,
     {List<Code>? initializers, CommentCode? docComments}) {
   assert(initializers == null || declaration is ConstructorDeclaration);
 
@@ -627,15 +626,19 @@
       ],
       ')',
     ],
-    ' ',
     if (initializers != null && initializers.isNotEmpty) ...[
-      ' : ',
+      '\n      : ',
       initializers.first,
       for (Code initializer in initializers.skip(1)) ...[
-        ',\n',
+        ',\n        ',
         initializer,
       ],
     ],
-    body,
+    if (body == null)
+      ';'
+    else ...[
+      ' ',
+      body,
+    ]
   ]);
 }
diff --git a/pkg/_macros/pubspec.yaml b/pkg/_macros/pubspec.yaml
index 661a452..93b1428 100644
--- a/pkg/_macros/pubspec.yaml
+++ b/pkg/_macros/pubspec.yaml
@@ -1,5 +1,5 @@
 name: _macros
-version: 0.1.3
+version: 0.1.4
 description: >-
   This is a private SDK vendored package, which is re-exported by the public
   `macros` package, which is a pub package. Every change to this package is
diff --git a/pkg/analyzer/test/src/summary/macro_test.dart b/pkg/analyzer/test/src/summary/macro_test.dart
index 71abcc5..5c21928 100644
--- a/pkg/analyzer/test/src/summary/macro_test.dart
+++ b/pkg/analyzer/test/src/summary/macro_test.dart
@@ -7798,8 +7798,9 @@
   external A.fromJson(prefix1.Map<prefix1.String, prefix1.Object?> json);
   @prefix0.ToJson()
   external prefix1.Map<prefix1.String, prefix1.Object?> toJson();
-  augment A.fromJson(prefix1.Map<prefix1.String, prefix1.Object?> json, )  : this.foo = json['foo'] as prefix1.int,
-this.bar = json['bar'] as prefix1.int;
+  augment A.fromJson(prefix1.Map<prefix1.String, prefix1.Object?> json, )
+      : this.foo = json['foo'] as prefix1.int,
+        this.bar = json['bar'] as prefix1.int;
   augment prefix1.Map<prefix1.String, prefix1.Object?> toJson() {
     var json = <prefix1.String, prefix1.Object?>{};
     json['foo'] = this.foo;
@@ -7839,7 +7840,7 @@
                 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@method::toJson
                 returnType: Map<String, Object?>
                 augmentation: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@methodAugmentation::toJson
-              augment toJson @542
+              augment toJson @555
                 reference: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@methodAugmentation::toJson
                 returnType: Map<String, Object?>
                 augmentationTarget: self::@augmentation::package:test/test.macro.dart::@classAugmentation::A::@method::toJson
diff --git a/pkg/json/lib/json.dart b/pkg/json/lib/json.dart
index 4e60621..b3f9aff 100644
--- a/pkg/json/lib/json.dart
+++ b/pkg/json/lib/json.dart
@@ -239,17 +239,17 @@
             ]),
             builder,
             introspectionData),
-        ';\n',
+        ';\n    ',
       ]);
       if (doNullCheck) {
-        parts.add('    }\n');
+        parts.add('}\n    ');
       }
       return RawCode.fromParts(parts);
     }
 
     parts.addAll(await Future.wait(fields.map(addEntryForField)));
 
-    parts.add('    return json;\n  }');
+    parts.add('return json;\n  }');
 
     builder.augment(FunctionBodyCode.fromParts(parts));
   }
diff --git a/pkg/macros/CHANGELOG.md b/pkg/macros/CHANGELOG.md
index dbc6a3c..3b322f0 100644
--- a/pkg/macros/CHANGELOG.md
+++ b/pkg/macros/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.1.0-main.4
+
+- Improve formatting of constructor initializer augmentations.
+
 ## 0.1.0-main.3
 
 - Validate parts in `Code.fromParts()`.
diff --git a/pkg/macros/pubspec.yaml b/pkg/macros/pubspec.yaml
index a7f087a..07d2dc2 100644
--- a/pkg/macros/pubspec.yaml
+++ b/pkg/macros/pubspec.yaml
@@ -1,5 +1,5 @@
 name: macros
-version: 0.1.0-main.3
+version: 0.1.0-main.4
 description: >-
   This package is for macro authors, and exposes the APIs necessary to write
   a macro. It exports the APIs from the private `_macros` SDK vendored package.
@@ -11,4 +11,4 @@
 dependencies:
   _macros:
     sdk: dart
-    version: 0.1.3
+    version: 0.1.4