Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 28c20c0

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Fix for crash in ReplaceCascadeWithDot, simplify it.
[email protected] Change-Id: I3243203e42856b6a4e65603aafb708473bdfe3fc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/156701 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 1d1a775 commit 28c20c0

File tree

2 files changed

+60
-41
lines changed

2 files changed

+60
-41
lines changed

pkg/analysis_server/lib/src/services/correction/dart/replace_cascade_with_dot.dart

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1111
import 'package:analyzer_plugin/utilities/range_factory.dart';
1212

1313
class ReplaceCascadeWithDot extends CorrectionProducer {
14+
static final Map<TokenType, String> _indexReplacement = {
15+
TokenType.PERIOD_PERIOD: '',
16+
TokenType.QUESTION_PERIOD_PERIOD: '?',
17+
};
18+
19+
static final Map<TokenType, String> _propertyReplacement = {
20+
TokenType.PERIOD_PERIOD: '.',
21+
TokenType.QUESTION_PERIOD_PERIOD: '?.',
22+
};
23+
1424
@override
1525
FixKind get fixKind => DartFixKind.REPLACE_CASCADE_WITH_DOT;
1626

@@ -20,52 +30,41 @@ class ReplaceCascadeWithDot extends CorrectionProducer {
2030
if (node is CascadeExpression) {
2131
var sections = node.cascadeSections;
2232
if (sections.length == 1) {
23-
var section = sections[0];
24-
Token cascadeOperator;
25-
if (section is MethodInvocation) {
26-
cascadeOperator = section.operator;
27-
} else if (section is PropertyAccess) {
28-
cascadeOperator = section.operator;
29-
} else if (section is IndexExpression) {
30-
await _handleIndexExpression(builder, section);
31-
return;
32-
} else if (section is AssignmentExpression) {
33-
var leftHandSide = section.leftHandSide;
34-
if (leftHandSide is PropertyAccess) {
35-
cascadeOperator = leftHandSide.operator;
36-
} else if (leftHandSide is IndexExpression) {
37-
await _handleIndexExpression(builder, leftHandSide);
38-
return;
39-
} else {
40-
return;
41-
}
42-
} else {
43-
return;
44-
}
45-
var type = cascadeOperator.type;
46-
if (type == TokenType.PERIOD_PERIOD ||
47-
type == TokenType.QUESTION_PERIOD_PERIOD) {
48-
await builder.addDartFileEdit(file, (builder) {
49-
var end = cascadeOperator.end;
50-
builder.addDeletion(range.startOffsetEndOffset(end - 1, end));
51-
});
52-
}
33+
await _replaceFor(builder, sections[0]);
5334
}
5435
}
5536
}
5637

57-
void _handleIndexExpression(
58-
ChangeBuilder builder, IndexExpression section) async {
59-
var cascadeOperator = section.period;
60-
var type = cascadeOperator.type;
61-
if (type == TokenType.PERIOD_PERIOD) {
62-
await builder.addDartFileEdit(file, (builder) {
63-
builder.addDeletion(
64-
range.startStart(cascadeOperator, section.leftBracket));
65-
});
66-
} else if (type == TokenType.QUESTION_PERIOD_PERIOD) {
38+
Future<void> _replaceFor(ChangeBuilder builder, Expression section) async {
39+
if (section is AssignmentExpression) {
40+
return _replaceFor(builder, section.leftHandSide);
41+
}
42+
43+
if (section is IndexExpression) {
44+
if (section.period != null) {
45+
return _replaceToken(builder, section.period, _indexReplacement);
46+
}
47+
return _replaceFor(builder, section.target);
48+
}
49+
50+
if (section is MethodInvocation) {
51+
return _replaceToken(builder, section.operator, _propertyReplacement);
52+
}
53+
54+
if (section is PropertyAccess) {
55+
return _replaceToken(builder, section.operator, _propertyReplacement);
56+
}
57+
}
58+
59+
Future<void> _replaceToken(
60+
ChangeBuilder builder,
61+
Token token,
62+
Map<TokenType, String> map,
63+
) async {
64+
var replacement = map[token.type];
65+
if (replacement != null) {
6766
await builder.addDartFileEdit(file, (builder) {
68-
builder.addSimpleReplacement(range.token(cascadeOperator), '?');
67+
builder.addSimpleReplacement(range.token(token), replacement);
6968
});
7069
}
7170
}

pkg/analysis_server/test/src/services/correction/fix/replace_cascade_with_dot_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ void f(List<int> l) {
3939
''');
4040
}
4141

42+
Future<void> test_assignment_index_propertyAccess_normalCascade() async {
43+
await resolveTestUnit('''
44+
class A {
45+
void foo() {
46+
0..bar[1] = 2;
47+
}
48+
}
49+
''');
50+
await assertHasFix('''
51+
class A {
52+
void foo() {
53+
0.bar[1] = 2;
54+
}
55+
}
56+
''',
57+
errorFilter: (e) =>
58+
e.errorCode.name ==
59+
LintNames.avoid_single_cascade_in_expression_statements);
60+
}
61+
4262
Future<void> test_assignment_property_normalCascade() async {
4363
await resolveTestUnit('''
4464
void f(C c) {

0 commit comments

Comments
 (0)