rubocop 1.18.4 → 1.21.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 (130) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +65 -13
  4. data/lib/rubocop/cli.rb +18 -0
  5. data/lib/rubocop/config_loader.rb +2 -2
  6. data/lib/rubocop/config_loader_resolver.rb +21 -6
  7. data/lib/rubocop/cop/base.rb +2 -2
  8. data/lib/rubocop/cop/bundler/gem_filename.rb +103 -0
  9. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +12 -11
  10. data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
  11. data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +2 -2
  12. data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
  13. data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
  14. data/lib/rubocop/cop/documentation.rb +1 -1
  15. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  16. data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
  17. data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
  18. data/lib/rubocop/cop/internal_affairs.rb +2 -0
  19. data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
  20. data/lib/rubocop/cop/layout/class_structure.rb +2 -1
  21. data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
  22. data/lib/rubocop/cop/layout/end_alignment.rb +3 -2
  23. data/lib/rubocop/cop/layout/hash_alignment.rb +7 -3
  24. data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
  25. data/lib/rubocop/cop/layout/leading_comment_space.rb +2 -2
  26. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +33 -14
  27. data/lib/rubocop/cop/layout/line_length.rb +1 -1
  28. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  29. data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +3 -0
  30. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +22 -9
  31. data/lib/rubocop/cop/layout/single_line_block_chain.rb +15 -4
  32. data/lib/rubocop/cop/layout/space_after_not.rb +1 -0
  33. data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +2 -1
  34. data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
  35. data/lib/rubocop/cop/layout/space_around_operators.rb +8 -1
  36. data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -0
  37. data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
  38. data/lib/rubocop/cop/layout/space_inside_parens.rb +5 -5
  39. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -1
  40. data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
  41. data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +107 -0
  42. data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
  43. data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
  44. data/lib/rubocop/cop/lint/debugger.rb +2 -4
  45. data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
  46. data/lib/rubocop/cop/lint/empty_in_pattern.rb +1 -1
  47. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
  48. data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
  49. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +67 -0
  50. data/lib/rubocop/cop/lint/number_conversion.rb +7 -1
  51. data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
  52. data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -3
  53. data/lib/rubocop/cop/lint/useless_times.rb +1 -1
  54. data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
  55. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
  56. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  57. data/lib/rubocop/cop/mixin/annotation_comment.rb +57 -34
  58. data/lib/rubocop/cop/mixin/code_length.rb +1 -1
  59. data/lib/rubocop/cop/mixin/documentation_comment.rb +5 -2
  60. data/lib/rubocop/cop/mixin/frozen_string_literal.rb +19 -1
  61. data/lib/rubocop/cop/mixin/heredoc.rb +7 -0
  62. data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
  63. data/lib/rubocop/cop/mixin/percent_array.rb +13 -7
  64. data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +9 -1
  65. data/lib/rubocop/cop/mixin/require_library.rb +59 -0
  66. data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
  67. data/lib/rubocop/cop/naming/ascii_identifiers.rb +0 -3
  68. data/lib/rubocop/cop/naming/constant_name.rb +1 -1
  69. data/lib/rubocop/cop/naming/inclusive_language.rb +27 -10
  70. data/lib/rubocop/cop/style/accessor_grouping.rb +2 -2
  71. data/lib/rubocop/cop/style/and_or.rb +4 -0
  72. data/lib/rubocop/cop/style/ascii_comments.rb +0 -3
  73. data/lib/rubocop/cop/style/block_delimiters.rb +39 -6
  74. data/lib/rubocop/cop/style/case_equality.rb +6 -9
  75. data/lib/rubocop/cop/style/collection_methods.rb +2 -1
  76. data/lib/rubocop/cop/style/comment_annotation.rb +25 -39
  77. data/lib/rubocop/cop/style/commented_keyword.rb +2 -1
  78. data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
  79. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  80. data/lib/rubocop/cop/style/documentation.rb +23 -8
  81. data/lib/rubocop/cop/style/double_negation.rb +12 -1
  82. data/lib/rubocop/cop/style/empty_method.rb +1 -1
  83. data/lib/rubocop/cop/style/encoding.rb +26 -15
  84. data/lib/rubocop/cop/style/explicit_block_argument.rb +46 -11
  85. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  86. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +11 -0
  87. data/lib/rubocop/cop/style/hash_except.rb +4 -3
  88. data/lib/rubocop/cop/style/hash_transform_keys.rb +0 -3
  89. data/lib/rubocop/cop/style/identical_conditional_branches.rb +30 -5
  90. data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +3 -2
  91. data/lib/rubocop/cop/style/lambda_call.rb +1 -1
  92. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -6
  93. data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
  94. data/lib/rubocop/cop/style/missing_else.rb +7 -0
  95. data/lib/rubocop/cop/style/mutable_constant.rb +68 -6
  96. data/lib/rubocop/cop/style/negated_if.rb +1 -1
  97. data/lib/rubocop/cop/style/negated_unless.rb +1 -1
  98. data/lib/rubocop/cop/style/non_nil_check.rb +2 -2
  99. data/lib/rubocop/cop/style/not.rb +2 -2
  100. data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
  101. data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
  102. data/lib/rubocop/cop/style/raise_args.rb +1 -1
  103. data/lib/rubocop/cop/style/redundant_begin.rb +25 -0
  104. data/lib/rubocop/cop/style/redundant_condition.rb +2 -3
  105. data/lib/rubocop/cop/style/redundant_freeze.rb +4 -3
  106. data/lib/rubocop/cop/style/redundant_interpolation.rb +1 -1
  107. data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -3
  108. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +83 -0
  109. data/lib/rubocop/cop/style/redundant_sort.rb +19 -4
  110. data/lib/rubocop/cop/style/regexp_literal.rb +3 -3
  111. data/lib/rubocop/cop/style/return_nil.rb +2 -1
  112. data/lib/rubocop/cop/style/semicolon.rb +32 -24
  113. data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
  114. data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -0
  115. data/lib/rubocop/cop/style/special_global_vars.rb +21 -0
  116. data/lib/rubocop/cop/style/static_class.rb +1 -2
  117. data/lib/rubocop/cop/style/string_concatenation.rb +1 -1
  118. data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
  119. data/lib/rubocop/cop/style/symbol_array.rb +3 -3
  120. data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
  121. data/lib/rubocop/cop/style/word_array.rb +23 -5
  122. data/lib/rubocop/cop/style/yoda_condition.rb +4 -7
  123. data/lib/rubocop/cop/util.rb +7 -2
  124. data/lib/rubocop/magic_comment.rb +44 -15
  125. data/lib/rubocop/options.rb +1 -1
  126. data/lib/rubocop/result_cache.rb +1 -1
  127. data/lib/rubocop/runner.rb +1 -2
  128. data/lib/rubocop/version.rb +1 -1
  129. data/lib/rubocop.rb +9 -2
  130. metadata +14 -5
