From f9f3ade2dcc3a2753bd78d080f51afa0eb6e96d4 Mon Sep 17 00:00:00 2001 From: Robert Nystrom Date: Thu, 24 Oct 2024 18:20:25 -0700 Subject: [PATCH] Pass a language version to DartFormatter(). We're in [the process of](https://github.com/dart-lang/dart_style/issues/1403) moving dart_style to [a new formatting style](https://github.com/dart-lang/dart_style/issues/1253). That involves making the formatter [aware of the language version of what it's formatting](https://github.com/dart-lang/dart_style/issues/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. --- .../bin/make_examples_const.dart | 9 +++++-- .../bin/rewrite_intl_messages.dart | 9 +++++-- .../lib/src/language_version.dart | 25 +++++++++++++++++++ pkgs/intl_translation/pubspec.yaml | 2 ++ 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 pkgs/intl_translation/lib/src/language_version.dart diff --git a/pkgs/intl_translation/bin/make_examples_const.dart b/pkgs/intl_translation/bin/make_examples_const.dart index bc8404a5..e77e4faf 100644 --- a/pkgs/intl_translation/bin/make_examples_const.dart +++ b/pkgs/intl_translation/bin/make_examples_const.dart @@ -10,10 +10,11 @@ import 'dart:io'; import 'package:args/args.dart'; import 'package:dart_style/dart_style.dart'; +import 'package:intl_translation/src/language_version.dart'; import 'package:intl_translation/src/message_rewriter.dart'; import 'package:intl_translation/src/messages/main_message.dart'; -void main(List args) { +Future main(List args) async { var parser = ArgParser(); var rest = parser.parse(args).rest; if (rest.isEmpty) { @@ -24,7 +25,6 @@ void main(List args) { exit(0); } - var formatter = DartFormatter(); for (var inputFile in rest) { var outputFile = inputFile; var file = File(inputFile); @@ -35,6 +35,11 @@ void main(List args) { } else { print('Writing new source to $outputFile'); var out = File(outputFile); + + var languageVersion = (await findPackageLanguageVersion(file)) ?? + DartFormatter.latestLanguageVersion; + var formatter = DartFormatter(languageVersion: languageVersion); + out.writeAsStringSync(formatter.format(newSource)); } } diff --git a/pkgs/intl_translation/bin/rewrite_intl_messages.dart b/pkgs/intl_translation/bin/rewrite_intl_messages.dart index 00a385df..bfd792e7 100644 --- a/pkgs/intl_translation/bin/rewrite_intl_messages.dart +++ b/pkgs/intl_translation/bin/rewrite_intl_messages.dart @@ -16,6 +16,7 @@ import 'dart:io'; import 'package:args/args.dart'; import 'package:dart_style/dart_style.dart'; +import 'package:intl_translation/src/language_version.dart'; import 'package:intl_translation/src/message_rewriter.dart'; String? outputFileOption = 'transformed_output.dart'; @@ -23,7 +24,7 @@ String? outputFileOption = 'transformed_output.dart'; bool useStringSubstitution = true; bool replace = false; -void main(List args) { +Future main(List args) async { var parser = ArgParser(); parser.addOption('output', defaultsTo: 'transformed_output.dart', @@ -55,7 +56,6 @@ void main(List args) { exit(0); } - var formatter = DartFormatter(); for (var inputFile in rest) { var outputFile = replace ? inputFile : outputFileOption; var file = File(inputFile); @@ -67,6 +67,11 @@ void main(List args) { } else { print('Writing new source to $outputFile'); var out = File(outputFile!); + + var languageVersion = (await findPackageLanguageVersion(file)) ?? + DartFormatter.latestLanguageVersion; + var formatter = DartFormatter(languageVersion: languageVersion); + out.writeAsStringSync(formatter.format(newSource)); } } diff --git a/pkgs/intl_translation/lib/src/language_version.dart b/pkgs/intl_translation/lib/src/language_version.dart new file mode 100644 index 00000000..3e02051d --- /dev/null +++ b/pkgs/intl_translation/lib/src/language_version.dart @@ -0,0 +1,25 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. +import 'dart:async'; +import 'dart:io'; + +import 'package:package_config/package_config.dart'; +import 'package:pub_semver/pub_semver.dart'; + +/// Looks for a package surrounding [file] and, if found, returns the default +/// language version specified by that package. +Future findPackageLanguageVersion(File file) async { + try { + var config = await findPackageConfig(file.parent); + if (config?.packageOf(file.absolute.uri)?.languageVersion + case var languageVersion?) { + return Version(languageVersion.major, languageVersion.minor, 0); + } + } catch (error) { + // If we fail to find or read a config, just silently do nothing and + // default to the latest language version. + } + + return null; +} diff --git a/pkgs/intl_translation/pubspec.yaml b/pkgs/intl_translation/pubspec.yaml index 6dd4cb0b..caeb6e3e 100644 --- a/pkgs/intl_translation/pubspec.yaml +++ b/pkgs/intl_translation/pubspec.yaml @@ -18,7 +18,9 @@ dependencies: args: ^2.0.0 dart_style: ^2.0.0 intl: '>=0.18.0 <0.20.0' + package_config: ^2.1.0 path: ^1.0.0 + pub_semver: '>=1.4.4 <3.0.0' dev_dependencies: lints: ^4.0.0