Skip to content

Commit

Permalink
Version 3.7.0-47.0.dev
Browse files Browse the repository at this point in the history
Merge ec6dfd4 into dev
  • Loading branch information
Dart CI committed Oct 21, 2024
2 parents 10387c3 + ec6dfd4 commit 86ac622
Show file tree
Hide file tree
Showing 5 changed files with 313 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class JoinIfWithInner extends ResolvedCorrectionProducer {
if (innerIfStatement.elseStatement != null) {
return;
}

// If inner is if-case, we cannot join them.
if (innerIfStatement.caseClause != null) {
return;
}

// prepare environment
var prefix = utils.getNodePrefix(targetIfStatement);
// merge conditions
Expand All @@ -55,7 +61,30 @@ class JoinIfWithInner extends ResolvedCorrectionProducer {
if (innerCondition.shouldWrapParenthesisBeforeAnd) {
innerConditionSource = '($innerConditionSource)';
}

var condition = '$targetConditionSource && $innerConditionSource';

// If outer is if-case.
var outerCaseClause = targetIfStatement.caseClause;
if (outerCaseClause != null) {
var casePattern = outerCaseClause.guardedPattern.pattern;
var caseWhenExpression =
outerCaseClause.guardedPattern.whenClause?.expression;

if (caseWhenExpression != null) {
var caseWhenSource = '$caseWhenExpression';
if (caseWhenExpression.shouldWrapParenthesisBeforeAnd) {
caseWhenSource = '($caseWhenSource)';
}

condition =
'$targetConditionSource case $casePattern when $caseWhenSource && $innerConditionSource';
} else {
condition =
'$targetConditionSource case $casePattern when $innerConditionSource';
}
}

// replace target "if" statement
var innerThenStatement = innerIfStatement.thenStatement;
var innerThenStatements = getStatements(innerThenStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class JoinIfWithOuter extends ResolvedCorrectionProducer {
if (outerIfStatement.elseStatement != null) {
return;
}

// If target (inner) is if-case, we cannot join them.
if (targetIfStatement.caseClause != null) {
return;
}

// prepare environment
var prefix = utils.getNodePrefix(outerIfStatement);
// merge conditions
Expand All @@ -61,7 +67,30 @@ class JoinIfWithOuter extends ResolvedCorrectionProducer {
if (outerCondition.shouldWrapParenthesisBeforeAnd) {
outerConditionSource = '($outerConditionSource)';
}

var condition = '$outerConditionSource && $targetConditionSource';

// If outer is if-case.
var outerCaseClause = outerIfStatement.caseClause;
if (outerCaseClause != null) {
var casePattern = outerCaseClause.guardedPattern.pattern;
var caseWhenExpression =
outerCaseClause.guardedPattern.whenClause?.expression;

if (caseWhenExpression != null) {
var caseWhenSource = '$caseWhenExpression';
if (caseWhenExpression.shouldWrapParenthesisBeforeAnd) {
caseWhenSource = '($caseWhenSource)';
}

condition =
'$outerConditionSource case $casePattern when $caseWhenSource && $targetConditionSource';
} else {
condition =
'$outerConditionSource case $casePattern when $targetConditionSource';
}
}

// replace outer "if" statement
var targetThenStatement = targetIfStatement.thenStatement;
var targetThenStatements = getStatements(targetThenStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ class JoinIfWithInnerTest extends AssistProcessorTest {
@override
AssistKind get kind => DartAssistKind.JOIN_IF_WITH_INNER;

Future<void> test_bothOuterAndInnerAreIfCase() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final v?) {
if (v case final int x) {
print(0);
}
}
}
''');
await assertNoAssistAt('if (p');
}

Future<void> test_conditionAndOr() async {
await resolveTestCode('''
void f() {
Expand Down Expand Up @@ -78,6 +91,120 @@ void f() {
''');
}

Future<void> test_ifCaseAddWhen() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final int v) {
if (v == 5) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (p', '''
void f(Object? p) {
if (p case final int v when v == 5) {
print(0);
}
}
''');
}

Future<void> test_ifCaseAddWhenUnrelated() async {
await resolveTestCode('''
void f(Object? p, Object? q) {
if (p case final int v) {
if (q != null) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (p', '''
void f(Object? p, Object? q) {
if (p case final int v when q != null) {
print(0);
}
}
''');
}

Future<void> test_ifCaseAppendWhen() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final int v when v.isOdd) {
if (v == 5) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (p', '''
void f(Object? p) {
if (p case final int v when v.isOdd && v == 5) {
print(0);
}
}
''');
}

Future<void> test_ifCaseAppendWhenWithParenthesisBoth() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final int v when v.isOdd || v > 3) {
if (v == 5 || v != 6) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (p', '''
void f(Object? p) {
if (p case final int v when (v.isOdd || v > 3) && (v == 5 || v != 6)) {
print(0);
}
}
''');
}

Future<void> test_ifCaseAppendWhenWithParenthesisInner() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final int v when v.isOdd) {
if (v == 5 || v != 3) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (p', '''
void f(Object? p) {
if (p case final int v when v.isOdd && (v == 5 || v != 3)) {
print(0);
}
}
''');
}

Future<void> test_ifCaseAppendWhenWithParenthesisOuter() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final int v when v.isOdd || v != 3) {
if (v == 5) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (p', '''
void f(Object? p) {
if (p case final int v when (v.isOdd || v != 3) && v == 5) {
print(0);
}
}
''');
}

Future<void> test_innerNotIf() async {
await resolveTestCode('''
void f() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ class JoinIfWithOuterTest extends AssistProcessorTest {
@override
AssistKind get kind => DartAssistKind.JOIN_IF_WITH_OUTER;

Future<void> test_bothOuterAndInnerAreIfCase() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final v?) {
if (v case final int x) {
print(x);
}
}
}
''');
await assertNoAssistAt('if (v');
}

Future<void> test_conditionAndOr() async {
await resolveTestCode('''
void f() {
Expand Down Expand Up @@ -78,6 +91,120 @@ void f() {
''');
}

Future<void> test_ifCaseAddWhen() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final int v) {
if (v == 5) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (v == 5', '''
void f(Object? p) {
if (p case final int v when v == 5) {
print(0);
}
}
''');
}

Future<void> test_ifCaseAddWhenUnrelated() async {
await resolveTestCode('''
void f(Object? p, Object? q) {
if (p case final int v) {
if (q != null) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (q != null', '''
void f(Object? p, Object? q) {
if (p case final int v when q != null) {
print(0);
}
}
''');
}

Future<void> test_ifCaseAppendWhen() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final int v when v.isOdd) {
if (v == 5) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (v == 5', '''
void f(Object? p) {
if (p case final int v when v.isOdd && v == 5) {
print(0);
}
}
''');
}

Future<void> test_ifCaseAppendWhenWithParenthesisBoth() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final int v when v.isOdd || v > 3) {
if (v == 5 || v != 6) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (v', '''
void f(Object? p) {
if (p case final int v when (v.isOdd || v > 3) && (v == 5 || v != 6)) {
print(0);
}
}
''');
}

Future<void> test_ifCaseAppendWhenWithParenthesisInner() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final int v when v.isOdd) {
if (v == 5 || v != 3) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (v', '''
void f(Object? p) {
if (p case final int v when v.isOdd && (v == 5 || v != 3)) {
print(0);
}
}
''');
}

Future<void> test_ifCaseAppendWhenWithParenthesisOuter() async {
await resolveTestCode('''
void f(Object? p) {
if (p case final int v when v.isOdd || v != 3) {
if (v == 5) {
print(0);
}
}
}
''');
await assertHasAssistAt('if (v', '''
void f(Object? p) {
if (p case final int v when (v.isOdd || v != 3) && v == 5) {
print(0);
}
}
''');
}

Future<void> test_onCondition() async {
await resolveTestCode('''
void f() {
Expand Down
2 changes: 1 addition & 1 deletion tools/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ CHANNEL dev
MAJOR 3
MINOR 7
PATCH 0
PRERELEASE 46
PRERELEASE 47
PRERELEASE_PATCH 0

0 comments on commit 86ac622

Please sign in to comment.