rubocop 1.30.1 → 1.31.2

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 (132) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +41 -8
  4. data/config/obsoletion.yml +2 -0
  5. data/exe/rubocop +15 -7
  6. data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
  7. data/lib/rubocop/cli/command/suggest_extensions.rb +3 -3
  8. data/lib/rubocop/config.rb +4 -0
  9. data/lib/rubocop/config_loader.rb +1 -0
  10. data/lib/rubocop/config_loader_resolver.rb +1 -1
  11. data/lib/rubocop/config_validator.rb +3 -3
  12. data/lib/rubocop/cop/base.rb +5 -1
  13. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
  14. data/lib/rubocop/cop/bundler/gem_filename.rb +4 -4
  15. data/lib/rubocop/cop/bundler/ordered_gems.rb +2 -2
  16. data/lib/rubocop/cop/corrector.rb +2 -2
  17. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +3 -3
  18. data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +1 -1
  19. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +31 -16
  20. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +1 -1
  21. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  22. data/lib/rubocop/cop/gemspec/require_mfa.rb +20 -20
  23. data/lib/rubocop/cop/generator.rb +5 -1
  24. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -5
  25. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
  26. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -1
  27. data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +54 -0
  28. data/lib/rubocop/cop/internal_affairs.rb +1 -0
  29. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  30. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +25 -4
  31. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +20 -13
  32. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +51 -12
  33. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  34. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +68 -0
  35. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +130 -0
  36. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  37. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
  38. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -2
  39. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  40. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  41. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  42. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  43. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +5 -3
  44. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  45. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +51 -0
  46. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  47. data/lib/rubocop/cop/lint/literal_as_condition.rb +5 -0
  48. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +126 -0
  49. data/lib/rubocop/cop/lint/number_conversion.rb +3 -3
  50. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  51. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +5 -5
  52. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +1 -1
  53. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -1
  54. data/lib/rubocop/cop/lint/regexp_as_condition.rb +2 -2
  55. data/lib/rubocop/cop/lint/struct_new_override.rb +2 -2
  56. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
  57. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +44 -0
  58. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  59. data/lib/rubocop/cop/mixin/def_node.rb +2 -7
  60. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +34 -12
  61. data/lib/rubocop/cop/mixin/range_help.rb +7 -3
  62. data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
  63. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -1
  64. data/lib/rubocop/cop/style/block_delimiters.rb +4 -2
  65. data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
  66. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -0
  67. data/lib/rubocop/cop/style/empty_method.rb +16 -1
  68. data/lib/rubocop/cop/style/encoding.rb +1 -1
  69. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  70. data/lib/rubocop/cop/style/format_string_token.rb +48 -17
  71. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  72. data/lib/rubocop/cop/style/guard_clause.rb +8 -6
  73. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
  74. data/lib/rubocop/cop/style/hash_except.rb +88 -8
  75. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  76. data/lib/rubocop/cop/style/implicit_runtime_error.rb +2 -2
  77. data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -1
  78. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  79. data/lib/rubocop/cop/style/map_to_hash.rb +1 -1
  80. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -2
  81. data/lib/rubocop/cop/style/module_function.rb +2 -2
  82. data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -2
  83. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  84. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -3
  85. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  86. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  87. data/lib/rubocop/cop/style/multiline_when_then.rb +1 -3
  88. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
  89. data/lib/rubocop/cop/style/nested_ternary_operator.rb +19 -7
  90. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  91. data/lib/rubocop/cop/style/not.rb +1 -1
  92. data/lib/rubocop/cop/style/redundant_argument.rb +1 -1
  93. data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -1
  94. data/lib/rubocop/cop/style/redundant_return.rb +1 -1
  95. data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
  96. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  97. data/lib/rubocop/cop/style/struct_inheritance.rb +2 -2
  98. data/lib/rubocop/cop/style/swap_values.rb +1 -1
  99. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  100. data/lib/rubocop/cop/style/top_level_method_definition.rb +0 -2
  101. data/lib/rubocop/cop/style/unpack_first.rb +1 -1
  102. data/lib/rubocop/cop/util.rb +1 -1
  103. data/lib/rubocop/formatter/formatter_set.rb +20 -19
  104. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +15 -2
  105. data/lib/rubocop/formatter/html_formatter.rb +0 -1
  106. data/lib/rubocop/formatter/offense_count_formatter.rb +2 -0
  107. data/lib/rubocop/formatter/simple_text_formatter.rb +6 -7
  108. data/lib/rubocop/formatter.rb +31 -0
  109. data/lib/rubocop/options.rb +24 -1
  110. data/lib/rubocop/rake_task.rb +34 -9
  111. data/lib/rubocop/server/cache.rb +109 -0
  112. data/lib/rubocop/server/cli.rb +104 -0
  113. data/lib/rubocop/server/client_command/base.rb +44 -0
  114. data/lib/rubocop/server/client_command/exec.rb +59 -0
  115. data/lib/rubocop/server/client_command/restart.rb +25 -0
  116. data/lib/rubocop/server/client_command/start.rb +43 -0
  117. data/lib/rubocop/server/client_command/status.rb +28 -0
  118. data/lib/rubocop/server/client_command/stop.rb +31 -0
  119. data/lib/rubocop/server/client_command.rb +26 -0
  120. data/lib/rubocop/server/core.rb +79 -0
  121. data/lib/rubocop/server/errors.rb +23 -0
  122. data/lib/rubocop/server/helper.rb +34 -0
  123. data/lib/rubocop/server/server_command/base.rb +50 -0
  124. data/lib/rubocop/server/server_command/exec.rb +34 -0
  125. data/lib/rubocop/server/server_command/stop.rb +24 -0
  126. data/lib/rubocop/server/server_command.rb +21 -0
  127. data/lib/rubocop/server/socket_reader.rb +65 -0
  128. data/lib/rubocop/server.rb +53 -0
  129. data/lib/rubocop/version.rb +15 -8
  130. data/lib/rubocop.rb +8 -27
  131. metadata +42 -4
  132. data/lib/rubocop/cop/gemspec/date_assignment.rb +0 -49
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 86a53f724e77a824d22b44ec21ac28819f73e5766ffcc5734b1908edda92d37d
4
- data.tar.gz: 924864c8350f97c98bea2f25420d1adda3f6595474baced2edec3b409302c413
3
+ metadata.gz: 998eb88b2df9c70d2b432d79cd43901b7e72d33714c2a1ac1e1ebf3f64d75b85
4
+ data.tar.gz: 335fd1c15204af498ebee7200f2958902ccfd96923f9530b589517a857967a72
5
5
  SHA512:
