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

feat: Extracting the type for arguments to arb files #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions lib/extract_messages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,10 @@ class MessageFindingVisitor extends GeneralizingAstVisitor {
message.sourcePosition = node.offset;
message.endPosition = node.end;
message.arguments = parameters?.parameters
.map((x) => x.name?.lexeme)
.where((element) => element != null)
.cast<String>()
.where((element) => element.name?.lexeme != null)
.map((x) => x is SimpleFormalParameter ?
MessageArgument(type: x.type?.toString(), name: x.name!.lexeme) :
MessageArgument(name: x.name!.lexeme))
.toList() ??
[];
var arguments = node.argumentList.arguments;
Expand Down Expand Up @@ -543,7 +544,7 @@ class InterpolationVisitor extends SimpleAstVisitor {
}

void handleSimpleInterpolation(InterpolationExpression node) {
var index = arguments.indexOf(node.expression.toString());
var index = arguments.indexWhere((x) => x.name == node.expression.toString());
if (index == -1) {
throw new IntlMessageExtractionException(
"Cannot find argument ${node.expression}");
Expand Down
13 changes: 8 additions & 5 deletions lib/src/arb_generation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,23 @@ Map arbMetadata(MainMessage message) {
var placeholders = {};
var placeholderNames = [];
for (var arg in message.arguments) {
placeholderNames.add(arg);
placeholderNames.add(arg.name);
addArgumentFor(message, arg, placeholders);
}
out["placeholders_order"] = placeholderNames;
out["placeholders"] = placeholders;
return out;
}

void addArgumentFor(MainMessage message, String arg, Map result) {
void addArgumentFor(MainMessage message, MessageArgument arg, Map result) {
var extraInfo = {};
if (message.examples.isNotEmpty && message.examples[arg] != null) {
extraInfo["example"] = message.examples[arg];
if (arg.type != null) {
extraInfo["type"] = arg.type;
}
result[arg] = extraInfo;
if (message.examples.isNotEmpty && message.examples[arg.name] != null) {
extraInfo["example"] = message.examples[arg.name];
}
result[arg.name] = extraInfo;
}

/// Return a version of the message string with with ICU parameters "{variable}"
Expand Down
12 changes: 11 additions & 1 deletion lib/src/intl_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ class MainMessage extends ComplexMessage {
String? id;

/// The arguments list from the Intl.message call.
List<String> arguments = [];
List<MessageArgument> arguments = [];

/// The locale argument from the Intl.message call
String? locale;
Expand Down Expand Up @@ -977,3 +977,13 @@ class IntlMessageExtractionException implements Exception {

String toString() => "IntlMessageExtractionException: $message";
}

// An argument for a message. Arguments might have a type and must have a name.
class MessageArgument {
final String? type;
final String name;

const MessageArgument({this.type, required this.name});

String toString() => "$type $name";
}