Skip to content

Commit 4f4d1bd

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Migration: Add if-statement and block-statement support to FixBuilder.
Change-Id: I3e3f0b3824a9a51b63f9ee8ffbe414415fc2b2e1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121340 Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
1 parent 2796749 commit 4f4d1bd

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

pkg/nnbd_migration/lib/src/fix_builder.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,34 @@ abstract class FixBuilder extends GeneralizingAstVisitor<DartType> {
232232
}
233233
}
234234

235+
@override
236+
DartType visitBlock(Block node) {
237+
for (var statement in node.statements) {
238+
statement.accept(this);
239+
}
240+
return null;
241+
}
242+
235243
@override
236244
DartType visitExpressionStatement(ExpressionStatement node) {
237245
visitSubexpression(node.expression, UnknownInferredType.instance);
238246
return null;
239247
}
240248

249+
@override
250+
DartType visitIfStatement(IfStatement node) {
251+
visitSubexpression(node.condition, _typeProvider.boolType);
252+
_flowAnalysis.ifStatement_thenBegin(node.condition);
253+
node.thenStatement.accept(this);
254+
bool hasElse = node.elseStatement != null;
255+
if (hasElse) {
256+
_flowAnalysis.ifStatement_elseBegin();
257+
node.elseStatement.accept(this);
258+
}
259+
_flowAnalysis.ifStatement_end(hasElse);
260+
return null;
261+
}
262+
241263
@override
242264
DartType visitListLiteral(ListLiteral node) {
243265
DartType contextType;

pkg/nnbd_migration/test/fix_builder_test.dart

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,19 @@ _f(_C<int, String/*?*/> c, String/*?*/ s) => c + s;
509509
visitSubexpression(findNode.binary('c +'), 'int');
510510
}
511511

512+
test_block() async {
513+
await analyze('''
514+
_f(int/*?*/ x, int/*?*/ y) {
515+
{ // block
516+
x + 1;
517+
y + 1;
518+
}
519+
}
520+
''');
521+
visitStatement(findNode.statement('{ // block'),
522+
nullChecked: {findNode.simple('x + 1'), findNode.simple('y + 1')});
523+
}
524+
512525
test_booleanLiteral() async {
513526
await analyze('''
514527
f() => true;
@@ -533,6 +546,45 @@ _f(int/*!*/ x, int/*?*/ y) {
533546
nullChecked: {findNode.simple('y;')});
534547
}
535548

549+
test_ifStatement_flow_promote_in_else() async {
550+
await analyze('''
551+
_f(int/*?*/ x) {
552+
if (x == null) {
553+
x + 1;
554+
} else {
555+
x + 2;
556+
}
557+
}
558+
''');
559+
visitStatement(findNode.statement('if'),
560+
nullChecked: {findNode.simple('x + 1')});
561+
}
562+
563+
test_ifStatement_flow_promote_in_then() async {
564+
await analyze('''
565+
_f(int/*?*/ x) {
566+
if (x != null) {
567+
x + 1;
568+
} else {
569+
x + 2;
570+
}
571+
}
572+
''');
573+
visitStatement(findNode.statement('if'),
574+
nullChecked: {findNode.simple('x + 2')});
575+
}
576+
577+
test_ifStatement_flow_promote_in_then_no_else() async {
578+
await analyze('''
579+
_f(int/*?*/ x) {
580+
if (x != null) {
581+
x + 1;
582+
}
583+
}
584+
''');
585+
visitStatement(findNode.statement('if'));
586+
}
587+
536588
test_integerLiteral() async {
537589
await analyze('''
538590
f() => 1;

0 commit comments

Comments
 (0)