rubocop 0.90.0 → 0.91.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -2
  3. data/config/default.yml +35 -0
  4. data/lib/rubocop.rb +10 -1
  5. data/lib/rubocop/cli/command/execute_runner.rb +8 -0
  6. data/lib/rubocop/config_loader.rb +3 -3
  7. data/lib/rubocop/config_store.rb +3 -3
  8. data/lib/rubocop/cop/bundler/duplicated_gem.rb +5 -1
  9. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
  10. data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +2 -0
  11. data/lib/rubocop/cop/generator.rb +1 -1
  12. data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
  13. data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
  14. data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -0
  15. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
  16. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -0
  17. data/lib/rubocop/cop/layout/begin_end_alignment.rb +77 -0
  18. data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
  19. data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +6 -5
  20. data/lib/rubocop/cop/layout/end_alignment.rb +5 -10
  21. data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +17 -4
  22. data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -3
  23. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -6
  24. data/lib/rubocop/cop/lint/ambiguous_operator.rb +2 -0
  25. data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -2
  26. data/lib/rubocop/cop/lint/constant_definition_in_block.rb +54 -0
  27. data/lib/rubocop/cop/lint/debugger.rb +2 -3
  28. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -3
  29. data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -4
  30. data/lib/rubocop/cop/lint/duplicate_require.rb +7 -2
  31. data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
  32. data/lib/rubocop/cop/lint/empty_file.rb +1 -4
  33. data/lib/rubocop/cop/lint/erb_new_arguments.rb +2 -0
  34. data/lib/rubocop/cop/lint/float_comparison.rb +2 -2
  35. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +2 -2
  36. data/lib/rubocop/cop/lint/identity_comparison.rb +49 -0
  37. data/lib/rubocop/cop/lint/inherit_exception.rb +2 -2
  38. data/lib/rubocop/cop/lint/multiple_comparison.rb +3 -1
  39. data/lib/rubocop/cop/lint/number_conversion.rb +1 -0
  40. data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -2
  41. data/lib/rubocop/cop/lint/raise_exception.rb +1 -0
  42. data/lib/rubocop/cop/lint/rand_one.rb +2 -1
  43. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
  44. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +3 -1
  45. data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
  46. data/lib/rubocop/cop/lint/to_json.rb +16 -5
  47. data/lib/rubocop/cop/lint/unreachable_loop.rb +2 -1
  48. data/lib/rubocop/cop/lint/uri_escape_unescape.rb +3 -1
  49. data/lib/rubocop/cop/lint/uri_regexp.rb +2 -1
  50. data/lib/rubocop/cop/lint/useless_method_definition.rb +20 -27
  51. data/lib/rubocop/cop/lint/useless_times.rb +97 -0
  52. data/lib/rubocop/cop/mixin/comments_help.rb +3 -9
  53. data/lib/rubocop/cop/mixin/configurable_naming.rb +2 -2
  54. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +9 -0
  55. data/lib/rubocop/cop/mixin/hash_transform_method.rb +9 -1
  56. data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -1
  57. data/lib/rubocop/cop/naming/file_name.rb +1 -1
  58. data/lib/rubocop/cop/security/eval.rb +1 -0
  59. data/lib/rubocop/cop/security/json_load.rb +1 -0
  60. data/lib/rubocop/cop/security/marshal_load.rb +1 -0
  61. data/lib/rubocop/cop/security/open.rb +1 -0
  62. data/lib/rubocop/cop/security/yaml_load.rb +1 -0
  63. data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -11
  64. data/lib/rubocop/cop/style/alias.rb +2 -0
  65. data/lib/rubocop/cop/style/array_join.rb +1 -0
  66. data/lib/rubocop/cop/style/attr.rb +1 -0
  67. data/lib/rubocop/cop/style/auto_resource_cleanup.rb +2 -0
  68. data/lib/rubocop/cop/style/case_equality.rb +3 -0
  69. data/lib/rubocop/cop/style/class_and_module_children.rb +2 -0
  70. data/lib/rubocop/cop/style/class_check.rb +6 -9
  71. data/lib/rubocop/cop/style/class_methods_definitions.rb +42 -16
  72. data/lib/rubocop/cop/style/class_vars.rb +1 -2
  73. data/lib/rubocop/cop/style/conditional_assignment.rb +49 -60
  74. data/lib/rubocop/cop/style/dir.rb +1 -0
  75. data/lib/rubocop/cop/style/double_negation.rb +1 -0
  76. data/lib/rubocop/cop/style/empty_literal.rb +3 -1
  77. data/lib/rubocop/cop/style/eval_with_location.rb +1 -3
  78. data/lib/rubocop/cop/style/even_odd.rb +1 -0
  79. data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
  80. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  81. data/lib/rubocop/cop/style/float_division.rb +2 -0
  82. data/lib/rubocop/cop/style/format_string.rb +1 -4
  83. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +12 -2
  84. data/lib/rubocop/cop/style/hash_transform_keys.rb +5 -11
  85. data/lib/rubocop/cop/style/hash_transform_values.rb +5 -11
  86. data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
  87. data/lib/rubocop/cop/style/lambda_call.rb +3 -1
  88. data/lib/rubocop/cop/style/mixin_usage.rb +1 -0
  89. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +14 -1
  90. data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
  91. data/lib/rubocop/cop/style/non_nil_check.rb +2 -0
  92. data/lib/rubocop/cop/style/not.rb +1 -0
  93. data/lib/rubocop/cop/style/numeric_predicate.rb +1 -3
  94. data/lib/rubocop/cop/style/preferred_hash_methods.rb +2 -0
  95. data/lib/rubocop/cop/style/raise_args.rb +2 -0
  96. data/lib/rubocop/cop/style/random_with_offset.rb +1 -0
  97. data/lib/rubocop/cop/style/redundant_exception.rb +1 -3
  98. data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
  99. data/lib/rubocop/cop/style/redundant_freeze.rb +2 -1
  100. data/lib/rubocop/cop/style/redundant_parentheses.rb +12 -3
  101. data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -2
  102. data/lib/rubocop/cop/style/redundant_sort.rb +1 -7
  103. data/lib/rubocop/cop/style/safe_navigation.rb +5 -0
  104. data/lib/rubocop/cop/style/sample.rb +2 -1
  105. data/lib/rubocop/cop/style/send.rb +2 -3
  106. data/lib/rubocop/cop/style/signal_exception.rb +2 -0
  107. data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
  108. data/lib/rubocop/cop/style/slicing_with_range.rb +2 -1
  109. data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
  110. data/lib/rubocop/cop/style/string_concatenation.rb +16 -2
  111. data/lib/rubocop/cop/style/strip.rb +1 -0
  112. data/lib/rubocop/cop/style/unpack_first.rb +1 -0
  113. data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -5
  114. data/lib/rubocop/core_ext/string.rb +1 -1
  115. data/lib/rubocop/ext/regexp_node.rb +4 -4
  116. data/lib/rubocop/options.rb +19 -1
  117. data/lib/rubocop/result_cache.rb +30 -13
  118. data/lib/rubocop/runner.rb +29 -14
  119. data/lib/rubocop/version.rb +1 -1
  120. metadata +8 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39db2826fa916853dc6641e30da24937e009a06b843daf1a7cdb2eca458dfaad
