Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions lib/method_source/code_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ def complete_expression?(str)
eval("BEGIN{throw :valid}\n#{str}")
end

# Assert that a line which ends with a , or \ is incomplete.
str !~ /[,\\]\s*\z/
no_ending_comma?(str) || backslashed_string?(str)
rescue IncompleteExpression
false
ensure
Expand All @@ -81,6 +80,32 @@ def complete_expression?(str)

private

# Determine if a representation of a string as a string ends with comma.
#
# @param [String] The string to validate.
# @return [Boolean] Whether or not the string ends with comma
def no_ending_comma?(str)
str !~ /[,\\]\s*\z/
end

# Determine if a backslashed string as a string,
# after removing the matching backslashes, ends with
# comma or a backslash.
#
# @param [String] The string to validate.
# @return [Boolean] Whether or not the remaining string ends with
# comma or backslash.
#
# @example
# backslashed_string?("%\a\\") #=> true
# backslashed_string?("%\\\\") #=> true
# backslashed_string?("%\abc\\") #=> true
def backslashed_string?(str)
remaining_str = str[/%\\(.*)\\\s*\z/, 1]

!remaining_str.nil? && remaining_str !~ /[,\\]\s*\z/
end

# Get the first expression from the input.
#
# @param [Array<String>] lines
Expand Down
10 changes: 10 additions & 0 deletions test/test_code_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
end
end

[
'%\a\\',
'%\abc\\',
'%\\\\',
].each do |line|
it "should not complain on backslashed strings: #{line}" do
@tester.complete_expression?(line).should == true
end
end

[
["end"],
["puts )("],
Expand Down