Skip to content

Commit f746ad0

Browse files
committed
Improved import regex
1 parent 2a6699e commit f746ad0

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

swift_code_metrics/_helpers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
class AnalyzerHelpers:
5-
65
# Constants
76

87
SWIFT_FILE_EXTENSION = '.swift'
@@ -161,7 +160,6 @@ def is_path_in_list(subdir, exclude_paths):
161160

162161

163162
class ParsingHelpers:
164-
165163
# Constants
166164

167165
DEFAULT_FRAMEWORK_NAME = 'AppTarget'
@@ -174,7 +172,7 @@ class ParsingHelpers:
174172
END_COMMENT = '\*/$'
175173
SINGLE_COMMENT = '^//'
176174

177-
IMPORTS = '(?<=^import)(?:[^.]+)\\b(\w+)'
175+
IMPORTS = '(?<=^import )(?:\\b\w+\s|)([^.; ]+)'
178176

179177
PROTOCOLS = '.*protocol (.*?)[:|{|\s]'
180178
STRUCTS = '.*struct (.*?)[:|{|\s]'

swift_code_metrics/tests/test_helper.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ def test_helpers_single_comment_check_existence_expectedTrue(self):
3636
string = '// True single line comment'
3737
self.assertTrue(_helpers.ParsingHelpers.check_existence(regex, string))
3838

39-
4039
# Imports
4140

4241
def test_helpers_imports_extract_substring_with_pattern_expectFalse(self):
4342
regex = _helpers.ParsingHelpers.IMPORTS
4443
string = '//import Foundation '
4544
self.assertEqual('', _helpers.ParsingHelpers.extract_substring_with_pattern(regex, string))
4645

46+
def test_helpers_imports_leading_semicolon_expectFalse(self):
47+
regex = _helpers.ParsingHelpers.IMPORTS
48+
string = 'import Foundation;'
49+
self.assertEqual('Foundation', _helpers.ParsingHelpers.extract_substring_with_pattern(regex, string))
50+
4751
def test_helpers_imports_extract_substring_with_pattern_expectTrue(self):
4852
regex = _helpers.ParsingHelpers.IMPORTS
49-
string = 'import Foundation'
53+
string = 'import Foundation '
5054
self.assertEqual('Foundation', _helpers.ParsingHelpers.extract_substring_with_pattern(regex, string))
5155

5256
def test_helpers_imports_submodule_expectTrue(self):
@@ -59,6 +63,11 @@ def test_helpers_imports_subcomponent_expectTrue(self):
5963
string = 'import struct Foundation.KeyChain'
6064
self.assertEqual('Foundation', _helpers.ParsingHelpers.extract_substring_with_pattern(regex, string))
6165

66+
def test_helpers_imports_specialwords_expectFalse(self):
67+
regex = _helpers.ParsingHelpers.IMPORTS
68+
string = 'importedMigratedComponents: AnyImportedComponent'
69+
self.assertEqual('', _helpers.ParsingHelpers.extract_substring_with_pattern(regex, string))
70+
6271
# Protocols
6372

6473
def test_helpers_protocols_extract_substring_with_pattern_expectFalse(self):
@@ -147,5 +156,6 @@ def test_helpers_funcs_property_modifier_extract_substring_with_pattern_expectTr
147156
string = 'private func funzione(){'
148157
self.assertEqual('funzione', _helpers.ParsingHelpers.extract_substring_with_pattern(regex, string))
149158

159+
150160
if __name__ == '__main__':
151161
unittest.main()

0 commit comments

Comments
 (0)