rubocop 0.77.0 → 0.81.0

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.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +1 -1
  3. data/README.md +3 -3
  4. data/config/default.yml +136 -60
  5. data/lib/rubocop.rb +20 -4
  6. data/lib/rubocop/ast/builder.rb +45 -42
  7. data/lib/rubocop/ast/node.rb +11 -18
  8. data/lib/rubocop/ast/node/block_node.rb +5 -1
  9. data/lib/rubocop/ast/node/case_match_node.rb +56 -0
  10. data/lib/rubocop/ast/node/def_node.rb +11 -0
  11. data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
  12. data/lib/rubocop/ast/node/regexp_node.rb +2 -4
  13. data/lib/rubocop/ast/traversal.rb +29 -10
  14. data/lib/rubocop/cli/command/auto_genenerate_config.rb +7 -7
  15. data/lib/rubocop/cli/command/show_cops.rb +11 -4
  16. data/lib/rubocop/comment_config.rb +6 -1
  17. data/lib/rubocop/config.rb +28 -10
  18. data/lib/rubocop/config_loader.rb +19 -19
  19. data/lib/rubocop/config_obsoletion.rb +6 -4
  20. data/lib/rubocop/config_validator.rb +55 -95
  21. data/lib/rubocop/cop/autocorrect_logic.rb +7 -4
  22. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -2
  23. data/lib/rubocop/cop/cop.rb +3 -1
  24. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  25. data/lib/rubocop/cop/generator.rb +3 -4
  26. data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
  27. data/lib/rubocop/cop/layout/array_alignment.rb +53 -10
  28. data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
  29. data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
  30. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
  31. data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -2
  32. data/lib/rubocop/cop/layout/hash_alignment.rb +8 -4
  33. data/lib/rubocop/cop/layout/heredoc_indentation.rb +4 -4
  34. data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -2
  35. data/lib/rubocop/cop/{metrics → layout}/line_length.rb +35 -79
  36. data/lib/rubocop/cop/layout/multiline_block_layout.rb +14 -5
  37. data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
  38. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
  39. data/lib/rubocop/cop/layout/space_around_operators.rb +49 -6
  40. data/lib/rubocop/cop/layout/space_before_block_braces.rb +17 -0
  41. data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
  42. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
  43. data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
  44. data/lib/rubocop/cop/lint/debugger.rb +1 -1
  45. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
  46. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  47. data/lib/rubocop/cop/lint/loop.rb +6 -4
  48. data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
  49. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +89 -0
  50. data/lib/rubocop/cop/lint/raise_exception.rb +39 -0
  51. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +3 -3
  52. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +13 -8
  53. data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
  54. data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
  55. data/lib/rubocop/cop/lint/suppressed_exception.rb +12 -22
  56. data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
  57. data/lib/rubocop/cop/lint/useless_setter_call.rb +4 -0
  58. data/lib/rubocop/cop/migration/department_name.rb +47 -6
  59. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  60. data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
  61. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
  62. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
  63. data/lib/rubocop/cop/mixin/hash_transform_method.rb +171 -0
  64. data/lib/rubocop/cop/mixin/line_length_help.rb +88 -0
  65. data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
  66. data/lib/rubocop/cop/mixin/rational_literal.rb +18 -0
  67. data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -2
  68. data/lib/rubocop/cop/mixin/trailing_comma.rb +8 -12
  69. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
  70. data/lib/rubocop/cop/naming/method_name.rb +30 -0
  71. data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
  72. data/lib/rubocop/cop/offense.rb +11 -0
  73. data/lib/rubocop/cop/registry.rb +7 -2
  74. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
  75. data/lib/rubocop/cop/style/attr.rb +8 -0
  76. data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
  77. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  78. data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
  79. data/lib/rubocop/cop/style/documentation.rb +43 -5
  80. data/lib/rubocop/cop/style/end_block.rb +6 -0
  81. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
  82. data/lib/rubocop/cop/style/guard_clause.rb +3 -2
  83. data/lib/rubocop/cop/style/hash_each_methods.rb +89 -0
  84. data/lib/rubocop/cop/style/hash_transform_keys.rb +83 -0
  85. data/lib/rubocop/cop/style/hash_transform_values.rb +83 -0
  86. data/lib/rubocop/cop/style/if_unless_modifier.rb +38 -3
  87. data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
  88. data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
  89. data/lib/rubocop/cop/style/lambda.rb +1 -0
  90. data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
  91. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
  92. data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
  93. data/lib/rubocop/cop/style/module_function.rb +56 -10
  94. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  95. data/lib/rubocop/cop/style/multiline_when_then.rb +5 -1
  96. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -4
  97. data/lib/rubocop/cop/style/numeric_predicate.rb +4 -3
  98. data/lib/rubocop/cop/style/one_line_conditional.rb +3 -2
  99. data/lib/rubocop/cop/style/or_assignment.rb +3 -2
  100. data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
  101. data/lib/rubocop/cop/style/redundant_condition.rb +17 -4
  102. data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
  103. data/lib/rubocop/cop/style/symbol_array.rb +2 -2
  104. data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
  105. data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +34 -22
  106. data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
  107. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +85 -0
  108. data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
  109. data/lib/rubocop/cop/style/trailing_underscore_variable.rb +7 -1
  110. data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
  111. data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
  112. data/lib/rubocop/cop/variable_force.rb +4 -1
  113. data/lib/rubocop/formatter/base_formatter.rb +2 -2
  114. data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
  115. data/lib/rubocop/formatter/formatter_set.rb +1 -0
  116. data/lib/rubocop/formatter/json_formatter.rb +6 -5
  117. data/lib/rubocop/formatter/junit_formatter.rb +74 -0
  118. data/lib/rubocop/formatter/tap_formatter.rb +1 -1
  119. data/lib/rubocop/node_pattern.rb +97 -11
  120. data/lib/rubocop/options.rb +8 -8
  121. data/lib/rubocop/processed_source.rb +1 -1
  122. data/lib/rubocop/result_cache.rb +2 -0
  123. data/lib/rubocop/rspec/shared_contexts.rb +5 -0
  124. data/lib/rubocop/runner.rb +5 -1
  125. data/lib/rubocop/target_ruby.rb +151 -0
  126. data/lib/rubocop/version.rb +1 -1
  127. metadata +38 -10
  128. data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
  129. data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