@@ -3,18 +3,18 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Bundler
6
- # The symbol argument `:gemcutter`, `:rubygems`, and `:rubyforge`
7
- # are deprecated. So please change your source to URL string that
8
- # 'https://rubygems.org' if possible, or 'http://rubygems.org' if not.
6
+ # Passing symbol arguments to `source` (e.g. `source :rubygems`) is
7
+ # deprecated because they default to using HTTP requests. Instead, specify
8
+ # `'https://rubygems.org'` if possible, or `'http://rubygems.org'` if not.
9
9
  #
10
- # This autocorrect will replace these symbols with 'https://rubygems.org'.
11
- # Because it is secure, HTTPS request is strongly recommended. And in
12
- # most use cases HTTPS will be fine.
10
+ # When autocorrecting, this cop will replace symbol arguments with
11
+ # `'https://rubygems.org'`.
13
12
  #
14
- # However, it don't replace all `sources` of `http://` with `https://`.
15
- # For example, when specifying an internal gem server using HTTP on the
16
- # intranet, a use case where HTTPS cannot be specified was considered.
17
- # Consider using HTTP only if you cannot use HTTPS.
13
+ # This cop will not replace existing sources that use `http://`. This may
14
+ # be necessary where HTTPS is not available. For example, where using an
15
+ # internal gem server via an intranet, or where HTTPS is prohibited.
16
+ # However, you should strongly prefer `https://` where possible, as it is
17
+ # more secure.
18
18
  #
