processParameterLines method

List<Parameter> processParameterLines(
  1. List<String> parameterLines,
  2. List<String> remainingLines, {
  3. bool intialIsOptional = false,
})

Implementation

List<Parameter> processParameterLines(
  List<String> parameterLines,
  List<String> remainingLines, {
  bool intialIsOptional = false,
}) {
  var debuggingLines = fileDebugLvl > 0;
  var parameters = <Parameter>[];

  if (debuggingLines) {
    print('intialIsOptional: $intialIsOptional');
  }

  var isOptional = intialIsOptional;
  var skipLines = 0;
  parameterLines.forEach((parameterLine) {
    var line = WhiteSpaceRemover(line: parameterLine).removeFromStart();

    // Indicates if we should just this line ?
    var skipOnlyThisLine = false;

    // This means that this is a modifier and not a field
    // so we should just skip it
    if (line.startsWith('@')) {
      skipOnlyThisLine = true;
    }

    // This means that this is start of a Multi-Line object initialization
    // so we should start skipping lines until this initialization ends.
    if (line.endsWith('(')) {
      skipLines += 1;
    }

    if ((skipLines == 0) || skipOnlyThisLine) {
      if (line.endsWith(',') || line.endsWith(', {')) {
        var _isOptional = isOptional;
        if (line.endsWith(', {')) {
          // This means that all fields after this will be optional to specify
          isOptional = true;
          line = line.split(', {').first;
        } else {
          line = line.substring(0, line.length - 1);
        }

        var defaultValue;

        var lineParts = line.split('=');
        if (lineParts.length == 2) {
          defaultValue =
              WhiteSpaceRemover(line: lineParts.last).removeFromStart();
          line = WhiteSpaceRemover(line: lineParts.first).removeFromEnd();
        }

        // Extract 'required'
        var isRequired = line.startsWith('required');
        line = line.split('required ').last;

        // Determine if it's final type field
        var isFinal = line.contains('this.');
        line = line.replaceAll('this.', '');

        if (line.contains('[') && line.contains(']')) {
          line = line.replaceAll('[', '').replaceAll(']', '');
          line = WhiteSpaceRemover(line: line).remove();

          _isOptional = true;
          isRequired = false;
          isFinal = true;
        }

        lineParts = line.split(' ');

        late String name;
        late String? type;
        if (lineParts.length > 1) {
          name = line.split(' ').last;
          type = line.split(' $name').first;
        } else {
          name = line;
          type = null;
        }

        if (type == null) {
          for (var i = 0; i < remainingLines.length; i++) {
            var remainingLine = remainingLines[i];

            var regExStr = 'final .* $name;';
            var regEx = RegExp(regExStr);

            if (regEx.hasMatch(remainingLine)) {
              var match = regEx.firstMatch(remainingLine);
              if (match != null) {
                var match0 = match.group(0);
                if (match0 != null) {
                  type = match0.split('final ')[1].split(' $name;')[0];
                }
              }

              break;
            }
          }
        }

        var parameter = Parameter(
          isFinal: isFinal,
          name: name,
          type: type,
          isOptional: _isOptional,
          defaultValue: defaultValue,
          isRequired: isRequired,
          rawLine: parameterLine,
        );

        parameters.add(parameter);
      }
    }

    // detects last of skipping lines.
    if (skipLines > 0) {
      if (line == '},') {
        skipLines -= 1;
      }
      if (line == '),') {
        skipLines -= 1;
      }
    }
  });

  return parameters;
}