Skip to content

Commit 2bb6076

Browse files
bwilkersoncommit-bot@chromium.org
authored andcommitted
Fix remove_type_annotation fix/assist to work with for loops (issue 39628)
Change-Id: Ie212ea369cb2c0d4d8b37b50928d3935fad6a445 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127101 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
1 parent c52acad commit 2bb6076

File tree

2 files changed

+69
-27
lines changed

2 files changed

+69
-27
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,8 +1094,28 @@ abstract class BaseProcessor {
10941094
VariableDeclarationList declarationList =
10951095
node.thisOrAncestorOfType<VariableDeclarationList>();
10961096
if (declarationList == null) {
1097-
_coverageMarker();
1098-
return null;
1097+
DeclaredIdentifier declaration = node.thisOrAncestorOfType();
1098+
if (declaration == null) {
1099+
_coverageMarker();
1100+
return null;
1101+
}
1102+
TypeAnnotation typeNode = declaration.type;
1103+
if (typeNode == null) {
1104+
_coverageMarker();
1105+
return null;
1106+
}
1107+
Token keyword = declaration.keyword;
1108+
var variableName = declaration.identifier;
1109+
var changeBuilder = _newDartChangeBuilder();
1110+
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
1111+
SourceRange typeRange = range.startStart(typeNode, variableName);
1112+
if (keyword != null && keyword.lexeme != 'var') {
1113+
builder.addSimpleReplacement(typeRange, '');
1114+
} else {
1115+
builder.addSimpleReplacement(typeRange, 'var ');
1116+
}
1117+
});
1118+
return changeBuilder;
10991119
}
11001120
// we need a type
11011121
TypeAnnotation typeNode = declarationList.type;

pkg/analysis_server/test/src/services/correction/assist/remove_type_annotation_test.dart

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,6 @@ class A {
5454
await assertNoAssistAt('v;');
5555
}
5656

57-
test_localVariable_noInitializer() async {
58-
await resolveTestUnit('''
59-
main() {
60-
int v;
61-
}
62-
''');
63-
await assertNoAssistAt('v;');
64-
}
65-
66-
test_localVariable_onInitializer() async {
67-
await resolveTestUnit('''
68-
main() {
69-
final int v = 1;
70-
}
71-
''');
72-
await assertNoAssistAt('1;');
73-
}
74-
7557
test_localVariable() async {
7658
await resolveTestUnit('''
7759
main() {
@@ -111,20 +93,44 @@ main() {
11193
''');
11294
}
11395

114-
test_topLevelVariable_noInitializer() async {
115-
verifyNoTestUnitErrors = false;
96+
test_localVariable_noInitializer() async {
11697
await resolveTestUnit('''
117-
int v;
98+
main() {
99+
int v;
100+
}
118101
''');
119102
await assertNoAssistAt('v;');
120103
}
121104

122-
test_topLevelVariable_syntheticName() async {
123-
verifyNoTestUnitErrors = false;
105+
test_localVariable_onInitializer() async {
124106
await resolveTestUnit('''
125-
MyType
107+
main() {
108+
final int v = 1;
109+
}
126110
''');
127-
await assertNoAssistAt('MyType');
111+
await assertNoAssistAt('1;');
112+
}
113+
114+
test_loopVariable() async {
115+
await resolveTestUnit('''
116+
main() {
117+
for(int i = 0; i < 3; i++) {}
118+
}
119+
''');
120+
await assertHasAssistAt('int ', '''
121+
main() {
122+
for(var i = 0; i < 3; i++) {}
123+
}
124+
''');
125+
}
126+
127+
test_loopVariable_noType() async {
128+
await resolveTestUnit('''
129+
main() {
130+
for(var i = 0; i < 3; i++) {}
131+
}
132+
''');
133+
await assertNoAssistAt('var ');
128134
}
129135

130136
test_topLevelVariable() async {
@@ -144,4 +150,20 @@ final int V = 1;
144150
final V = 1;
145151
''');
146152
}
153+
154+
test_topLevelVariable_noInitializer() async {
155+
verifyNoTestUnitErrors = false;
156+
await resolveTestUnit('''
157+
int v;
158+
''');
159+
await assertNoAssistAt('v;');
160+
}
161+
162+
test_topLevelVariable_syntheticName() async {
163+
verifyNoTestUnitErrors = false;
164+
await resolveTestUnit('''
165+
MyType
166+
''');
167+
await assertNoAssistAt('MyType');
168+
}
147169
}

0 commit comments

Comments
 (0)