6
- metadata.gz: f8b462b23760d3f4a51c0a94dfa01c0b43c0715255b65d58883fd8b67f6841d5d8384c367d3711d68ba6f8f4d72fac1533fdaa33d817986434c185a5c6574f13
7
- data.tar.gz: 3751cee1ebfd9d5968f6510d1440675286bbcb5a4f20c9bebb1cdd916ac70a07d4fd8f111352c10fa90ec546bfa36dc2cdae413b25bd24afcf62ed54db7c78a3
6
+ metadata.gz: ceda90af46b2317f03cc4a86ea8aba7c63a96f12d55d6bea533ae2537d68bdc75a647f2d2a1f5103c1799f2dda6ec0f923351891050915a237919df241d39e1e
7
+ data.tar.gz: 8a0c5273cf612ca4717d619ce33b37dc2206beb4c6d247c3b501a67ec364861e18c24878b513c37b67e499084162cb70ab618e6e090f7876fda8da37c93d45d1
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.30', require: false
56
+ gem 'rubocop', '~> 1.31', 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
@@ -153,6 +153,8 @@ AllCops:
153
153
  rubocop-sequel: [sequel]
154
154
  rubocop-rake: [rake]
155
155
  rubocop-graphql: [graphql]
156
+ # Enable/Disable checking the methods extended by Active Support.
157
+ ActiveSupportExtensionsEnabled: false
156
158
 
157
159
  #################### Bundler ###############################
158
160
 
@@ -235,13 +237,6 @@ Bundler/OrderedGems:
235
237
 
236
238
  #################### Gemspec ###############################
237
239
 
238
- Gemspec/DateAssignment:
239
- Description: 'Checks that `date =` is not used in gemspec file, it is set automatically when the gem is packaged.'
240
- Enabled: pending
241
- VersionAdded: '1.10'
242
- Include:
243
- - '**/*.gemspec'
244
-
245
240
  Gemspec/DependencyVersion:
246
241
  Description: 'Requires or forbids specifying gem dependency versions.'
247
242
  Enabled: false
@@ -974,6 +969,26 @@ Layout/LeadingEmptyLines:
974
969
  VersionAdded: '0.57'
975
970
  VersionChanged: '0.77'
976
971
 
972
+ Layout/LineContinuationLeadingSpace:
973
+ Description: >-
974
+ Use trailing spaces instead of leading spaces in strings
975
+ broken over multiple lines (by a backslash).
976
+ Enabled: pending
977
+ AutoCorrect: false
978
+ SafeAutoCorrect: false
979
+ VersionAdded: '1.31'
980
+
981
+ Layout/LineContinuationSpacing:
982
+ Description: 'Checks the spacing in front of backslash in line continuations.'
983
+ Enabled: pending
984
+ AutoCorrect: true
985
+ SafeAutoCorrect: true
986
+ VersionAdded: '1.31'
987
+ EnforcedStyle: space
988
+ SupportedStyles:
989
+ - space
990
+ - no_space
991
+
977
992
  Layout/LineEndStringConcatenationIndentation:
978
993
  Description: >-
979
994
  Checks the indentation of the next line after a line that
@@ -1550,6 +1565,11 @@ Lint/ConstantDefinitionInBlock:
1550
1565
  AllowedMethods:
1551
1566
  - enums
1552
1567
 
1568
+ Lint/ConstantOverwrittenInRescue:
1569
+ Description: 'Checks for overwriting an exception with an exception result by use `rescue =>`.'
1570
+ Enabled: pending
1571
+ VersionAdded: '1.31'
1572
+
1553
1573
  Lint/ConstantResolution:
1554
1574
  Description: 'Check that constants are fully qualified with `::`.'
1555
1575
  Enabled: false
@@ -1936,6 +1956,12 @@ Lint/NoReturnInBeginEndBlocks:
1936
1956
  Enabled: pending
1937
1957
  VersionAdded: '1.2'
1938
1958
 
1959
+ Lint/NonAtomicFileOperation:
1960
+ Description: Checks for non-atomic file operations.
1961
+ Enabled: pending
1962
+ VersionAdded: '1.31'
1963
+ SafeAutoCorrect: false
1964
+
1939
1965
  Lint/NonDeterministicRequireOrder:
1940
1966
  Description: 'Always sort arrays returned by Dir.glob when requiring files.'
1941
1967
  Enabled: true
@@ -2348,6 +2374,12 @@ Lint/UselessAssignment:
2348
2374
  Enabled: true
2349
2375
  VersionAdded: '0.11'
2350
2376
 
2377
+ Lint/UselessElseWithoutRescue:
2378
+ Description: 'Checks for useless `else` in `begin..end` without `rescue`.'
2379
+ Enabled: true
2380
+ VersionAdded: '0.17'
2381
+ VersionChanged: '1.31'
2382
+
2351
2383
  Lint/UselessMethodDefinition:
2352
2384
  Description: 'Checks for useless method definitions.'
2353
2385
  Enabled: true
@@ -3677,7 +3709,7 @@ Style/GuardClause:
3677
3709
  StyleGuide: '#no-nested-conditionals'
3678
3710
  Enabled: true
3679
3711
  VersionAdded: '0.20'
3680
- VersionChanged: '1.28'
3712
+ VersionChanged: '1.31'
3681
3713
  # `MinBodyLength` defines the number of lines of the a body of an `if` or `unless`
3682
3714
  # needs to have to trigger this cop
3683
3715
  MinBodyLength: 1
@@ -3718,6 +3750,7 @@ Style/HashExcept:
3718
3750
  that can be replaced with `Hash#except` method.
3719
3751
  Enabled: pending
3720
3752
  VersionAdded: '1.7'
3753
+ VersionChanged: '1.31'
3721
3754
 
3722
3755
  Style/HashLikeCase:
3723
3756
  Description: >-
@@ -55,6 +55,8 @@ renamed:
55
55
 
56
56
  # Cops that were removed
57
57
  removed:
58
+ Gemspec/DateAssignment:
59
+ alternatives: Gemspec/DeprecatedAttributeAssignment
58
60
  Layout/SpaceAfterControlKeyword:
59
61
  alternatives: Layout/SpaceAroundKeyword
60
62
  Layout/SpaceBeforeModifierKeyword:
data/exe/rubocop CHANGED
@@ -3,13 +3,21 @@
3
3
 
4
4
  $LOAD_PATH.unshift("#{__dir__}/../lib")
5
5
 
6
- require 'rubocop'
7
- require 'benchmark'
6
+ require 'rubocop/server'
7
+ server_cli = RuboCop::Server::CLI.new
8
+ exit_status = server_cli.run
9
+ exit exit_status if server_cli.exit?
8
10
 
9
- cli = RuboCop::CLI.new
10
- result = 0
11
+ if RuboCop::Server.running?
12
+ exit_status = RuboCop::Server::ClientCommand::Exec.new.run
13
+ else
14
+ require 'rubocop'
15
+ require 'benchmark'
11
16
 
12
- time = Benchmark.realtime { result = cli.run }
17
+ cli = RuboCop::CLI.new
13
18
 
14
- puts "Finished in #{time} seconds" if cli.options[:debug] || cli.options[:display_time]
15
- exit result
19
+ time = Benchmark.realtime { exit_status = cli.run }
20
+
21
+ puts "Finished in #{time} seconds" if cli.options[:debug] || cli.options[:display_time]
22
+ end
23
+ exit exit_status
@@ -112,7 +112,7 @@ module RuboCop
112
112
 
113
113
  write_config_file(config_file, file_string, rubocop_yml_contents)
114
114
 
115
- puts "Added inheritance from `#{relative_path_to_todo_from_options_config}` "\
115
+ puts "Added inheritance from `#{relative_path_to_todo_from_options_config}` " \
116
116
  "in `#{ConfigLoader::DOTFILE}`."
117
117
  end
118
118
 
@@ -18,7 +18,7 @@ module RuboCop
18
18
  return if skip? || extensions.none?
19
19
 
20
20
  puts
21
- puts 'Tip: Based on detected gems, the following '\
21
+ puts 'Tip: Based on detected gems, the following ' \
22
22
  'RuboCop extension libraries might be helpful:'
23
23
 
24
24
  extensions.sort.each do |extension|
@@ -26,8 +26,8 @@ module RuboCop
26
26
  end
27
27
 
28
28
  puts
29
- puts 'You can opt out of this message by adding the following to your config '\
30
- '(see https://docs.rubocop.org/rubocop/extensions.html#extension-suggestions '\
29
+ puts 'You can opt out of this message by adding the following to your config ' \
30
+ '(see https://docs.rubocop.org/rubocop/extensions.html#extension-suggestions ' \
31
31
  'for more options):'
32
32
  puts ' AllCops:'
33
33
  puts ' SuggestExtensions: false'
@@ -146,6 +146,10 @@ module RuboCop
146
146
  for_all_cops['NewCops'] == 'enable'
147
147
  end
148
148
 
149
+ def active_support_extensions_enabled?
150
+ for_all_cops['ActiveSupportExtensionsEnabled']
151
+ end
152
+
149
153
  def file_to_include?(file)
150
154
  relative_file_path = path_relative_to_config(file)
151
155
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'erb'
3
4
  require 'yaml'
4
5
  require 'pathname'
5
6
 
@@ -179,7 +179,7 @@ module RuboCop
179
179
 
180
180
  def determine_inherit_mode(hash, key)
181
181
  cop_cfg = hash[key]
182
- local_inherit = cop_cfg.delete('inherit_mode') if cop_cfg.is_a?(Hash)
182
+ local_inherit = cop_cfg['inherit_mode'] if cop_cfg.is_a?(Hash)
183
183
  local_inherit || hash['inherit_mode'] || {}
184
184
  end
185
185
 
@@ -107,7 +107,7 @@ module RuboCop
107
107
  return if unknown_cops.empty?
108
108
 