@@ -24,15 +24,18 @@ module RuboCop
24
24
  @options[:disable_uncorrectable] == true
25
25
  end
26
26
 
27
+ def safe_autocorrect?
28
+ cop_config.fetch('Safe', true) &&
29
+ cop_config.fetch('SafeAutoCorrect', true)
30
+ end
31
+
27
32
  def autocorrect_enabled?
28
33
  # allow turning off autocorrect on a cop by cop basis
29
34
  return true unless cop_config
30
35
 
31
36
  return false if cop_config['AutoCorrect'] == false
32
37
 
33
- if @options.fetch(:safe_auto_correct, false)
34
- return cop_config.fetch('SafeAutoCorrect', true)
35
- end
38
+ return safe_autocorrect? if @options.fetch(:safe_auto_correct, false)
36
39
 
37
40
  true
38
41
  end
@@ -76,7 +79,7 @@ module RuboCop
76
79
  end
77
80
 
78
81
  def max_line_length
79
- config.for_cop('Metrics/LineLength')['Max'] || 80
82
+ config.for_cop('Layout/LineLength')['Max'] || 80
80
83
  end
81
84
 
82
85
  def disable_offense_at_end_of_line(range, eol_comment)
@@ -13,8 +13,8 @@ module RuboCop
13
13
  #
14
14
  # However, it don't replace all `sources` of `http://` with `https://`.
15
15
  # For example, when specifying an internal gem server using HTTP on the
16
- # intranet, a use case where HTTPS can not be specified was considered.
17
- # Consider using HTTP only if you can not use HTTPS.
16
+ # intranet, a use case where HTTPS cannot be specified was considered.
17
+ # Consider using HTTP only if you cannot use HTTPS.
18
18
  #
19
19
  # @example
20
20
  # # bad
@@ -172,6 +172,8 @@ module RuboCop
172
172
  end
173
173
 
174
174
  def disable_uncorrectable(node)
175
+ return unless node
176
+
175
177
  @disabled_lines ||= {}
176
178
  line = node.location.line
177
179
  return if @disabled_lines.key?(line)
@@ -217,7 +219,7 @@ module RuboCop
217
219
  !relevant_file?(file)
218
220
  end
219
221
 
220
- # This method should be overriden when a cop's behavior depends
222
+ # This method should be overridden when a cop's behavior depends
221
223
  # on state that lives outside of these locations:
222
224
  #
223
225
  # (1) the file under inspection
