diff --git a/slang/lib/src/runner/analyze.dart b/slang/lib/src/runner/analyze.dart index 45643640..fab27e32 100644 --- a/slang/lib/src/runner/analyze.dart +++ b/slang/lib/src/runner/analyze.dart @@ -347,9 +347,16 @@ void _getUnusedTranslationsInSourceCodeRecursive({ /// and joins them into a single (huge) string without any spaces. String loadSourceCode(List 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(); diff --git a/slang/test/unit/runner/analyze_test.dart b/slang/test/unit/runner/analyze_test.dart index ef62a6a1..fe4f75af 100644 --- a/slang/test/unit/runner/analyze_test.dart +++ b/slang/test/unit/runner/analyze_test.dart @@ -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'); + }); }); }