Skip to content

Throw if the main argument in plurals does not match the argument list. #873

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

Merged
merged 6 commits into from
Jun 30, 2025
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
1 change: 1 addition & 0 deletions pkgs/intl_translation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.21.0-wip
* BREAKING CHANGE: Update `dart_style` to `^3.0.0`
* Allow analyzer `>=6.3.0 <8.0.0`
* Throw if the main argument in plurals and selects does not match the argument list.

## 0.20.1
* Add topics to `pubspec.yaml`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ abstract class SubMessage extends ComplexMessage {
List toJson() {
var json = [];
json.add(dartMessageName);
json.add(arguments.indexOf(mainArgument));
var indexOf = arguments.indexOf(mainArgument);
if (indexOf == -1) {
// This is an error as this should have been checked in
// lib/visitors/plural_gender_visitor.dart already.
throw ArgumentError('The argument `$mainArgument` could not be found in '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentionally an error and not an exception? Normally for data validation you'd want an exception (errors being more programming-time mistakes - calling a function with invalid arguments, ...).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is, as the validation happens somewhere else. If this fails here, something went wrong in the code in between..

'`$arguments`.');
}
json.add(indexOf);
for (var arg in codeAttributeNames) {
json.add(this[arg]?.toJson());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ class PluralAndGenderVisitor extends SimpleAstVisitor<void> {
extraction.warnings.add(errString);
}

if (!message.arguments.contains(message.mainArgument)) {
throw Exception(
'Argument `${message.mainArgument}` could not be found in '
'${message.arguments} while processing $node. The parent argument '
'name must match the one in the `Intl.` call.');
}

return message;
}
}
Loading