Skip to content

Commit f9f3ade

Browse files
committed
Pass 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 the two calls to `DartFormatter()` in intl_translation/bin to pass in a language version. It attempts to look for a surrounding package_config.json file and uses the language version from that (which is what other Dart tools do). If that files, it just defaults to using the latest language version that the formatter supports.
1 parent f30ad0f commit f9f3ade

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

pkgs/intl_translation/bin/make_examples_const.dart

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import 'dart:io';
1010

1111
import 'package:args/args.dart';
1212
import 'package:dart_style/dart_style.dart';
13+
import 'package:intl_translation/src/language_version.dart';
1314
import 'package:intl_translation/src/message_rewriter.dart';
1415
import 'package:intl_translation/src/messages/main_message.dart';
1516

16-
void main(List<String> args) {
17+
Future<void> main(List<String> args) async {
1718
var parser = ArgParser();
1819
var rest = parser.parse(args).rest;
1920
if (rest.isEmpty) {
@@ -24,7 +25,6 @@ void main(List<String> args) {
2425
exit(0);
2526
}
2627

27-
var formatter = DartFormatter();
2828
for (var inputFile in rest) {
2929
var outputFile = inputFile;
3030
var file = File(inputFile);
@@ -35,6 +35,11 @@ void main(List<String> args) {
3535
} else {
3636
print('Writing new source to $outputFile');
3737
var out = File(outputFile);
38+
39+
var languageVersion = (await findPackageLanguageVersion(file)) ??
40+
DartFormatter.latestLanguageVersion;
41+
var formatter = DartFormatter(languageVersion: languageVersion);
42+
3843
out.writeAsStringSync(formatter.format(newSource));
3944
}
4045
}

pkgs/intl_translation/bin/rewrite_intl_messages.dart

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ import 'dart:io';
1616

1717
import 'package:args/args.dart';
1818
import 'package:dart_style/dart_style.dart';
19+
import 'package:intl_translation/src/language_version.dart';
1920
import 'package:intl_translation/src/message_rewriter.dart';
2021

2122
String? outputFileOption = 'transformed_output.dart';
2223

2324
bool useStringSubstitution = true;
2425
bool replace = false;
2526

26-
void main(List<String> args) {
27+
Future<void> main(List<String> args) async {
2728
var parser = ArgParser();
2829
parser.addOption('output',
2930
defaultsTo: 'transformed_output.dart',
@@ -55,7 +56,6 @@ void main(List<String> args) {
5556
exit(0);
5657
}
5758

58-
var formatter = DartFormatter();
5959
for (var inputFile in rest) {
6060
var outputFile = replace ? inputFile : outputFileOption;
6161
var file = File(inputFile);
@@ -67,6 +67,11 @@ void main(List<String> args) {
6767
} else {
6868
print('Writing new source to $outputFile');
6969
var out = File(outputFile!);
70+
71+
var languageVersion = (await findPackageLanguageVersion(file)) ??
72+
DartFormatter.latestLanguageVersion;
73+
var formatter = DartFormatter(languageVersion: languageVersion);
74+
7075
out.writeAsStringSync(formatter.format(newSource));
7176
}
7277
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
import 'dart:async';
5+
import 'dart:io';
6+
7+
import 'package:package_config/package_config.dart';
8+
import 'package:pub_semver/pub_semver.dart';
9+
10+
/// Looks for a package surrounding [file] and, if found, returns the default
11+
/// language version specified by that package.
12+
Future<Version?> findPackageLanguageVersion(File file) async {
13+
try {
14+
var config = await findPackageConfig(file.parent);
15+
if (config?.packageOf(file.absolute.uri)?.languageVersion
16+
case var languageVersion?) {
17+
return Version(languageVersion.major, languageVersion.minor, 0);
18+
}
19+
} catch (error) {
20+
// If we fail to find or read a config, just silently do nothing and
21+
// default to the latest language version.
22+
}
23+
24+
return null;
25+
}

pkgs/intl_translation/pubspec.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ dependencies:
1818
args: ^2.0.0
1919
dart_style: ^2.0.0
2020
intl: '>=0.18.0 <0.20.0'
21+
package_config: ^2.1.0
2122
path: ^1.0.0
23+
pub_semver: '>=1.4.4 <3.0.0'
2224

2325
dev_dependencies:
2426
lints: ^4.0.0

0 commit comments

Comments
 (0)