messageFromMethodInvocation method

Message? messageFromMethodInvocation(
  1. MethodInvocation node
)

Create a MainMessage from node using the name and parameters of the last function/method declaration we encountered and the parameters to the Intl.message call.

Implementation

Message? messageFromMethodInvocation(MethodInvocation node) {
  SubMessage message;
  Map<String, Expression> arguments;
  switch (node.methodName.name) {
    case 'gender':
      message = Gender();
      arguments = SubMessage.argumentsOfInterestFor(node);
      break;
    case 'plural':
      message = Plural();
      arguments = SubMessage.argumentsOfInterestFor(node);
      break;
    case 'select':
      message = Select();
      arguments = Select.argumentsOfInterestFor(node);
      break;
    default:
      throw MessageExtractionException(
          'Invalid plural/gender/select message ${node.methodName.name} '
          'in $node');
  }
  message.parent = parent;

  var buildNotJustCollectErrors = true;
  arguments.forEach((key, Expression value) {
    try {
      var interpolation = InterpolationVisitor(
        message,
        extraction,
      );
      value.accept(interpolation);
      if (buildNotJustCollectErrors) {
        message[key] = interpolation.pieces;
      }
    } on MessageExtractionException catch (e) {
      buildNotJustCollectErrors = false;
      var errString = (StringBuffer()
            ..writeAll(['Error ', e, '\nProcessing <', node, '>'])
            ..write(extraction.reportErrorLocation(node)))
          .toString();
      extraction.onMessage(errString);
      extraction.warnings.add(errString);
    }
  });
  var mainArg = node.argumentList.arguments
      .firstWhere((each) => each is! NamedExpression);
  if (mainArg is SimpleStringLiteral) {
    message.mainArgument = mainArg.toString();
  } else if (mainArg is SimpleIdentifier) {
    message.mainArgument = mainArg.name;
  } else {
    var errString = (StringBuffer()
          ..write('Error (Invalid argument to plural/gender/select, '
              'must be simple variable reference) '
              '\nProcessing <$node>')
          ..write(extraction.reportErrorLocation(node)))
        .toString();
    extraction.onMessage(errString);
    extraction.warnings.add(errString);
  }

  return message;
}