Skip to content

Commit f2776df

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes no parenthesis when inlining variables in literal expression
Fixes: #60761 Change-Id: Ide0f446712e5cda235e82bf69030bdc24c1441fe Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/430001 Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 9482f95 commit f2776df

File tree

2 files changed

+119
-29
lines changed

2 files changed

+119
-29
lines changed

pkg/analysis_server/lib/src/services/refactoring/legacy/inline_local.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:analyzer/dart/ast/ast.dart';
1414
import 'package:analyzer/dart/ast/token.dart';
1515
import 'package:analyzer/dart/element/element.dart';
1616
import 'package:analyzer/src/dart/analysis/session_helper.dart';
17+
import 'package:analyzer/src/dart/ast/ast.dart';
1718
import 'package:analyzer/src/generated/java_core.dart';
1819
import 'package:analyzer_plugin/utilities/range_factory.dart';
1920

@@ -235,6 +236,15 @@ class InlineLocalRefactoringImpl extends RefactoringImpl
235236
return false;
236237
}
237238

239+
switch (parent) {
240+
case RecordLiteral():
241+
case ListLiteral():
242+
case SetOrMapLiteral():
243+
return false;
244+
default:
245+
break;
246+
}
247+
238248
// check precedence
239249
var initPrecedence = getExpressionPrecedence(init);
240250
if (initPrecedence < getExpressionParentPrecedence(node)) {

pkg/analysis_server/test/services/refactoring/legacy/inline_local_test.dart

Lines changed: 109 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void f() {
125125
''');
126126
_createRefactoring('test =');
127127
// validate change
128-
return assertSuccessfulRefactoring(r'''
128+
await assertSuccessfulRefactoring(r'''
129129
class A {
130130
foo() {}
131131
bar() {}
@@ -149,7 +149,7 @@ void f() {
149149
''');
150150
_createRefactoring('test =');
151151
// validate change
152-
return assertSuccessfulRefactoring(r'''
152+
await assertSuccessfulRefactoring(r'''
153153
class A {
154154
foo() {}
155155
bar() {}
@@ -174,7 +174,7 @@ void f(int p) {
174174
''');
175175
_createRefactoring('test =');
176176
// validate change
177-
return assertSuccessfulRefactoring('''
177+
await assertSuccessfulRefactoring('''
178178
// @dart=2.19
179179
void f(int p) {
180180
switch (p) {
@@ -198,7 +198,7 @@ void f(Object? x) {
198198
''');
199199
_createRefactoring('test =');
200200
// validate change
201-
return assertSuccessfulRefactoring('''
201+
await assertSuccessfulRefactoring('''
202202
void f(Object? x) {
203203
switch (x) {
204204
case _?:
@@ -225,7 +225,7 @@ void f() {
225225
''');
226226
_createRefactoring('v =');
227227
// validate change
228-
return assertSuccessfulRefactoring('''
228+
await assertSuccessfulRefactoring('''
229229
class C {
230230
static final i = 5;
231231
}
@@ -250,7 +250,7 @@ process(x) {}
250250
''');
251251
_createRefactoring('test =');
252252
// validate change
253-
return assertSuccessfulRefactoring(r'''
253+
await assertSuccessfulRefactoring(r'''
254254
void f() {
255255
print('test = ${1 + 2}');
256256
print('test = ${1 + 2}');
@@ -273,7 +273,7 @@ process(x) {}
273273
''');
274274
_createRefactoring('test =');
275275
// validate change
276-
return assertSuccessfulRefactoring(r'''
276+
await assertSuccessfulRefactoring(r'''
277277
void f() {
278278
int foo = 1 + 2;
279279
print('test = $foo');
@@ -293,7 +293,7 @@ void f() {
293293
''');
294294
_createRefactoring('a =');
295295
// validate change
296-
return assertSuccessfulRefactoring(r'''
296+
await assertSuccessfulRefactoring(r'''
297297
void f() {
298298
String b = '${"aaa"} bbb';
299299
}
@@ -309,7 +309,7 @@ void f() {
309309
''');
310310
_createRefactoring('a =');
311311
// validate change
312-
return assertSuccessfulRefactoring(r'''
312+
await assertSuccessfulRefactoring(r'''
313313
void f() {
314314
String b = "aaa bbb";
315315
}
@@ -330,7 +330,7 @@ bbb''';
330330
""");
331331
_createRefactoring('a =');
332332
// validate change
333-
return assertSuccessfulRefactoring(r"""
333+
await assertSuccessfulRefactoring(r"""
334334
void f() {
335335
String b = '''
336336
a
@@ -355,7 +355,7 @@ bbb''';
355355
""");
356356
_createRefactoring('a =');
357357
// validate change
358-
return assertSuccessfulRefactoring(r"""
358+
await assertSuccessfulRefactoring(r"""
359359
void f() {
360360
String b = '''
361361
a
@@ -383,7 +383,7 @@ bbb''';
383383
);
384384
_createRefactoring('a =');
385385
// validate change
386-
return assertSuccessfulRefactoring(
386+
await assertSuccessfulRefactoring(
387387
r"""
388388
void f() {
389389
String b = '''
@@ -406,7 +406,7 @@ void f() {
406406
''');
407407
_createRefactoring('a =');
408408
// validate change
409-
return assertSuccessfulRefactoring(r'''
409+
await assertSuccessfulRefactoring(r'''
410410
void f() {
411411
String b = "${"""aaa"""} bbb";
412412
}
@@ -422,7 +422,7 @@ void f() {
422422
''');
423423
_createRefactoring('a =');
424424
// we don't unwrap raw strings
425-
return assertSuccessfulRefactoring(r'''
425+
await assertSuccessfulRefactoring(r'''
426426
void f() {
427427
String b = '${r'an $ignored interpolation'} bbb';
428428
}
@@ -439,7 +439,7 @@ void f() {
439439
''');
440440
_createRefactoring('a =');
441441
// validate change
442-
return assertSuccessfulRefactoring(r'''
442+
await assertSuccessfulRefactoring(r'''
443443
void f() {
444444
String b = """aaa bbb""";
445445
}
@@ -456,7 +456,7 @@ void f() {
456456
""");
457457
_createRefactoring('a =');
458458
// validate change
459-
return assertSuccessfulRefactoring(r"""
459+
await assertSuccessfulRefactoring(r"""
460460
void f() {
461461
String b = '''aaa bbb''';
462462
}
@@ -472,7 +472,7 @@ void f() {
472472
''');
473473
_createRefactoring('a =');
474474
// validate change
475-
return assertSuccessfulRefactoring(r'''
475+
await assertSuccessfulRefactoring(r'''
476476
void f() {
477477
String b = 'aaa bbb';
478478
}
@@ -489,7 +489,7 @@ void f() {
489489
''');
490490
_createRefactoring('b =');
491491
// validate change
492-
return assertSuccessfulRefactoring(r'''
492+
await assertSuccessfulRefactoring(r'''
493493
void f() {
494494
String a = 'aaa';
495495
String c = '$a bbb ccc';
@@ -509,7 +509,7 @@ void f() {
509509
''');
510510
_createRefactoring('test =');
511511
// validate change
512-
return assertSuccessfulRefactoring('''
512+
await assertSuccessfulRefactoring('''
513513
void f() {
514514
// foo
515515
print(1 + 2);
@@ -518,6 +518,54 @@ void f() {
518518
''');
519519
}
520520

521+
Future<void> test_OK_listLiteral() async {
522+
await indexTestUnit('''
523+
void f() {
524+
var test = 42.isEven;
525+
[0, test, 1];
526+
}
527+
''');
528+
_createRefactoring('test =');
529+
// validate change
530+
await assertSuccessfulRefactoring('''
531+
void f() {
532+
[0, 42.isEven, 1];
533+
}
534+
''');
535+
}
536+
537+
Future<void> test_OK_mapLiteral1() async {
538+
await indexTestUnit('''
539+
void f() {
540+
var test = 42.isEven;
541+
<dynamic, dynamic>{0: test};
542+
}
543+
''');
544+
_createRefactoring('test =');
545+
// validate change
546+
await assertSuccessfulRefactoring('''
547+
void f() {
548+
<dynamic, dynamic>{0: 42.isEven};
549+
}
550+
''');
551+
}
552+
553+
Future<void> test_OK_mapLiteral2() async {
554+
await indexTestUnit('''
555+
void f() {
556+
var test = 42.isEven;
557+
<dynamic, dynamic>{test: 1};
558+
}
559+
''');
560+
_createRefactoring('test =');
561+
// validate change
562+
await assertSuccessfulRefactoring('''
563+
void f() {
564+
<dynamic, dynamic>{42.isEven: 1};
565+
}
566+
''');
567+
}
568+
521569
Future<void> test_OK_noUsages_1() async {
522570
await indexTestUnit('''
523571
void f() {
@@ -527,7 +575,7 @@ void f() {
527575
''');
528576
_createRefactoring('test =');
529577
// validate change
530-
return assertSuccessfulRefactoring('''
578+
await assertSuccessfulRefactoring('''
531579
void f() {
532580
print(0);
533581
}
@@ -542,7 +590,7 @@ void f() {
542590
''');
543591
_createRefactoring('test =');
544592
// validate change
545-
return assertSuccessfulRefactoring('''
593+
await assertSuccessfulRefactoring('''
546594
void f() {
547595
}
548596
''');
@@ -557,7 +605,7 @@ void f() {
557605
''');
558606
_createRefactoring('test =');
559607
// validate change
560-
return assertSuccessfulRefactoring('''
608+
await assertSuccessfulRefactoring('''
561609
void f() {
562610
print(1 + 2);
563611
}
@@ -574,7 +622,7 @@ void f() {
574622
''');
575623
_createRefactoring('test =');
576624
// validate change
577-
return assertSuccessfulRefactoring('''
625+
await assertSuccessfulRefactoring('''
578626
void f() {
579627
var a = 1;
580628
var b = -(--a);
@@ -592,7 +640,7 @@ void f() {
592640
''');
593641
_createRefactoring('test =');
594642
// validate change
595-
return assertSuccessfulRefactoring('''
643+
await assertSuccessfulRefactoring('''
596644
class A {}
597645
void f() {
598646
var list = [new A()];
@@ -610,7 +658,7 @@ void f() {
610658
''');
611659
_createRefactoring('test =');
612660
// validate change
613-
return assertSuccessfulRefactoring('''
661+
await assertSuccessfulRefactoring('''
614662
void f() {
615663
var items = [];
616664
items[1 + 2] * 5;
@@ -628,7 +676,7 @@ f(m, x, y) {
628676
''');
629677
_createRefactoring('test =');
630678
// validate change
631-
return assertSuccessfulRefactoring('''
679+
await assertSuccessfulRefactoring('''
632680
f(m, x, y) {
633681
m[x as int] = y;
634682
return m[x as int];
@@ -646,7 +694,7 @@ void f() {
646694
''');
647695
_createRefactoring('test =');
648696
// validate change
649-
return assertSuccessfulRefactoring('''
697+
await assertSuccessfulRefactoring('''
650698
void f() {
651699
var a = 1;
652700
var b = -(-a);
@@ -663,13 +711,45 @@ void f() {
663711
''');
664712
_createRefactoring('test =');
665713
// validate change
666-
return assertSuccessfulRefactoring('''
714+
await assertSuccessfulRefactoring('''
667715
void f() {
668716
print((1 + 2) * 3);
669717
}
670718
''');
671719
}
672720

721+
Future<void> test_OK_recordLiteral() async {
722+
await indexTestUnit('''
723+
void f() {
724+
var test = 42.isEven;
725+
(0, test, 1);
726+
}
727+
''');
728+
_createRefactoring('test =');
729+
// validate change
730+
await assertSuccessfulRefactoring('''
731+
void f() {
732+
(0, 42.isEven, 1);
733+
}
734+
''');
735+
}
736+
737+
Future<void> test_OK_setLiteral() async {
738+
await indexTestUnit('''
739+
void f() {
740+
var test = 42.isEven;
741+
<dynamic>{0, test, 1};
742+
}
743+
''');
744+
_createRefactoring('test =');
745+
// validate change
746+
await assertSuccessfulRefactoring('''
747+
void f() {
748+
<dynamic>{0, 42.isEven, 1};
749+
}
750+
''');
751+
}
752+
673753
Future<void> test_OK_twoUsages() async {
674754
await indexTestUnit('''
675755
void f() {
@@ -680,7 +760,7 @@ void f() {
680760
''');
681761
_createRefactoring('test =');
682762
// validate change
683-
return assertSuccessfulRefactoring('''
763+
await assertSuccessfulRefactoring('''
684764
void f() {
685765
print(1 + 2);
686766
print(1 + 2);

0 commit comments

Comments
 (0)