Skip to content

Commit 7bd23ff

Browse files
authored
Merge pull request #24 from aboutcode-org/23-fix-mutation-ending-without-newline
Fix mutation in files ending without a newline
2 parents 8e12eae + a0e8353 commit 7bd23ff

File tree

4 files changed

+13
-1
lines changed

4 files changed

+13
-1
lines changed

src/matchcode_toolkit/stemming.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ def get_stem_code(location):
149149

150150
# Apply mutations bottom-up
151151
mutations = dict(sorted(mutations.items(), reverse=True))
152-
text = source.decode()
152+
# Ensure the text always ends with a newline character.
153+
text = source.decode().rstrip("\n") + "\n"
153154
cur_count = 0
154155
lines = text.splitlines(keepends=True)
155156
successive_line_count = [cur_count := cur_count + len(line) for line in lines]

tests/test_stemming.py

+6
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,9 @@ def test_rust_code_stemming(self):
7878
expected_file_location = self.test_data_dir / "rust/metrics-stemmeds.rs"
7979
results = stemming.get_stem_code(location=str(file_location))
8080
check_against_expected_code_file(results, expected_file_location)
81+
82+
def test_javascript_code_stemming_endwith_no_newline(self):
83+
file_location = self.test_data_dir / "javascript/main.js"
84+
expected_file_location = self.test_data_dir / "javascript/main-stemmed.js"
85+
results = stemming.get_stem_code(location=str(file_location))
86+
check_against_expected_code_file(results, expected_file_location)

tests/testfiles/stemming/javascript/main-stemmed.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testfiles/stemming/javascript/main.js

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)