Skip to content

Commit

Permalink
new fix: MAKE_VARIABLE_NOT_FINAL
Browse files Browse the repository at this point in the history
Fixes: #33290

Change-Id: Ice1c11907ed696d9d209145a4cacf2ca250c17c6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115273
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
pq authored and commit-bot@chromium.org committed Sep 4, 2019
1 parent 4bd13a7 commit ec62b1a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
34 changes: 34 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -2647,6 +2650,37 @@ class FixProcessor {
}
}

Future<void> _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<void> _addFix_moveTypeArgumentsToClass() async {
if (coveredNode is TypeArgumentList) {
TypeArgumentList typeArguments = coveredNode;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
''');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ec62b1a

Please sign in to comment.