processParameterLines method

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

Implementation

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

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

  var isOptional = intialIsOptional;
  var skipLines = 0;
  parameterLines.forEach((parameterLine) {
    // Use this when debugging parsing of a particular line.
    // if ((skipLines == 0)) {
    //   print('');
    //   print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
    //   print('$currentConstructorName @ $currentFileName');
    //   print('$parameterLine');
    //   debuggingLines = true;
    // }

    // var line = parameterLine.split('    ').last;
    var line = WhiteSpaceRemover(line: parameterLine).removeFromStart();

    if (debuggingLines) {
      print('1. $line');
    }

    // 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);
        }

        if (debuggingLines) {
          print('1.5. $line [$_isOptional]');
        }

        if (debuggingLines) {
          print('2. $line');
        }

        var defaultValue;

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

        if (debuggingLines) {
          print('3. $line [$defaultValue]');
        }

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

        if (debuggingLines) {
          print('4. $line [$isRequired]');
        }

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

        if (debuggingLines) {
          print('5. $line [$isFinal]');
        }

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

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

          if (debuggingLines) {
            print('5.5. $lineBefore => $line');
          }
        }

        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 (line.split(' ').length == 2) {
        //   lineParts = line.split(' ');
        //   type = lineParts.first;
        //   line = lineParts.last;
        // }

        if (debuggingLines) {
          print('6. [$type] [$name]');
        }

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

        // print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
        // print('$parameterLine');
        // print('$parameter');
        // print('');

        parameters.add(parameter);
      }
    }

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

  return parameters;
}