Skip to content

Commit

Permalink
CreateConstructorForFinalFields to handle NNBD and super_parameters
Browse files Browse the repository at this point in the history
Fixes #45812

Change-Id: I8d0f9677cbc9c7628dc9068c83409a1145638e0d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/239300
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
asashour authored and Commit Bot committed Mar 30, 2022
1 parent 06e0672 commit d2d83df
Show file tree
Hide file tree
Showing 2 changed files with 298 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server/src/services/correction/util.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_dart.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';

class CreateConstructorForFinalFields extends CorrectionProducer {
@override
FixKind get fixKind => DartFixKind.CREATE_CONSTRUCTOR_FOR_FINAL_FIELDS;

bool get _isNonNullable => unit.featureSet.isEnabled(Feature.non_nullable);

@override
Future<void> compute(ChangeBuilder builder) async {
if (node is! SimpleIdentifier || node.parent is! VariableDeclaration) {
Expand All @@ -33,15 +34,12 @@ class CreateConstructorForFinalFields extends CorrectionProducer {
return;
}

// prepare names of uninitialized final fields
var fieldNames = <String>[];
var variableLists = <VariableDeclarationList>[];
for (var member in classDeclaration.members) {
if (member is FieldDeclaration) {
var variableList = member.fields;
if (variableList.isFinal && !variableList.isLate) {
fieldNames.addAll(variableList.variables
.where((v) => v.initializer == null)
.map((v) => v.name.name));
variableLists.add(variableList);
}
}
}
Expand All @@ -59,41 +57,22 @@ class CreateConstructorForFinalFields extends CorrectionProducer {
if (keyClass == null) {
return;
}
await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(targetLocation.offset, (builder) {
builder.write(targetLocation.prefix);
builder.write('const ');
builder.write(className);
builder.write('({');
builder.writeType(
keyClass.instantiate(
typeArguments: const [],
nullabilitySuffix: _isNonNullable
? NullabilitySuffix.question
: NullabilitySuffix.star,
),
);
builder.write(' key');

var childrenFields = <String>[];
for (var fieldName in fieldNames) {
if (fieldName == 'child' || fieldName == 'children') {
childrenFields.add(fieldName);
continue;
}
builder.write(', this.');
builder.write(fieldName);
}
for (var fieldName in childrenFields) {
builder.write(', this.');
builder.write(fieldName);
}

builder.write('}) : super(key: key);');
builder.write(targetLocation.suffix);
});
});

if (unit.featureSet.isEnabled(Feature.super_parameters)) {
await _withSuperParameters(
builder, targetLocation, className, variableLists);
} else {
await _withoutSuperParameters(
builder, targetLocation, className, keyClass, variableLists);
}
} else {
var fieldNames = <String>[];
for (var variableList in variableLists) {
fieldNames.addAll(variableList.variables
.where((v) => v.initializer == null)
.map((v) => v.name.name));
}

await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(targetLocation.offset, (builder) {
builder.write(targetLocation.prefix);
Expand All @@ -105,6 +84,96 @@ class CreateConstructorForFinalFields extends CorrectionProducer {
}
}

Future<void> _withoutSuperParameters(
ChangeBuilder builder,
ClassMemberLocation targetLocation,
String className,
ClassElement keyClass,
List<VariableDeclarationList> variableLists) async {
var isNonNullable = unit.featureSet.isEnabled(Feature.non_nullable);
await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(targetLocation.offset, (builder) {
builder.write(targetLocation.prefix);
builder.write('const ');
builder.write(className);
builder.write('({');
builder.writeType(
keyClass.instantiate(
typeArguments: const [],
nullabilitySuffix: isNonNullable
? NullabilitySuffix.question
: NullabilitySuffix.star,
),
);
builder.write(' key');

_writeParameters(builder, variableLists, isNonNullable);

builder.write('}) : super(key: key);');
builder.write(targetLocation.suffix);
});
});
}

Future<void> _withSuperParameters(
ChangeBuilder builder,
ClassMemberLocation targetLocation,
String className,
List<VariableDeclarationList> variableLists) async {
await builder.addDartFileEdit(file, (builder) {
builder.addInsertion(targetLocation.offset, (builder) {
builder.write(targetLocation.prefix);
builder.write('const ');
builder.write(className);
builder.write('({');
builder.write('super.key');

_writeParameters(builder, variableLists, true);

builder.write('});');
builder.write(targetLocation.suffix);
});
});
}

void _writeParameters(DartEditBuilder builder,
List<VariableDeclarationList> variableLists, bool isNonNullable) {
var childrenFields = <String>[];
var childrenNullables = <bool>[];
for (var variableList in variableLists) {
var fieldNames = variableList.variables
.where((v) => v.initializer == null)
.map((v) => v.name.name);

for (var fieldName in fieldNames) {
if (fieldName == 'child' || fieldName == 'children') {
childrenFields.add(fieldName);
childrenNullables.add(variableList.type?.type?.nullabilitySuffix ==
NullabilitySuffix.question);
continue;
}
builder.write(', ');
if (isNonNullable &&
variableList.type?.type?.nullabilitySuffix !=
NullabilitySuffix.question) {
builder.write('required ');
}
builder.write('this.');
builder.write(fieldName);
}
}
for (var i = 0; i < childrenFields.length; i++) {
var fieldName = childrenFields[i];
var nullableField = childrenNullables[i];
builder.write(', ');
if (isNonNullable && !nullableField) {
builder.write('required ');
}
builder.write('this.');
builder.write(fieldName);
}
}

/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static CreateConstructorForFinalFields newInstance() =>
CreateConstructorForFinalFields();
Expand Down
Loading

0 comments on commit d2d83df

Please sign in to comment.