Skip to content

Commit

Permalink
Merge pull request #200 from nikaera/fix/exclude-comments-for-analyze
Browse files Browse the repository at this point in the history
fix: exclude comments from analysis during the execution of 'analyze'
  • Loading branch information
Tienisto authored Mar 17, 2024
2 parents 306e642 + 19e71bb commit 94c213c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
11 changes: 9 additions & 2 deletions slang/lib/src/runner/analyze.dart
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,16 @@ void _getUnusedTranslationsInSourceCodeRecursive({
/// and joins them into a single (huge) string without any spaces.
String loadSourceCode(List<File> files) {
final buffer = StringBuffer();
final regex = RegExp(r'\s');
final spacesRegex = RegExp(r'\s');
final singleLineCommentsRegex = RegExp(r'//.*');
final multiLineCommentsRegex = RegExp(r'/\*.*?\*/', dotAll: true);

for (final file in files) {
buffer.write(file.readAsStringSync().replaceAll(regex, ''));
buffer.write(file
.readAsStringSync()
.replaceAll(singleLineCommentsRegex, '')
.replaceAll(multiLineCommentsRegex, '')
.replaceAll(spacesRegex, ''));
}

return buffer.toString();
Expand Down
24 changes: 24 additions & 0 deletions slang/test/unit/runner/analyze_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,29 @@ void main() {

expect(result, 'ABCDEFGH;');
});

test('should ignore inline comments', () {
final files = [
FakeFile('A // B\nC'),
FakeFile('D /* E */ F'),
FakeFile('G'),
];

final result = loadSourceCode(files);

expect(result, 'ACDFG');
});

test('should ignore block comments', () {
final files = [
FakeFile('A /* B\nC */ D'),
FakeFile('E // F'),
FakeFile('G //'),
];

final result = loadSourceCode(files);

expect(result, 'ADEG');
});
});
}

0 comments on commit 94c213c

Please sign in to comment.