rubocop 1.34.1 → 1.36.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 (101) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +33 -0
  4. data/lib/rubocop/cli/command/{auto_genenerate_config.rb → auto_generate_config.rb} +0 -0
  5. data/lib/rubocop/config.rb +1 -1
  6. data/lib/rubocop/cop/correctors/parentheses_corrector.rb +32 -2
  7. data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
  8. data/lib/rubocop/cop/generator/require_file_injector.rb +2 -2
  9. data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +69 -0
  10. data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +5 -4
  11. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  12. data/lib/rubocop/cop/layout/block_alignment.rb +16 -12
  13. data/lib/rubocop/cop/layout/block_end_newline.rb +2 -0
  14. data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +5 -2
  15. data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +2 -0
  16. data/lib/rubocop/cop/layout/end_of_line.rb +4 -4
  17. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +2 -2
  18. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +2 -2
  19. data/lib/rubocop/cop/layout/indentation_width.rb +5 -1
  20. data/lib/rubocop/cop/layout/line_length.rb +4 -1
  21. data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
  22. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
  23. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  24. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  25. data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -0
  26. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +25 -9
  27. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +1 -1
  28. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
  29. data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
  30. data/lib/rubocop/cop/lint/empty_block.rb +1 -1
  31. data/lib/rubocop/cop/lint/empty_conditional_body.rb +31 -1
  32. data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -9
  33. data/lib/rubocop/cop/lint/literal_in_interpolation.rb +4 -0
  34. data/lib/rubocop/cop/lint/next_without_accumulator.rb +25 -6
  35. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +6 -6
  36. data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +12 -0
  37. data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +9 -3
  38. data/lib/rubocop/cop/lint/redundant_with_index.rb +13 -10
  39. data/lib/rubocop/cop/lint/redundant_with_object.rb +12 -11
  40. data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
  41. data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +15 -2
  42. data/lib/rubocop/cop/lint/unreachable_loop.rb +8 -2
  43. data/lib/rubocop/cop/lint/useless_access_modifier.rb +8 -6
  44. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +1 -1
  45. data/lib/rubocop/cop/lint/void.rb +2 -0
  46. data/lib/rubocop/cop/mixin/allowed_methods.rb +10 -5
  47. data/lib/rubocop/cop/mixin/allowed_pattern.rb +13 -5
  48. data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
  49. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +76 -1
  50. data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -6
  51. data/lib/rubocop/cop/mixin/method_complexity.rb +4 -4
  52. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +1 -1
  53. data/lib/rubocop/cop/mixin/range_help.rb +2 -3
  54. data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
  55. data/lib/rubocop/cop/naming/constant_name.rb +2 -2
  56. data/lib/rubocop/cop/style/access_modifier_declarations.rb +77 -1
  57. data/lib/rubocop/cop/style/arguments_forwarding.rb +2 -2
  58. data/lib/rubocop/cop/style/case_equality.rb +40 -10
  59. data/lib/rubocop/cop/style/class_methods_definitions.rb +2 -1
  60. data/lib/rubocop/cop/style/collection_methods.rb +2 -0
  61. data/lib/rubocop/cop/style/combinable_loops.rb +3 -1
  62. data/lib/rubocop/cop/style/each_for_simple_loop.rb +41 -6
  63. data/lib/rubocop/cop/style/each_with_object.rb +39 -8
  64. data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
  65. data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
  66. data/lib/rubocop/cop/style/for.rb +2 -0
  67. data/lib/rubocop/cop/style/guard_clause.rb +27 -16
  68. data/lib/rubocop/cop/style/hash_each_methods.rb +3 -1
  69. data/lib/rubocop/cop/style/hash_syntax.rb +17 -0
  70. data/lib/rubocop/cop/style/inverse_methods.rb +8 -6
  71. data/lib/rubocop/cop/style/magic_comment_format.rb +307 -0
  72. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -2
  73. data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
  74. data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -1
  75. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -1
  76. data/lib/rubocop/cop/style/next.rb +3 -5
  77. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  78. data/lib/rubocop/cop/style/object_then.rb +2 -0
  79. data/lib/rubocop/cop/style/perl_backrefs.rb +22 -1
  80. data/lib/rubocop/cop/style/proc.rb +4 -1
  81. data/lib/rubocop/cop/style/redundant_begin.rb +2 -0
  82. data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
  83. data/lib/rubocop/cop/style/redundant_parentheses.rb +4 -0
  84. data/lib/rubocop/cop/style/redundant_self.rb +2 -0
  85. data/lib/rubocop/cop/style/redundant_sort_by.rb +24 -8
  86. data/lib/rubocop/cop/style/safe_navigation.rb +4 -2
  87. data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
  88. data/lib/rubocop/cop/style/sole_nested_conditional.rb +0 -2
  89. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  90. data/lib/rubocop/cop/style/symbol_proc.rb +5 -4
  91. data/lib/rubocop/cop/style/top_level_method_definition.rb +3 -1
  92. data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
  93. data/lib/rubocop/cop/style/word_array.rb +1 -1
  94. data/lib/rubocop/cop/util.rb +1 -1
  95. data/lib/rubocop/feature_loader.rb +6 -2
  96. data/lib/rubocop/formatter/html_formatter.rb +2 -2
  97. data/lib/rubocop/runner.rb +4 -0
  98. data/lib/rubocop/server/cache.rb +11 -8
  99. data/lib/rubocop/version.rb +3 -2
  100. data/lib/rubocop.rb +3 -2
  101. metadata +9 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 11dc3da37672d6576b483ea0d39db975af0044d4c960fa14944fadac1d6f5bd5
