Skip to content

Commit 5845d2a

Browse files
authored
Merge pull request #482 from StressTestor/fix/317-comment-line-continuation
fix(script): stop a '#' comment continuing past a trailing backslash (#317)
2 parents ceb899a + 712d44f commit 5845d2a

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

lizard_languages/script_language.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ def get_comment_from_token(token):
2424

2525
@staticmethod
2626
def generate_common_tokens(source_code, addition, match_holder=None):
27-
_until_end = r"(?:\\\n|[^\n])*"
27+
# A '#' line comment ends at the newline; a trailing backslash does
28+
# not continue it onto the next line (issue #317). C/C++ '//' comments,
29+
# which legitimately continue on a backslash, go through CodeReader
30+
# directly and are unaffected.
2831
return CodeReader.generate_tokens(
2932
source_code,
30-
r"|\#" + _until_end + addition,
33+
r"|\#[^\n]*" + addition,
3134
match_holder)

test/test_languages/testRuby.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,23 @@ def adapt_search_params
433433
self.assertEqual(2, len(result))
434434
self.assertEqual("adapt_widget_params", result[0].name)
435435
self.assertEqual("adapt_search_params", result[1].name)
436+
437+
438+
class Test_Ruby_comment_line_continuation(unittest.TestCase):
439+
"""A '#' comment ending in a backslash must not continue onto the next
440+
line and swallow it (issue #317, the shared-tokenizer half)."""
441+
442+
def test_backslash_at_end_of_comment_keeps_next_line(self):
443+
code = (
444+
"def foo(x)\n"
445+
" # a trailing comment\\\n"
446+
" if x > 10\n"
447+
" return 1\n"
448+
" end\n"
449+
" return 0\n"
450+
"end\n"
451+
)
452+
result = get_ruby_function_list(code)
453+
self.assertEqual(1, len(result))
454+
self.assertEqual("foo", result[0].name)
455+
self.assertEqual(2, result[0].cyclomatic_complexity)

0 commit comments

Comments
 (0)