Skip to content

Commit d947ba2

Browse files
committed
Preserve space between "?" and "." in null aware element dot shorthand. (#1748)
Preserve space between "?" and "." in null aware element dot shorthand. Fix #1642.
1 parent ca4bdb8 commit d947ba2

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

lib/src/front_end/ast_node_visitor.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,18 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
15991599

16001600
@override
16011601
void visitNullAwareElement(NullAwareElement node) {
1602-
writePrefix(node.question, node.value);
1602+
// A null-aware element containing a dot shorthand means there is a `?` and
1603+
// `.` next to each other. In that case, make sure we put a space between
1604+
// them so that they don't incorrectly get collapsed into a `?.` null-aware
1605+
// access token.
1606+
var space = switch (node.value) {
1607+
DotShorthandConstructorInvocation() => true,
1608+
DotShorthandInvocation() => true,
1609+
DotShorthandPropertyAccess() => true,
1610+
_ => false,
1611+
};
1612+
1613+
writePrefix(node.question, space: space, node.value);
16031614
}
16041615

16051616
@override

test/tall/expression/collection_null_aware.stmt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,17 @@ var list = [
2727
?(veryLongExpression +
2828
thatIsForcedToSplit),
2929
];
30+
>>> (experiment dot-shorthands) Preserves space for dot shorthand.
31+
### If the space between `?` and `.` is removed, it would become a single `?.`
32+
### token and thus a parse error.
33+
var list = [
34+
? . property ,
35+
? . invocation ( ) ,
36+
? . new ( ) ,
37+
];
38+
<<< 3.10
39+
var list = [
40+
? .property,
41+
? .invocation(),
42+
? .new(),
43+
];

test/tall/expression/collection_null_aware_comment.stmt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,20 @@ var map = {
3333
? // c
3434
value,
3535
};
36+
>>> (experiment dot-shorthands) Comment between `?` and `.` of dot shorthand.
37+
var list = [
38+
? /* c */ . property ,
39+
? // c
40+
. invocation ( ) ,
41+
? /// c
42+
. new ( ) ,
43+
];
44+
<<< 3.10
45+
var list = [
46+
? /* c */ .property,
47+
? // c
48+
.invocation(),
49+
?
50+
/// c
51+
.new(),
52+
];

0 commit comments

Comments
 (0)