4
- data.tar.gz: e37200a88a516b923c5c86149de704eec9147f8f35a96df30ced1a0892be8ab4
3
+ metadata.gz: 4bc2d4bd29fd7e81f4d7118fef9f55bcd2f086fd20ce383d85c2a1b7b26ea4e9
4
+ data.tar.gz: 9cb3cef8736b03dd0834b94fefe4415a301d78d842df184a1a1d290973376070
5
5
  SHA512:
6
- metadata.gz: 42deab573616fd15dfee978f2009b2dafc17b4bb0cf7528c0e0a26f415bad22ede108980403c6036b0b84d28b72981b3862b915a608495bc368935c1bc1ea18b
7
- data.tar.gz: 5afa69eeccbcf3ead2bb51f2feaf4dcd81420fa08b0b17b372909647d7b4b559fe1d76d376fec271c7b9d2e8f1ee1cb586075d9a461d5894c1263854fa6f2eea
6
+ metadata.gz: abeba580bae46458f6427c3d21c6532e95ff88e7d981d196f5013fee29c8f911146f229daa8e6f92d2f28bb14790edc46373a4366f9881700df18a7f5a1352fa
7
+ data.tar.gz: 4bee5fc42db940e95cd6e3fba83680e6d7fd573a8ed3b7c65136a48d5f3b67ba8d299ecdd30a422afb95961d84c9b0452e3d1b6c1952e0edee1cea97ca704952
data/README.md CHANGED
@@ -53,7 +53,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
53
53
  in your `Gemfile`:
54
54
 
55
55
  ```rb
56
- gem 'rubocop', '~> 1.34', require: false
56
+ gem 'rubocop', '~> 1.36', require: false
57
57
  ```
58
58
 
59
59
  See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
data/config/default.yml CHANGED
@@ -2794,6 +2794,7 @@ Naming/MethodParameterName:
2794
2794
  AllowNamesEndingInNumbers: true
2795
2795
  # Allowed names that will not register an offense
2796
2796
  AllowedNames:
2797
+ - as
2797
2798
  - at
2798
2799
  - by
2799
2800
  - db
@@ -2948,6 +2949,7 @@ Style/AccessModifierDeclarations:
2948
2949
  - inline
2949
2950
  - group
2950
2951
  AllowModifiersOnSymbols: true
2952
+ SafeAutoCorrect: false
2951
2953
 
2952
2954
  Style/AccessorGrouping:
