@@ -6,6 +6,8 @@ import 'package:analysis_server/src/services/correction/dart/abstract_producer.d
66import 'package:analysis_server/src/services/correction/fix.dart' ;
77import 'package:analyzer/dart/ast/ast.dart' ;
88import 'package:analyzer/dart/ast/token.dart' ;
9+ import 'package:analyzer/dart/element/element.dart' ;
10+ import 'package:analyzer/src/dart/ast/extensions.dart' ;
911import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart' ;
1012import '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 }
0 commit comments