4
- data.tar.gz: 9a4ffd95ff0401aeee427555d1baf51517e06580becfa9243dac9c75305998aa
3
+ metadata.gz: 2e27cb6b7c6fcded0825904076fe0d1f5bdc7f3dd6d7391e1b3b09900f276eda
4
+ data.tar.gz: df22bf3745be7f53ec2c96a0c00f9d467bed247123d929a19938bb15b62b4a1c
5
5
  SHA512:
6
- metadata.gz: 7e502aeecd9ceaeec36624190f4e9b72d075eed552f2e6204e2e842db1a77758f5fd11336f01f400af45bb36b27f13885a27847b17b84595aeef90df6b0db63b
7
- data.tar.gz: e7ecf12bfc331ef99870f03e034a5154765ceb93c159fdde0fdf8802609c81fa3459250eb3bf6aa7ee140d5c80958783614f5da81585ba75ad9e252afdb8fc50
6
+ metadata.gz: 29e908f8706eedf177e93d909e744f310a57cfdbfb75d1071b4cddfeb3bd4e480ca7d654ec442ceac848b917cc75eebc500321740f8e68c59356e3b99a349b00
7
+ data.tar.gz: 4857c9ba3dec5f0c163e63101d630a543ba8a3d3b6a2d2b6237dca2e0072fbc6885222130957617ade1ae98f9a0e2ca624164f10d3fd146aa96270c777dbfc03
data/README.md CHANGED
@@ -22,7 +22,7 @@
22
22
  **RuboCop** is a Ruby static code analyzer (a.k.a. `linter`) and code formatter. Out of the box it