2953
2955
  Description: 'Checks for grouping of accessors in `class` and `module` bodies.'
@@ -3186,6 +3188,15 @@ Style/CaseEquality:
3186
3188
  # # good
3187
3189
  # String === "string"
3188
3190
  AllowOnConstant: false
3191
+ # If `AllowOnSelfClass` option is enabled, the cop will ignore violations when the receiver of
3192
+ # the case equality operator is `self.class`.
3193
+ #
3194
+ # # bad
3195
+ # some_class === object
3196
+ #
3197
+ # # good
3198
+ # self.class === object
3199
+ AllowOnSelfClass: false
3189
3200
 
3190
3201
  Style/CaseLikeIf:
3191
3202
  Description: 'Identifies places where `if-elsif` constructions can be replaced with `case-when`.'
@@ -3839,6 +3850,8 @@ Style/HashSyntax:
3839
3850
  - never
3840
3851
  # accepts both shorthand and explicit use of hash literal value.
3841
3852
  - either
3853
+ # like "always", but will avoid mixing styles in a single hash
3854
+ - consistent
3842
3855
  # Force hashes that have a symbol value to use hash rockets
3843
3856
  UseHashRocketsWithSymbolValues: false
3844
3857
  # Do not suggest { a?: 1 } over { :a? => 1 } in ruby19 style
@@ -4015,6 +4028,26 @@ Style/LineEndConcatenation:
4015
4028
  VersionAdded: '0.18'
4016
4029
  VersionChanged: '0.64'
4017
4030
 
4031
+ Style/MagicCommentFormat:
4032
+ Description: 'Use a consistent style for magic comments.'
4033
+ Enabled: pending
4034
+ VersionAdded: '1.35'
4035
+ EnforcedStyle: snake_case
4036
+ SupportedStyles:
4037
+ # `snake` will enforce the magic comment is written
4038
+ # in snake case (words separated by underscores).
4039
+ # Eg: froze_string_literal: true
4040
+ - snake_case
4041
+ # `kebab` will enforce the magic comment is written
4042
+ # in kebab case (words separated by hyphens).
4043
+ # Eg: froze-string-literal: true
4044
+ - kebab_case
4045
+ DirectiveCapitalization: lowercase
4046
+ ValueCapitalization: ~
4047
+ SupportedCapitalizations:
4048
+ - lowercase
4049
+ - uppercase
4050
+
4018
4051
  Style/MapCompactWithConditionalBlock:
4019
4052
  Description: 'Prefer `select` or `reject` over `map { ... }.compact`.'
4020
4053
  Enabled: pending
@@ -242,7 +242,7 @@ module RuboCop
242
242
  return nil unless loaded_path
243
243
 
244
244
  base_path = base_dir_for_path_parameters
245
- ['gems.locked', 'Gemfile.lock'].each do |file_name|
245
+ ['Gemfile.lock', 'gems.locked'].each do |file_name|
246
246
  path = find_file_upwards(file_name, base_path)
247
247
  return path if path
248
248
  end
@@ -7,6 +7,8 @@ module RuboCop
7
7
  class << self
8
8
  include RangeHelp
9
9
 
10
+ COMMA_REGEXP = /(?<=\))\s*,/.freeze
11
+
10
12
  def correct(corrector, node)
11
13
  corrector.remove(node.loc.begin)
12
14
  corrector.remove(node.loc.end)
@@ -39,7 +41,15 @@ module RuboCop
39
41
  def handle_orphaned_comma(corrector, node)
40
42
  return unless only_closing_paren_before_comma?(node)
41
43
 