@@ -98,7 +98,7 @@ module RuboCop
98
98
  end
99
99
 
100
100
  def_node_search :dependency_declarations, <<~PATTERN
101
- (send (lvar _) {:add_dependency :add_runtime_dependency :add_development_dependency} ...)
101
+ (send (lvar _) {:add_dependency :add_runtime_dependency :add_development_dependency} (str _) ...)
102
102
  PATTERN
103
103
  end
104
104
  end
@@ -104,10 +104,9 @@ module RuboCop
104
104
  end
105
105
  SPEC
106
106
 
107
- CONFIGURATION_ADDED_MESSAGE = <<~MESSAGE
108
- [modify] A configuration for the cop is added into %<configuration_file_path>s.
109
- If you want to disable the cop by default, set `Enabled` option to false.
110
- MESSAGE
107
+ CONFIGURATION_ADDED_MESSAGE =
108
+ '[modify] A configuration for the cop is added into ' \
109
+ '%<configuration_file_path>s.'
111
110
 
112
111
  def initialize(name, github_user, output: $stdout)
113
112
  @badge = Badge.parse(name)
@@ -10,7 +10,7 @@ module RuboCop
10
10
  TEMPLATE = <<~YAML
11
11
  %<badge>s:
12
12
  Description: 'TODO: Write a description of the cop.'
13
- Enabled: true
13
+ Enabled: pending
14
14
  VersionAdded: '%<version_added>s'
15
15
  YAML
16
16
 
@@ -6,33 +6,76 @@ module RuboCop
6
6
  # Here we check if the elements of a multi-line array literal are
7
7
  # aligned.
8
8
  #
9
- # @example
9
+ # @example EnforcedStyle: with_first_element (default)
10
+ # # good
11
+ #
12
+ # array = [1, 2, 3,
13
+ # 4, 5, 6]
14
+ # array = ['run',
15
+ # 'forrest',
16
+ # 'run']
17
+ #
10
18
  # # bad
11
- # a = [1, 2, 3,
19
+ #
20
+ # array = [1, 2, 3,
12
21
  # 4, 5, 6]
13
22
  # array = ['run',
14
23
  # 'forrest',
15
24
  # 'run']
16
25
  #
26
+ # @example EnforcedStyle: with_fixed_indentation
17
27
  # # good
18
- # a = [1, 2, 3,
19
- # 4, 5, 6]
20
- # a = ['run',
21
- # 'forrest',
22
- # 'run']
28
+ #
29
+ # array = [1, 2, 3,
30
+ # 4, 5, 6]
31
+ #
32
+ # # bad
33
+ #
34
+ # array = [1, 2, 3,
35
+ # 4, 5, 6]
23
36
  class ArrayAlignment < Cop
24
37
  include Alignment
25
38
 
26
- MSG = 'Align the elements of an array literal if they span more ' \
27
- 'than one line.'
39
+ ALIGN_ELEMENTS_MSG = 'Align the elements of an array literal ' \
40
+ 'if they span more than one line.'
41
+
42
+ FIXED_INDENT_MSG = 'Use one level of indentation for elements ' \
43
+ 'following the first line of a multi-line array.'
28
44
 
29
45
  def on_array(node)
30
- check_alignment(node.children)
46
+ return if node.children.size < 2
47
+
48
+ check_alignment(node.children, base_column(node, node.children))
31
49
  end
32
50
 
33
51
  def autocorrect(node)
34
52
  AlignmentCorrector.correct(processed_source, node, column_delta)
35
53
  end
54
+
55
+ private
56
+
57
+ def message(_node)
58
+ fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_ELEMENTS_MSG
59
+ end
60
+
61
+ def fixed_indentation?
62
+ cop_config['EnforcedStyle'] == 'with_fixed_indentation'
63
+ end
64
+
65
+ def base_column(node, args)
66
+ if fixed_indentation?
67
+ lineno = target_method_lineno(node)
68
+ line = node.source_range.source_buffer.source_line(lineno)
69
+ indentation_of_line = /\S.*/.match(line).begin(0)
70
+ indentation_of_line + configured_indentation_width
71
+ else
72
+ display_column(args.first.source_range)
73
+ end
74
+ end
75
+
76
+ def target_method_lineno(node)
77
+ node.loc.line
78
+ end
36
79
  end
37
80
  end
38
81
  end
@@ -52,9 +52,11 @@ module RuboCop
52
52
  end
53
53
 
