countSloc function

int countSloc(
  1. File dartFile
)

Return the number of lines of code ignoring comments and blank lines. This is a naive SLOC counter. It doesn't deal correctly with comments inside multi-line strings or multi-line comments in the middle of code.

Implementation

int countSloc(File dartFile) {
  var lines = dartFile.readAsLinesSync();
  var sloc = 0;
  var multilineComment = false;
  for (var line in lines) {
    line = line.trim();
    if (line.startsWith('//')) {
      continue;
    }
    if (line == '') {
      continue;
    }
    if (line.startsWith('/*')) {
      multilineComment = true;
      continue;
    }
    if (multilineComment) {
      if (line.endsWith('*/')) {
        multilineComment = false;
      }
      continue;
    }
    sloc += 1;
  }
  return sloc;
}