Skip to content

Commit 5d2ad56

Browse files
authored
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 4f2fe38 commit 5d2ad56

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
@@ -1587,7 +1587,18 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
15871587

15881588
@override
15891589
void visitNullAwareElement(NullAwareElement node) {
1590-
writePrefix(node.question, node.value);
1590+
// A null-aware element containing a dot shorthand means there is a `?` and
1591+
// `.` next to each other. In that case, make sure we put a space between
1592+
// them so that they don't incorrectly get collapsed into a `?.` null-aware
1593+
// access token.
1594+
var space = switch (node.value) {
1595+
DotShorthandConstructorInvocation() => true,
1596+
DotShorthandInvocation() => true,
1597+
DotShorthandPropertyAccess() => true,
1598+
_ => false,
1599+
};
1600+
1601+
writePrefix(node.question, space: space, node.value);
15911602
}
15921603

15931604
@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)