Skip to content

Commit b5a166e

Browse files
authored
Remove special case handling of dot shorthands in null aware elements. (#1757)
We fixed the parser so that it can correctly handle: ```dart [?.property] ``` Even if there is no space between `?` and `.`, it knows to treat it as a dot shorthand inside a null-aware element here. So the formatter doesn't need to force insert a space in there to avoid `?.` from being tokenized as a null-aware access.
1 parent 00b5f3f commit b5a166e

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 3.1.3-wip
2+
3+
* Don't force a space between `?` and `.` if a null-aware element contains a
4+
dot shorthand.
5+
16
## 3.1.2
27

38
* Support dot shorthand syntax.

lib/src/front_end/ast_node_visitor.dart

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,18 +1605,7 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
16051605

16061606
@override
16071607
void visitNullAwareElement(NullAwareElement node) {
1608-
// A null-aware element containing a dot shorthand means there is a `?` and
1609-
// `.` next to each other. In that case, make sure we put a space between
1610-
// them so that they don't incorrectly get collapsed into a `?.` null-aware
1611-
// access token.
1612-
var space = switch (node.value) {
1613-
DotShorthandConstructorInvocation() => true,
1614-
DotShorthandInvocation() => true,
1615-
DotShorthandPropertyAccess() => true,
1616-
_ => false,
1617-
};
1618-
1619-
writePrefix(node.question, space: space, node.value);
1608+
writePrefix(node.question, node.value);
16201609
}
16211610

16221611
@override

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: dart_style
22
# Note: See tool/grind.dart for how to bump the version.
3-
version: 3.1.2
3+
version: 3.1.3-wip
44
description: >-
55
Opinionated, automatic Dart source code formatter.
66
Provides an API and a CLI tool.
@@ -9,7 +9,7 @@ environment:
99
sdk: ^3.7.0
1010

1111
dependencies:
12-
analyzer: ^8.0.0
12+
analyzer: ^8.1.0
1313
args: ^2.0.0
1414
collection: ^1.19.0
1515
package_config: ^2.1.0

test/tall/expression/collection_null_aware.stmt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,23 @@ var list = [
3131
### If the space between `?` and `.` is removed, it would become a single `?.`
3232
### token and thus a parse error.
3333
var list = [
34-
? . property ,
34+
?. property ,
3535
? . invocation ( ) ,
3636
? . new ( ) ,
3737
];
3838
<<< 3.10
3939
var list = [
40-
? .property,
41-
? .invocation(),
42-
? .new(),
40+
?.property,
41+
?.invocation(),
42+
?.new(),
4343
];
44+
>>> (experiment dot-shorthands) Ambiguous dot shorthand versus null-aware.
45+
### Because there is no space in "?.", it must be a null-aware access in a map.
46+
c = { e1 ?. e2 : e3 };
47+
<<< 3.10
48+
c = {e1?.e2: e3};
49+
>>> (experiment dot-shorthands) Ambiguous dot shorthand versus null-aware.
50+
### Because there is a space in "? .", it must be a dot shorthand in a set.
51+
c = { e1 ? . e2 : e3 };
52+
<<< 3.10
53+
c = {e1 ? .e2 : e3};

0 commit comments

Comments
 (0)