Class: RuboCop::Cop::Homebrew::TernaryToIf Private
- Extended by:
- AutoCorrector
- Defined in:
- rubocops/ternary_to_if.rb
Overview
This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.
Prefers modifier if or unless when initialising a local variable with a nil branch.
Existing locals are excluded because skipping assignment would retain their values.
Example
# bad
source = reuse_platforms ? @sources[key] : nil
source = skip_platforms ? nil : @sources[key]
# good
source = @sources[key] if reuse_platforms
source = @sources[key] unless skip_platforms
Constant Summary collapse
- MSG =
This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.
"Use modifier `%<keyword>s` instead of a ternary with a `nil` fallback."
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.joining_forces ⇒ T.class_of(VariableForce)
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
27 28 29 |
# File 'rubocops/ternary_to_if.rb', line 27 def self.joining_forces VariableForce end |
Instance Method Details
#before_declaring_variable(variable, _variable_table) ⇒ void
This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.
This method returns an undefined value.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'rubocops/ternary_to_if.rb', line 32 def before_declaring_variable(variable, _variable_table) node = variable.declaration_node return unless node.is_a?(RuboCop::AST::AsgnNode) return unless node.lvasgn_type? return unless node.single_line? ternary = node.expression return unless ternary.is_a?(RuboCop::AST::IfNode) return unless ternary.ternary? keyword = if ternary.else_branch&.nil_type? "if" elsif ternary.if_branch&.nil_type? "unless" end return unless keyword # Keep generated conditions compatible with Style/UnlessLogicalOperators. return if keyword == "unless" && ternary.condition.each_node(:and, :or).any? return if ternary.each_descendant(:lvasgn, :match_var, :match_with_lvasgn).any? # Loops and retry can reach this declaration with an existing value. return if node.each_ancestor(:while, :until, :while_post, :until_post, :for, :rescue).any? return if node.each_ancestor(:block, :numblock, :itblock).any? do |block| block.each_descendant(:redo).any? end parent = node.parent if parent return if parent.is_a?(RuboCop::AST::DefNode) && parent.endless? return unless parent.type?(:begin, :kwbegin, :def, :defs, :block, :numblock, :itblock) end add_offense(node, message: format(MSG, keyword:)) do |corrector| corrector.replace(ternary, ((keyword == "if") ? ternary.if_branch : ternary.else_branch).source) corrector.insert_after(node, " #{keyword} #{ternary.condition.source}") end end |