@@ -115,31 +115,10 @@ class ConvertAddAllToSpread extends ResolvedCorrectionProducer {
115115 return ;
116116 }
117117
118- bool isEmptyListLiteral (Expression expression) =>
119- expression is ListLiteral && expression.elements.isEmpty;
120-
121118 var argument = invocation.argumentList.arguments[0 ];
122119 assert (argument is ListLiteral == _isInlineInvocation);
123-
124- String ? elementText;
125- if (argument is BinaryExpression &&
126- argument.operator .type == TokenType .QUESTION_QUESTION ) {
127- var right = argument.rightOperand;
128- if (isEmptyListLiteral (right)) {
129- // ..addAll(things ?? const [])
130- // ..addAll(things ?? [])
131- elementText = '...?${utils .getNodeText (argument .leftOperand )}' ;
132- }
133- } else if (argument is ConditionalExpression ) {
134- var elseExpression = argument.elseExpression;
135- if (isEmptyListLiteral (elseExpression)) {
136- // ..addAll(condition ? things : const [])
137- // ..addAll(condition ? things : [])
138- var conditionText = utils.getNodeText (argument.condition);
139- var thenText = utils.getNodeText (argument.thenExpression);
140- elementText = 'if ($conditionText ) ...$thenText ' ;
141- }
142- } else if (argument is ListLiteral ) {
120+ String elementText;
121+ if (argument is ListLiteral ) {
143122 // ..addAll([ ... ])
144123 var elements = argument.elements;
145124 if (elements.isEmpty) {
@@ -152,25 +131,51 @@ class ConvertAddAllToSpread extends ResolvedCorrectionProducer {
152131 var endOffset = elements.last.end;
153132 elementText = utils.getText (startOffset, endOffset - startOffset);
154133 _args = ['addAll' ];
134+ } else {
135+ elementText = _computeElementText (argument);
155136 }
156- elementText ?? = '...${utils .getNodeText (argument )}' ;
157137
158- var elementText_final = elementText;
159138 await builder.addDartFileEdit (file, (builder) {
160139 if (targetList.elements.isNotEmpty) {
161140 // ['a']..addAll(['b', 'c']);
162141 builder.addSimpleInsertion (
163142 targetList.elements.last.end,
164- ', $elementText_final ' ,
143+ ', $elementText ' ,
165144 );
166145 } else {
167146 // []..addAll(['b', 'c']);
168- builder.addSimpleInsertion (
169- targetList.leftBracket.end,
170- elementText_final,
171- );
147+ builder.addSimpleInsertion (targetList.leftBracket.end, elementText);
172148 }
173149 builder.addDeletion (range.node (invocation));
174150 });
175151 }
152+
153+ String _computeElementText (Expression argument) {
154+ if (argument is BinaryExpression &&
155+ argument.operator .type == TokenType .QUESTION_QUESTION ) {
156+ var right = argument.rightOperand;
157+ if (right.isEmptyListLiteral) {
158+ // ..addAll(things ?? const [])
159+ // ..addAll(things ?? [])
160+ return '...?${utils .getNodeText (argument .leftOperand )}' ;
161+ }
162+ } else if (argument is ConditionalExpression ) {
163+ var elseExpression = argument.elseExpression;
164+ if (elseExpression.isEmptyListLiteral) {
165+ // ..addAll(condition ? things : const [])
166+ // ..addAll(condition ? things : [])
167+ var conditionText = utils.getNodeText (argument.condition);
168+ var thenText = utils.getNodeText (argument.thenExpression);
169+ return 'if ($conditionText ) ...$thenText ' ;
170+ }
171+ }
172+ return '...${utils .getNodeText (argument )}' ;
173+ }
174+ }
175+
176+ extension on Expression {
177+ bool get isEmptyListLiteral {
178+ var self = this ;
179+ return self is ListLiteral && self.elements.isEmpty;
180+ }
176181}
0 commit comments