42
- range = range_with_surrounding_space(
44
+ range = extend_range_for_heredoc(node, parens_range(node))
45
+ corrector.remove(range)
46
+
47
+ add_heredoc_comma(corrector, node)
48
+ end
49
+
50
+ # Get a range for the closing parenthesis and all whitespace to the left of it
51
+ def parens_range(node)
52
+ range_with_surrounding_space(
43
53
  range: node.loc.end,
44
54
  buffer: node.source_range.source_buffer,
45
55
  side: :left,
@@ -47,8 +57,28 @@ module RuboCop
47
57
  whitespace: true,
48
58
  continuations: true
49
59
  )
60
+ end
50
61
 
51
- corrector.remove(range)
62
+ # If the node contains a heredoc, remove the comma too
63
+ # It'll be added back in the right place later
64
+ def extend_range_for_heredoc(node, range)
65
+ return range unless heredoc?(node)
66
+
67
+ comma_line = range_by_whole_lines(node.loc.end, buffer: node.source_range.source_buffer)
68
+ offset = comma_line.source.match(COMMA_REGEXP)[0]&.size || 0
69
+
70
+ range.adjust(end_pos: offset)
71
+ end
72
+
73
+ # Add a comma back after the heredoc identifier
74
+ def add_heredoc_comma(corrector, node)
75
+ return unless heredoc?(node)
76
+
77
+ corrector.insert_after(node.child_nodes.last.loc.expression, ',')
78
+ end
79
+
80
+ def heredoc?(node)
81
+ node.child_nodes.last.loc.is_a?(Parser::Source::Map::Heredoc)
52
82
  end
53
83
  end
54
84
  end
@@ -84,7 +84,7 @@ module RuboCop
84
84
  (str "true")
85
85
  PATTERN
86
86
 
87
- def on_block(node) # rubocop:disable Metrics/MethodLength
87
+ def on_block(node) # rubocop:disable Metrics/MethodLength, InternalAffairs/NumblockHandler
88
88
  gem_specification(node) do |block_var|
89
89
  metadata_value = metadata(node)
90
90
  mfa_value = mfa_value(metadata_value)
@@ -55,8 +55,8 @@ module RuboCop
55
55
  end
56
56
  end
57
57
 
58
- def require_path_fragments(require_directove)
59
- path = require_directove.match(REQUIRE_PATH)
58
+ def require_path_fragments(require_directive)
59
+ path = require_directive.match(REQUIRE_PATH)
60
60
 
61
61
  path ? path.captures.first.split('/') : []
62
62
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Checks for missing `numblock handlers. The blocks with numbered
7
+ # arguments introduced in Ruby 2.7 are parsed with a node type of
8
+ # `numblock` instead of block. Cops that define `block` handlers
9
+ # need to define `numblock` handlers or disable this cope for them.
10
+ #
11
+ # @example
12
+ #
13
+ # # bad
14
+ # class BlockRelatedCop < Base
15
+ # def on_block(node)
16
+ # end
17
+ # end
18
+ #
19
+ # # good
20
+ # class BlockRelatedCop < Base
21
+ # def on_block(node)
22
+ # end
23
+ #
24
+ # alias on_numblock on_block
25
+ # end
26
+ #
27
+ # class BlockRelatedCop < Base
28
+ # def on_block(node)
29
+ # end
30
+ #
31
+ # alias_method :on_numblock, :on_block
32
+ # end
33
+ #
34
+ # class BlockRelatedCop < Base
35
+ # def on_block(node)
36
+ # end
37
+ #
38
+ # def on_numblock(node)
39
+ # end
40
+ # end
41
+ class NumblockHandler < Base
42
+ MSG = 'Define on_numblock to handle blocks with numbered arguments.'
43
+
44
+ def on_def(node)
45
+ return unless block_handler?(node)
46
+ return unless node.parent
47
+
48
+ add_offense(node) unless numblock_handler?(node.parent)
49
+ end
50
+
51
+ private
52
+
53
+ # @!method block_handler?(node)
54
+ def_node_matcher :block_handler?, <<~PATTERN
55
+ (def :on_block (args (arg :node)) ...)
56
+ PATTERN
57
+
58
+ # @!method numblock_handler?(node)
59
+ def_node_matcher :numblock_handler?, <<~PATTERN
60
+ {
61
+ `(def :on_numblock (args (arg :node)) ...)
62
+ `(alias (sym :on_numblock) (sym :on_block))
63
+ `(send nil? :alias_method (sym :on_numblock) (sym :on_block))
64
+ }
65
+ PATTERN
66
+ end
67
+ end
68
+ end
69
+ end
@@ -29,20 +29,21 @@ module RuboCop
29
29
  extend AutoCorrector
30
30
 
31
31
  MSG = 'Use `%<preferred>s`.'
32
- RESTRICT_ON_SEND = %i[==].freeze
32
+ RESTRICT_ON_SEND = %i[== !=].freeze
33
33
 
34
34
  # @!method single_line_comparison(node)
35
35
  def_node_matcher :single_line_comparison, <<~PATTERN
36
36
  {
37
- (send (send $_receiver {:line :first_line}) :== (send _receiver :last_line))
38
- (send (send $_receiver :last_line) :== (send _receiver {:line :first_line}))
37
+ (send (send $_receiver {:line :first_line}) {:== :!=} (send _receiver :last_line))
38
+ (send (send $_receiver :last_line) {:== :!=} (send _receiver {:line :first_line}))
39
39
  }
40
40
  PATTERN
41
41
 
42
42
  def on_send(node)
43
43
  return unless (receiver = single_line_comparison(node))
44
44
 
45
- preferred = "#{extract_receiver(receiver)}.single_line?"
45
+ bang = node.method?(:!=) ? '!' : ''
46
+ preferred = "#{bang}#{extract_receiver(receiver)}.single_line?"
46
47
 
47
48
  add_offense(node, message: format(MSG, preferred: preferred)) do |corrector|
48
49
  corrector.replace(node, preferred)
@@ -10,6 +10,7 @@ require_relative 'internal_affairs/method_name_equal'
10
10
  require_relative 'internal_affairs/node_destructuring'
11
11
  require_relative 'internal_affairs/node_matcher_directive'
12
12
  require_relative 'internal_affairs/node_type_predicate'
13
+ require_relative 'internal_affairs/numblock_handler'
13
14
  require_relative 'internal_affairs/offense_location_keyword'
14
15
  require_relative 'internal_affairs/redundant_context_config_parameter'
15
16
  require_relative 'internal_affairs/redundant_described_class_as_subject'
@@ -22,23 +22,24 @@ module RuboCop
22
22
  # # bad
23
23
  #
24
24
  # foo.bar
25
- # .each do
26
- # baz
27
- # end
25
+ # .each do
26
+ # baz
27
+ # end
28
28
  #
29
29
  # # good
30
30
  #
31
- # variable = lambda do |i|
32
- # i
31
+ # foo.bar
32
+ # .each do
33
+ # baz
33
34
  # end
34
35
  #
35
36
  # @example EnforcedStyleAlignWith: start_of_block
36
37
  # # bad
37
38
  #
38
39
  # foo.bar
39
- # .each do
40
- # baz
41
- # end
40
+ # .each do
41
+ # baz
42
+ # end
42
43
  #
43
44
  # # good
44
45
  #
@@ -51,16 +52,17 @@ module RuboCop
51
52
  # # bad
52
53
  #
53
54
  # foo.bar
54
- # .each do
55
- # baz
56
- # end
55
+ # .each do
56
+ # baz
57
+ # end
57
58
  #
58
59
  # # good
59
60
  #
60
61
  # foo.bar
61
62
  # .each do
62
- # baz
63
+ # baz
63
64
  # end
65
+ #
64
66
  class BlockAlignment < Base
65
67
  include ConfigurableEnforcedStyle
66
68
  include RangeHelp
@@ -82,6 +84,8 @@ module RuboCop
82
84
  check_block_alignment(start_for_block_node(node), node)
83
85
  end
84
86
 
87
+ alias on_numblock on_block
88
+
85
89
  def style_parameter_name
86
90
  'EnforcedStyleAlignWith'
87
91
  end
@@ -39,6 +39,8 @@ module RuboCop
39
39
  register_offense(node)
40
40
  end
41
41
 
42
+ alias on_numblock on_block
43
+
42
44
  private
43
45
 
44
46
  def register_offense(node)
@@ -80,8 +80,11 @@ module RuboCop
80
80
  @block_line = node.source_range.first_line
81
81
  end
