Skip to content

Commit

Permalink
Update dart_style to pass in a language version to DartFormatter. (da…
Browse files Browse the repository at this point in the history
…rt-archive/code_builder#466)

* Update dart_style to pass in a language version to DartFormatter.

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.

* Update CHANGELOG and analysis_options.yaml.

* Require Dart SDK 3.5.0.
  • Loading branch information
munificent authored Oct 23, 2024
1 parent faf9ea1 commit e4785f7
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
4 changes: 2 additions & 2 deletions pkgs/code_builder/.github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ env:

jobs:
# Check code formatting and static analysis on a single OS (linux)
# against Dart dev and 2.12.0.
# against Dart dev and an earlier stable version.
analyze:
runs-on: ubuntu-latest
strategy:
Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
sdk: [3.1.0, dev]
sdk: [3.5.0, dev]
steps:
- uses: actions/checkout@d632683dd7b4114ad314bca15554477dd762a938
- uses: dart-lang/setup-dart@0a8a0fc875eb934c15d08629302413c671d3f672
Expand Down
3 changes: 2 additions & 1 deletion pkgs/code_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 4.10.1-wip

* Require Dart `^3.1.0`
* Require Dart `^3.5.0`
* Upgrade to `dart_style` 2.3.7.

## 4.10.0

Expand Down
12 changes: 9 additions & 3 deletions pkgs/code_builder/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
1 change: 0 additions & 1 deletion pkgs/code_builder/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ linter:
- missing_whitespace_between_adjacent_strings
- no_adjacent_strings_in_list
- no_runtimeType_toString
- package_api_docs
- prefer_const_declarations
- prefer_expression_function_bodies
- prefer_final_locals
Expand Down
4 changes: 3 additions & 1 deletion pkgs/code_builder/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
4 changes: 2 additions & 2 deletions pkgs/code_builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A fluent, builder-based library for generating valid Dart code.
repository: https://github.com/dart-lang/code_builder

environment:
sdk: ^3.1.0
sdk: ^3.5.0

dependencies:
built_collection: ^5.0.0
Expand All @@ -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 pkgs/code_builder/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 e4785f7

Please sign in to comment.