23
23
  will enforce many of the guidelines outlined in the community [Ruby Style
24
24
  Guide](https://rubystyle.guide). Apart from reporting the problems discovered in your code,
25
- RuboCop can also automatically fix many of them you.
25
+ RuboCop can also automatically fix many of them for you.
26
26
 
27
27
  RuboCop is extremely flexible and most aspects of its behavior can be tweaked via various
28
28
  [configuration options](https://github.com/rubocop-hq/rubocop/blob/master/config/default.yml).
@@ -49,7 +49,7 @@ haven't reached version 1.0 yet). To prevent an unwanted RuboCop update you
49
49
  might want to use a conservative version lock in your `Gemfile`:
50
50
 
51
51
  ```rb
52
- gem 'rubocop', '~> 0.90.0', require: false
52
+ gem 'rubocop', '~> 0.91.0', require: false
53
53
  ```
54
54
 
55
55
  ## Quickstart
@@ -117,6 +117,8 @@ AllCops:
117
117
  # CacheRootDirectory is ~ (nil), which it is by default, the root will be
118
118
  # taken from the environment variable `$XDG_CACHE_HOME` if it is set, or if
119
119
  # `$XDG_CACHE_HOME` is not set, it will be `$HOME/.cache/`.
120
+ # The CacheRootDirectory can be overwritten by passing the `--cache-root` command
121
+ # line option or by setting `$RUBOCOP_CACHE_ROOT` environment variable.
120
122
  CacheRootDirectory: ~
121
123
  # It is possible for a malicious user to know the location of RuboCop's cache
122
124
  # directory by looking at CacheRootDirectory, and create a symlink in its
@@ -309,6 +311,19 @@ Layout/AssignmentIndentation:
309
311
  # But it can be overridden by setting this parameter
310
312
  IndentationWidth: ~
311
313
 
314
+ Layout/BeginEndAlignment:
315
+ Description: 'Align ends corresponding to begins correctly.'
316
+ Enabled: pending
317
+ VersionAdded: '0.91'
318
+ # The value `start_of_line` means that `end` should be aligned the start of the line
319
+ # where the `begin` keyword is.
320
+ # The value `begin` means that `end` should be aligned with the `begin` keyword.
321
+ EnforcedStyleAlignWith: start_of_line
322
+ SupportedStylesAlignWith:
323
+ - start_of_line
324
+ - begin
325
+ Severity: warning
326
+
312
327
  Layout/BlockAlignment:
313
328
  Description: 'Align block ends correctly.'
314
329
  Enabled: true
@@ -1380,6 +1395,12 @@ Lint/CircularArgumentReference:
1380
1395
  Enabled: true
1381
1396
  VersionAdded: '0.33'
1382
1397
 
1398
+ Lint/ConstantDefinitionInBlock:
1399
+ Description: 'Do not define constants within a block.'
1400
+ StyleGuide: '#no-constant-definition-in-block'
1401
+ Enabled: pending
1402
+ VersionAdded: '0.91'
1403
+
1383
1404
  Lint/ConstantResolution:
1384
1405
  Description: 'Check that constants are fully qualified with `::`.'
1385
1406
  Enabled: false
@@ -1533,6 +1554,12 @@ Lint/HeredocMethodCallPosition:
1533
1554
  StyleGuide: '#heredoc-method-calls'
1534
1555
  VersionAdded: '0.68'
1535
1556
 
1557
+ Lint/IdentityComparison:
1558
+ Description: 'Prefer `equal?` over `==` when comparing `object_id`.'
1559
+ Enabled: pending
1560
+ StyleGuide: '#identity-comparison'
1561
+ VersionAdded: '0.91'
1562
+
1536
1563
  Lint/ImplicitStringConcatenation:
1537
1564
  Description: >-
1538
1565
  Checks for adjacent string literals on the same line, which
@@ -1784,6 +1811,7 @@ Lint/SafeNavigationChain:
1784
1811
  - presence
1785
1812
  - try
1786
1813
  - try!
1814
+ - in?
1787
1815
 
1788
1816
  Lint/SafeNavigationConsistency:
1789
1817
  Description: >-
@@ -1966,6 +1994,12 @@ Lint/UselessSetterCall:
1966
1994
  VersionChanged: '0.80'
1967
1995
  Safe: false
1968
1996
 
1997
+ Lint/UselessTimes:
1998
+ Description: 'Checks for useless `Integer#times` calls.'
1999
+ Enabled: pending
2000
+ VersionAdded: '0.91'
2001
+ Safe: false
2002
+
1969
2003
  Lint/Void:
1970
2004
  Description: 'Possible use of operator/literal/variable in void context.'
1971
2005
  Enabled: true
@@ -3074,6 +3108,7 @@ Style/GlobalStdStream:
3074
3108
  StyleGuide: '#global-stdout'
3075
3109
  Enabled: pending
3076
3110
  VersionAdded: '0.89'
3111
+ SafeAutoCorrect: false
3077
3112
 
3078
3113
  Style/GlobalVars:
3079
3114
  Description: 'Do not introduce global variables.'
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'English'
4
+ before_us = $LOADED_FEATURES.dup
3
5
  require 'rainbow'
4
6
 
5
- require 'English'
6
7
  require 'set'
7
8
  require 'forwardable'
8
9
  require 'regexp_parser'
@@ -154,6 +155,7 @@ require_relative 'rubocop/cop/layout/access_modifier_indentation'
154
155
  require_relative 'rubocop/cop/layout/argument_alignment'
155
156
  require_relative 'rubocop/cop/layout/array_alignment'
156
157
  require_relative 'rubocop/cop/layout/assignment_indentation'
158
+ require_relative 'rubocop/cop/layout/begin_end_alignment'
157
159
  require_relative 'rubocop/cop/layout/block_alignment'
158
160
  require_relative 'rubocop/cop/layout/block_end_newline'
159
161
  require_relative 'rubocop/cop/layout/case_indentation'
@@ -250,6 +252,7 @@ require_relative 'rubocop/cop/lint/big_decimal_new'
250
252
  require_relative 'rubocop/cop/lint/binary_operator_with_identical_operands'
251
253
  require_relative 'rubocop/cop/lint/boolean_symbol'
252
254
  require_relative 'rubocop/cop/lint/circular_argument_reference'
255
+ require_relative 'rubocop/cop/lint/constant_definition_in_block'
253
256
  require_relative 'rubocop/cop/lint/constant_resolution'
254
257
  require_relative 'rubocop/cop/lint/debugger'
255
258
  require_relative 'rubocop/cop/lint/deprecated_class_methods'
@@ -276,6 +279,7 @@ require_relative 'rubocop/cop/lint/float_comparison'
276
279
  require_relative 'rubocop/cop/lint/float_out_of_range'
277
280
  require_relative 'rubocop/cop/lint/format_parameter_mismatch'
278
281
  require_relative 'rubocop/cop/lint/heredoc_method_call_position'
282
+ require_relative 'rubocop/cop/lint/identity_comparison'
279
283
  require_relative 'rubocop/cop/lint/implicit_string_concatenation'
280
284
  require_relative 'rubocop/cop/lint/inherit_exception'
281
285
  require_relative 'rubocop/cop/lint/ineffective_access_modifier'
@@ -340,6 +344,7 @@ require_relative 'rubocop/cop/lint/useless_assignment'
340
344
  require_relative 'rubocop/cop/lint/useless_else_without_rescue'
341
345
  require_relative 'rubocop/cop/lint/useless_method_definition'
342
346
  require_relative 'rubocop/cop/lint/useless_setter_call'
347
+ require_relative 'rubocop/cop/lint/useless_times'
343
348
  require_relative 'rubocop/cop/lint/void'
344
349
 
345
350
  require_relative 'rubocop/cop/metrics/utils/iterating_block'
@@ -627,3 +632,7 @@ require_relative 'rubocop/options'
627
632
  require_relative 'rubocop/remote_config'
628
633
  require_relative 'rubocop/target_ruby'
629
634
  require_relative 'rubocop/yaml_duplication_checker'
635
+
636
+ unless File.exist?("#{__dir__}/../rubocop.gemspec") # Check if we are a gem
637
+ RuboCop::ResultCache.rubocop_required_features = $LOADED_FEATURES - before_us
638
+ end
@@ -8,6 +8,9 @@ module RuboCop
8
8
  class ExecuteRunner < Base
9
9
  include Formatter::TextUtil
10
10
 
11
+ # Combination of short and long formatter names.
12
+ INTEGRATION_FORMATTERS = %w[h html j json ju junit].freeze
13
+
11
14
  self.command_name = :execute_runner
12
15
 
13
16
  def run
@@ -61,6 +64,11 @@ module RuboCop
61
64
  end
62
65
 
63
66
  def maybe_print_corrected_source
67
+ # Integration tools (like RubyMine) expect to have only the JSON result
68
+ # when specifying JSON format. Similar HTML and JUnit are targeted as well.
69
+ # See: https://github.com/rubocop-hq/rubocop/issues/8673
70
+ return if INTEGRATION_FORMATTERS.include?(@options[:format])
71
+
64
72
  # If we are asked to autocorrect source code read from stdin, the only
65
73
  # reasonable place to write it is to stdout
66
74
  # Unfortunately, we also write other information to stdout
@@ -129,9 +129,9 @@ module RuboCop
129
129
 
130
130
  def default_configuration
131
131
  @default_configuration ||= begin
132
- print 'Default ' if debug?
133
- load_file(DEFAULT_FILE)
134
- end
132
+ print 'Default ' if debug?
133
+ load_file(DEFAULT_FILE)
134
+ end
135
135
  end
136
136
 
137
137
  # Returns the path rubocop inferred as the root of the project. No file
@@ -54,9 +54,9 @@ module RuboCop
54
54
  @path_cache[dir] ||= ConfigLoader.configuration_file_for(dir)
55
55
  path = @path_cache[dir]
56
56
  @object_cache[path] ||= begin
57
- print "For #{dir}: " if ConfigLoader.debug?
58
- ConfigLoader.configuration_from_file(path)
59
- end
57
+ print "For #{dir}: " if ConfigLoader.debug?
58
+ ConfigLoader.configuration_from_file(path)
59
+ end
60
60
  end
61
61
  end
62
62
  end
@@ -53,7 +53,11 @@ module RuboCop
53
53
  gem_declarations(processed_source.ast)
54
54
  .group_by(&:first_argument)
55
55
  .values
56
- .select { |nodes| nodes.size > 1 }
56
+ .select { |nodes| nodes.size > 1 && !condition?(nodes) }
57
+ end
58
+
59
+ def condition?(nodes)
60
+ nodes[0].parent&.if_type? && nodes[0].parent == nodes[1].parent
57
61
  end
58
62
 
59
63
  def register_offense(node, gem_name, line_of_first_occurrence)
@@ -64,6 +64,7 @@ module RuboCop
64
64
  MSG = 'Missing gem description comment.'
65
65
  CHECKED_OPTIONS_CONFIG = 'OnlyFor'
66
66
  VERSION_SPECIFIERS_OPTION = 'version_specifiers'
67
+ RESTRICT_ON_SEND = %i[gem].freeze
67
68
 
68
69
  def_node_matcher :gem_declaration?, '(send nil? :gem str ...)'
69
70
 
@@ -34,6 +34,8 @@ module RuboCop
34
34
  "Please change your source to 'https://rubygems.org' " \
35
35
  "if possible, or 'http://rubygems.org' if not."
36
36
 
37
+ RESTRICT_ON_SEND = %i[source].freeze
38
+
37
39
  def_node_matcher :insecure_protocol_source?, <<~PATTERN
38
40
  (send nil? :source
39
41
  $(sym ${:gemcutter :rubygems :rubyforge}))
@@ -59,7 +59,7 @@ module RuboCop
59
59
  # TODO: Implement the cop in here.
60
60
  #
61
61
  # In many cases, you can use a node matcher for matching node pattern.
62
- # See https://github.com/rubocop-hq/rubocop-ast/blob/master/lib/rubocop/node_pattern.rb
62
+ # See https://github.com/rubocop-hq/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb
63
63
  #
64
64
  # For example
65
65
  MSG = 'Use `#good_method` instead of `#bad_method`.'
@@ -18,6 +18,7 @@ module RuboCop
18
18
 
19
19
  MSG = 'Use `method?(%<method_name>s)` instead of ' \
20
20
  '`method_name == %<method_name>s`.'
21
+ RESTRICT_ON_SEND = %i[==].freeze
21
22
 
22
23
  def_node_matcher :method_name?, <<~PATTERN
23
24
  (send
@@ -17,6 +17,7 @@ module RuboCop
17
17
  extend AutoCorrector
18
18
 
19
19
  MSG = 'Use `#%<type>s_type?` to check node type.'
20
+ RESTRICT_ON_SEND = %i[==].freeze
20
21
 
21
22
  def_node_matcher :node_type_check, <<~PATTERN
22
23
  (send (send $_ :type) :== (sym $_))
@@ -18,6 +18,7 @@ module RuboCop
18
18
 
19
19
  MSG = 'Use `:%<keyword>s` as the location argument to ' \
20
20
  '`#add_offense`.'
21
+ RESTRICT_ON_SEND = %i[add_offense].freeze
21
22
 
22
23
  def on_send(node)
23
24
  node_type_check(node) do |node_arg, kwargs|
@@ -21,6 +21,7 @@ module RuboCop
21
21
  extend AutoCorrector
22
22
 
23
23
  MSG = 'Redundant location argument to `#add_offense`.'
24
+ RESTRICT_ON_SEND = %i[add_offense].freeze
24
25
 
25
26
  def_node_matcher :redundant_location_argument, <<~PATTERN
26
27
  (send nil? :add_offense _
@@ -24,6 +24,7 @@ module RuboCop
24
24
  extend AutoCorrector
25
25
 
26
26
  MSG = 'Redundant message argument to `#add_offense`.'
27
+ RESTRICT_ON_SEND = %i[add_offense].freeze
27
28
 
28
29
  def_node_matcher :node_type_check, <<~PATTERN
29
30
  (send nil? :add_offense $_node $hash)
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Layout
6
+ # This cop checks whether the end keyword of `begin` is aligned properly.
7
+ #
8
+ # Two modes are supported through the `EnforcedStyleAlignWith` configuration
9
+ # parameter. If it's set to `start_of_line` (which is the default), the
10
+ # `end` shall be aligned with the start of the line where the `begin`
11
+ # keyword is. If it's set to `begin`, the `end` shall be aligned with the
12
+ # `begin` keyword.
13
+ #
14
+ # `Layout/EndAlignment` cop aligns with keywords (e.g. `if`, `while`, `case`)
15
+ # by default. On the other hand, `||= begin` that this cop targets tends to
16
+ # align with the start of the line, it defaults to `EnforcedStyleAlignWith: start_of_line`.
17
+ # These style can be configured by each cop.
18
+ #
19
+ # @example EnforcedStyleAlignWith: start_of_line (default)
20
+ # # bad
21
+ # foo ||= begin
22
+ # do_something
23
+ # end
24
+ #
25
+ # # good
26
+ # foo ||= begin
27
+ # do_something
28
+ # end
29
+ #
30
+ # @example EnforcedStyleAlignWith: begin
31
+ # # bad
32
+ # foo ||= begin
33
+ # do_something
34
+ # end
35
+ #
36
+ # # good
37
+ # foo ||= begin
38
+ # do_something
39
+ # end
40
+ #
41
+ class BeginEndAlignment < Base
42
+ include EndKeywordAlignment
43
+ include RangeHelp
44
+ extend AutoCorrector
45
+
46
+ MSG = '`end` at %d, %d is not aligned with `%s` at %d, %d.'
47
+
48
+ def on_kwbegin(node)
49
+ check_begin_alignment(node)
50
+ end
51
+
52
+ private
53
+
54
+ def check_begin_alignment(node)
55
+ align_with = {
56
+ begin: node.loc.begin,
57
+ start_of_line: start_line_range(node)
58
+ }
59
+ check_end_kw_alignment(node, align_with)
60
+ end
61
+
62
+ def autocorrect(corrector, node)
63
+ AlignmentCorrector.align_end(corrector, processed_source, node, alignment_node(node))
64
+ end
65
+
66
+ def alignment_node(node)
67
+ case style
68
+ when :begin
69
+ node
70
+ else
71
+ start_line_range(node)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -46,7 +46,7 @@ module RuboCop
46
46
  alias on_defs on_def
47
47
 
48
48
  def on_send(node)
49
- return unless node.def_modifier?
49
+ return if !node.def_modifier? || node.method?(:using)
50
50
 
51
51
  method_def = node.each_descendant(:def, :defs).first
52
52
  expr = node.source_range
@@ -62,7 +62,7 @@ module RuboCop
62
62
  return if node.ternary?
63
63
 
64
64
  if node.modifier_form?
65
- check_condition(node.condition) unless next_sibling_empty?(node)
65
+ check_condition(node.condition) if right_sibling(node)
66
66
  else
67
67
  check_condition(node.condition)
68
68
  end
@@ -74,7 +74,7 @@ module RuboCop
74
74
  alias on_until on_while
75
75
 
76
76
  def on_while_post(node)
77
- return if next_sibling_empty?(node)
77
+ return unless right_sibling(node)
78
78
 
79
79
  check_condition(node.condition)
80
80
  end
@@ -116,9 +116,10 @@ module RuboCop
116
116
  processed_source[line].blank?
117
117
  end
118
118
 
119
- def next_sibling_empty?(node)
120
- next_sibling = node.parent.children[node.sibling_index + 1]
121
- next_sibling.nil?
119
+ def right_sibling(node)
120
+ return unless node.parent
121
+
122
+ node.parent.children[node.sibling_index + 1]
122
123
  end
123
124
 
124
125
  def multiline_when_condition?(when_node)
@@ -17,6 +17,11 @@ module RuboCop
17
17
  # If it's set to `start_of_line`, the `end` shall be aligned with the
18
18
  # start of the line where the matching keyword appears.
19
19
  #
20
+ # This `Layout/EndAlignment` cop aligns with keywords (e.g. `if`, `while`, `case`)
21
+ # by default. On the other hand, `Layout/BeginEndAlignment` cop aligns with
22
+ # `EnforcedStyleAlignWith: start_of_line` by default due to `||= begin` tends
23
+ # to align with the start of the line. These style can be configured by each cop.
24
+ #
20
25
  # @example EnforcedStyleAlignWith: keyword (default)
21
26
  # # bad
22
27
  #
@@ -173,16 +178,6 @@ module RuboCop
173
178
  node
174
179
  end
175
180
  end
176
-
177
- def start_line_range(node)
178
- expr = node.source_range
179
- buffer = expr.source_buffer
180
- source = buffer.source_line(expr.line)
181
- range = buffer.line_range(expr.line)
182
-
183
- range_between(range.begin_pos + (source =~ /\S/),
184
- range.begin_pos + (source =~ /\s*\z/))
185
- end
186
181
  end
187
182
  end
188
183
  end