19
19
  # @example
20
20
  # # bad
@@ -24,7 +24,8 @@ module RuboCop
24
24
  #
25
25
  # # good
26
26
  # source 'https://rubygems.org' # strongly recommended
27
- # source 'http://rubygems.org'
27
+ # source 'http://rubygems.org' # use only if HTTPS is unavailable
28
+ #
28
29
  class InsecureProtocolSource < Base
29
30
  include RangeHelp
30
31
  extend AutoCorrector
@@ -24,7 +24,7 @@ module RuboCop
24
24
  # gem 'rubocop'
25
25
  # # For tests
26
26
  # gem 'rspec'
27
- class OrderedGems < Cop
27
+ class OrderedGems < Cop # rubocop:disable InternalAffairs/InheritDeprecatedCopClass
28
28
  include ConfigurableEnforcedStyle
29
29
  include OrderedGemNode
30
30
 
@@ -87,8 +87,8 @@ module RuboCop
87
87
  end
88
88
 
89
89
  def needs_separating_space?
90
- block_begin.begin_pos == arguments_end_pos &&
91
- selector_end.end_pos == arguments_begin_pos ||
90
+ (block_begin.begin_pos == arguments_end_pos &&
91
+ selector_end.end_pos == arguments_begin_pos) ||
92
92
  block_begin.begin_pos == selector_end.end_pos
93
93
  end
94
94
 
@@ -28,7 +28,7 @@ module RuboCop
28
28
  indent_steps: 1)
29
29
  corrector.insert_before(
30
30
  range,
31
- "\n#{' ' * (node.loc.keyword.column + indent_steps * configured_width)}"
31
+ "\n#{' ' * (node.loc.keyword.column + (indent_steps * configured_width))}"
32
32
  )
33
33
  end
34
34
 
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # This class ensures a require statement is present for a standard library
6
+ # determined by the variable library_name
7
+ class RequireLibraryCorrector
8
+ extend RangeHelp
9
+
10
+ class << self
11
+ def correct(corrector, node, library_name)
12
+ node = node.parent while node.parent?
13
+ node = node.children.first if node.begin_type?
14
+ corrector.insert_before(node, require_statement(library_name))
15
+ end
16
+
17
+ def require_statement(library_name)
18
+ "require '#{library_name}'\n"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -8,7 +8,7 @@ module RuboCop
8
8
 
9
9
  # @api private
10
10
  def department_to_basename(department)
11
- "cops_#{department.downcase}"
11
+ "cops_#{department.to_s.downcase.tr('/', '_')}"
12
12
  end
13
13
 
14
14
  # @api private
@@ -50,7 +50,7 @@ module RuboCop
50
50
  # spec.add_dependency 'rubocop'
51
51
  # # For tests
52
52
  # spec.add_dependency 'rspec'
53
- class OrderedDependencies < Cop
53
+ class OrderedDependencies < Cop # rubocop:disable InternalAffairs/InheritDeprecatedCopClass
54
54
  include ConfigurableEnforcedStyle
55
55
  include OrderedGemNode