54
54
  def delimiter_range(node)
55
- Parser::Source::Range.new(node.loc.expression.source_buffer,
56
- node.children.last.loc.expression.end_pos,
57
- node.loc.expression.end_pos)
55
+ Parser::Source::Range.new(
56
+ node.loc.expression.source_buffer,
57
+ node.children.compact.last.loc.expression.end_pos,
58
+ node.loc.expression.end_pos
59
+ )
58
60
  end
59
61
  end
60
62
  end
@@ -59,6 +59,14 @@ module RuboCop
59
59
  check_alignment(node.when_branches.last.loc.keyword, node.loc.else)
60
60
  end
61
61
 
62
+ def on_case_match(node)
63
+ return unless node.else?
64
+
65
+ check_alignment(
66
+ node.in_pattern_branches.last.loc.keyword, node.loc.else
67
+ )
68
+ end
69
+
62
70
  def autocorrect(node)
63
71
  AlignmentCorrector.correct(processed_source, node, column_delta)
64
72
  end
@@ -56,7 +56,8 @@ module RuboCop
56
56
  return if nodes.all?(&:single_line?) &&
57
57
  cop_config['AllowAdjacentOneLineDefs']
58
58
 
59
- add_offense(nodes.last, location: :keyword)
59
+ location = nodes.last.loc.keyword.join(nodes.last.loc.name)
60
+ add_offense(nodes.last, location: location)
60
61
  end
61
62
 
62
63
  def autocorrect(node)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module RuboCop
4
4
  module Cop
5
- # rubocop:disable Metrics/LineLength
5
+ # rubocop:disable Layout/LineLength
6
6
  module Layout
7
7
  # This cop checks the indentation of the first argument in a method call.
8
8
  # Arguments after the first one are checked by Layout/ArgumentAlignment,
@@ -143,7 +143,7 @@ module RuboCop
143
143
  # second_param
144
144
  #
145
145
  class FirstArgumentIndentation < Cop
146
- # rubocop:enable Metrics/LineLength
146
+ # rubocop:enable Layout/LineLength
147
147
  include Alignment
148
148
  include ConfigurableEnforcedStyle
149
149
  include RangeHelp
@@ -179,8 +179,12 @@ module RuboCop
179
179
  include HashAlignmentStyles
180
180
  include RangeHelp
181
181
 
182
- MSG = 'Align the elements of a hash literal if they span more than ' \
183
- 'one line.'
182
+ MESSAGES = { KeyAlignment => 'Align the keys of a hash literal if ' \
183
+ 'they span more than one line.',
184
+ SeparatorAlignment => 'Align the separators of a hash ' \
185
+ 'literal if they span more than one line.',
186
+ TableAlignment => 'Align the keys and values of a hash ' \
187
+ 'literal if they span more than one line.' }.freeze
184
188
 
185
189
  def on_send(node)
186
190
  return if double_splat?(node)
@@ -249,9 +253,9 @@ module RuboCop
249
253
  end
250
254
 
251
255
  def add_offences
252
- _format, offences = offences_by.min_by { |_, v| v.length }
256
+ format, offences = offences_by.min_by { |_, v| v.length }
253
257
  (offences || []).each do |offence|
254
- add_offense offence
258
+ add_offense(offence, message: MESSAGES[format])
255
259
  end
256
260
  end
257
261
 
@@ -8,9 +8,9 @@ module RuboCop
8
8
  # In Ruby 2.3 or newer, squiggly heredocs (`<<~`) should be used. If you
9
9
  # use the older rubies, you should introduce some library to your project
10
10
  # (e.g. ActiveSupport, Powerpack or Unindent).
11
- # Note: When `Metrics/LineLength`'s `AllowHeredoc` is false (not default),
11
+ # Note: When `Layout/LineLength`'s `AllowHeredoc` is false (not default),
12
12
  # this cop does not add any offenses for long here documents to
13
- # avoid `Metrics/LineLength`'s offenses.
13
+ # avoid `Layout/LineLength`'s offenses.
14
14
  #
15
15
  # @example EnforcedStyle: squiggly (default)
16
16
  # # bad
@@ -159,11 +159,11 @@ module RuboCop
159
159
  end
160
160
 
161
161
  def unlimited_heredoc_length?
162
- config.for_cop('Metrics/LineLength')['AllowHeredoc']
162
+ config.for_cop('Layout/LineLength')['AllowHeredoc']
163
163
  end
164
164
 
165
165
  def max_line_length
166
- config.for_cop('Metrics/LineLength')['Max']
166
+ config.for_cop('Layout/LineLength')['Max']
167
167
  end
168
168
 
169
169
  def correct_by_squiggly(node)
@@ -35,6 +35,20 @@ module RuboCop
35
35
  # # Another line of comment
36
36
  # #*
37
37
  #
38
+ # @example AllowGemfileRubyComment: false (default)
39
+ #
40
+ # # bad
41
+ #
42
+ # #ruby=2.7.0
43
+ # #ruby-gemset=myproject
44
+ #
45
+ # @example AllowGemfileRubyComment: true
46
+ #
47
+ # # good
48
+ #
49
+ # #ruby=2.7.0
50
+ # #ruby-gemset=myproject
51
+ #
38
52
  class LeadingCommentSpace < Cop
39
53
  include RangeHelp
40
54
 
@@ -44,7 +58,8 @@ module RuboCop
44
58
  processed_source.each_comment do |comment|
45
59
  next unless comment.text =~ /\A#+[^#\s=:+-]/
46
60
  next if comment.loc.line == 1 && allowed_on_first_line?(comment)
47
- next if allow_doxygen_comment? && doxygen_comment_style?(comment)
61
+ next if doxygen_comment_style?(comment)
62
+ next if gemfile_ruby_comment?(comment)
48
63
 
49
64
  add_offense(comment)
50
65
  end
@@ -80,7 +95,23 @@ module RuboCop
80
95
  end
81
96
 
82
97
  def doxygen_comment_style?(comment)
83
- comment.text.start_with?('#*')
98
+ allow_doxygen_comment? && comment.text.start_with?('#*')
99
+ end
100
+
101
+ def allow_gemfile_ruby_comment?
102
+ cop_config['AllowGemfileRubyComment']
103
+ end
104
+
105
+ def gemfile?
106
+ File.basename(processed_source.file_path).eql?('Gemfile')
107
+ end
108
+
109
+ def ruby_comment_in_gemfile?(comment)
110
+ gemfile? && comment.text.start_with?('#ruby')
111
+ end
112
+
113
+ def gemfile_ruby_comment?(comment)
114
+ allow_gemfile_ruby_comment? && ruby_comment_in_gemfile?(comment)
84
115
  end
85
116
  end
86
117
  end
@@ -2,10 +2,9 @@
2
2
 
3
3
  require 'uri'
4
4
 
5
- # rubocop:disable Metrics/ClassLength
6
5
  module RuboCop
7
6
  module Cop
8
- module Metrics
7
+ module Layout
9
8
  # This cop checks the length of lines in the source code.
10
9
  # The maximum length is configurable.
11
10
  # The tab size is configured in the `IndentationWidth`
@@ -20,19 +19,25 @@ module RuboCop
20
19
  #
21
20
  # If autocorrection is enabled, the following Layout cops
22
21
  # are recommended to further format the broken lines.
22
+ # (Many of these are enabled by default.)
23
23
  #
24
- # - ParameterAlignment
25
24
  # - ArgumentAlignment
25
+ # - BlockAlignment
26
+ # - BlockDelimiters
27
+ # - BlockEndNewline
26
28
  # - ClosingParenthesisIndentation
27
29
  # - FirstArgumentIndentation
28
30
  # - FirstArrayElementIndentation
29
31
  # - FirstHashElementIndentation
30
32
  # - FirstParameterIndentation
31
33
  # - HashAlignment
34
+ # - IndentationWidth
32
35
  # - MultilineArrayLineBreaks
36
+ # - MultilineBlockLayout
33
37
  # - MultilineHashBraceLayout
34
38
  # - MultilineHashKeyLineBreaks
35
39
  # - MultilineMethodArgumentLineBreaks
40
+ # - ParameterAlignment
36
41
  #
37
42
  # Together, these cops will pretty print hashes, arrays,
38
43
  # method calls, etc. For example, let's say the max columns
@@ -58,9 +63,14 @@ module RuboCop
58
63
  include ConfigurableMax
59
64
  include IgnoredPattern
60
65
  include RangeHelp
66
+ include LineLengthHelp
61
67
 
62
68
  MSG = 'Line is too long. [%<length>d/%<max>d]'
63
69
 
70
+ def on_block(node)
71
+ check_for_breakable_block(node)
72
+ end
73
+
64
74
  def on_potential_breakable_node(node)