82
82
 
83
- def on_send(node)
84
- return unless node.bare_access_modifier? && !node.parent&.block_type?
83
+ alias on_numblock on_block
84
+
85
+ def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity
86
+ return unless node.bare_access_modifier? &&
87
+ !(node.parent&.block_type? || node.parent&.numblock_type?)
85
88
  return if expected_empty_lines?(node)
86
89
 
87
90
  message = message(node)
@@ -32,6 +32,8 @@ module RuboCop
32
32
 
33
33
  check(node, node.body, adjusted_first_line: first_line)
34
34
  end
35
+
36
+ alias on_numblock on_block
35
37
  end
36
38
  end
37
39
  end
@@ -22,20 +22,20 @@ module RuboCop
22
22
  # # all platforms.
23
23
  #
24
24
  # # bad
25
- # puts 'Hello' # Return character is CR+LF on all platfoms.
25
+ # puts 'Hello' # Return character is CR+LF on all platforms.
26
26
  #
27
27
  # # good
28
- # puts 'Hello' # Return character is LF on all platfoms.
28
+ # puts 'Hello' # Return character is LF on all platforms.
29
29
  #
30
30
  # @example EnforcedStyle: crlf
31
31
  # # The `crlf` style means that CR+LF (Carriage Return + Line Feed) is
32
32
  # # enforced on all platforms.
33
33
  #
34
34
  # # bad
35
- # puts 'Hello' # Return character is LF on all platfoms.
35
+ # puts 'Hello' # Return character is LF on all platforms.
36
36
  #
37
37
  # # good
38
- # puts 'Hello' # Return character is CR+LF on all platfoms.
38
+ # puts 'Hello' # Return character is CR+LF on all platforms.
39
39
  #
40
40
  class EndOfLine < Base
41
41
  include ConfigurableEnforcedStyle
@@ -143,7 +143,7 @@ module RuboCop
143
143
  case indent_base_type
144
144
  when :left_brace_or_bracket
145
145
  'the position of the opening bracket'
146
- when :first_colmn_after_left_parenthesis
146
+ when :first_column_after_left_parenthesis
147
147
  'the first position after the preceding left parenthesis'
148
148
  when :parent_hash_key
149
149
  'the parent hash key'
@@ -164,7 +164,7 @@ module RuboCop
164
164
  case indent_base_type
165
165
  when :left_brace_or_bracket
166
166
  'Indent the right bracket the same as the left bracket.'
167
- when :first_colmn_after_left_parenthesis
167
+ when :first_column_after_left_parenthesis
168
168
  'Indent the right bracket the same as the first position ' \
169
169
  'after the preceding left parenthesis.'
170
170
  when :parent_hash_key
@@ -192,7 +192,7 @@ module RuboCop
192
192
  case indent_base_type
193
193
  when :left_brace_or_bracket
194
194
  'the position of the opening brace'
195
- when :first_colmn_after_left_parenthesis
195
+ when :first_column_after_left_parenthesis
196
196
  'the first position after the preceding left parenthesis'
197
197
  when :parent_hash_key
198
198
  'the parent hash key'
@@ -213,7 +213,7 @@ module RuboCop
213
213
  case indent_base_type
214
214
  when :left_brace_or_bracket
215
215
  'Indent the right brace the same as the left brace.'
216
- when :first_colmn_after_left_parenthesis
216
+ when :first_column_after_left_parenthesis
217
217
  'Indent the right brace the same as the first position ' \
218
218
  'after the preceding left parenthesis.'
219
219
  when :parent_hash_key
@@ -90,6 +90,8 @@ module RuboCop
90
90
  check_members(end_loc, [node.body])
91
91
  end
92
92
 
93
+ alias on_numblock on_block
94
+
93
95
  def on_class(node)
94
96
  base = node.loc.keyword
95
97
  return if same_line?(base, node.body)
@@ -146,7 +148,9 @@ module RuboCop
146
148
  check_indentation(in_pattern_node.loc.keyword, in_pattern_node.body)
147
149
  end
148
150
 
