Skip to content

Commit 6b4d761

Browse files
authored
Don't split before . following a record literal. (#1218)
Fix #1213.
1 parent f1cc57e commit 6b4d761

File tree

6 files changed

+42
-5
lines changed

6 files changed

+42
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# 2.3.2-dev
22

3+
* Don't split before `.` following a record literal (#1213).
34
* Don't force split on a line comment before a switch expression case (#1215).
45

56
# 2.3.1

lib/src/ast_extensions.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ extension AstNodeExtensions on AstNode {
3838
return body is BlockFunctionBody && body.block.statements.isNotEmpty;
3939
}
4040

41+
/// Whether this node is a bracket-delimited collection literal.
42+
bool get isCollectionLiteral =>
43+
this is ListLiteral || this is RecordLiteral || this is SetOrMapLiteral;
44+
4145
bool get isControlFlowElement => this is IfElement || this is ForElement;
4246

4347
/// Whether this is immediately contained within an anonymous

lib/src/call_chain_visitor.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ class CallChainVisitor {
284284
}
285285

286286
// Don't split right after a collection literal.
287-
if (expression is ListLiteral) return false;
288-
if (expression is SetOrMapLiteral) return false;
287+
if (expression.isCollectionLiteral) return false;
289288

290289
// Don't split right after a non-empty curly-bodied function.
291290
if (expression is FunctionExpression) {

lib/src/source_visitor.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,7 @@ class SourceVisitor extends ThrowingAstVisitor {
428428
var splitIfTargetSplits = true;
429429
if (node.cascadeSections.length > 1) {
430430
// Always split if there are multiple cascade sections.
431-
} else if (target is ListLiteral ||
432-
target is RecordLiteral ||
433-
target is SetOrMapLiteral) {
431+
} else if (target.isCollectionLiteral) {
434432
splitIfTargetSplits = false;
435433
} else if (target is InvocationExpression) {
436434
// If the target is a call with a trailing comma in the argument list,

test/regression/1200/1213.unit

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
>>>
2+
main() {
3+
final a = (
4+
element,
5+
element,
6+
)
7+
.getter;
8+
9+
final b = [
10+
element,
11+
element,
12+
].getter;
13+
}
14+
<<<
15+
main() {
16+
final a = (
17+
element,
18+
element,
19+
).getter;
20+
21+
final b = [
22+
element,
23+
element,
24+
].getter;
25+
}

test/splitting/invocations.stmt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,16 @@ someVeryLongTargetFunction(
278278
"key": "value",
279279
"another": "another value"
280280
}.someLongMethod();
281+
>>> do not split on "." when target is record
282+
(element, element, element, element, element).someLongMethod();
283+
<<<
284+
(
285+
element,
286+
element,
287+
element,
288+
element,
289+
element
290+
).someLongMethod();
281291
>>> do not split on "." when target is function literal passed to method
282292
method(() {;}).someLongMethod();
283293
<<<

0 commit comments

Comments
 (0)