Skip to content

Commit

Permalink
[nnbd_migration] Fix ConditionalModification on IfElement
Browse files Browse the repository at this point in the history
Change-Id: I1e8ec4bead2aaa9147850d27cd5ac4d9c2a700be
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/130137
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Mike Fairhurst <mfairhurst@google.com>
  • Loading branch information
MichaelRFairhurst authored and commit-bot@chromium.org committed Jan 3, 2020
1 parent be4acb4 commit 6e7a900
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pkg/analyzer/lib/src/test_utilities/find_node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class FindNode {
return _node(search, (n) => n is ClassDeclaration);
}

CollectionElement collectionElement(String search) {
return _node(search, (n) => n is CollectionElement);
}

CommentReference commentReference(String search) {
return _node(search, (n) => n is CommentReference);
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/nnbd_migration/lib/src/potential_modification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ class ConditionalModification extends PotentialModification {
_KeepNode(node.condition),
_KeepNode(node.thenStatement),
node.elseStatement == null ? null : _KeepNode(node.elseStatement));
} else if (node is IfElement) {
return ConditionalModification._(
node.offset,
node.end,
node is Statement,
discard,
_KeepNode(node.condition),
_KeepNode(node.thenElement),
node.elseElement == null ? null : _KeepNode(node.elseElement));
} else {
throw new UnimplementedError('TODO(paulberry)');
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/nnbd_migration/test/edge_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2812,9 +2812,7 @@ void f(bool b, int i, int j) {
assertEdge(decoratedTypeAnnotation('int j').node, never, hard: true);
}

@failingTest
Future<void> test_if_element_guard_equals_null() async {
// failing because of an unimplemented exception in conditional modification
await analyze('''
dynamic f(int i, int j, int k) {
<int>[if (i == null) j/*check*/ else k/*check*/];
Expand All @@ -2830,7 +2828,7 @@ dynamic f(int i, int j, int k) {
guards: [nullable_i], hard: false));
assertNullCheck(checkExpression('k/*check*/'),
assertEdge(nullable_k, nullable_itemType, hard: false));
var discard = statementDiscard('if (i == null)');
var discard = elementDiscard('if (i == null)');
expect(discard.trueGuard, same(nullable_i));
expect(discard.falseGuard, null);
expect(discard.pureCondition, true);
Expand Down
6 changes: 6 additions & 0 deletions pkg/nnbd_migration/test/migration_visitor_test_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ class MigrationVisitorTestBase extends AbstractSingleUnitTest with EdgeTester {
ConditionalDiscard statementDiscard(String text) {
return variables.conditionalDiscard(findNode.statement(text));
}

/// Gets the [ConditionalDiscard] information associated with the collection
/// element whose text is [text].
ConditionalDiscard elementDiscard(String text) {
return variables.conditionalDiscard(findNode.collectionElement(text));
}
}

/// Abstract base class representing a thing that can be matched against
Expand Down

0 comments on commit 6e7a900

Please sign in to comment.