Skip to content

Commit ec62b1a

Browse files
pqcommit-bot@chromium.org
authored andcommitted
new fix: MAKE_VARIABLE_NOT_FINAL
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>
1 parent 4bd13a7 commit ec62b1a

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

pkg/analysis_server/lib/src/services/correction/fix.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ class DartFixKind {
238238
'MOVE_TYPE_ARGUMENTS_TO_CLASS',
239239
50,
240240
"Move type arguments to after class name");
241+
static const MAKE_VARIABLE_NOT_FINAL = const FixKind(
242+
'MAKE_VARIABLE_NOT_FINAL', 50, "Make variable '{0}' not final");
241243
static const REMOVE_ANNOTATION =
242244
const FixKind('REMOVE_ANNOTATION', 50, "Remove the '{0}' annotation");
243245
static const REMOVE_AWAIT = const FixKind('REMOVE_AWAIT', 50, "Remove await");

pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,9 @@ class FixProcessor {
412412
if (errorCode == StaticWarningCode.ASSIGNMENT_TO_FINAL) {
413413
await _addFix_makeFieldNotFinal();
414414
}
415+
if (errorCode == StaticWarningCode.ASSIGNMENT_TO_FINAL_LOCAL) {
416+
await _addFix_makeVariableNotFinal();
417+
}
415418
if (errorCode == StaticWarningCode.CONCRETE_CLASS_WITH_ABSTRACT_MEMBER) {
416419
await _addFix_makeEnclosingClassAbstract();
417420
}
@@ -2647,6 +2650,37 @@ class FixProcessor {
26472650
}
26482651
}
26492652

2653+
Future<void> _addFix_makeVariableNotFinal() async {
2654+
AstNode node = this.node;
2655+
if (node is SimpleIdentifier &&
2656+
node.staticElement is LocalVariableElement) {
2657+
LocalVariableElement variable = node.staticElement;
2658+
var id = NodeLocator(variable.nameOffset).searchWithin(unit);
2659+
var decl = id?.parent;
2660+
if (decl is VariableDeclaration &&
2661+
decl.parent is VariableDeclarationList) {
2662+
VariableDeclarationList declarationList = decl.parent;
2663+
var keywordToken = declarationList.keyword;
2664+
if (declarationList.variables.length == 1 &&
2665+
keywordToken.keyword == Keyword.FINAL) {
2666+
var changeBuilder = _newDartChangeBuilder();
2667+
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
2668+
if (declarationList.type != null) {
2669+
builder.addDeletion(
2670+
range.startStart(keywordToken, declarationList.type));
2671+
} else {
2672+
builder.addSimpleReplacement(range.token(keywordToken), 'var');
2673+
}
2674+
});
2675+
declarationList.variables[0].name.name;
2676+
var varName = declarationList.variables[0].name.name;
2677+
_addFixFromBuilder(changeBuilder, DartFixKind.MAKE_VARIABLE_NOT_FINAL,
2678+
args: [varName]);
2679+
}
2680+
}
2681+
}
2682+
}
2683+
26502684
Future<void> _addFix_moveTypeArgumentsToClass() async {
26512685
if (coveredNode is TypeArgumentList) {
26522686
TypeArgumentList typeArguments = coveredNode;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/services/correction/fix.dart';
6+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
7+
import 'package:test_reflective_loader/test_reflective_loader.dart';
8+
9+
import 'fix_processor.dart';
10+
11+
main() {
12+
defineReflectiveSuite(() {
13+
defineReflectiveTests(MakeVariableNotFinalTest);
14+
});
15+
}
16+
17+
@reflectiveTest
18+
class MakeVariableNotFinalTest extends FixProcessorTest {
19+
@override
20+
FixKind get kind => DartFixKind.MAKE_VARIABLE_NOT_FINAL;
21+
22+
test_hasType() async {
23+
await resolveTestUnit('''
24+
main() {
25+
final int fff = 1;
26+
fff = 2;
27+
print(fff);
28+
}
29+
''');
30+
await assertHasFix('''
31+
main() {
32+
int fff = 1;
33+
fff = 2;
34+
print(fff);
35+
}
36+
''');
37+
}
38+
39+
test_noType() async {
40+
await resolveTestUnit('''
41+
main() {
42+
final fff = 1;
43+
fff = 2;
44+
print(fff);
45+
}
46+
''');
47+
await assertHasFix('''
48+
main() {
49+
var fff = 1;
50+
fff = 2;
51+
print(fff);
52+
}
53+
''');
54+
}
55+
}

pkg/analysis_server/test/src/services/correction/fix/test_all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import 'insert_semicolon_test.dart' as insert_semicolon;
5959
import 'make_class_abstract_test.dart' as make_class_abstract;
6060
import 'make_field_not_final_test.dart' as make_field_not_final;
6161
import 'make_final_test.dart' as make_final;
62+
import 'make_variable_not_final_test.dart' as make_variable_not_final;
6263
import 'move_type_arguments_to_class_test.dart' as move_type_arguments_to_class;
6364
import 'remove_annotation_test.dart' as remove_annotation;
6465
import 'remove_await_test.dart' as remove_await;
@@ -161,6 +162,7 @@ main() {
161162
make_class_abstract.main();
162163
make_field_not_final.main();
163164
make_final.main();
165+
make_variable_not_final.main();
164166
move_type_arguments_to_class.main();
165167
remove_annotation.main();
166168
remove_await.main();

0 commit comments

Comments
 (0)