65
75
  check_for_breakable_node(node)
66
76
  end
@@ -109,6 +119,25 @@ module RuboCop
109
119
  end
110
120
  end
111
121
 
122
+ def check_for_breakable_block(block_node)
123
+ return unless block_node.single_line?
124
+
125
+ line_index = block_node.loc.line - 1
126
+ range = breakable_block_range(block_node)
127
+ pos = range.begin_pos + 1
128
+
129
+ breakable_range_by_line_index[line_index] =
130
+ range_between(pos, pos + 1)
131
+ end
132
+
133
+ def breakable_block_range(block_node)
134
+ if block_node.arguments? && !block_node.lambda?
135
+ block_node.arguments.loc.end
136
+ else
137
+ block_node.loc.begin
138
+ end
139
+ end
140
+
112
141
  def breakable_range_after_semicolon(semicolon_token)
113
142
  range = semicolon_token.pos
114
143
  end_pos = range.end_pos
@@ -130,20 +159,6 @@ module RuboCop
130
159
  @heredocs ||= extract_heredocs(processed_source.ast)
131
160
  end
132
161
 
133
- def tab_indentation_width
134
- config.for_cop('Layout/Tab')['IndentationWidth']
135
- end
136
-
137
- def indentation_difference(line)
138
- return 0 unless tab_indentation_width
139
-
140
- line.match(/^\t*/)[0].size * (tab_indentation_width - 1)
141
- end
142
-
143
- def line_length(line)
144
- line.length + indentation_difference(line)
145
- end
146
-
147
162
  def highlight_start(line)
148
163
  max - indentation_difference(line)
149
164
  end
@@ -225,51 +240,10 @@ module RuboCop
225
240
  end
226
241
  end
227
242
 
228
- def allow_uri?
229
- cop_config['AllowURI']
230
- end
231
-
232
- def ignore_cop_directives?
233
- cop_config['IgnoreCopDirectives']
234
- end
235
-
236
- def allowed_uri_position?(line, uri_range)
237
- uri_range.begin < max &&
238
- (uri_range.end == line_length(line) ||
239
- uri_range.end == line_length(line) - 1)
240
- end
241
-
242
- def find_excessive_uri_range(line)
243
- last_uri_match = match_uris(line).last
244
- return nil unless last_uri_match
245
-
246
- begin_position, end_position =
247
- last_uri_match.offset(0).map do |pos|
248
- pos + indentation_difference(line)
249
- end
250
- return nil if begin_position < max && end_position < max
251
-
252
- begin_position...end_position
253
- end
254
-
255
- def match_uris(string)
256
- matches = []
257
- string.scan(uri_regexp) do
258
- matches << $LAST_MATCH_INFO if valid_uri?($LAST_MATCH_INFO[0])
243
+ def line_in_heredoc?(line_number)
244
+ heredocs.any? do |range, _delimiter|
245
+ range.cover?(line_number)
259
246
  end
260
- matches
261
- end
262
-
263
- def valid_uri?(uri_ish_string)
264
- URI.parse(uri_ish_string)
265
- true
266
- rescue URI::InvalidURIError, NoMethodError
267
- false
268
- end
269
-
270
- def uri_regexp
271
- @uri_regexp ||=
272
- URI::DEFAULT_PARSER.make_regexp(cop_config['URISchemes'])
273
247
  end
274
248
 
275
249
  def check_directive_line(line, line_index)
@@ -287,23 +261,6 @@ module RuboCop
287
261
  )
288
262
  end
289
263
 
290
- def directive_on_source_line?(line_index)
291
- source_line_number = line_index + processed_source.buffer.first_line
292
- comment =
293
- processed_source
294
- .comments
295
- .detect { |e| e.location.line == source_line_number }
296
-
297
- return false unless comment
298
-
299
- comment.text.match(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
300
- end
301
-
302
- def line_length_without_directive(line)
303
- before_comment, = line.split(CommentConfig::COMMENT_DIRECTIVE_REGEXP)
304
- before_comment.rstrip.length
305
- end
306
-
307
264
  def check_uri_line(line, line_index)
308
265
  uri_range = find_excessive_uri_range(line)
309
266
  return if uri_range && allowed_uri_position?(line, uri_range)
@@ -318,4 +275,3 @@ module RuboCop
318
275
  end
319
276
  end
320
277
  end
321
- # rubocop:enable Metrics/ClassLength