Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass a language version to DartFormatter(). #901

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkgs/intl_translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 0.20.1-wip
* Add topics to `pubspec.yaml`
* Update to `dart_style `2.3.7`. `bin/make_examples_const.dart` and
`rewrite_intl_messages.dart` will now look for a surrounding
`.dart_tool/package_config.json` file to infer the language version of the
files updated by the script.

## 0.20.0
* Throw if the `Intl.select` `arg` is not in the list of `args`.
Expand Down Expand Up @@ -30,7 +34,7 @@
* Remove petit_parser dependency.
* Address analyzer deprecations, see [#168](https://github.com/dart-lang/intl_translation/issues/168).
* Migrate to null safety.

## 0.17.10+1
* Generate code that passes analysis with `implicit-casts: false`.
* Allow use of `MessageExtraction` and `MessageGeneration` without `File`.
Expand Down
9 changes: 7 additions & 2 deletions pkgs/intl_translation/bin/make_examples_const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> args) {
Future<void> main(List<String> args) async {
var parser = ArgParser();
var rest = parser.parse(args).rest;
if (rest.isEmpty) {
Expand All @@ -24,7 +25,6 @@ void main(List<String> args) {
exit(0);
}

var formatter = DartFormatter();
for (var inputFile in rest) {
var outputFile = inputFile;
var file = File(inputFile);
Expand All @@ -35,6 +35,11 @@ void main(List<String> 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));
}
}
Expand Down
9 changes: 7 additions & 2 deletions pkgs/intl_translation/bin/rewrite_intl_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ 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';

bool useStringSubstitution = true;
bool replace = false;

void main(List<String> args) {
Future<void> main(List<String> args) async {
var parser = ArgParser();
parser.addOption('output',
defaultsTo: 'transformed_output.dart',
Expand Down Expand Up @@ -55,7 +56,6 @@ void main(List<String> args) {
exit(0);
}

var formatter = DartFormatter();
for (var inputFile in rest) {
var outputFile = replace ? inputFile : outputFileOption;
var file = File(inputFile);
Expand All @@ -67,6 +67,11 @@ void main(List<String> 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));
}
}
Expand Down
25 changes: 25 additions & 0 deletions pkgs/intl_translation/lib/src/language_version.dart
Original file line number Diff line number Diff line change
@@ -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<Version?> 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;
}
2 changes: 2 additions & 0 deletions pkgs/intl_translation/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading