rubocop 1.30.1 → 1.31.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) 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_validator.rb +3 -3
  11. data/lib/rubocop/cop/base.rb +4 -0
  12. data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
  13. data/lib/rubocop/cop/bundler/gem_filename.rb +4 -4
  14. data/lib/rubocop/cop/bundler/ordered_gems.rb +2 -2
  15. data/lib/rubocop/cop/corrector.rb +2 -2
  16. data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +3 -3
  17. data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +1 -1
  18. data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +31 -16
  19. data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +1 -1
  20. data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
  21. data/lib/rubocop/cop/gemspec/require_mfa.rb +20 -20
  22. data/lib/rubocop/cop/generator.rb +1 -1
  23. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -5
  24. data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -1
  25. data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +1 -1
  26. data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
  27. data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +25 -4
  28. data/lib/rubocop/cop/layout/first_array_element_indentation.rb +17 -11
  29. data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +49 -10
  30. data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
  31. data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +68 -0
  32. data/lib/rubocop/cop/layout/line_continuation_spacing.rb +130 -0
  33. data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
  34. data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +2 -2
  35. data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -2
  36. data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
  37. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  38. data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
  39. data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
  40. data/lib/rubocop/cop/layout/space_inside_block_braces.rb +5 -3
  41. data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
  42. data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +51 -0
  43. data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
  44. data/lib/rubocop/cop/lint/literal_as_condition.rb +5 -0
  45. data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +122 -0
  46. data/lib/rubocop/cop/lint/number_conversion.rb +3 -3
  47. data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
  48. data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +5 -5
  49. data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +1 -1
  50. data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -1
  51. data/lib/rubocop/cop/lint/regexp_as_condition.rb +2 -2
  52. data/lib/rubocop/cop/lint/struct_new_override.rb +2 -2
  53. data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
  54. data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +44 -0
  55. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
  56. data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +41 -12
  57. data/lib/rubocop/cop/mixin/range_help.rb +7 -3
  58. data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
  59. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -1
  60. data/lib/rubocop/cop/style/block_delimiters.rb +4 -2
  61. data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
  62. data/lib/rubocop/cop/style/conditional_assignment.rb +1 -0
  63. data/lib/rubocop/cop/style/empty_method.rb +16 -1
  64. data/lib/rubocop/cop/style/encoding.rb +1 -1
  65. data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
  66. data/lib/rubocop/cop/style/format_string_token.rb +48 -17
  67. data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
  68. data/lib/rubocop/cop/style/guard_clause.rb +8 -6
  69. data/lib/rubocop/cop/style/hash_as_last_array_item.rb +1 -1
  70. data/lib/rubocop/cop/style/hash_except.rb +88 -8
  71. data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
  72. data/lib/rubocop/cop/style/implicit_runtime_error.rb +2 -2
  73. data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -1
  74. data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
  75. data/lib/rubocop/cop/style/map_to_hash.rb +1 -1
  76. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +2 -2
  77. data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -2
  78. data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
  79. data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -3
  80. data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -1
  81. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
  82. data/lib/rubocop/cop/style/multiline_when_then.rb +1 -3
  83. data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
  84. data/lib/rubocop/cop/style/nested_ternary_operator.rb +19 -7
  85. data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
  86. data/lib/rubocop/cop/style/not.rb +1 -1
  87. data/lib/rubocop/cop/style/redundant_argument.rb +1 -1
  88. data/lib/rubocop/cop/style/redundant_return.rb +1 -1
  89. data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
  90. data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
  91. data/lib/rubocop/cop/style/struct_inheritance.rb +2 -2
  92. data/lib/rubocop/cop/style/swap_values.rb +1 -1
  93. data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
  94. data/lib/rubocop/cop/style/unpack_first.rb +1 -1
  95. data/lib/rubocop/cop/util.rb +1 -1
  96. data/lib/rubocop/formatter/formatter_set.rb +20 -19
  97. data/lib/rubocop/formatter/git_hub_actions_formatter.rb +15 -2
  98. data/lib/rubocop/formatter/html_formatter.rb +0 -1
  99. data/lib/rubocop/formatter/offense_count_formatter.rb +2 -0
  100. data/lib/rubocop/formatter/simple_text_formatter.rb +6 -7
  101. data/lib/rubocop/formatter.rb +31 -0
  102. data/lib/rubocop/options.rb +24 -1
  103. data/lib/rubocop/rake_task.rb +30 -9
  104. data/lib/rubocop/server/cache.rb +109 -0
  105. data/lib/rubocop/server/cli.rb +104 -0
  106. data/lib/rubocop/server/client_command/base.rb +44 -0
  107. data/lib/rubocop/server/client_command/exec.rb +59 -0
  108. data/lib/rubocop/server/client_command/restart.rb +25 -0
  109. data/lib/rubocop/server/client_command/start.rb +43 -0
  110. data/lib/rubocop/server/client_command/status.rb +28 -0
  111. data/lib/rubocop/server/client_command/stop.rb +31 -0
  112. data/lib/rubocop/server/client_command.rb +26 -0
  113. data/lib/rubocop/server/core.rb +79 -0
  114. data/lib/rubocop/server/errors.rb +23 -0
  115. data/lib/rubocop/server/helper.rb +34 -0
  116. data/lib/rubocop/server/server_command/base.rb +50 -0
  117. data/lib/rubocop/server/server_command/exec.rb +34 -0
  118. data/lib/rubocop/server/server_command/stop.rb +24 -0
  119. data/lib/rubocop/server/server_command.rb +21 -0
  120. data/lib/rubocop/server/socket_reader.rb +65 -0
  121. data/lib/rubocop/server.rb +53 -0
  122. data/lib/rubocop/version.rb +15 -8
  123. data/lib/rubocop.rb +7 -26
  124. metadata +42 -5
  125. 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: 3e6cb927ae20cdc9cc2258d8f57b483c0fad68f03563e4f4a1823070510c8fac
4
+ data.tar.gz: 68a3e3f99f11ef787da421d67a2674027c425978e494fd7ab6c1ea0a614748df
5
5
  SHA512:
6
- metadata.gz: f8b462b23760d3f4a51c0a94dfa01c0b43c0715255b65d58883fd8b67f6841d5d8384c367d3711d68ba6f8f4d72fac1533fdaa33d817986434c185a5c6574f13
7
- data.tar.gz: 3751cee1ebfd9d5968f6510d1440675286bbcb5a4f20c9bebb1cdd916ac70a07d4fd8f111352c10fa90ec546bfa36dc2cdae413b25bd24afcf62ed54db7c78a3
6
+ metadata.gz: 991b16c032399b14752c8a71ad92d3e2000c57941eafdace759cc5cb4f7a8f18dc58952597575375cb2a7df425ffffd1e2b029dabc9d98834c5142cc38435382
7
+ data.tar.gz: f92f9b6704829816959e3ccd8c61fa439dfd75cbc09a145028de9d8cae2a636946da775b49e35a7b42a78d9af288969e72afaeb1b2fd47235a9e83201cdf4df7
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
 
@@ -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
@@ -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
@@ -162,7 +162,7 @@ module RuboCop
162
162
  end
163
163
 
164
164
  dir = File.dirname(path)
165
- FileUtils.mkdir_p(dir) unless File.exist?(dir)
165
+ FileUtils.mkdir_p(dir)
166
166
 
167
167
  File.write(path, contents)
168
168
  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
@@ -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
@@ -70,18 +70,39 @@ module RuboCop
70
70
  def on_send(node)
71
71
  return unless node.attribute_accessor?
72
72
  return if next_line_empty?(node.last_line)
73
+ return if next_line_empty_or_enable_directive_comment?(node.last_line)
73
74
 