56
56
 
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # `RuboCop::Cop::Cop` is deprecated and will be removed in Rubocop 2.0.
7
+ # Your custom cop class should inherit from `RuboCop::Cop::Base` instead of
8
+ # `RuboCop::Cop::Cop`.
9
+ #
10
+ # See "v1 Upgrade Notes" for more details:
11
+ # https://docs.rubocop.org/rubocop/v1_upgrade_notes.html
12
+ #
13
+ # @example
14
+ # # bad
15
+ # class Foo < Cop
16
+ # end
17
+ #
18
+ # # good
19
+ # class Foo < Base
20
+ # end
21
+ #
22
+ class InheritDeprecatedCopClass < Base
23
+ MSG = 'Use `Base` instead of `Cop`.'
24
+
25
+ def on_class(node)
26
+ return unless (parent_class = node.parent_class)
27
+ return unless parent_class.children.last == :Cop
28
+
29
+ add_offense(parent_class)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Looks for references to a cop configuration key that isn't defined in config/default.yml.
7
+ class UndefinedConfig < Base
8
+ ALLOWED_CONFIGURATIONS = %w[
9
+ Safe SafeAutoCorrect AutoCorrect Severity StyleGuide Details Reference Include Exclude
10
+ ].freeze
11
+ RESTRICT_ON_SEND = %i[[] fetch].freeze
12
+ MSG = '`%<name>s` is not defined in the configuration for `%<cop>s` ' \
13
+ 'in `config/default.yml`.'
14
+
15
+ # @!method cop_class_def(node)
16
+ def_node_search :cop_class_def, <<~PATTERN
17
+ (class _ (const _ {:Base :Cop}) ...)
18
+ PATTERN
19
+
20
+ # @!method cop_config_accessor?(node)
21
+ def_node_matcher :cop_config_accessor?, <<~PATTERN
22
+ (send (send nil? :cop_config) {:[] :fetch} ${str sym}...)
23
+ PATTERN
24
+
25
+ def on_new_investigation
26
+ super
27
+ return unless processed_source.ast
28
+
29
+ cop_class = cop_class_def(processed_source.ast).first
30
+ return unless (@cop_class_name = extract_cop_name(cop_class))
31
+
32
+ @config_for_cop = RuboCop::ConfigLoader.default_configuration.for_cop(@cop_class_name)
33
+ end
34
+
35
+ def on_send(node)
36
+ return unless cop_class_name
37
+ return unless (config_name_node = cop_config_accessor?(node))
38
+ return if always_allowed?(config_name_node)
39
+ return if configuration_key_defined?(config_name_node)
40
+
41
+ message = format(MSG, name: config_name_node.value, cop: cop_class_name)
42
+ add_offense(config_name_node, message: message)
43
+ end
44
+
45
+ private
46
+
47
+ attr_reader :config_for_cop, :cop_class_name
48
+
49
+ def extract_cop_name(class_node)
50
+ return unless class_node
51
+
52
+ segments = [class_node].concat(
53
+ class_node.each_ancestor(:class, :module).take_while do |n|
54
+ n.identifier.short_name != :Cop
55
+ end
56
+ )
57
+
58
+ segments.reverse_each.map { |s| s.identifier.short_name }.join('/')
59
+ end
60
+
61
+ def always_allowed?(node)
62
+ ALLOWED_CONFIGURATIONS.include?(node.value)
63
+ end
64
+
65
+ def configuration_key_defined?(node)
66
+ config_for_cop.key?(node.value)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'internal_affairs/empty_line_between_expect_offense_and_correction'
4
4
  require_relative 'internal_affairs/example_description'
5
+ require_relative 'internal_affairs/inherit_deprecated_cop_class'
5
6
  require_relative 'internal_affairs/method_name_equal'
6
7
  require_relative 'internal_affairs/node_destructuring'
7
8
  require_relative 'internal_affairs/node_matcher_directive'
@@ -12,4 +13,5 @@ require_relative 'internal_affairs/redundant_let_rubocop_config_new'
12
13
  require_relative 'internal_affairs/redundant_location_argument'
13
14
  require_relative 'internal_affairs/redundant_message_argument'
14
15
  require_relative 'internal_affairs/style_detected_api_use'
16
+ require_relative 'internal_affairs/undefined_config'
15
17
  require_relative 'internal_affairs/useless_message_assertion'
@@ -54,7 +54,7 @@ module RuboCop
54
54
 
55
55
  def on_send(node)
56
56
  first_arg = node.first_argument
57
- return if !multiple_arguments?(node, first_arg) || node.send_type? && node.method?(:[]=)
57
+ return if !multiple_arguments?(node, first_arg) || (node.send_type? && node.method?(:[]=))
58
58
 
59
59
  if first_arg.hash_type? && !first_arg.braces?
60
60
  pairs = first_arg.pairs
@@ -264,7 +264,8 @@ module RuboCop
264
264
 
265
265
  def source_range_with_comment(node)
266
266
  begin_pos, end_pos =