149
- check_indentation(case_match.in_pattern_branches.last.loc.keyword, case_match.else_branch)
151
+ else_branch = case_match.else_branch&.empty_else_type? ? nil : case_match.else_branch
152
+
153
+ check_indentation(case_match.in_pattern_branches.last.loc.keyword, else_branch)
150
154
  end
151
155
 
152
156
  def on_if(node, base = node)
@@ -22,6 +22,7 @@ module RuboCop
22
22
  # (Many of these are enabled by default.)
23
23
  #
24
24
  # * ArgumentAlignment
25
+ # * ArrayAlignment
25
26
  # * BlockAlignment
26
27
  # * BlockDelimiters
27
28
  # * BlockEndNewline
@@ -74,6 +75,8 @@ module RuboCop
74
75
  check_for_breakable_block(node)
75
76
  end
76
77
 
78
+ alias on_numblock on_block
79
+
77
80
  def on_potential_breakable_node(node)
78
81
  check_for_breakable_node(node)
79
82
  end
@@ -131,7 +134,7 @@ module RuboCop
131
134
  if block_node.arguments? && !block_node.lambda?
132
135
  block_node.arguments.loc.end
133
136
  else
134
- block_node.loc.begin
137
+ block_node.braces? ? block_node.loc.begin : block_node.loc.begin.adjust(begin_pos: 1)
135
138
  end
136
139
  end
137
140
 
@@ -68,6 +68,8 @@ module RuboCop
68
68
  add_offense_for_expression(node, node.body, MSG)
69
69
  end
70
70
 
71
+ alias on_numblock on_block
72
+
71
73
  private
72
74
 
73
75
  def args_on_beginning_line?(node)
@@ -101,7 +101,7 @@ module RuboCop
101
101
  !comment_within?(node) &&
102
102
  node.each_descendant(:if, :case, :kwbegin, :def).none? &&
103
103
  node.each_descendant(:dstr, :str).none?(&:heredoc?) &&
104
- node.each_descendant(:begin).none? { |b| b.first_line != b.last_line }
104
+ node.each_descendant(:begin).none? { |b| !b.single_line? }
105
105
  end
106
106
 
107
107
  def convertible_block?(node)
@@ -29,7 +29,7 @@ module RuboCop
29
29
  include RangeHelp
30
30
  extend AutoCorrector
31
31
 
32
- def on_block(node)
32
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
33
33
  arguments = node.arguments
34
34
 
35
35
  return unless node.arguments? && pipes?(arguments)
@@ -41,7 +41,7 @@ module RuboCop
41
41
  check(node, [:operator].freeze) if node.keyword?
42
42
  end
43
43
 
44
- def on_block(node)
44
+ def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
45
45
  check(node, %i[begin end].freeze)
46
46
  end
47
47
 
@@ -76,6 +76,8 @@ module RuboCop
76
76
  end
77
77
  end
78
78
 
79
+ alias on_numblock on_block
80
+
79
81
  private
80
82
 
81
83
  def check_empty(left_brace, space_plus_brace, used_style)
@@ -131,7 +131,7 @@ module RuboCop
131
131
  args_delimiter = node.arguments.loc.begin if node.block_type? # Can be ( | or nil.
132
132
 
133
133
  check_left_brace(inner, node.loc.begin, args_delimiter)
134
- check_right_brace(inner, node.loc.begin, node.loc.end, node.single_line?)
134
+ check_right_brace(node, inner, node.loc.begin, node.loc.end, node.single_line?)
135
135
  end
136
136
 
137
137
  def check_left_brace(inner, left_brace, args_delimiter)
@@ -142,14 +142,15 @@ module RuboCop
142
142
  end
143
143
  end
144
144
 
145
- def check_right_brace(inner, left_brace, right_brace, single_line)
145
+ def check_right_brace(node, inner, left_brace, right_brace, single_line)
146
146
  if single_line && /\S$/.match?(inner)
147
147
  no_space(right_brace.begin_pos, right_brace.end_pos, 'Space missing inside }.')
148
148
  else
