Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6e1161e

Browse files
author
Dart CI
committed
Version 2.12.0-176.0.dev
Merge commit '35f9425b6de92c151c4cf195ca930c59d1208398' into 'dev'
2 parents f5b451f + 35f9425 commit 6e1161e

File tree

4 files changed

+119
-18
lines changed

4 files changed

+119
-18
lines changed

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

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'package:analysis_server/src/services/correction/dart/abstract_producer.d
66
import 'package:analysis_server/src/services/correction/fix.dart';
77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/ast/token.dart';
9+
import 'package:analyzer/dart/element/element.dart';
10+
import 'package:analyzer/src/dart/ast/extensions.dart';
911
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1012
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1113

@@ -18,27 +20,59 @@ class AddLate extends CorrectionProducer {
1820
if (!libraryElement.isNonNullableByDefault) {
1921
return;
2022
}
21-
if (node is SimpleIdentifier &&
22-
node.parent is VariableDeclaration &&
23-
node.parent.parent is VariableDeclarationList) {
24-
var list = node.parent.parent as VariableDeclarationList;
25-
if (!list.isLate) {
26-
if (list.type == null) {
27-
var keyword = list.keyword;
28-
if (keyword == null) {
29-
await _insertAt(builder, list.variables[0].offset);
30-
// TODO(brianwilkerson) Consider converting this into an assist and
31-
// expand it to support converting `var` to `late` as well as
32-
// working anywhere a non-late local variable or field is selected.
23+
var node = this.node;
24+
if (node is SimpleIdentifier) {
25+
if (node.parent is VariableDeclaration &&
26+
node.parent.parent is VariableDeclarationList) {
27+
var list = node.parent.parent as VariableDeclarationList;
28+
if (!list.isLate) {
29+
if (list.type == null) {
30+
var keyword = list.keyword;
31+
if (keyword == null) {
32+
await _insertAt(builder, list.variables[0].offset);
33+
// TODO(brianwilkerson) Consider converting this into an assist and
34+
// expand it to support converting `var` to `late` as well as
35+
// working anywhere a non-late local variable or field is selected.
3336
// } else if (keyword.type == Keyword.VAR) {
3437
// builder.addFileEdit(file, (builder) {
3538
// builder.addSimpleReplacement(range.token(keyword), 'late');
3639
// });
37-
} else if (keyword.type != Keyword.CONST) {
38-
await _insertAt(builder, list.variables[0].offset);
40+
} else if (keyword.type != Keyword.CONST) {
41+
await _insertAt(builder, list.variables[0].offset);
42+
}
43+
} else {
44+
var keyword = list.keyword;
45+
if (keyword != null) {
46+
await _insertAt(builder, keyword.offset);
47+
} else {
48+
var type = list.type;
49+
if (type != null) {
50+
await _insertAt(builder, type.offset);
51+
}
52+
}
53+
}
54+
}
55+
} else {
56+
var getter = node.writeOrReadElement;
57+
if (getter is PropertyAccessorElement &&
58+
getter.isGetter &&
59+
getter.isSynthetic &&
60+
!getter.variable.isSynthetic &&
61+
getter.variable.setter == null &&
62+
getter.enclosingElement is ClassElement) {
63+
var declarationResult =
64+
await sessionHelper.getElementDeclaration(getter.variable);
65+
var variable = declarationResult.node;
66+
if (variable is VariableDeclaration &&
67+
variable.parent is VariableDeclarationList &&
68+
variable.parent.parent is FieldDeclaration) {
69+
VariableDeclarationList declarationList = variable.parent;
70+
var keywordToken = declarationList.keyword;
71+
if (declarationList.variables.length == 1 &&
72+
keywordToken.keyword == Keyword.FINAL) {
73+
await _insertAt(builder, keywordToken.offset);
74+
}
3975
}
40-
} else {
41-
await _insertAt(builder, list.type.offset);
4276
}
4377
}
4478
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ class FixProcessor extends BaseProcessor {
632632
static const Map<ErrorCode, List<ProducerGenerator>> nonLintProducerMap = {
633633
CompileTimeErrorCode.ASSIGNMENT_TO_FINAL: [
634634
MakeFieldNotFinal.newInstance,
635+
AddLate.newInstance,
635636
],
636637
CompileTimeErrorCode.ASSIGNMENT_TO_FINAL_LOCAL: [
637638
MakeVariableNotFinal.newInstance,

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/src/services/correction/fix.dart';
6+
import 'package:analyzer/src/error/codes.dart';
67
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
78
import 'package:test_reflective_loader/test_reflective_loader.dart';
89

@@ -44,11 +45,76 @@ class C {
4445
''');
4546
await assertHasFix('''
4647
class C {
47-
final late String s;
48+
late final String s;
4849
}
4950
''');
5051
}
5152

53+
Future<void> test_withFinalAssignedInConstructor() async {
54+
await resolveTestCode('''
55+
class C {
56+
final String s;
57+
C() {
58+
s = '';
59+
}
60+
}
61+
''');
62+
await assertHasFix('''
63+
class C {
64+
late final String s;
65+
C() {
66+
s = '';
67+
}
68+
}
69+
''',
70+
errorFilter: (error) =>
71+
error.errorCode == CompileTimeErrorCode.ASSIGNMENT_TO_FINAL);
72+
}
73+
74+
Future<void> test_withFinalAssignedInLibrary() async {
75+
await resolveTestCode('''
76+
class C {
77+
final String s;
78+
}
79+
80+
void f(C c) {
81+
c.s = '';
82+
}
83+
''');
84+
await assertHasFix('''
85+
class C {
86+
late final String s;
87+
}
88+
89+
void f(C c) {
90+
c.s = '';
91+
}
92+
''',
93+
errorFilter: (error) =>
94+
error.errorCode == CompileTimeErrorCode.ASSIGNMENT_TO_FINAL);
95+
}
96+
97+
Future<void> test_withFinalStaticAssignedInConstructor() async {
98+
await resolveTestCode('''
99+
class C {
100+
static final String s;
101+
C() {
102+
s = '';
103+
}
104+
}
105+
''');
106+
await assertHasFix('''
107+
class C {
108+
static late final String s;
109+
C() {
110+
s = '';
111+
}
112+
}
113+
''',
114+
errorFilter: (error) =>
115+
error.errorCode == CompileTimeErrorCode.ASSIGNMENT_TO_FINAL);
116+
}
117+
52118
Future<void> test_withLate() async {
53119
await resolveTestCode('''
54120
class C {

tools/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ CHANNEL dev
2727
MAJOR 2
2828
MINOR 12
2929
PATCH 0
30-
PRERELEASE 175
30+
PRERELEASE 176
3131
PRERELEASE_PATCH 0

0 commit comments

Comments
 (0)