Sha256: 7860eb5b38722bf9dc55c5f67e97a3f64567014f0aac31beb789b17517a0238d

Contents?: true

Size: 696 Bytes

Versions: 73

Compression:

Stored size: 696 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # This auto-corrects parentheses
    class ParenthesesCorrector
      class << self
        def correct(corrector, node)
          corrector.remove(node.loc.begin)
          corrector.remove(node.loc.end)
          return unless ternary_condition?(node) && next_char_is_question_mark?(node)

          corrector.insert_after(node.loc.end, ' ')
        end

        private

        def ternary_condition?(node)
          node.parent&.if_type? && node.parent&.ternary?
        end

        def next_char_is_question_mark?(node)
          node.loc.last_column == node.parent.loc.question.column
        end
      end
    end
  end
end

Version data entries

73 entries across 73 versions & 8 rubygems

Version Path
rubocop-1.15.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
cocRb-0.1.0 .bundle/ruby/3.0.0/gems/rubocop-1.14.0/lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.14.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.13.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.12.1 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.12.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.11.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.10.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.9.1 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.9.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.8.1 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.8.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.7.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.6.1 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.6.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.5.2 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.5.1 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.5.0 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.4.2 lib/rubocop/cop/correctors/parentheses_corrector.rb
rubocop-1.4.1 lib/rubocop/cop/correctors/parentheses_corrector.rb