149
+ column = node.loc.expression.column
149
150
  return if multiline_block?(left_brace, right_brace) &&
150
- aligned_braces?(left_brace, right_brace)
151
+ aligned_braces?(inner, right_brace, column)
151
152
 
152
- space_inside_right_brace(right_brace)
153
+ space_inside_right_brace(inner, right_brace, column)
153
154
  end
154
155
  end
155
156
 
@@ -157,8 +158,12 @@ module RuboCop
157
158
  left_brace.first_line != right_brace.first_line
158
159
  end
159
160
 
160
- def aligned_braces?(left_brace, right_brace)
161
- left_brace.first_line == right_brace.last_column
161
+ def aligned_braces?(inner, right_brace, column)
162
+ column == right_brace.column || column == inner_last_space_count(inner)
163
+ end
164
+
165
+ def inner_last_space_count(inner)
166
+ inner.split("\n").last.count(' ')
162
167
  end
163
168
 
164
169
  def no_space_inside_left_brace(left_brace, args_delimiter)
@@ -197,10 +202,21 @@ module RuboCop
197
202
  args_delimiter&.is?('|')
198
203
  end
199
204
 
200
- def space_inside_right_brace(right_brace)
205
+ def space_inside_right_brace(inner, right_brace, column)
201
206
  brace_with_space = range_with_surrounding_space(right_brace, side: :left)
202
- space(brace_with_space.begin_pos, brace_with_space.end_pos - 1,
203
- 'Space inside } detected.')
207
+ begin_pos = brace_with_space.begin_pos
208
+ end_pos = brace_with_space.end_pos - 1
209
+
210
+ if brace_with_space.source.match?(/\R/)
211
+ begin_pos = end_pos - (right_brace.column - column)
212
+ end
213
+
214
+ if inner.end_with?(']')
215
+ end_pos -= 1
216
+ begin_pos = end_pos - (inner_last_space_count(inner) - column)
217
+ end
218
+
219
+ space(begin_pos, end_pos, 'Space inside } detected.')
204
220
  end
205
221
 
206
222
  def no_space(begin_pos, end_pos, msg)
@@ -81,7 +81,7 @@ module RuboCop
81
81
  def allowed_method_pattern?(node)
82
82
  node.assignment? || node.operator_method? || node.method?(:[]) ||
83
83
  allowed_method?(node.last_argument.method_name) ||
84
- matches_allowed_pattern?(node.last_argument.method_name)
84
+ matches_allowed_pattern?(node.last_argument.send_node.source)
85
85
  end
86
86
 
87
87
  def message(send_node)
@@ -66,7 +66,7 @@ module RuboCop
66
66
  private
67
67
 
68
68
  def delimiter
69
- CLASS_METHOD_DELIMETER
69
+ CLASS_METHOD_DELIMITER
70
70
  end
71
71
  end
72
72
 
@@ -89,7 +89,7 @@ module RuboCop
89
89
  private
90
90
 
91
91
  def delimiter
92
- instance_method? ? INSTANCE_METHOD_DELIMETER : CLASS_METHOD_DELIMETER
92
+ instance_method? ? INSTANCE_METHOD_DELIMITER : CLASS_METHOD_DELIMITER
93
93
  end
94
94
 
95
95
  def instance_method?
@@ -126,8 +126,8 @@ module RuboCop
126
126
 
127
127
  RESTRICT_ON_SEND = DEPRECATED_METHODS_OBJECT.keys.map(&:method).freeze
128
128
 
129
- CLASS_METHOD_DELIMETER = '.'
130
- INSTANCE_METHOD_DELIMETER = '#'
129
+ CLASS_METHOD_DELIMITER = '.'
130
+ INSTANCE_METHOD_DELIMITER = '#'
131
131
 
132
132
  def on_send(node)
133
133
  check(node) do |deprecated|
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Lint
6
- # Checks for duplicate `require`s and `require_relative`s.
6
+ # Checks for duplicate ``require``s and ``require_relative``s.
7
7
  #
8
8
  # @safety
9
9
  # This cop's autocorrection is unsafe because it may break the dependency order