267
- if node.def_type? && !node.method?(:initialize) || node.send_type? && node.def_modifier?
267
+ if (node.def_type? && !node.method?(:initialize)) ||
268
+ (node.send_type? && node.def_modifier?)
268
269
  start_node = find_visibility_start(node) || node
269
270
  end_node = find_visibility_end(node) || node
270
271
  [begin_pos_with_comment(start_node),
@@ -38,12 +38,14 @@ module RuboCop
38
38
  class EmptyLineAfterGuardClause < Base
39
39
  include RangeHelp
40
40
  extend AutoCorrector
41
+ extend Util
41
42
 
42
43
  MSG = 'Add empty line after guard clause.'
43
44
  END_OF_HEREDOC_LINE = 1
44
45
 
45
46
  def on_if(node)
46
47
  return if correct_style?(node)
48
+ return if multiple_statements_on_line?(node)
47
49
 
48
50
  if node.modifier_form? && (heredoc_node = last_heredoc_argument(node))
49
51
  return if next_line_empty_or_enable_directive_comment?(heredoc_line(node, heredoc_node))
@@ -166,6 +168,13 @@ module RuboCop
166
168
  node
167
169
  end
168
170
  end
171
+
172
+ def multiple_statements_on_line?(node)
173
+ parent = node.parent
174
+ return false unless parent
175
+
176
+ parent.begin_type? && parent.single_line?
177
+ end
169
178
  end
170
179
  end
171
180
  end
@@ -165,7 +165,8 @@ module RuboCop
165
165
  end
166
166
 
167
167
  def alignment_node_for_variable_style(node)
168
- return node.parent if node.case_type? && node.argument?
168
+ return node.parent if node.case_type? && node.argument? &&
169
+ node.loc.line == node.parent.loc.line
169
170
 
170
171
  assignment = assignment_or_operator_method(node)
171
172
 
@@ -181,7 +182,7 @@ module RuboCop
181
182
 
182
183
  def assignment_or_operator_method(node)
183
184
  node.ancestors.find do |ancestor|
184
- ancestor.assignment_or_similar? || ancestor.send_type? && ancestor.operator_method?
185
+ ancestor.assignment_or_similar? || (ancestor.send_type? && ancestor.operator_method?)
185
186
  end
186
187
  end
187
188
  end
@@ -218,9 +218,13 @@ module RuboCop
218
218
  private
219
219
 
220
220
  def autocorrect_incompatible_with_other_cops?(node)
221
- enforce_first_argument_with_fixed_indentation? &&
222
- node.pairs.any? &&
223
- node.parent&.call_type? && node.parent.loc.selector&.line == node.pairs.first.loc.line
221
+ return false unless enforce_first_argument_with_fixed_indentation? &&
222
+ node.pairs.any? &&
223
+ node.parent&.call_type?
224
+
225
+ parent_loc = node.parent.loc
226
+ selector = parent_loc.selector || parent_loc.expression
227
+ selector.line == node.pairs.first.loc.line
224
228
  end
225
229
 
226
230
  def reset!
@@ -143,13 +143,6 @@ module RuboCop
143
143
  indent_level(base_line)
144
144
  end
145
145
 
146
- def indent_level(str)
147
- indentations = str.lines
148
- .map { |line| line[/^\s*/] }
149
- .reject { |line| line.end_with?("\n") }
150
- indentations.empty? ? 0 : indentations.min_by(&:size).size
151
- end
152
-
153
146
  # Returns '~', '-' or nil
154
147
  def heredoc_indent_type(node)
155
148
  node.source[/^<<([~-])/, 1]
@@ -57,7 +57,7 @@ module RuboCop
57
57
 
58
58
  def on_new_investigation
59
59
  processed_source.comments.each do |comment|
60
- next unless /\A#+[^#\s=:+-]/.match?(comment.text)
60
+ next unless /\A#+[^#\s=+-]/.match?(comment.text)
61
61
  next if comment.loc.line == 1 && allowed_on_first_line?(comment)
62
62
  next if doxygen_comment_style?(comment)
63
63
  next if gemfile_ruby_comment?(comment)
@@ -77,7 +77,7 @@ module RuboCop
77
77
  end
78
78
 
79
79
  def allowed_on_first_line?(comment)
80
- shebang?(comment) || rackup_config_file? && rackup_options?(comment)
80
+ shebang?(comment) || (rackup_config_file? && rackup_options?(comment))
81
81
  end
82
82
 
83
83
  def shebang?(comment)
@@ -11,8 +11,7 @@ module RuboCop
11
11
  # concatenated string parts shall be indented regardless of `EnforcedStyle` configuration.
12
12
  #
13
13
  # If `EnforcedStyle: indented` is set, it's the second line that shall be indented one step
14
- # more than the first line. Lines 3 and forward shall be aligned with line 2. Here too there
15
- # are exceptions. Values in a hash literal are always aligned.
14
+ # more than the first line. Lines 3 and forward shall be aligned with line 2.
16
15
  #
17
16
  # @example
18
17
  # # bad
@@ -34,29 +33,44 @@ module RuboCop
34
33
  # 'z'
35
34
  # end
36
35
  #
37
- # my_hash = {
38
- # first: 'a message' \
39
- # 'in two parts'
40
- # }
41
- #
42
36
  # @example EnforcedStyle: aligned (default)
43
37
  # # bad
44
38
  # puts 'x' \
45
39
  # 'y'
46
40
  #
41
+ # my_hash = {
42
+ # first: 'a message' \
43
+ # 'in two parts'
44
+ # }
45
+ #
47
46
  # # good
48
47
  # puts 'x' \
49
48
  # 'y'
50
49
  #
50
+ # my_hash = {
51
+ # first: 'a message' \
52
+ # 'in two parts'
53
+ # }
54
+ #
51
55
  # @example EnforcedStyle: indented
52
56
  # # bad
53
57
  # result = 'x' \
54
58
  # 'y'
55
59
  #
60
+ # my_hash = {
61
+ # first: 'a message' \
62
+ # 'in two parts'
63
+ # }
64
+ #
56
65
  # # good
57
66
  # result = 'x' \
58
67
  # 'y'
59
68
  #
69
+ # my_hash = {
70
+ # first: 'a message' \
71
+ # 'in two parts'
72
+ # }
73
+ #
60
74
  class LineEndStringConcatenationIndentation < Base
61
75
  include ConfigurableEnforcedStyle
62
76
  include Alignment
@@ -70,7 +84,7 @@ module RuboCop
70
84
  return unless strings_concatenated_with_backslash?(node)
71
85
 
72
86
  children = node.children
73
- if style == :aligned && !always_indented?(node) || always_aligned?(node)
87
+ if style == :aligned && !always_indented?(node)
74
88
  check_aligned(children, 1)
75
89
  else
76
90
  check_indented(children)
@@ -94,10 +108,6 @@ module RuboCop
94
108
  PARENT_TYPES_FOR_INDENTED.include?(dstr_node.parent&.type)
95
109
  end
96
110
 
97
- def always_aligned?(dstr_node)
98
- dstr_node.parent&.pair_type?
99
- end
100
-
101
111
  def check_aligned(children, start_index)
102
112
  base_column = children[start_index - 1].loc.column
103
113
  children[start_index..-1].each do |child|
@@ -108,11 +118,20 @@ module RuboCop
108
118
  end
109
119
 
110
120
  def check_indented(children)
111
- base_column = children[0].source_range.source_line =~ /\S/
112
- @column_delta = base_column + configured_indentation_width - children[1].loc.column
121
+ @column_delta = base_column(children[0]) + configured_indentation_width -
122
+ children[1].loc.column
113
123
  add_offense_and_correction(children[1], MSG_INDENT) if @column_delta != 0
114
124
  end
115
125
 
126
+ def base_column(child)
127
+ grandparent = child.parent.parent
128
+ if grandparent&.type == :pair
129
+ grandparent.loc.column
130
+ else
131
+ child.source_range.source_line =~ /\S/
132
+ end
133
+ end
134
+
116
135
  def add_offense_and_correction(node, message)
117
136
  add_offense(node, message: message) { |corrector| autocorrect(corrector, node) }
118
137
  end