74
75
  next_line_node = next_line_node(node)
75
76
  return unless require_empty_line?(next_line_node)
76
77
 
77
- add_offense(node) do |corrector|
78
- range = range_by_whole_lines(node.source_range)
78
+ add_offense(node) { |corrector| autocorrect(corrector, node) }
79
+ end
80
+
81
+ private
79
82
 
80
- corrector.insert_after(range, "\n")
83
+ def autocorrect(corrector, node)
84
+ node_range = range_by_whole_lines(node.source_range)
85
+
86
+ next_line = node_range.last_line + 1
87
+ if next_line_enable_directive_comment?(next_line)
88
+ node_range = processed_source.comment_at_line(next_line)
81
89
  end
90
+
91
+ corrector.insert_after(node_range, "\n")
82
92
  end
83
93
 
84
- private
94
+ def next_line_empty_or_enable_directive_comment?(line)
95
+ return true if next_line_empty?(line)
96
+
97
+ next_line = line + 1
98
+ next_line_enable_directive_comment?(next_line) && next_line_empty?(next_line)
99
+ end
100
+
101
+ def next_line_enable_directive_comment?(line)
102
+ return false unless (comment = processed_source.comment_at_line(line))
103
+
104
+ DirectiveComment.new(comment).enabled?
105
+ end
85
106
 
86
107
  def next_line_empty?(line)
87
108
  processed_source[line].nil? || processed_source[line].blank?
@@ -127,22 +127,25 @@ module RuboCop
127
127
  # if the right bracket is on the same line as the last value, accept
128
128
  return if /\S/.match?(right_bracket.source_line[0...right_bracket.column])
129
129
 
130
- expected_column = base_column(left_bracket, left_parenthesis)
130
+ expected_column, indent_base_type = indent_base(left_bracket, left_parenthesis)
131
131
  @column_delta = expected_column - right_bracket.column
132
132
  return if @column_delta.zero?
133
133
 
134
- msg = msg(left_parenthesis)
134
+ msg = message_for_right_bracket(indent_base_type)
135
135
  add_offense(right_bracket, message: msg) do |corrector|
136
136
  autocorrect(corrector, right_bracket)
137
137
  end
138
138
  end
139
139
 
140
140
  # Returns the description of what the correct indentation is based on.
141
- def base_description(left_parenthesis)
142
- if style == :align_brackets
141
+ def base_description(indent_base_type)
142
+ case indent_base_type
143
+ when :left_brace_or_bracket
143
144
  'the position of the opening bracket'
144
- elsif left_parenthesis && style == :special_inside_parentheses
145
+ when :first_colmn_after_left_parenthesis
145
146
  'the first position after the preceding left parenthesis'
147
+ when :parent_hash_key
148
+ 'the parent hash key'
146
149
  else
147
150
  'the start of the line where the left square bracket is'
148
151
  end
@@ -156,15 +159,18 @@ module RuboCop
156
159
  )
157
160
  end
158
161
 
159
- def msg(left_parenthesis)
160
- if style == :align_brackets
162
+ def message_for_right_bracket(indent_base_type)
163
+ case indent_base_type
164
+ when :left_brace_or_bracket
161
165
  'Indent the right bracket the same as the left bracket.'
162
- elsif style == :special_inside_parentheses && left_parenthesis
166
+ when :first_colmn_after_left_parenthesis
163
167
  'Indent the right bracket the same as the first position ' \
164
- 'after the preceding left parenthesis.'
168
+ 'after the preceding left parenthesis.'
169
+ when :parent_hash_key
170
+ 'Indent the right bracket the same as the parent hash key.' \
165
171
  else
166
- 'Indent the right bracket the same as the start of the line' \
167
- ' where the left bracket is.'
172
+ 'Indent the right bracket the same as the start of the line ' \
173
+ 'where the left bracket is.'
168
174
  end
169
175
  end
170
176
  end