Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Unified Diff: pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Issue 1018433002: Quick Fix for adding field formal parameters of not initialized final fields. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analysis_server/lib/src/services/correction/fix_internal.dart
diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
index 5ce50a57cdd169fb2e811c37441de9b6b71ea04e..a629ad52eb6f0fadb2971edef5a8e6a08d5a9f43 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
@@ -202,6 +202,12 @@ class FixProcessor {
if (errorCode == StaticWarningCode.FINAL_NOT_INITIALIZED) {
_addFix_createConstructor_forUninitializedFinalFields();
}
+ if (errorCode == StaticWarningCode.FINAL_NOT_INITIALIZED_CONSTRUCTOR_1 ||
+ errorCode == StaticWarningCode.FINAL_NOT_INITIALIZED_CONSTRUCTOR_2 ||
+ errorCode ==
+ StaticWarningCode.FINAL_NOT_INITIALIZED_CONSTRUCTOR_3_PLUS) {
+ _addFix_updateConstructor_forUninitializedFinalFields();
+ }
if (errorCode == StaticWarningCode.UNDEFINED_IDENTIFIER) {
bool isAsync = _addFix_addAsync();
if (!isAsync) {
@@ -387,6 +393,46 @@ class FixProcessor {
_addFix(FixKind.CREATE_CONSTRUCTOR_FOR_FINAL_FIELDS, []);
}
+ /**
+ * Here we handle cases when a constructors does not initialize all of the
+ * final fields.
+ */
+ void _addFix_updateConstructor_forUninitializedFinalFields() {
+ if (node is! SimpleIdentifier || node.parent is! ConstructorDeclaration) {
+ return;
+ }
+ ConstructorDeclaration constructor = node.parent;
+ // add these fields
+ List<FieldElement> fields =
+ error.getProperty(ErrorProperty.NOT_INITIALIZED_FIELDS);
+ if (fields != null) {
+ // prepare new parameters code
+ fields.sort((a, b) => a.nameOffset - b.nameOffset);
+ String fieldParametersCode =
+ fields.map((field) => 'this.${field.name}').join(', ');
+ // prepare the last required parameter
+ FormalParameter lastRequiredParameter;
+ List<FormalParameter> parameters = constructor.parameters.parameters;
+ for (FormalParameter parameter in parameters) {
+ if (parameter.kind == ParameterKind.REQUIRED) {
+ lastRequiredParameter = parameter;
+ }
+ }
+ // append new field formal initializers
+ if (lastRequiredParameter != null) {
+ _addInsertEdit(lastRequiredParameter.end, ', $fieldParametersCode');
+ } else {
+ int offset = constructor.parameters.leftParenthesis.end;
+ if (parameters.isNotEmpty) {
+ fieldParametersCode += ', ';
+ }
+ _addInsertEdit(offset, fieldParametersCode);
+ }
+ // add proposal
+ _addFix(FixKind.ADD_FIELD_FORMAL_PARAMETERS, []);
+ }
+ }
+
void _addFix_createConstructor_insteadOfSyntheticDefault() {
TypeName typeName = null;
ConstructorName constructorName = null;
« no previous file with comments | « pkg/analysis_server/lib/src/services/correction/fix.dart ('k') | pkg/analysis_server/test/services/correction/fix_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698