New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Empty array instead of nil for source_extract #36532
Empty array instead of nil for source_extract #36532
Conversation
90cd90d
to
73fc006
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good to merge this, but I want a second opinion as this could be considered overly defensive programming.
73fc006
to
8cf47a4
Compare
I've been looking into the surrounding code and I don't My take is that we should simplify def formatted_code_for(source_code, line_counter, indent)
indent_template = "%#{indent}s: %s"
source_code.map.with_index(line_counter) do |line, counter|
indent_template % [counter, line]
end
end Otherwise calling
Finally |
f230f18
to
ce2bd04
Compare
@kaspth good catch 👍 I've removed the This is a much cleaner solution ❤️ |
ce2bd04
to
48eb4d8
Compare
48eb4d8
to
f33dd81
Compare
The source_extract method will return nil when it can't find the file name in the backtrace, methods that consume this method expect an array and the nil ends up causing type errors down the road like it happened here: rails#36341. This patch refactors the source_extract method so that it returns an empty array instead of nil when it can't find the source code. Co-authored-by: Kasper Timm Hansen <kaspth@gmail.com>
f33dd81
to
526a5eb
Compare
❤️💪 |
…rce_code Empty array instead of nil for source_extract
Summary
The source_extract method will return nil when it can't find the file name in
the backtrace, methods that consume this method expect an array and the nil ends
up causing type errors down the road like it happened here: #36341. This patch
introduces a to_a call that converts the nil into an empty array this makes it so
client classes can correctly handle a empty list of source code lines and not
trigger a type error.
Other Information
Note this is a bit defensive and preventative imo, this occured for the edge case described here: #36341 (comment) and potentially other unknown code paths could still cause this behaviour to occur, but I don't know of any.
At any rate I do like having the method return a
[]
instead of a nil and thing that is a better interface for this class in general since the method will be returning consistent types.discussed this with @rafaelfranca and @gmcgibbon