Skip to content
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

Fix #979 Address crashes with three state boolean detection #980

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 4 additions & 1 deletion lib/rubocop/cop/rails/three_state_boolean_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def on_send(node)

def_node = node.each_ancestor(:def, :defs).first
table_node = table_node(node)

return unless table_node && column_node

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following line calling change_column_null?, I read as: "if we can find code that changes the column nullness later, then don't add a violation". But if that code can't be found, because there's no enclosing def block to search, then the exception can't be made, and it will report a violation, maybe causing a false positive.

To be consistent with the above, if it can't find the nullness change because other parts of the context are missing (like the table name), I think it should also skip the exception and report a violation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not 100% clear here, are you suggesting:

return if def_node && change_column_null?(def_node, table_node&.value, column_node&.value)

Or something else?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would go with

return if def_node && table_node && column_node && \
  change_column_null?(def_node, table_node.value, column_node.value)


return if def_node && change_column_null?(def_node, table_node.value, column_node.value)

add_offense(node)
Expand All @@ -60,7 +63,7 @@ def table_node(node)
node.first_argument
when :column, :boolean
ancestor = node.each_ancestor(:block).find do |n|
n.method?(:create_table) || n.method?(:change_table)
n.method?(:create_table) || n.method?(:change_table) || n.method?(:drop_table)
end
ancestor&.send_node&.first_argument
end
Expand Down