Skip to content

Commit

Permalink
Format null-aware elements (#1663)
Browse files Browse the repository at this point in the history
Format null-aware elements

- Add support for passing experiment flags with tests. The old way of
  doing this was to just hardcode experiments inside the formatter when
  a new feature was supported. That made sense when the formatter didn't
  know about language version, but now that it does, it should probably
  require experiment flags explicitly and tests for new features should
  enable them.
  • Loading branch information
chloestefantsova authored Mar 5, 2025
1 parent 852e54f commit 348a146
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 3.0.2-wip

* Format null-aware elements.

* Don't indent conditional branches redundantly after `=`, `:`, and `=>`.

```dart
Expand Down
3 changes: 2 additions & 1 deletion example/format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ Future<void> _runTest(String path, int line,
var formatter = DartFormatter(
languageVersion: formatTest.languageVersion,
pageWidth: testFile.pageWidth,
indent: formatTest.leadingIndent);
indent: formatTest.leadingIndent,
experimentFlags: formatTest.experimentFlags);

var actual = formatter.formatSource(formatTest.input);

Expand Down
24 changes: 23 additions & 1 deletion lib/src/front_end/ast_node_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,24 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {

@override
void visitMapLiteralEntry(MapLiteralEntry node) {
writeAssignment(node.key, node.separator, node.value);
var leftPiece = pieces.build(() {
pieces.token(node.keyQuestion);
pieces.visit(node.key);
});

var operatorPiece = tokenPiece(node.separator);

var rightPiece = pieces.build(() {
pieces.token(node.valueQuestion);
pieces.visit(node.value, context: NodeContext.assignment);
});

pieces.add(AssignPiece(
left: leftPiece,
operatorPiece,
rightPiece,
canBlockSplitLeft: node.key.canBlockSplit,
canBlockSplitRight: node.value.canBlockSplit));
}

@override
Expand Down Expand Up @@ -1353,6 +1370,11 @@ final class AstNodeVisitor extends ThrowingAstVisitor<void> with PieceFactory {
writePostfix(node.pattern, node.operator);
}

@override
void visitNullAwareElement(NullAwareElement node) {
writePrefix(node.question, node.value);
}

@override
void visitNullCheckPattern(NullCheckPattern node) {
writePostfix(node.pattern, node.operator);
Expand Down
14 changes: 14 additions & 0 deletions lib/src/testing/test_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import '../../dart_style.dart';

final _indentPattern = RegExp(r'\(indent (\d+)\)');
final _versionPattern = RegExp(r'\(version (\d+)\.(\d+)\)');
final _experimentPattern = RegExp(r'\(experiment ([a-z-]+)\)');
final _unicodeUnescapePattern = RegExp(r'×([0-9a-fA-F]{2,4})');
final _unicodeEscapePattern = RegExp('[\x0a\x0c\x0d]');

Expand Down Expand Up @@ -111,6 +112,14 @@ final class TestFile {
return '';
});

// Let the test enable experiments for features that are supported but not
// released yet.
var experiments = <String>[];
description = description.replaceAllMapped(_experimentPattern, (match) {
experiments.add(match[1]!);
return '';
});

var inputComments = readComments();

var inputBuffer = StringBuffer();
Expand Down Expand Up @@ -160,6 +169,7 @@ final class TestFile {
lineNumber,
languageVersion,
leadingIndent,
experiments,
inputComments,
outputComments));
}
Expand Down Expand Up @@ -217,6 +227,9 @@ final class FormatTest {
/// line.
final int leadingIndent;

/// Experiments that should be enabled when running this test.
final List<String> experimentFlags;

FormatTest(
this.input,
this.output,
Expand All @@ -225,6 +238,7 @@ final class FormatTest {
this.line,
this.languageVersion,
this.leadingIndent,
this.experimentFlags,
this.inputComments,
this.outputComments);

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
sdk: "^3.4.0"

dependencies:
analyzer: ">=6.5.0 <8.0.0"
analyzer: ">=7.3.0 <8.0.0"
args: ">=1.0.0 <3.0.0"
collection: "^1.17.0"
package_config: ^2.1.0
Expand Down
28 changes: 28 additions & 0 deletions test/tall/expression/collection_null_aware.stmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
40 columns |
>>> (experiment null-aware-elements) List element.
var list = [ ? x ];
<<<
var list = [?x];
>>> (experiment null-aware-elements) Set element.
var set = { ? x };
<<<
var set = {?x};
>>> (experiment null-aware-elements) Map key.
var map = { ? key : value};
<<<
var map = {?key: value};
>>> (experiment null-aware-elements) Map value.
var map = { key: ? value };
<<<
var map = {key: ?value};
>>> (experiment null-aware-elements) Both key and value.
var map = { ? key : ? value };
<<<
var map = {?key: ?value};
>>> (experiment null-aware-elements) Split inside element.
var list = [?(veryLongExpression +thatIsForcedToSplit)];
<<<
var list = [
?(veryLongExpression +
thatIsForcedToSplit),
];
34 changes: 34 additions & 0 deletions test/tall/expression/collection_null_aware_comment.stmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
40 columns |
>>> (experiment null-aware-elements) Inline comment after `?`.
var list = [ ? /* c */ x ];
<<<
var list = [? /* c */ x];
>>> (experiment null-aware-elements)
var map = { ? /* c */ key : ? /* c */ value };
<<<
var map = {
? /* c */ key: ? /* c */ value,
};
>>> (experiment null-aware-elements) Line comment after `?`.
var list = [ ? // c
x ];
<<<
### This is an odd place for a comment so the formatting is odd, but we want to
### at least pin it down with a test.
var list = [
? // c
x,
];
>>> (experiment null-aware-elements)
var map = { ? // c
key : ? // c
value };
<<<
### This is an odd place for a comment so the formatting is odd, but we want to
### at least pin it down with a test.
var map = {
? // c
key:
? // c
value,
};
3 changes: 2 additions & 1 deletion test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ void _testFile(TestFile testFile) {
var formatter = DartFormatter(
languageVersion: formatTest.languageVersion,
pageWidth: testFile.pageWidth,
indent: formatTest.leadingIndent);
indent: formatTest.leadingIndent,
experimentFlags: formatTest.experimentFlags);

var actual = _validateFormat(
formatter,
Expand Down
5 changes: 4 additions & 1 deletion tool/update_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ Future<void> _updateTestFile(TestFile testFile) async {
var formatter = DartFormatter(
languageVersion: formatTest.languageVersion,
pageWidth: testFile.pageWidth,
indent: formatTest.leadingIndent);
indent: formatTest.leadingIndent,
experimentFlags: formatTest.experimentFlags);

var actual = formatter.formatSource(formatTest.input);

Expand All @@ -110,6 +111,8 @@ Future<void> _updateTestFile(TestFile testFile) async {
: DartFormatter.latestShortStyleLanguageVersion;

var descriptionParts = [
for (var experiment in formatTest.experimentFlags)
'(experiment $experiment)',
if (formatTest.leadingIndent != 0) '(indent ${formatTest.leadingIndent})',
if (formatTest.languageVersion != defaultLanguageVersion)
'(version ${formatTest.languageVersion.major}.'
Expand Down

0 comments on commit 348a146

Please sign in to comment.