visitRecordLiteral method

  1. @override
void visitRecordLiteral(
  1. RecordLiteral node
)
override

Implementation

@override
void visitRecordLiteral(RecordLiteral node) {
  final recordType = () {
    final paramType = node.correspondingParameter?.type;
    if (paramType is RecordType) return paramType;

    final declaredType = node.findDeclaredType();
    if (declaredType is RecordType) return declaredType;

    return null;
  }();
  if (recordType == null) return;

  var positionalIndex = 0;
  for (final field in node.fields) {
    final expression = switch (field) {
      NamedExpression(:final expression) => expression,
      _ => field,
    };

    if (expression.isDotShorthand) {
      if (field is! NamedExpression) positionalIndex++;
      continue;
    }

    // Get the expected type for this field
    final fieldName = field is NamedExpression ? field.name.label.name : null;
    final fieldType = recordType.getFieldTypeByNameOrIndex(
      positionalIndex,
      fieldName,
    );

    if (fieldType != null) {
      _checkAndReport(expression: expression, declaredType: fieldType);
    }

    if (field is! NamedExpression) positionalIndex++;
  }
}