Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Update dart_style to pass in a language version to DartFormatter.
Browse files Browse the repository at this point in the history
We're in [the process of](dart-lang/dart_style#1403) moving dart_style to [a new formatting style](dart-lang/dart_style#1253). That involves making the formatter [aware of the language version of what it's formatting](dart-lang/dart_style#1402).

That in turn means that the library API now lets you pass in a language version. In dart_style 2.3.7 [you can pass in a language version but the parameter is optional](https://pub.dev/documentation/dart_style/latest/dart_style/DartFormatter/DartFormatter.html). In the forthcoming 3.0.0 release, that parameter will become mandatory.

This updates every call to `DartFormatter()` to pass in the latest language version. If there's a more specific version that should be used, let me know and I'll update the PR.

Thanks!
  • Loading branch information
munificent committed Oct 22, 2024
1 parent c3349b7 commit 7ae5a3c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ void main() {
..name = 'eat'
..body = const Code("print('Yum!');"))));
final emitter = DartEmitter();
print(DartFormatter().format('${animal.accept(emitter)}'));
print(
DartFormatter(languageVersion: DartFormatter.latestLanguageVersion)
.format('${animal.accept(emitter)}'),
);
}
```

Expand Down Expand Up @@ -57,7 +60,10 @@ void main() {
..returns = refer('Other', 'package:b/b.dart')),
]));
final emitter = DartEmitter.scoped();
print(DartFormatter().format('${library.accept(emitter)}'));
print(
DartFormatter(languageVersion: DartFormatter.latestLanguageVersion)
.format('${library.accept(emitter)}'),
);
}
```

Expand Down Expand Up @@ -103,7 +109,7 @@ run from the snapshot instead of from source to avoid problems with deleted
files. These steps must be run without deleting the source files.
```bash
./tool/regenerate.sh
./tool/regenerate.sh
```
[build_runner]: https://pub.dev/packages/build_runner
4 changes: 3 additions & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';

final _dartfmt = DartFormatter();
final _dartfmt = DartFormatter(
languageVersion: DartFormatter.latestLanguageVersion,
);

void main() {
print('animalClass():\n${'=' * 40}\n${animalClass()}');
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ dev_dependencies:
build_runner: ^2.0.3
built_value_generator: ^8.0.0
dart_flutter_team_lints: ^3.0.0
dart_style: ^2.3.4
dart_style: ^2.3.7
source_gen: ^1.0.0
test: ^1.16.0
9 changes: 6 additions & 3 deletions test/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import 'package:code_builder/code_builder.dart';
import 'package:dart_style/dart_style.dart';

final DartFormatter _dartfmt = DartFormatter();
String _format(String source) {
final formatter = DartFormatter(
languageVersion: DartFormatter.latestLanguageVersion,
);

try {
return _dartfmt.format(source);
return formatter.format(source);
} on FormatterException catch (_) {
return _dartfmt.formatStatement(source);
return formatter.formatStatement(source);
}
}

Expand Down

0 comments on commit 7ae5a3c

Please sign in to comment.