From ec62b1a242c3391f76b9663e67d0d4555bc683aa Mon Sep 17 00:00:00 2001 From: pq Date: Wed, 4 Sep 2019 13:14:22 +0000 Subject: [PATCH] new fix: MAKE_VARIABLE_NOT_FINAL Fixes: https://github.com/dart-lang/sdk/issues/33290 Change-Id: Ice1c11907ed696d9d209145a4cacf2ca250c17c6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115273 Commit-Queue: Phil Quitslund Reviewed-by: Brian Wilkerson --- .../lib/src/services/correction/fix.dart | 2 + .../src/services/correction/fix_internal.dart | 34 ++++++++++++ .../fix/make_variable_not_final_test.dart | 55 +++++++++++++++++++ .../src/services/correction/fix/test_all.dart | 2 + 4 files changed, 93 insertions(+) create mode 100644 pkg/analysis_server/test/src/services/correction/fix/make_variable_not_final_test.dart diff --git a/pkg/analysis_server/lib/src/services/correction/fix.dart b/pkg/analysis_server/lib/src/services/correction/fix.dart index f6369f81d7cd..9970181b8ca2 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix.dart @@ -238,6 +238,8 @@ class DartFixKind { 'MOVE_TYPE_ARGUMENTS_TO_CLASS', 50, "Move type arguments to after class name"); + static const MAKE_VARIABLE_NOT_FINAL = const FixKind( + 'MAKE_VARIABLE_NOT_FINAL', 50, "Make variable '{0}' not final"); static const REMOVE_ANNOTATION = const FixKind('REMOVE_ANNOTATION', 50, "Remove the '{0}' annotation"); static const REMOVE_AWAIT = const FixKind('REMOVE_AWAIT', 50, "Remove await"); 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 5894571c2b43..ba5ad94acf61 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart @@ -412,6 +412,9 @@ class FixProcessor { if (errorCode == StaticWarningCode.ASSIGNMENT_TO_FINAL) { await _addFix_makeFieldNotFinal(); } + if (errorCode == StaticWarningCode.ASSIGNMENT_TO_FINAL_LOCAL) { + await _addFix_makeVariableNotFinal(); + } if (errorCode == StaticWarningCode.CONCRETE_CLASS_WITH_ABSTRACT_MEMBER) { await _addFix_makeEnclosingClassAbstract(); } @@ -2647,6 +2650,37 @@ class FixProcessor { } } + Future _addFix_makeVariableNotFinal() async { + AstNode node = this.node; + if (node is SimpleIdentifier && + node.staticElement is LocalVariableElement) { + LocalVariableElement variable = node.staticElement; + var id = NodeLocator(variable.nameOffset).searchWithin(unit); + var decl = id?.parent; + if (decl is VariableDeclaration && + decl.parent is VariableDeclarationList) { + VariableDeclarationList declarationList = decl.parent; + var keywordToken = declarationList.keyword; + if (declarationList.variables.length == 1 && + keywordToken.keyword == Keyword.FINAL) { + var changeBuilder = _newDartChangeBuilder(); + await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) { + if (declarationList.type != null) { + builder.addDeletion( + range.startStart(keywordToken, declarationList.type)); + } else { + builder.addSimpleReplacement(range.token(keywordToken), 'var'); + } + }); + declarationList.variables[0].name.name; + var varName = declarationList.variables[0].name.name; + _addFixFromBuilder(changeBuilder, DartFixKind.MAKE_VARIABLE_NOT_FINAL, + args: [varName]); + } + } + } + } + Future _addFix_moveTypeArgumentsToClass() async { if (coveredNode is TypeArgumentList) { TypeArgumentList typeArguments = coveredNode; diff --git a/pkg/analysis_server/test/src/services/correction/fix/make_variable_not_final_test.dart b/pkg/analysis_server/test/src/services/correction/fix/make_variable_not_final_test.dart new file mode 100644 index 000000000000..3c210a04f87f --- /dev/null +++ b/pkg/analysis_server/test/src/services/correction/fix/make_variable_not_final_test.dart @@ -0,0 +1,55 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:analysis_server/src/services/correction/fix.dart'; +import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import 'fix_processor.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(MakeVariableNotFinalTest); + }); +} + +@reflectiveTest +class MakeVariableNotFinalTest extends FixProcessorTest { + @override + FixKind get kind => DartFixKind.MAKE_VARIABLE_NOT_FINAL; + + test_hasType() async { + await resolveTestUnit(''' +main() { + final int fff = 1; + fff = 2; + print(fff); +} +'''); + await assertHasFix(''' +main() { + int fff = 1; + fff = 2; + print(fff); +} +'''); + } + + test_noType() async { + await resolveTestUnit(''' +main() { + final fff = 1; + fff = 2; + print(fff); +} +'''); + await assertHasFix(''' +main() { + var fff = 1; + fff = 2; + print(fff); +} +'''); + } +} diff --git a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart index c49237b1e6d5..04f4247344fc 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart @@ -59,6 +59,7 @@ import 'insert_semicolon_test.dart' as insert_semicolon; import 'make_class_abstract_test.dart' as make_class_abstract; import 'make_field_not_final_test.dart' as make_field_not_final; import 'make_final_test.dart' as make_final; +import 'make_variable_not_final_test.dart' as make_variable_not_final; import 'move_type_arguments_to_class_test.dart' as move_type_arguments_to_class; import 'remove_annotation_test.dart' as remove_annotation; import 'remove_await_test.dart' as remove_await; @@ -161,6 +162,7 @@ main() { make_class_abstract.main(); make_field_not_final.main(); make_final.main(); + make_variable_not_final.main(); move_type_arguments_to_class.main(); remove_annotation.main(); remove_await.main();