Sha256: 4c22fd84b5b1f7ee6fdb3d9400b336eb0f7f252c9969710e1d48d8b04c61768f
Contents?: true
Size: 1.03 KB
Versions: 22
Compression:
Stored size: 1.03 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Style # Checks for uses of semicolon in if statements. # # @example # # # bad # result = if some_condition; something else another_thing end # # # good # result = some_condition ? something : another_thing # class IfWithSemicolon < Base include OnNormalIfUnless extend AutoCorrector MSG = 'Do not use if x; Use the ternary operator instead.' def on_normal_if_unless(node) return unless node.else_branch beginning = node.loc.begin return unless beginning&.is?(';') add_offense(node) do |corrector| corrector.replace(node, correct_to_ternary(node)) end end private def correct_to_ternary(node) else_code = node.else_branch ? node.else_branch.source : 'nil' "#{node.condition.source} ? #{node.if_branch.source} : #{else_code}" end end end end end
Version data entries
22 entries across 22 versions & 3 rubygems
Version | Path |
---|---|
rubocop-0.90.0 | lib/rubocop/cop/style/if_with_semicolon.rb |
rubocop-0.89.1 | lib/rubocop/cop/style/if_with_semicolon.rb |