File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments