rubocop 1.9.0 → 1.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +3 -0
- data/lib/rubocop.rb +1 -0
- data/lib/rubocop/config.rb +4 -1
- data/lib/rubocop/cop/base.rb +1 -0
- data/lib/rubocop/cop/exclude_limit.rb +26 -0
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +16 -2
- data/lib/rubocop/cop/layout/line_length.rb +2 -1
- data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
- data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +3 -2
- data/lib/rubocop/cop/message_annotator.rb +4 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
- data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
- data/lib/rubocop/cop/mixin/code_length.rb +3 -1
- data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
- data/lib/rubocop/cop/mixin/method_complexity.rb +3 -1
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +35 -11
- data/lib/rubocop/cop/style/nil_comparison.rb +3 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
- data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +2 -0
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -4
- data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +3 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 86b427813730f667d5978bd4b4ca7e8f6766adf4ddf713e413cf07a0345b84b2
         | 
| 4 | 
            +
              data.tar.gz: ede1028f4ebe7b6b1cd59de4d604e655c7115d928d38393ebd7febdbbff5b3e7
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: b5e3ca723e7530916204e9bfce59bec1452232765a0903093ba2b4783fc35f109e655251c2fce8e3b08dbebcf4a4e67a4301b00c8113d45f5f0bb2a63cc4be7f
         | 
| 7 | 
            +
              data.tar.gz: d91068c77da3ea787a5c36c287f1913bc94c23d98fc315604883cb21742f4c1031014e10c9470c67dca2fa3dfdc41eb848134f0a9565ff5a82e65aed5d6d2cf3
         | 
    
        data/README.md
    CHANGED
    
    | @@ -45,7 +45,7 @@ gem 'rubocop', require: false | |
| 45 45 | 
             
            ```
         | 
| 46 46 |  | 
| 47 47 | 
             
            RuboCop is stable between major versions, both in terms of API and cop configuration.
         | 
| 48 | 
            -
            We aim  | 
| 48 | 
            +
            We aim to ease the maintenance of RuboCop extensions and the upgrades between RuboCop
         | 
| 49 49 | 
             
            releases. All big changes are reserved for major releases.
         | 
| 50 50 | 
             
            To prevent an unwanted RuboCop update you might want to use a conservative version lock
         | 
| 51 51 | 
             
            in your `Gemfile`:
         | 
    
        data/config/default.yml
    CHANGED
    
    | @@ -3521,6 +3521,9 @@ Style/IfWithBooleanLiteralBranches: | |
| 3521 3521 | 
             
              Description: 'Checks for redundant `if` with boolean literal branches.'
         | 
| 3522 3522 | 
             
              Enabled: pending
         | 
| 3523 3523 | 
             
              VersionAdded: '1.9'
         | 
| 3524 | 
            +
              SafeAutoCorrect: false
         | 
| 3525 | 
            +
              AllowedMethods:
         | 
| 3526 | 
            +
                - nonzero?
         | 
| 3524 3527 |  | 
| 3525 3528 | 
             
            Style/IfWithSemicolon:
         | 
| 3526 3529 | 
             
              Description: 'Do not use if x; .... Use the ternary operator instead.'
         | 
    
        data/lib/rubocop.rb
    CHANGED
    
    | @@ -35,6 +35,7 @@ require_relative 'rubocop/cop/offense' | |
| 35 35 | 
             
            require_relative 'rubocop/cop/message_annotator'
         | 
| 36 36 | 
             
            require_relative 'rubocop/cop/ignored_node'
         | 
| 37 37 | 
             
            require_relative 'rubocop/cop/autocorrect_logic'
         | 
| 38 | 
            +
            require_relative 'rubocop/cop/exclude_limit'
         | 
| 38 39 | 
             
            require_relative 'rubocop/cop/badge'
         | 
| 39 40 | 
             
            require_relative 'rubocop/cop/registry'
         | 
| 40 41 | 
             
            require_relative 'rubocop/cop/base'
         | 
    
        data/lib/rubocop/config.rb
    CHANGED
    
    
    
        data/lib/rubocop/cop/base.rb
    CHANGED
    
    
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module RuboCop
         | 
| 4 | 
            +
              # Allows specified configuration options to have an exclude limit
         | 
| 5 | 
            +
              # ie. a maximum value tracked that it can be used by `--auto-gen-config`.
         | 
| 6 | 
            +
              module ExcludeLimit
         | 
| 7 | 
            +
                # Sets up a configuration option to have an exclude limit tracked.
         | 
| 8 | 
            +
                # The parameter name given is transformed into a method name (eg. `Max`
         | 
| 9 | 
            +
                # becomes `self.max=` and `MinDigits` becomes `self.min_digits=`).
         | 
| 10 | 
            +
                def exclude_limit(parameter_name, method_name: transform(parameter_name))
         | 
| 11 | 
            +
                  define_method("#{method_name}=") do |value|
         | 
| 12 | 
            +
                    cfg = config_to_allow_offenses
         | 
| 13 | 
            +
                    cfg[:exclude_limit] ||= {}
         | 
| 14 | 
            +
                    current_max = cfg[:exclude_limit][parameter_name]
         | 
| 15 | 
            +
                    value = [current_max, value].max if current_max
         | 
| 16 | 
            +
                    cfg[:exclude_limit][parameter_name] = value
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                private
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                def transform(parameter_name)
         | 
| 23 | 
            +
                  parameter_name.gsub(/(?<!\A)(?=[A-Z])/, '_').downcase
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
              end
         | 
| 26 | 
            +
            end
         | 
| @@ -88,7 +88,7 @@ module RuboCop | |
| 88 88 | 
             
                    include RangeHelp
         | 
| 89 89 | 
             
                    extend AutoCorrector
         | 
| 90 90 |  | 
| 91 | 
            -
                    MSG = ' | 
| 91 | 
            +
                    MSG = 'Expected %<expected>s between %<type>s definitions; found %<actual>d.'
         | 
| 92 92 |  | 
| 93 93 | 
             
                    def self.autocorrect_incompatible_with
         | 
| 94 94 | 
             
                      [Layout::EmptyLines]
         | 
| @@ -107,19 +107,21 @@ module RuboCop | |
| 107 107 | 
             
                    end
         | 
| 108 108 |  | 
| 109 109 | 
             
                    def check_defs(nodes)
         | 
| 110 | 
            -
                       | 
| 110 | 
            +
                      count = blank_lines_count_between(*nodes)
         | 
| 111 | 
            +
             | 
| 112 | 
            +
                      return if line_count_allowed?(count)
         | 
| 111 113 | 
             
                      return if multiple_blank_lines_groups?(*nodes)
         | 
| 112 114 | 
             
                      return if nodes.all?(&:single_line?) &&
         | 
| 113 115 | 
             
                                cop_config['AllowAdjacentOneLineDefs']
         | 
| 114 116 |  | 
| 115 117 | 
             
                      correction_node = nodes.last
         | 
| 116 118 | 
             
                      location = correction_node.loc.keyword.join(correction_node.loc.name)
         | 
| 117 | 
            -
                      add_offense(location, message: message(correction_node)) do |corrector|
         | 
| 118 | 
            -
                        autocorrect(corrector, *nodes)
         | 
| 119 | 
            +
                      add_offense(location, message: message(correction_node, count: count)) do |corrector|
         | 
| 120 | 
            +
                        autocorrect(corrector, *nodes, count)
         | 
| 119 121 | 
             
                      end
         | 
| 120 122 | 
             
                    end
         | 
| 121 123 |  | 
| 122 | 
            -
                    def autocorrect(corrector, prev_def, node)
         | 
| 124 | 
            +
                    def autocorrect(corrector, prev_def, node, count)
         | 
| 123 125 | 
             
                      # finds position of first newline
         | 
| 124 126 | 
             
                      end_pos = end_loc(prev_def).end_pos
         | 
| 125 127 | 
             
                      source_buffer = end_loc(prev_def).source_buffer
         | 
| @@ -128,8 +130,6 @@ module RuboCop | |
| 128 130 | 
             
                      # Handle the case when multiple one-liners are on the same line.
         | 
| 129 131 | 
             
                      newline_pos = end_pos + 1 if newline_pos > node.source_range.begin_pos
         | 
| 130 132 |  | 
| 131 | 
            -
                      count = blank_lines_count_between(prev_def, node)
         | 
| 132 | 
            -
             | 
| 133 133 | 
             
                      if count > maximum_empty_lines
         | 
| 134 134 | 
             
                        autocorrect_remove_lines(corrector, newline_pos, count)
         | 
| 135 135 | 
             
                      else
         | 
| @@ -157,14 +157,22 @@ module RuboCop | |
| 157 157 | 
             
                      cop_config['EmptyLineBetweenModuleDefs'] && node.module_type?
         | 
| 158 158 | 
             
                    end
         | 
| 159 159 |  | 
| 160 | 
            -
                    def message(node)
         | 
| 161 | 
            -
                      type =  | 
| 162 | 
            -
             | 
| 163 | 
            -
             | 
| 164 | 
            -
                              | 
| 165 | 
            -
             | 
| 166 | 
            -
                              | 
| 167 | 
            -
             | 
| 160 | 
            +
                    def message(node, count: nil)
         | 
| 161 | 
            +
                      type = node_type(node)
         | 
| 162 | 
            +
             | 
| 163 | 
            +
                      format(MSG,
         | 
| 164 | 
            +
                             type: type,
         | 
| 165 | 
            +
                             expected: expected_lines,
         | 
| 166 | 
            +
                             actual: count)
         | 
| 167 | 
            +
                    end
         | 
| 168 | 
            +
             | 
| 169 | 
            +
                    def expected_lines
         | 
| 170 | 
            +
                      if allowance_range?
         | 
| 171 | 
            +
                        "#{minimum_empty_lines..maximum_empty_lines} empty lines"
         | 
| 172 | 
            +
                      else
         | 
| 173 | 
            +
                        lines = maximum_empty_lines == 1 ? 'line' : 'lines'
         | 
| 174 | 
            +
                        "#{maximum_empty_lines} empty #{lines}"
         | 
| 175 | 
            +
                      end
         | 
| 168 176 | 
             
                    end
         | 
| 169 177 |  | 
| 170 178 | 
             
                    def multiple_blank_lines_groups?(first_def_node, second_def_node)
         | 
| @@ -176,8 +184,7 @@ module RuboCop | |
| 176 184 | 
             
                      blank_start > non_blank_end
         | 
| 177 185 | 
             
                    end
         | 
| 178 186 |  | 
| 179 | 
            -
                    def  | 
| 180 | 
            -
                      count = blank_lines_count_between(first_def_node, second_def_node)
         | 
| 187 | 
            +
                    def line_count_allowed?(count)
         | 
| 181 188 | 
             
                      (minimum_empty_lines..maximum_empty_lines).cover?(count)
         | 
| 182 189 | 
             
                    end
         | 
| 183 190 |  | 
| @@ -230,6 +237,19 @@ module RuboCop | |
| 230 237 |  | 
| 231 238 | 
             
                      corrector.insert_after(where_to_insert, "\n" * difference)
         | 
| 232 239 | 
             
                    end
         | 
| 240 | 
            +
             | 
| 241 | 
            +
                    def node_type(node)
         | 
| 242 | 
            +
                      case node.type
         | 
| 243 | 
            +
                      when :def, :defs
         | 
| 244 | 
            +
                        :method
         | 
| 245 | 
            +
                      else
         | 
| 246 | 
            +
                        node.type
         | 
| 247 | 
            +
                      end
         | 
| 248 | 
            +
                    end
         | 
| 249 | 
            +
             | 
| 250 | 
            +
                    def allowance_range?
         | 
| 251 | 
            +
                      minimum_empty_lines != maximum_empty_lines
         | 
| 252 | 
            +
                    end
         | 
| 233 253 | 
             
                  end
         | 
| 234 254 | 
             
                end
         | 
| 235 255 | 
             
              end
         | 
| @@ -4,11 +4,14 @@ module RuboCop | |
| 4 4 | 
             
              module Cop
         | 
| 5 5 | 
             
                module Layout
         | 
| 6 6 | 
             
                  # This cop checks the indentation of the first argument in a method call.
         | 
| 7 | 
            -
                  # Arguments after the first one are checked by Layout/ArgumentAlignment | 
| 7 | 
            +
                  # Arguments after the first one are checked by `Layout/ArgumentAlignment`,
         | 
| 8 8 | 
             
                  # not by this cop.
         | 
| 9 9 | 
             
                  #
         | 
| 10 10 | 
             
                  # For indenting the first parameter of method _definitions_, check out
         | 
| 11 | 
            -
                  # Layout/FirstParameterIndentation | 
| 11 | 
            +
                  # `Layout/FirstParameterIndentation`.
         | 
| 12 | 
            +
                  #
         | 
| 13 | 
            +
                  # This cop will respect `Layout/ArgumentAlignment` and will not work when
         | 
| 14 | 
            +
                  # `EnforcedStyle: with_fixed_indentation` is specified for `Layout/ArgumentAlignment`.
         | 
| 12 15 | 
             
                  #
         | 
| 13 16 | 
             
                  # @example
         | 
| 14 17 | 
             
                  #
         | 
| @@ -149,6 +152,7 @@ module RuboCop | |
| 149 152 | 
             
                    MSG = 'Indent the first argument one step more than %<base>s.'
         | 
| 150 153 |  | 
| 151 154 | 
             
                    def on_send(node)
         | 
| 155 | 
            +
                      return if enforce_first_argument_with_fixed_indentation?
         | 
| 152 156 | 
             
                      return if !node.arguments? || node.operator_method?
         | 
| 153 157 |  | 
| 154 158 | 
             
                      indent = base_indentation(node) + configured_indentation_width
         | 
| @@ -250,6 +254,16 @@ module RuboCop | |
| 250 254 | 
             
                    def on_new_investigation
         | 
| 251 255 | 
             
                      @comment_lines = nil
         | 
| 252 256 | 
             
                    end
         | 
| 257 | 
            +
             | 
| 258 | 
            +
                    def enforce_first_argument_with_fixed_indentation?
         | 
| 259 | 
            +
                      return false unless argument_alignment_config['Enabled']
         | 
| 260 | 
            +
             | 
| 261 | 
            +
                      argument_alignment_config['EnforcedStyle'] == 'with_fixed_indentation'
         | 
| 262 | 
            +
                    end
         | 
| 263 | 
            +
             | 
| 264 | 
            +
                    def argument_alignment_config
         | 
| 265 | 
            +
                      config.for_cop('Layout/ArgumentAlignment')
         | 
| 266 | 
            +
                    end
         | 
| 253 267 | 
             
                  end
         | 
| 254 268 | 
             
                end
         | 
| 255 269 | 
             
              end
         | 
| @@ -60,12 +60,13 @@ module RuboCop | |
| 60 60 | 
             
                  #   }
         | 
| 61 61 | 
             
                  class LineLength < Base
         | 
| 62 62 | 
             
                    include CheckLineBreakable
         | 
| 63 | 
            -
                    include ConfigurableMax
         | 
| 64 63 | 
             
                    include IgnoredPattern
         | 
| 65 64 | 
             
                    include RangeHelp
         | 
| 66 65 | 
             
                    include LineLengthHelp
         | 
| 67 66 | 
             
                    extend AutoCorrector
         | 
| 68 67 |  | 
| 68 | 
            +
                    exclude_limit 'Max'
         | 
| 69 | 
            +
             | 
| 69 70 | 
             
                    MSG = 'Line is too long. [%<length>d/%<max>d]'
         | 
| 70 71 |  | 
| 71 72 | 
             
                    def on_block(node)
         | 
| @@ -39,12 +39,17 @@ module RuboCop | |
| 39 39 |  | 
| 40 40 | 
             
                        range_between(receiver_end_pos, selector_begin_pos)
         | 
| 41 41 | 
             
                      elsif node.method?(:[]=)
         | 
| 42 | 
            -
                         | 
| 42 | 
            +
                        offense_range_for_assignment(node, begin_pos)
         | 
| 43 | 
            +
                      end
         | 
| 44 | 
            +
                    end
         | 
| 43 45 |  | 
| 44 | 
            -
             | 
| 46 | 
            +
                    def offense_range_for_assignment(node, begin_pos)
         | 
| 47 | 
            +
                      end_pos = node.receiver.source_range.end_pos
         | 
| 45 48 |  | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 49 | 
            +
                      return if begin_pos - end_pos == 1 ||
         | 
| 50 | 
            +
                                (range = range_between(end_pos, begin_pos - 1)).source == '['
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                      range
         | 
| 48 53 | 
             
                    end
         | 
| 49 54 |  | 
| 50 55 | 
             
                    def register_offense(range)
         | 
| @@ -37,6 +37,11 @@ module RuboCop | |
| 37 37 | 
             
                    DO_NOT_USE_MSG = 'Do not use `%<bad>s`%<deprecated_message>s.'
         | 
| 38 38 |  | 
| 39 39 | 
             
                    def on_const(node)
         | 
| 40 | 
            +
                      # FIXME: Workaround for "`undefined method `expression' for nil:NilClass`" when processing
         | 
| 41 | 
            +
                      #        `__ENCODING__`. It is better to be able to work without this condition.
         | 
| 42 | 
            +
                      #        Maybe further investigation of RuboCop AST will lead to an essential solution.
         | 
| 43 | 
            +
                      return unless node.loc
         | 
| 44 | 
            +
             | 
| 40 45 | 
             
                      constant = node.absolute? ? consntant_name(node, node.short_name.to_s) : node.source
         | 
| 41 46 | 
             
                      return unless (deprecated_constant = deprecated_constants[constant])
         | 
| 42 47 |  | 
| @@ -28,6 +28,7 @@ module RuboCop | |
| 28 28 | 
             
                    RESTRICT_ON_SEND = %i[to_sym intern].freeze
         | 
| 29 29 |  | 
| 30 30 | 
             
                    def on_send(node)
         | 
| 31 | 
            +
                      return unless node.receiver
         | 
| 31 32 | 
             
                      return unless node.receiver.str_type? || node.receiver.sym_type?
         | 
| 32 33 |  | 
| 33 34 | 
             
                      register_offense(node, correction: node.receiver.value.to_sym.inspect)
         | 
| @@ -59,7 +60,7 @@ module RuboCop | |
| 59 60 | 
             
                    end
         | 
| 60 61 |  | 
| 61 62 | 
             
                    def properly_quoted?(source, value)
         | 
| 62 | 
            -
                      return true  | 
| 63 | 
            +
                      return true if !source.match?(/['"]/) || value.end_with?('=')
         | 
| 63 64 |  | 
| 64 65 | 
             
                      source == value ||
         | 
| 65 66 | 
             
                        # `Symbol#inspect` uses double quotes, but allow single-quoted
         | 
| @@ -87,7 +88,7 @@ module RuboCop | |
| 87 88 | 
             
                      # will be ignored.
         | 
| 88 89 | 
             
                      return unless node.value.to_s.match?(/\A[a-z0-9_]/i)
         | 
| 89 90 |  | 
| 90 | 
            -
                      correction = node.value.inspect. | 
| 91 | 
            +
                      correction = node.value.inspect.gsub(/\A:/, '')
         | 
| 91 92 | 
             
                      return if properly_quoted?(node.source, correction)
         | 
| 92 93 |  | 
| 93 94 | 
             
                      register_offense(
         | 
| @@ -85,8 +85,11 @@ module RuboCop | |
| 85 85 | 
             
                    end
         | 
| 86 86 | 
             
                  end
         | 
| 87 87 |  | 
| 88 | 
            +
                  # Returns the base style guide URL from AllCops or the specific department
         | 
| 89 | 
            +
                  #
         | 
| 90 | 
            +
                  # @return [String] style guide URL
         | 
| 88 91 | 
             
                  def style_guide_base_url
         | 
| 89 | 
            -
                    department_name = cop_name.split('/'). | 
| 92 | 
            +
                    department_name = cop_name.split('/')[0..-2].join('/')
         | 
| 90 93 |  | 
| 91 94 | 
             
                    config.for_department(department_name)['StyleGuideBaseURL'] ||
         | 
| 92 95 | 
             
                      config.for_all_cops['StyleGuideBaseURL']
         | 
| @@ -12,13 +12,13 @@ module RuboCop | |
| 12 12 | 
             
                  #
         | 
| 13 13 | 
             
                  # The maximum level of nesting allowed is configurable.
         | 
| 14 14 | 
             
                  class BlockNesting < Base
         | 
| 15 | 
            -
                    include ConfigurableMax
         | 
| 16 | 
            -
             | 
| 17 15 | 
             
                    NESTING_BLOCKS = %i[
         | 
| 18 16 | 
             
                      case if while while_post
         | 
| 19 17 | 
             
                      until until_post for resbody
         | 
| 20 18 | 
             
                    ].freeze
         | 
| 21 19 |  | 
| 20 | 
            +
                    exclude_limit 'Max'
         | 
| 21 | 
            +
             | 
| 22 22 | 
             
                    def on_new_investigation
         | 
| 23 23 | 
             
                      return if processed_source.blank?
         | 
| 24 24 |  | 
| @@ -51,7 +51,8 @@ module RuboCop | |
| 51 51 | 
             
                  #   end
         | 
| 52 52 | 
             
                  #
         | 
| 53 53 | 
             
                  class ParameterLists < Base
         | 
| 54 | 
            -
                     | 
| 54 | 
            +
                    exclude_limit 'Max'
         | 
| 55 | 
            +
                    exclude_limit 'MaxOptionalParameters'
         | 
| 55 56 |  | 
| 56 57 | 
             
                    MSG = 'Avoid parameter lists longer than %<max>d parameters. ' \
         | 
| 57 58 | 
             
                          '[%<count>d/%<max>d]'
         | 
| @@ -70,7 +71,9 @@ module RuboCop | |
| 70 71 | 
             
                        count: optargs.count
         | 
| 71 72 | 
             
                      )
         | 
| 72 73 |  | 
| 73 | 
            -
                      add_offense(node, message: message)
         | 
| 74 | 
            +
                      add_offense(node, message: message) do
         | 
| 75 | 
            +
                        self.max_optional_parameters = optargs.count
         | 
| 76 | 
            +
                      end
         | 
| 74 77 | 
             
                    end
         | 
| 75 78 | 
             
                    alias on_defs on_def
         | 
| 76 79 |  | 
| @@ -4,10 +4,12 @@ module RuboCop | |
| 4 4 | 
             
              module Cop
         | 
| 5 5 | 
             
                # Common functionality for checking length of code segments.
         | 
| 6 6 | 
             
                module CodeLength
         | 
| 7 | 
            -
                   | 
| 7 | 
            +
                  extend ExcludeLimit
         | 
| 8 8 |  | 
| 9 9 | 
             
                  MSG = '%<label>s has too many lines. [%<length>d/%<max>d]'
         | 
| 10 10 |  | 
| 11 | 
            +
                  exclude_limit 'Max'
         | 
| 12 | 
            +
             | 
| 11 13 | 
             
                  private
         | 
| 12 14 |  | 
| 13 15 | 
             
                  def message(length, max_length)
         | 
| @@ -6,10 +6,12 @@ module RuboCop | |
| 6 6 | 
             
                #
         | 
| 7 7 | 
             
                # This module handles measurement and reporting of complexity in methods.
         | 
| 8 8 | 
             
                module MethodComplexity
         | 
| 9 | 
            -
                  include ConfigurableMax
         | 
| 10 9 | 
             
                  include IgnoredMethods
         | 
| 11 10 | 
             
                  include Metrics::Utils::RepeatedCsendDiscount
         | 
| 12 11 | 
             
                  extend NodePattern::Macros
         | 
| 12 | 
            +
                  extend ExcludeLimit
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  exclude_limit 'Max'
         | 
| 13 15 |  | 
| 14 16 | 
             
                  # Ensure cops that include `MethodComplexity` have the config
         | 
| 15 17 | 
             
                  # `attr_accessor`s that `ignored_method?` needs.
         | 
| @@ -71,7 +71,7 @@ module RuboCop | |
| 71 71 |  | 
| 72 72 | 
             
                    def directive_cops(comment)
         | 
| 73 73 | 
             
                      match = CommentConfig::COMMENT_DIRECTIVE_REGEXP.match(comment.text)
         | 
| 74 | 
            -
                      match[2] ? match[2].split(',').map(&:strip) : []
         | 
| 74 | 
            +
                      match && match[2] ? match[2].split(',').map(&:strip) : []
         | 
| 75 75 | 
             
                    end
         | 
| 76 76 |  | 
| 77 77 | 
             
                    def allowed_cops
         | 
| @@ -64,7 +64,7 @@ module RuboCop | |
| 64 64 | 
             
                      return if node.method?(:eval) && !valid_eval_receiver?(node.receiver)
         | 
| 65 65 |  | 
| 66 66 | 
             
                      code = node.arguments.first
         | 
| 67 | 
            -
                      return unless code.str_type? || code.dstr_type?
         | 
| 67 | 
            +
                      return unless code && (code.str_type? || code.dstr_type?)
         | 
| 68 68 |  | 
| 69 69 | 
             
                      file, line = file_and_line(node)
         | 
| 70 70 |  | 
| @@ -6,6 +6,8 @@ module RuboCop | |
| 6 6 | 
             
                  # This cop checks for redundant `if` with boolean literal branches.
         | 
| 7 7 | 
             
                  # It checks only conditions to return boolean value (`true` or `false`) for safe detection.
         | 
| 8 8 | 
             
                  # The conditions to be checked are comparison methods, predicate methods, and double negative.
         | 
| 9 | 
            +
                  # However, auto-correction is unsafe because there is no guarantee that all predicate methods
         | 
| 10 | 
            +
                  # will return boolean value. Those methods can be allowed with `AllowedMethods` config.
         | 
| 9 11 | 
             
                  #
         | 
| 10 12 | 
             
                  # @example
         | 
| 11 13 | 
             
                  #   # bad
         | 
| @@ -21,10 +23,16 @@ module RuboCop | |
| 21 23 | 
             
                  #   # good
         | 
| 22 24 | 
             
                  #   foo == bar
         | 
| 23 25 | 
             
                  #
         | 
| 26 | 
            +
                  # @example AllowedMethods: ['nonzero?']
         | 
| 27 | 
            +
                  #   # good
         | 
| 28 | 
            +
                  #   num.nonzero? ? true : false
         | 
| 29 | 
            +
                  #
         | 
| 24 30 | 
             
                  class IfWithBooleanLiteralBranches < Base
         | 
| 31 | 
            +
                    include AllowedMethods
         | 
| 25 32 | 
             
                    extend AutoCorrector
         | 
| 26 33 |  | 
| 27 34 | 
             
                    MSG = 'Remove redundant %<keyword>s with boolean literal branches.'
         | 
| 35 | 
            +
                    MSG_FOR_ELSIF = 'Use `else` instead of redundant `elsif` with boolean literal branches.'
         | 
| 28 36 |  | 
| 29 37 | 
             
                    def_node_matcher :if_with_boolean_literal_branches?, <<~PATTERN
         | 
| 30 38 | 
             
                      (if #return_boolean_value? {(true) (false) | (false) (true)})
         | 
| @@ -35,25 +43,40 @@ module RuboCop | |
| 35 43 | 
             
                      return unless if_with_boolean_literal_branches?(node)
         | 
| 36 44 |  | 
| 37 45 | 
             
                      condition = node.condition
         | 
| 38 | 
            -
                      range, keyword =  | 
| 39 | 
            -
                                         range = condition.source_range.end.join(node.source_range.end)
         | 
| 40 | 
            -
             | 
| 41 | 
            -
                                         [range, 'ternary operator']
         | 
| 42 | 
            -
                                       else
         | 
| 43 | 
            -
                                         keyword = node.loc.keyword
         | 
| 44 | 
            -
             | 
| 45 | 
            -
                                         [keyword, "`#{keyword.source}`"]
         | 
| 46 | 
            -
                                       end
         | 
| 46 | 
            +
                      range, keyword = offense_range_with_keyword(node, condition)
         | 
| 47 47 |  | 
| 48 | 
            -
                      add_offense(range, message:  | 
| 48 | 
            +
                      add_offense(range, message: message(node, keyword)) do |corrector|
         | 
| 49 49 | 
             
                        replacement = replacement_condition(node, condition)
         | 
| 50 50 |  | 
| 51 | 
            -
                         | 
| 51 | 
            +
                        if node.elsif?
         | 
| 52 | 
            +
                          corrector.insert_before(node, "else\n")
         | 
| 53 | 
            +
                          corrector.replace(node, "#{indent(node.if_branch)}#{replacement}")
         | 
| 54 | 
            +
                        else
         | 
| 55 | 
            +
                          corrector.replace(node, replacement)
         | 
| 56 | 
            +
                        end
         | 
| 52 57 | 
             
                      end
         | 
| 53 58 | 
             
                    end
         | 
| 54 59 |  | 
| 55 60 | 
             
                    private
         | 
| 56 61 |  | 
| 62 | 
            +
                    def offense_range_with_keyword(node, condition)
         | 
| 63 | 
            +
                      if node.ternary?
         | 
| 64 | 
            +
                        range = condition.source_range.end.join(node.source_range.end)
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                        [range, 'ternary operator']
         | 
| 67 | 
            +
                      else
         | 
| 68 | 
            +
                        keyword = node.loc.keyword
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                        [keyword, "`#{keyword.source}`"]
         | 
| 71 | 
            +
                      end
         | 
| 72 | 
            +
                    end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
                    def message(node, keyword)
         | 
| 75 | 
            +
                      message_template = node.elsif? ? MSG_FOR_ELSIF : MSG
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                      format(message_template, keyword: keyword)
         | 
| 78 | 
            +
                    end
         | 
| 79 | 
            +
             | 
| 57 80 | 
             
                    def return_boolean_value?(condition)
         | 
| 58 81 | 
             
                      if condition.begin_type?
         | 
| 59 82 | 
             
                        return_boolean_value?(condition.children.first)
         | 
| @@ -68,6 +91,7 @@ module RuboCop | |
| 68 91 |  | 
| 69 92 | 
             
                    def assume_boolean_value?(condition)
         | 
| 70 93 | 
             
                      return false unless condition.send_type?
         | 
| 94 | 
            +
                      return false if allowed_method?(condition.method_name)
         | 
| 71 95 |  | 
| 72 96 | 
             
                      condition.comparison_method? || condition.predicate_method? || double_negative?(condition)
         | 
| 73 97 | 
             
                    end
         | 
| @@ -50,7 +50,9 @@ module RuboCop | |
| 50 50 | 
             
                                     end
         | 
| 51 51 |  | 
| 52 52 | 
             
                          corrector.replace(node, new_code)
         | 
| 53 | 
            -
             | 
| 53 | 
            +
             | 
| 54 | 
            +
                          parent = node.parent
         | 
| 55 | 
            +
                          corrector.wrap(node, '(', ')') if parent.respond_to?(:method?) && parent.method?(:!)
         | 
| 54 56 | 
             
                        end
         | 
| 55 57 | 
             
                      end
         | 
| 56 58 | 
             
                    end
         | 
| @@ -28,10 +28,6 @@ module RuboCop | |
| 28 28 | 
             
                  #   10_000_00 # typical representation of $10,000 in cents
         | 
| 29 29 | 
             
                  #
         | 
| 30 30 | 
             
                  class NumericLiterals < Base
         | 
| 31 | 
            -
                    # The parameter is called MinDigits (meaning the minimum number of
         | 
| 32 | 
            -
                    # digits for which an offense can be registered), but essentially it's
         | 
| 33 | 
            -
                    # a Max parameter (the maximum number of something that's allowed).
         | 
| 34 | 
            -
                    include ConfigurableMax
         | 
| 35 31 | 
             
                    include IntegerNode
         | 
| 36 32 | 
             
                    extend AutoCorrector
         | 
| 37 33 |  | 
| @@ -39,6 +35,11 @@ module RuboCop | |
| 39 35 | 
             
                          'separate every 3 digits with them.'
         | 
| 40 36 | 
             
                    DELIMITER_REGEXP = /[eE.]/.freeze
         | 
| 41 37 |  | 
| 38 | 
            +
                    # The parameter is called MinDigits (meaning the minimum number of
         | 
| 39 | 
            +
                    # digits for which an offense can be registered), but essentially it's
         | 
| 40 | 
            +
                    # a Max parameter (the maximum number of something that's allowed).
         | 
| 41 | 
            +
                    exclude_limit 'MinDigits'
         | 
| 42 | 
            +
             | 
| 42 43 | 
             
                    def on_int(node)
         | 
| 43 44 | 
             
                      check(node)
         | 
| 44 45 | 
             
                    end
         | 
| @@ -49,10 +50,6 @@ module RuboCop | |
| 49 50 |  | 
| 50 51 | 
             
                    private
         | 
| 51 52 |  | 
| 52 | 
            -
                    def max_parameter_name
         | 
| 53 | 
            -
                      'MinDigits'
         | 
| 54 | 
            -
                    end
         | 
| 55 | 
            -
             | 
| 56 53 | 
             
                    def check(node)
         | 
| 57 54 | 
             
                      int = integer_part(node)
         | 
| 58 55 |  | 
| @@ -62,7 +59,7 @@ module RuboCop | |
| 62 59 |  | 
| 63 60 | 
             
                      case int
         | 
| 64 61 | 
             
                      when /^\d+$/
         | 
| 65 | 
            -
                        return unless (self. | 
| 62 | 
            +
                        return unless (self.min_digits = int.size + 1)
         | 
| 66 63 |  | 
| 67 64 | 
             
                        register_offense(node)
         | 
| 68 65 | 
             
                      when /\d{4}/, short_group_regex
         | 
| @@ -8,7 +8,7 @@ module RuboCop | |
| 8 8 | 
             
                  # These can be replaced by their respective predicate methods.
         | 
| 9 9 | 
             
                  # The cop can also be configured to do the reverse.
         | 
| 10 10 | 
             
                  #
         | 
| 11 | 
            -
                  # The cop disregards `#nonzero?` as  | 
| 11 | 
            +
                  # The cop disregards `#nonzero?` as its value is truthy or falsey,
         | 
| 12 12 | 
             
                  # but not `true` and `false`, and thus not always interchangeable with
         | 
| 13 13 | 
             
                  # `!= 0`.
         | 
| 14 14 | 
             
                  #
         | 
| @@ -112,11 +112,11 @@ module RuboCop | |
| 112 112 | 
             
                    def correct_outer_condition(corrector, condition)
         | 
| 113 113 | 
             
                      return unless requrie_parentheses?(condition)
         | 
| 114 114 |  | 
| 115 | 
            -
                       | 
| 116 | 
            -
             | 
| 117 | 
            -
                       | 
| 115 | 
            +
                      end_pos = condition.loc.selector.end_pos
         | 
| 116 | 
            +
                      begin_pos = condition.first_argument.source_range.begin_pos
         | 
| 117 | 
            +
                      return if end_pos > begin_pos
         | 
| 118 118 |  | 
| 119 | 
            -
                      corrector.replace( | 
| 119 | 
            +
                      corrector.replace(range_between(end_pos, begin_pos), '(')
         | 
| 120 120 | 
             
                      corrector.insert_after(condition.last_argument.source_range, ')')
         | 
| 121 121 | 
             
                    end
         | 
| 122 122 |  | 
| @@ -13,6 +13,7 @@ module RuboCop | |
| 13 13 | 
             
                  include PathUtil
         | 
| 14 14 |  | 
| 15 15 | 
             
                  COLOR_FOR_SEVERITY = {
         | 
| 16 | 
            +
                    info:       :gray,
         | 
| 16 17 | 
             
                    refactor:   :yellow,
         | 
| 17 18 | 
             
                    convention: :yellow,
         | 
| 18 19 | 
             
                    warning:    :magenta,
         | 
| @@ -76,7 +77,7 @@ module RuboCop | |
| 76 77 | 
             
                  end
         | 
| 77 78 |  | 
| 78 79 | 
             
                  def colored_severity_code(offense)
         | 
| 79 | 
            -
                    color = COLOR_FOR_SEVERITY | 
| 80 | 
            +
                    color = COLOR_FOR_SEVERITY.fetch(offense.severity.name)
         | 
| 80 81 | 
             
                    colorize(offense.severity.code, color)
         | 
| 81 82 | 
             
                  end
         | 
| 82 83 |  | 
    
        data/lib/rubocop/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: rubocop
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1.9. | 
| 4 | 
            +
              version: 1.9.1
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Bozhidar Batsov
         | 
| @@ -10,7 +10,7 @@ authors: | |
| 10 10 | 
             
            autorequire: 
         | 
| 11 11 | 
             
            bindir: exe
         | 
| 12 12 | 
             
            cert_chain: []
         | 
| 13 | 
            -
            date: 2021-01 | 
| 13 | 
            +
            date: 2021-02-01 00:00:00.000000000 Z
         | 
| 14 14 | 
             
            dependencies:
         | 
| 15 15 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 16 16 | 
             
              name: parallel
         | 
| @@ -242,6 +242,7 @@ files: | |
| 242 242 | 
             
            - lib/rubocop/cop/correctors/string_literal_corrector.rb
         | 
| 243 243 | 
             
            - lib/rubocop/cop/correctors/unused_arg_corrector.rb
         | 
| 244 244 | 
             
            - lib/rubocop/cop/documentation.rb
         | 
| 245 | 
            +
            - lib/rubocop/cop/exclude_limit.rb
         | 
| 245 246 | 
             
            - lib/rubocop/cop/force.rb
         | 
| 246 247 | 
             
            - lib/rubocop/cop/gemspec/duplicated_assignment.rb
         | 
| 247 248 | 
             
            - lib/rubocop/cop/gemspec/ordered_dependencies.rb
         |