[dds/dap] Use super-params in generated code

Fixes https://github.com/dart-lang/sdk/issues/53600

Change-Id: Ibffce274a1255285d1c9c804d826ae2c8a7665d8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/327881
Reviewed-by: Ben Konyi <bkonyi@google.com>
Reviewed-by: Helin Shiah <helinx@google.com>
Commit-Queue: Ben Konyi <bkonyi@google.com>
This commit is contained in:
Danny Tuppeny
2023-09-26 20:31:43 +00:00
committed by Commit Queue
parent 3880f0d381
commit 8745f78d8a
3 changed files with 395 additions and 732 deletions
-6
View File
@@ -1,7 +1 @@
include: package:lints/recommended.yaml
analyzer:
errors:
# TODO: We need to address existing instances in
# lib/src/protocol_generated.dart (#53600).
use_super_parameters: ignore
File diff suppressed because it is too large Load Diff
+22 -24
View File
@@ -299,8 +299,7 @@ class CodeGenerator {
buffer.writeln('this.$fieldName, ');
}
// Properties from the base class are standard arguments that will be
// passed to a super() call.
// Properties from the base class are super-parameters.
for (final entry in baseProperties.entries.sortedBy((e) => e.key)) {
final propertyName = entry.key;
// If this field is defined by the class and the base, prefer the
@@ -312,12 +311,11 @@ class CodeGenerator {
continue;
}
final isOptional = !type.requiresField(propertyName);
final dartType = propertyType.asDartType(isOptional: isOptional);
buffer.writeIndented('');
if (!isOptional) {
buffer.write('required ');
}
buffer.writeln('$dartType $fieldName, ');
buffer.writeln('super.$fieldName, ');
}
buffer
..outdent()
@@ -325,32 +323,29 @@ class CodeGenerator {
}
buffer.write(')');
// We might still need an explicit super() call if we have literal values
// to pass through.
if (baseType != null) {
buffer.write(': super(');
if (baseProperties.isNotEmpty) {
final requiredExplicitSuperArgs = baseProperties.entries
.map((entry) =>
MapEntry(entry.key, classProperties[entry.key]?.literalValue))
.where((entry) => entry.value != null)
.toList();
if (requiredExplicitSuperArgs.isNotEmpty) {
buffer
..write(': super(')
..writeln()
..indent();
for (final entry in baseProperties.entries) {
final propertyName = entry.key;
// Skip any properties that have literal values defined by the base
// as we won't need to supply them.
if (entry.value.literalValue != null) {
continue;
}
// If this field is defined by the class and the base, prefer the
// class one as it may contain things like the literal values.
final propertyType = classProperties[propertyName] ?? entry.value;
final fieldName = _dartSafeName(propertyName);
final literalValue = propertyType.literalValue;
final value = literalValue != null ? "'$literalValue'" : fieldName;
buffer.writeIndentedln('$fieldName: $value, ');
for (final entry in requiredExplicitSuperArgs) {
final name = entry.key;
final value = entry.value;
buffer.writeIndentedln('$name: \'$value\', ');
}
buffer
..outdent()
..writeIndented('');
..writeIndented('')
..write(')');
}
buffer.write(')');
}
buffer.writeln(';');
}
@@ -544,7 +539,10 @@ class CodeGenerator {
Map<String, JsonType> properties, {
bool callSuper = false,
}) {
buffer.writeIndented('$name.fromMap(Map<String, Object?> obj)');
buffer
..writeIndented('$name.fromMap(')
..write(callSuper ? 'super.obj' : 'Map<String, Object?> obj')
..write(')');
if (properties.isNotEmpty || callSuper) {
buffer
..writeln(':')
@@ -570,7 +568,7 @@ class CodeGenerator {
if (!isFirst) {
buffer.writeln(',');
}
buffer.writeIndented('super.fromMap(obj)');
buffer.writeIndented('super.fromMap()');
}
buffer.outdent();
}