109
109
  if ConfigLoader.ignore_unrecognized_cops
110
- warn Rainbow('The following cops or departments are not '\
110
+ warn Rainbow('The following cops or departments are not ' \
111
111
  'recognized and will be ignored:').yellow
112
112
  warn unknown_cops.join("\n")
113
113
 
@@ -265,8 +265,8 @@ module RuboCop
265
265
  # FIXME: Handling colors in exception messages like this is ugly.
266
266
  def msg_not_boolean(parent, key, value)
267
267
  "#{Rainbow('').reset}" \
268
- "Property #{Rainbow(key).yellow} of cop #{Rainbow(parent).yellow}" \
269
- " is supposed to be a boolean and #{Rainbow(value).yellow} is not."
268
+ "Property #{Rainbow(key).yellow} of cop #{Rainbow(parent).yellow} " \
269
+ "is supposed to be a boolean and #{Rainbow(value).yellow} is not."
270
270
  end
271
271
  end
272
272
  end
@@ -48,7 +48,7 @@ module RuboCop
48
48
  InvestigationReport = Struct.new(:cop, :processed_source, :offenses, :corrector)
49
49
 
50
50
  # List of methods names to restrict calls for `on_send` / `on_csend`
51
- RESTRICT_ON_SEND = Set[].freeze
51
+ RESTRICT_ON_SEND = Set[].freeze # rubocop:disable InternalAffairs/UselessRestrictOnSend
52
52
 
53
53
  # List of cops that should not try to autocorrect at the same
54
54
  # time as this cop
@@ -220,6 +220,10 @@ module RuboCop
220
220
  @config.target_rails_version
221
221
  end
222
222
 
223
+ def active_support_extensions_enabled?
224
+ @config.active_support_extensions_enabled?
225
+ end
226
+
223
227
  def relevant_file?(file)
224
228
  file == RuboCop::AST::ProcessedSource::STRING_SOURCE_NAME ||
225
229
  (file_name_matches_any?(file, 'Include', true) &&
@@ -38,7 +38,7 @@ module RuboCop
38
38
  class DuplicatedGem < Base
39
39
  include RangeHelp
40
40
 
41
- MSG = 'Gem `%<gem_name>s` requirements already given on line '\
41
+ MSG = 'Gem `%<gem_name>s` requirements already given on line ' \
42
42
  '%<line_of_first_occurrence>d of the Gemfile.'
43
43
 
44
44
  def on_new_investigation
@@ -29,13 +29,13 @@ module RuboCop
29
29
  include ConfigurableEnforcedStyle
30
30
  include RangeHelp
31
31
 
32
- MSG_GEMFILE_REQUIRED = '`gems.rb` file was found but `Gemfile` is required '\
32
+ MSG_GEMFILE_REQUIRED = '`gems.rb` file was found but `Gemfile` is required ' \
33
33
  '(file path: %<file_path>s).'
34
- MSG_GEMS_RB_REQUIRED = '`Gemfile` was found but `gems.rb` file is required '\
34
+ MSG_GEMS_RB_REQUIRED = '`Gemfile` was found but `gems.rb` file is required ' \
35
35
  '(file path: %<file_path>s).'
36
- MSG_GEMFILE_MISMATCHED = 'Expected a `Gemfile.lock` with `Gemfile` but found '\
36
+ MSG_GEMFILE_MISMATCHED = 'Expected a `Gemfile.lock` with `Gemfile` but found ' \
37
37
  '`gems.locked` file (file path: %<file_path>s).'
38
- MSG_GEMS_RB_MISMATCHED = 'Expected a `gems.locked` file with `gems.rb` but found '\
38
+ MSG_GEMS_RB_MISMATCHED = 'Expected a `gems.locked` file with `gems.rb` but found ' \
39
39
  '`Gemfile.lock` (file path: %<file_path>s).'
40
40
  GEMFILE_FILES = %w[Gemfile Gemfile.lock].freeze
41
41
  GEMS_RB_FILES = %w[gems.rb gems.locked].freeze
@@ -28,8 +28,8 @@ module RuboCop
28
28
  extend AutoCorrector
29
29
  include OrderedGemNode
30
30
 
31
- MSG = 'Gems should be sorted in an alphabetical order within their '\
32
- 'section of the Gemfile. '\
31
+ MSG = 'Gems should be sorted in an alphabetical order within their ' \
32
+ 'section of the Gemfile. ' \
33
33
  'Gem `%<previous>s` should appear before `%<current>s`.'
34
34
 
35
35
  def on_new_investigation
@@ -109,8 +109,8 @@ module RuboCop
109
109
  "Parser::Source::Buffer, but got #{buffer.class}"
110
110
  end
111
111
  raise "Correction target buffer #{buffer.object_id} " \
112
- "name:#{buffer.name.inspect}" \
113
- " is not current #{@source_buffer.object_id} " \
112
+ "name:#{buffer.name.inspect} " \
113
+ "is not current #{@source_buffer.object_id} " \
114
114
  "name:#{@source_buffer.name.inspect} under investigation"
115
115
  end
116
116
  end
@@ -41,7 +41,7 @@ module RuboCop
41
41
  end
42
42
 
43
43
  def correct_next_line_brace(corrector)
44
- corrector.remove(range_with_surrounding_space(range: node.loc.end, side: :left))
44
+ corrector.remove(range_with_surrounding_space(node.loc.end, side: :left))
45
45
 
46
46
  corrector.insert_before(
47
47
  last_element_range_with_trailing_comma(node).end,
@@ -51,7 +51,7 @@ module RuboCop
51
51
 
52
52
  def content_if_comment_present(corrector, node)
53
53
  range = range_with_surrounding_space(
54
- range: children(node).last.source_range,
54
+ children(node).last.source_range,
55
55
  side: :right
56
56
  ).end.resize(1)
57
57
  if range.source == '#'
@@ -86,7 +86,7 @@ module RuboCop
86
86
 
87
87
  def last_element_trailing_comma_range(node)
88
88
  range = range_with_surrounding_space(
89
- range: children(node).last.source_range,
89
+ children(node).last.source_range,
90
90
  side: :right
91
91
  ).end.resize(1)
92
92
 
@@ -29,7 +29,7 @@ module RuboCop
29
29
  end
30
30
 
31
31
  def correct_for_blockarg_type(corrector, node)
32
- range = range_with_surrounding_space(range: node.source_range, side: :left)
32
+ range = range_with_surrounding_space(node.source_range, side: :left)
33
33
  range = range_with_surrounding_comma(range, :left)
34
34
 
35
35
  corrector.remove(range)
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Gemspec
6
6
  # Checks that deprecated attribute attributes are not set in a gemspec file.
7
- # Removing `test_files` allows the user to receive smaller packed gems.
7
+ # Removing deprecated attributes allows the user to receive smaller packed gems.
8
8
  #
9
9
  # @example
10
10
  #
@@ -29,7 +29,7 @@ module RuboCop
29
29
  include RangeHelp
30
30
  extend AutoCorrector
31
31
 
32
- MSG = 'Do not set `test_files` in gemspec.'
32
+ MSG = 'Do not set `%<attribute>s` in gemspec.'
33
33
 
34
34
  # @!method gem_specification(node)
35
35
  def_node_matcher :gem_specification, <<~PATTERN
@@ -45,14 +45,14 @@ module RuboCop
45
45
 
46
46
  block_parameter = block_node.arguments.first.source
47
47
 
48
- date_assignment = block_node.descendants.detect do |node|
49
- use_test_files?(node, block_parameter)
48
+ assignment = block_node.descendants.detect do |node|
49
+ use_deprecated_attributes?(node, block_parameter)
50
50
  end
51
+ return unless assignment
51
52
 
52
- return unless date_assignment
53
-
54
- add_offense(date_assignment) do |corrector|
55
- range = range_by_whole_lines(date_assignment.source_range, include_final_newline: true)
53
+ message = format_message_from
54
+ add_offense(assignment, message: message) do |corrector|
55
+ range = range_by_whole_lines(assignment.source_range, include_final_newline: true)
56
56
 
57
57
  corrector.remove(range)
58
58
  end
@@ -60,16 +60,31 @@ module RuboCop
60
60
 
61
61
  private
62
62
 
63
- def use_test_files?(node, block_parameter)
64
- node, method_name = if node.op_asgn_type?
65
- lhs, _op, _rhs = *node
63
+ def node_and_method_name(node, attribute)
64
+ if node.op_asgn_type?
65
+ lhs, _op, _rhs = *node
66
+ [lhs, attribute]
67
+ else
68
+ [node, "#{attribute}=".to_sym]
69
+ end
70
+ end
71
+
72
+ def use_deprecated_attributes?(node, block_parameter)
73
+ %i[test_files date specification_version rubygems_version].each do |attribute|
74
+ node, method_name = node_and_method_name(node, attribute)
75
+ unless node.send_type? && node.receiver&.source == block_parameter &&
76
+ node.method?(method_name)
77
+ next
78
+ end
66
79
 
67
- [lhs, :test_files]
68
- else
69
- [node, :test_files=]
70
- end
80
+ @attribute = attribute.to_s
81
+ return true
82
+ end
83
+ false
84
+ end
71
85
 
72
- node.send_type? && node.receiver&.source == block_parameter && node.method?(method_name)
86
+ def format_message_from
87
+ format(MSG, attribute: @attribute)
73
88
  end
74
89
  end
75
90
  end
@@ -38,7 +38,7 @@ module RuboCop
38
38
  include RangeHelp
39
39
  include GemspecHelp
40
40
 
41
- MSG = '`%<assignment>s` method calls already given on line '\
41
+ MSG = '`%<assignment>s` method calls already given on line ' \
42
42
  '%<line_of_first_occurrence>d of the gemspec.'
43
43
 
44
44
  # @!method assignment_method_declarations(node)
@@ -55,7 +55,7 @@ module RuboCop
55
55
  include OrderedGemNode
56
56
 
57
57
  MSG = 'Dependencies should be sorted in an alphabetical order within ' \
58
- 'their section of the gemspec. '\
58
+ 'their section of the gemspec. ' \
59
59
  'Dependency `%<previous>s` should appear before `%<current>s`.'
60
60
 
61
61
  def on_new_investigation
@@ -36,29 +36,29 @@ module RuboCop
36
36
  # spec.metadata['rubygems_mfa_required'] = 'true'
37
37
  # end
38
38
  #
39
- # # bad
40
- # Gem::Specification.new do |spec|
41
- # spec.metadata = {
42
- # 'rubygems_mfa_required' => 'false'
43
- # }
44
- # end
39
+ # # bad
40
+ # Gem::Specification.new do |spec|
41
+ # spec.metadata = {
42
+ # 'rubygems_mfa_required' => 'false'
43
+ # }
44
+ # end
45
45
  #
46
- # # good
47
- # Gem::Specification.new do |spec|
48
- # spec.metadata = {
49
- # 'rubygems_mfa_required' => 'true'
50
- # }
51
- # end
46
+ # # good
47
+ # Gem::Specification.new do |spec|
48
+ # spec.metadata = {
49
+ # 'rubygems_mfa_required' => 'true'
50
+ # }
51
+ # end
52
52
  #
53
- # # bad
54
- # Gem::Specification.new do |spec|
55
- # spec.metadata['rubygems_mfa_required'] = 'false'
56
- # end
53
+ # # bad
54
+ # Gem::Specification.new do |spec|
55
+ # spec.metadata['rubygems_mfa_required'] = 'false'
56
+ # end
57
57
  #
58
- # # good
59
- # Gem::Specification.new do |spec|
60
- # spec.metadata['rubygems_mfa_required'] = 'true'
61
- # end
58
+ # # good
59
+ # Gem::Specification.new do |spec|
60
+ # spec.metadata['rubygems_mfa_required'] = 'true'
61
+ # end
62
62
  #
63
63
  class RequireMFA < Base
64
64
  include GemspecHelp
@@ -62,6 +62,10 @@ module RuboCop
62
62
  # For example
63
63
  MSG = 'Use `#good_method` instead of `#bad_method`.'
64
64
 
65
+ # TODO: Don't call `on_send` unless the method name is in this list
66
+ # If you don't need `on_send` in the cop you created, remove it.
67
+ RESTRICT_ON_SEND = %%i[bad_method].freeze
68
+
65
69
  # @!method bad_method?(node)
66
70
  def_node_matcher :bad_method?, <<~PATTERN
67
71
  (send nil? :bad_method ...)
@@ -162,7 +166,7 @@ module RuboCop
162
166
  end
163
167
 
164
168
  dir = File.dirname(path)
165
- FileUtils.mkdir_p(dir) unless File.exist?(dir)
169
+ FileUtils.mkdir_p(dir)
166
170
 
167
171
  File.write(path, contents)
168
172
  output.puts "[create] #{path}"
@@ -99,11 +99,7 @@ module RuboCop
99
99
  # If the pattern matcher uses arguments (`%1`, `%2`, etc.), include them in the directive
100
100
  arguments = pattern_arguments(node.arguments[1].source)
101
101
 
102
- range = range_with_surrounding_space(
103
- range: node.loc.expression,
104
- side: :left,
105
- newlines: false
106
- )
102
+ range = range_with_surrounding_space(node.loc.expression, side: :left, newlines: false)
107
103
  indentation = range.source.match(/^\s*/)[0]
108
104
  directive = "#{indentation}# @!method #{actual_name}(#{arguments.join(', ')})\n"
109
105
  directive = "\n#{directive}" if add_newline?(node)
@@ -43,7 +43,7 @@ module RuboCop
43
43
  private
44
44
 
45
45
  def offending_range(node)
46
- with_space = range_with_surrounding_space(range: node.loc.expression)
46
+ with_space = range_with_surrounding_space(node.loc.expression)
47
47
 
48
48
  range_with_surrounding_comma(with_space, :left)
49
49
  end
@@ -56,7 +56,7 @@ module RuboCop
56
56
  private
57
57
 
58
58
  def offending_range(node)
59
- with_space = range_with_surrounding_space(range: node.loc.expression)
59
+ with_space = range_with_surrounding_space(node.loc.expression)
60
60
 
61
61
  range_with_surrounding_comma(with_space, :left)
62
62
  end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module InternalAffairs
6
+ # Check for useless `RESTRICT_ON_SEND`.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # class FooCop
11
+ # RESTRICT_ON_SEND = %i[bad_method].freeze
12
+ # end
13
+ #
14
+ # # good
15
+ # class FooCop
16
+ # RESTRICT_ON_SEND = %i[bad_method].freeze
17
+ # def on_send(node)
18
+ # # ...
19
+ # end
20
+ # end
21
+ #
22
+ # # good
23
+ # class FooCop
24
+ # RESTRICT_ON_SEND = %i[bad_method].freeze
25
+ # def after_send(node)
26
+ # # ...
27
+ # end
28
+ # end
29
+ #
30
+ class UselessRestrictOnSend < Base
31
+ extend AutoCorrector
32
+
33
+ MSG = 'Useless `RESTRICT_ON_SEND` is defined.'
34
+
35
+ # @!method defined_send_callback?(node)
36
+ def_node_search :defined_send_callback?, '(def {:on_send :after_send} ...)'
37
+
38
+ def on_casgn(node)
39
+ return if !restrict_on_send?(node) || defined_send_callback?(node.parent)
40
+
41
+ add_offense(node) do |corrector|
42
+ corrector.remove(node)
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def restrict_on_send?(node)
49
+ node.name == :RESTRICT_ON_SEND
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -20,3 +20,4 @@ require_relative 'internal_affairs/redundant_method_dispatch_node'
20
20
  require_relative 'internal_affairs/style_detected_api_use'
21
21
  require_relative 'internal_affairs/undefined_config'
22
22
  require_relative 'internal_affairs/useless_message_assertion'
23
+ require_relative 'internal_affairs/useless_restrict_on_send'
@@ -97,7 +97,7 @@ module RuboCop
97
97
  def autocorrect(corrector, node)
98
98
  previous_token = previous_token(node)
99
99
  range = if previous_token && same_line?(node, previous_token)
100
- range_with_surrounding_space(range: node.loc.expression, newlines: false)
100
+ range_with_surrounding_space(node.loc.expression, newlines: false)
101
101
  else
102
102
  range_by_whole_lines(node.loc.expression, include_final_newline: true)
103
103
  end