rubocop 1.51.0 → 1.54.1

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 +1 -1
  3. data/config/default.yml +62 -3
  4. data/lib/rubocop/cli/command/lsp.rb +19 -0
  5. data/lib/rubocop/cli.rb +3 -0
  6. data/lib/rubocop/config_loader_resolver.rb +4 -3
  7. data/lib/rubocop/cop/base.rb +1 -1
  8. data/lib/rubocop/cop/bundler/gem_comment.rb +1 -1
  9. data/lib/rubocop/cop/bundler/gem_version.rb +2 -2
  10. data/lib/rubocop/cop/gemspec/dependency_version.rb +2 -2
  11. data/lib/rubocop/cop/internal_affairs/cop_description.rb +32 -8
  12. data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -1
  13. data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +3 -3
  14. data/lib/rubocop/cop/layout/class_structure.rb +7 -0
  15. data/lib/rubocop/cop/layout/closing_heredoc_indentation.rb +1 -2
  16. data/lib/rubocop/cop/layout/empty_line_between_defs.rb +27 -4
  17. data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +2 -0
  18. data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
  19. data/lib/rubocop/cop/layout/indentation_width.rb +2 -2
  20. data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +2 -0
  21. data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
  22. data/lib/rubocop/cop/layout/space_after_comma.rb +9 -1
  23. data/lib/rubocop/cop/layout/space_around_operators.rb +3 -1
  24. data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
  25. data/lib/rubocop/cop/lint/ambiguous_block_association.rb +2 -1
  26. data/lib/rubocop/cop/lint/debugger.rb +9 -5
  27. data/lib/rubocop/cop/lint/duplicate_hash_key.rb +2 -1
  28. data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +46 -19
  29. data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -2
  30. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +1 -1
  31. data/lib/rubocop/cop/lint/identity_comparison.rb +0 -1
  32. data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +1 -2
  33. data/lib/rubocop/cop/lint/inherit_exception.rb +9 -0
  34. data/lib/rubocop/cop/lint/missing_super.rb +34 -5
  35. data/lib/rubocop/cop/lint/mixed_case_range.rb +111 -0
  36. data/lib/rubocop/cop/lint/number_conversion.rb +5 -0
  37. data/lib/rubocop/cop/lint/ordered_magic_comments.rb +0 -1
  38. data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +130 -0
  39. data/lib/rubocop/cop/lint/redundant_require_statement.rb +8 -3
  40. data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -2
  41. data/lib/rubocop/cop/lint/shadowed_exception.rb +5 -11
  42. data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
  43. data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
  44. data/lib/rubocop/cop/lint/useless_assignment.rb +4 -1
  45. data/lib/rubocop/cop/lint/void.rb +12 -18
  46. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -2
  47. data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +30 -2
  48. data/lib/rubocop/cop/migration/department_name.rb +2 -2
  49. data/lib/rubocop/cop/mixin/allowed_receivers.rb +34 -0
  50. data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
  51. data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
  52. data/lib/rubocop/cop/mixin/heredoc.rb +6 -2
  53. data/lib/rubocop/cop/mixin/percent_literal.rb +1 -1
  54. data/lib/rubocop/cop/naming/block_forwarding.rb +1 -1
  55. data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +3 -3
  56. data/lib/rubocop/cop/naming/variable_name.rb +6 -1
  57. data/lib/rubocop/cop/style/accessor_grouping.rb +5 -1
  58. data/lib/rubocop/cop/style/begin_block.rb +1 -2
  59. data/lib/rubocop/cop/style/block_comments.rb +1 -1
  60. data/lib/rubocop/cop/style/block_delimiters.rb +3 -3
  61. data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
  62. data/lib/rubocop/cop/style/class_equality_comparison.rb +17 -39
  63. data/lib/rubocop/cop/style/collection_compact.rb +6 -0
  64. data/lib/rubocop/cop/style/conditional_assignment.rb +3 -1
  65. data/lib/rubocop/cop/style/dir.rb +1 -1
  66. data/lib/rubocop/cop/style/dir_empty.rb +8 -14
  67. data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
  68. data/lib/rubocop/cop/style/eval_with_location.rb +4 -4
  69. data/lib/rubocop/cop/style/exact_regexp_match.rb +8 -2
  70. data/lib/rubocop/cop/style/file_read.rb +2 -2
  71. data/lib/rubocop/cop/style/hash_each_methods.rb +1 -22
  72. data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
  73. data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
  74. data/lib/rubocop/cop/style/identical_conditional_branches.rb +6 -2
  75. data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -2
  76. data/lib/rubocop/cop/style/invertible_unless_condition.rb +1 -1
  77. data/lib/rubocop/cop/style/lambda.rb +3 -3
  78. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +3 -4
  79. data/lib/rubocop/cop/style/multiple_comparison.rb +14 -0
  80. data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
  81. data/lib/rubocop/cop/style/preferred_hash_methods.rb +1 -1
  82. data/lib/rubocop/cop/style/redundant_array_constructor.rb +77 -0
  83. data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
  84. data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
  85. data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +38 -0
  86. data/lib/rubocop/cop/style/redundant_filter_chain.rb +101 -0
  87. data/lib/rubocop/cop/style/redundant_line_continuation.rb +2 -2
  88. data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
  89. data/lib/rubocop/cop/style/redundant_regexp_argument.rb +100 -0
  90. data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +46 -0
  91. data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
  92. data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +3 -1
  93. data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
  94. data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -0
  95. data/lib/rubocop/cop/style/require_order.rb +2 -1
  96. data/lib/rubocop/cop/style/rescue_modifier.rb +1 -3
  97. data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +81 -0
  98. data/lib/rubocop/cop/style/select_by_regexp.rb +15 -5
  99. data/lib/rubocop/cop/style/signal_exception.rb +1 -1
  100. data/lib/rubocop/cop/style/single_line_methods.rb +1 -1
  101. data/lib/rubocop/cop/style/sole_nested_conditional.rb +3 -1
  102. data/lib/rubocop/cop/style/special_global_vars.rb +1 -2
  103. data/lib/rubocop/cop/style/yaml_file_read.rb +66 -0
  104. data/lib/rubocop/cop/style/yoda_condition.rb +4 -2
  105. data/lib/rubocop/cop/util.rb +1 -1
  106. data/lib/rubocop/cop/utils/regexp_ranges.rb +100 -0
  107. data/lib/rubocop/cop/variable_force/assignment.rb +43 -4
  108. data/lib/rubocop/cop/variable_force.rb +1 -0
  109. data/lib/rubocop/cops_documentation_generator.rb +1 -1
  110. data/lib/rubocop/ext/regexp_parser.rb +4 -1
  111. data/lib/rubocop/lsp/logger.rb +22 -0
  112. data/lib/rubocop/lsp/routes.rb +231 -0
  113. data/lib/rubocop/lsp/runtime.rb +82 -0
  114. data/lib/rubocop/lsp/server.rb +66 -0
  115. data/lib/rubocop/lsp/severity.rb +27 -0
  116. data/lib/rubocop/options.rb +11 -1
  117. data/lib/rubocop/server/client_command/exec.rb +2 -1
  118. data/lib/rubocop/version.rb +8 -4
  119. data/lib/rubocop.rb +12 -0
  120. metadata +36 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0863dd1792c6296e51bcdbbc4503423cbc61f66c40a7120b405f4eaf999349be'
4
- data.tar.gz: a4998dfeef1e7f82ffac34fb409494859b28afba9ce6bb007f6ca5870a5e371b
3
+ metadata.gz: c223b2bb07266543894d986f949feb9b6b0470bb43e511e82c8cdf12e9d48872
4
+ data.tar.gz: a785917f1251d60ebe36a2c41680e62a4b789ff40977b1b81a07e09c562c233c
5
5
  SHA512:
6
- metadata.gz: 1ff6067cf16502e16e5bf6691717f97b5f0cf34bdd03e0d893ee90185af1a26d5622c394b5b586530a9d596f8f92cdff4b5eb351d305fc8a2206906f903c9606
7
- data.tar.gz: 2ab3c9a30731adbd8818bfce7fba9f346ac8f66a3b0a52fe2ee65094a7ed580f617fa776664f59df71d6229cf35e3e9ad74a557ec6a760dcbc6d85ae1008a2bb
6
+ metadata.gz: 56c5e545a2440c9994ea57547601d322f940f5dc3215d2e76fc6ef1b59b2bf0a9b73f49c370e9f6eebe1e449bcc71f0e018d8a3d6f70a40a6b585b9034d28aa0
7
+ data.tar.gz: 4c38b1ab3e73fc9cd1579f72331240f3eeebaad1f10ce126fe85f7f57bdba283476310104244b883d90d86c6b78fe1ad2b9ff17b6ff5eb8cc64cf0c3e341dc55
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.51', require: false
56
+ gem 'rubocop', '~> 1.54', 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
@@ -30,6 +30,7 @@ AllCops:
30
30
  - '**/*.rbx'
31
31
  - '**/*.ru'
32
32
  - '**/*.ruby'
33
+ - '**/*.schema'
33
34
  - '**/*.spec'
34
35
  - '**/*.thor'
35
36
  - '**/*.watchr'
@@ -55,6 +56,7 @@ AllCops:
55
56
  - '**/Puppetfile'
56
57
  - '**/Rakefile'
57
58
  - '**/rakefile'
59
+ - '**/Schemafile'
58
60
  - '**/Snapfile'
59
61
  - '**/Steepfile'
60
62
  - '**/Thorfile'
@@ -467,7 +469,9 @@ Layout/ClassStructure:
467
469
  Description: 'Enforces a configured order of definitions within a class body.'
468
470
  StyleGuide: '#consistent-classes'
469
471
  Enabled: false
472
+ SafeAutoCorrect: false
470
473
  VersionAdded: '0.52'
474
+ VersionChanged: '1.53'
471
475
  Categories:
472
476
  module_inclusion:
473
477
  - include
@@ -575,6 +579,8 @@ Layout/EmptyLineBetweenDefs:
575
579
  EmptyLineBetweenMethodDefs: true
576
580
  EmptyLineBetweenClassDefs: true
577
581
  EmptyLineBetweenModuleDefs: true
582
+ # `DefLikeMacros` takes the name of any macro that you want to treat like a def.
583
+ DefLikeMacros: []
578
584
  # `AllowAdjacentOneLineDefs` means that single line method definitions don't
579
585
  # need an empty line between them. `true` by default.
580
586
  AllowAdjacentOneLineDefs: true
@@ -1530,7 +1536,6 @@ Lint/AmbiguousBlockAssociation:
1530
1536
  Description: >-
1531
1537
  Checks for ambiguous block association with method when param passed without
1532
1538
  parentheses.
1533
- StyleGuide: '#syntax'
1534
1539
  Enabled: true
1535
1540
  VersionAdded: '0.48'
1536
1541
  VersionChanged: '1.13'
@@ -1988,9 +1993,16 @@ Lint/MissingSuper:
1988
1993
  Checks for the presence of constructors and lifecycle callbacks
1989
1994
  without calls to `super`.
1990
1995
  Enabled: true
1996
+ AllowedParentClasses: []
1991
1997
  VersionAdded: '0.89'
1992
1998
  VersionChanged: '1.4'
1993
1999
 
2000
+ Lint/MixedCaseRange:
2001
+ Description: 'Checks for mixed-case character ranges since they include likely unintended characters.'
2002
+ Enabled: pending
2003
+ SafeAutoCorrect: false
2004
+ VersionAdded: '1.53'
2005
+
1994
2006
  Lint/MixedRegexpCaptureTypes:
1995
2007
  Description: 'Do not mix named captures and numbered captures in a Regexp literal.'
1996
2008
  Enabled: true
@@ -2140,6 +2152,11 @@ Lint/RedundantDirGlobSort:
2140
2152
  VersionChanged: '1.26'
2141
2153
  SafeAutoCorrect: false
2142
2154
 
2155
+ Lint/RedundantRegexpQuantifiers:
2156
+ Description: 'Checks for redundant quantifiers in Regexps.'
2157
+ Enabled: pending
2158
+ VersionAdded: '1.53'
2159
+
2143
2160
  Lint/RedundantRequireStatement:
2144
2161
  Description: 'Checks for unnecessary `require` statement.'
2145
2162
  Enabled: true
@@ -2484,10 +2501,9 @@ Lint/UselessRuby2Keywords:
2484
2501
  Lint/UselessSetterCall:
2485
2502
  Description: 'Checks for useless setter call to a local variable.'
2486
2503
  Enabled: true
2487
- SafeAutoCorrect: false
2504
+ Safe: false
2488
2505
  VersionAdded: '0.13'
2489
2506
  VersionChanged: '1.2'
2490
- Safe: false
2491
2507
 
2492
2508
  Lint/UselessTimes:
2493
2509
  Description: 'Checks for useless `Integer#times` calls.'
@@ -3363,6 +3379,7 @@ Style/CollectionCompact:
3363
3379
  Safe: false
3364
3380
  VersionAdded: '1.2'
3365
3381
  VersionChanged: '1.3'
3382
+ AllowedReceivers: []
3366
3383
 
3367
3384
  # Align with the style guide.
3368
3385
  Style/CollectionMethods:
@@ -4383,6 +4400,7 @@ Style/MultipleComparison:
4383
4400
  VersionAdded: '0.49'
4384
4401
  VersionChanged: '1.1'
4385
4402
  AllowMethodComparison: true
4403
+ ComparisonsThreshold: 2
4386
4404
 
4387
4405
  Style/MutableConstant:
4388
4406
  Description: 'Do not assign mutable objects to constants.'
@@ -4805,6 +4823,11 @@ Style/RedundantArgument:
4805
4823
  # String#chomp!
4806
4824
  chomp!: "\n"
4807
4825
 
4826
+ Style/RedundantArrayConstructor:
4827
+ Description: 'Checks for the instantiation of array using redundant `Array` constructor.'
4828
+ Enabled: pending
4829
+ VersionAdded: '1.52'
4830
+
4808
4831
  Style/RedundantAssignment:
4809
4832
  Description: 'Checks for redundant assignment before returning.'
4810
4833
  Enabled: true
@@ -4837,6 +4860,11 @@ Style/RedundantConstantBase:
4837
4860
  Enabled: pending
4838
4861
  VersionAdded: '1.40'
4839
4862
 
4863
+ Style/RedundantCurrentDirectoryInPath:
4864
+ Description: 'Checks for uses a redundant current directory in path.'
4865
+ Enabled: pending
4866
+ VersionAdded: '1.53'
4867
+
4840
4868
  Style/RedundantDoubleSplatHashBraces:
4841
4869
  Description: 'Checks for redundant uses of double splat hash braces.'
4842
4870
  Enabled: pending
@@ -4876,6 +4904,13 @@ Style/RedundantFileExtensionInRequire:
4876
4904
  Enabled: true
4877
4905
  VersionAdded: '0.88'
4878
4906
 
4907
+ Style/RedundantFilterChain:
4908
+ Description: >-
4909
+ Identifies usages of `any?`, `empty?`, `none?` or `one?` predicate methods chained to
4910
+ `select`/`filter`/`find_all` and change them to use predicate method instead.
4911
+ Enabled: pending
4912
+ VersionAdded: '1.52'
4913
+
4879
4914
  Style/RedundantFreeze:
4880
4915
  Description: "Checks usages of Object#freeze on immutable objects."
4881
4916
  Enabled: true
@@ -4918,11 +4953,21 @@ Style/RedundantPercentQ:
4918
4953
  Enabled: true
4919
4954
  VersionAdded: '0.76'
4920
4955
 
4956
+ Style/RedundantRegexpArgument:
4957
+ Description: 'Identifies places where argument can be replaced from a deterministic regexp to a string.'
4958
+ Enabled: pending
4959
+ VersionAdded: '1.53'
4960
+
4921
4961
  Style/RedundantRegexpCharacterClass:
4922
4962
  Description: 'Checks for unnecessary single-element Regexp character classes.'
4923
4963
  Enabled: true
4924
4964
  VersionAdded: '0.85'
4925
4965
 
4966
+ Style/RedundantRegexpConstructor:
4967
+ Description: 'Checks for the instantiation of regexp using redundant `Regexp.new` or `Regexp.compile`.'
4968
+ Enabled: pending
4969
+ VersionAdded: '1.52'
4970
+
4926
4971
  Style/RedundantRegexpEscape:
4927
4972
  Description: 'Checks for redundant escapes in Regexps.'
4928
4973
  Enabled: true
@@ -5025,6 +5070,15 @@ Style/ReturnNil:
5025
5070
  - return_nil
5026
5071
  VersionAdded: '0.50'
5027
5072
 
5073
+ Style/ReturnNilInPredicateMethodDefinition:
5074
+ Description: 'Checks if uses of `return` or `return nil` in predicate method definition.'
5075
+ StyleGuide: '#bool-methods-qmark'
5076
+ Enabled: pending
5077
+ SafeAutoCorrect: false
5078
+ AllowedMethods: []
5079
+ AllowedPatterns: []
5080
+ VersionAdded: '1.53'
5081
+
5028
5082
  Style/SafeNavigation:
5029
5083
  Description: >-
5030
5084
  Transforms usages of a method call safeguarded by
@@ -5509,6 +5563,11 @@ Style/WordArray:
5509
5563
  # The regular expression `WordRegex` decides what is considered a word.
5510
5564
  WordRegex: !ruby/regexp '/\A(?:\p{Word}|\p{Word}-\p{Word}|\n|\t)+\z/'
5511
5565
 
5566
+ Style/YAMLFileRead:
5567
+ Description: 'Checks for the use of `YAML.load`, `YAML.safe_load`, and `YAML.parse` with `File.read` argument.'
5568
+ Enabled: pending
5569
+ VersionAdded: '1.53'
5570
+
5512
5571
  Style/YodaCondition:
5513
5572
  Description: 'Forbid or enforce yoda conditions.'
5514
5573
  Reference: 'https://en.wikipedia.org/wiki/Yoda_conditions'
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../lsp/server'
4
+
5
+ module RuboCop
6
+ class CLI
7
+ module Command
8
+ # Start Language Server Protocol of RuboCop.
9
+ # @api private
10
+ class Lsp < Base
11
+ self.command_name = :lsp
12
+
13
+ def run
14
+ RuboCop::Lsp::Server.new(@config_store).start
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/rubocop/cli.rb CHANGED
@@ -174,14 +174,17 @@ module RuboCop
174
174
  ConfigLoader.ignore_unrecognized_cops = @options[:ignore_unrecognized_cops]
175
175
  end
176
176
 
177
+ # rubocop:disable Metrics/CyclomaticComplexity
177
178
  def handle_exiting_options
178
179
  return unless Options::EXITING_OPTIONS.any? { |o| @options.key? o }
179
180
 
180
181
  run_command(:version) if @options[:version] || @options[:verbose_version]
181
182
  run_command(:show_cops) if @options[:show_cops]
182
183
  run_command(:show_docs_url) if @options[:show_docs_url]
184
+ run_command(:lsp) if @options[:lsp]
183
185
  raise Finished
184
186
  end
187
+ # rubocop:enable Metrics/CyclomaticComplexity
185
188
 
186
189
  def apply_default_formatter
187
190
  # This must be done after the options have already been processed,
@@ -33,7 +33,7 @@ module RuboCop
33
33
  inherit_mode: determine_inherit_mode(hash, k))
34
34
  end
35
35
  hash[k] = v
36
- fix_include_paths(base_config.loaded_path, hash, k, v) if v.key?('Include')
36
+ fix_include_paths(base_config.loaded_path, hash, path, k, v) if v.key?('Include')
37
37
  end
38
38
  end
39
39
  end
@@ -42,12 +42,13 @@ module RuboCop
42
42
  # base configuration are relative to the directory where the base configuration file is. For the
43
43
  # derived configuration, we need to make those paths relative to where the derived configuration
44
44
  # file is.
45
- def fix_include_paths(base_config_path, hash, key, value)
45
+ def fix_include_paths(base_config_path, hash, path, key, value)
46
46
  return unless File.basename(base_config_path).start_with?('.rubocop')
47
47
 
48
48
  base_dir = File.dirname(base_config_path)
49
+ derived_dir = File.dirname(path)
49
50
  hash[key]['Include'] = value['Include'].map do |include_path|
50
- PathUtil.relative_path(File.join(base_dir, include_path), Dir.pwd)
51
+ PathUtil.relative_path(File.join(base_dir, include_path), derived_dir)
51
52
  end
52
53
  end
53
54
 
@@ -284,7 +284,7 @@ module RuboCop
284
284
  # @api private
285
285
  def self.callbacks_needed
286
286
  @callbacks_needed ||= public_instance_methods.select do |m|
287
- m.match?(/^on_|^after_/) &&
287
+ m.start_with?(/on_|after_/) &&
288
288
  !Base.method_defined?(m) # exclude standard "callbacks" like 'on_begin_investigation'
289
289
  end
290
290
  end
@@ -150,7 +150,7 @@ module RuboCop
150
150
  # Version specifications that restrict all updates going forward. This excludes versions
151
151
  # like ">= 1.0" or "!= 2.0.3".
152
152
  def restrictive_version_specified_gem?(node)
153
- return unless version_specified_gem?(node)
153
+ return false unless version_specified_gem?(node)
154
154
 
155
155
  node.arguments[1..]
156
156
  .any? { |arg| arg&.str_type? && RESTRICTIVE_VERSION_PATTERN.match?(arg.value) }
@@ -105,13 +105,13 @@ module RuboCop
105
105
  end
106
106
 
107
107
  def required_offense?(node)
108
- return unless required_style?
108
+ return false unless required_style?
109
109
 
110
110
  !includes_version_specification?(node) && !includes_commit_reference?(node)
111
111
  end
112
112
 
113
113
  def forbidden_offense?(node)
114
- return unless forbidden_style?
114
+ return false unless forbidden_style?
115
115
 
116
116
  includes_version_specification?(node) || includes_commit_reference?(node)
117
117
  end
@@ -126,13 +126,13 @@ module RuboCop
126
126
  end
127
127
 
128
128
  def required_offense?(node)
129
- return unless required_style?
129
+ return false unless required_style?
130
130
 
131
131
  !includes_version_specification?(node) && !includes_commit_reference?(node)
132
132
  end
133
133
 
134
134
  def forbidden_offense?(node)
135
- return unless forbidden_style?
135
+ return false unless forbidden_style?
136
136
 
137
137
  includes_version_specification?(node) || includes_commit_reference?(node)
138
138
  end
@@ -12,6 +12,13 @@ module RuboCop
12
12
  # ....
13
13
  # end
14
14
  #
15
+ # # bad
16
+ # #
17
+ # # Checks ...
18
+ # class SomeCop < Base
19
+ # ...
20
+ # end
21
+ #
15
22
  # # good
16
23
  # # Checks ...
17
24
  # class SomeCop < Base
@@ -21,27 +28,47 @@ module RuboCop
21
28
  class CopDescription < Base
22
29
  extend AutoCorrector
23
30
 
24
- MSG = 'Description should be started with %<suggestion>s instead of `This cop ...`.'
31
+ MSG_STARTS_WITH_WRONG_WORD =
32
+ 'Description should be started with %<suggestion>s instead of `This cop ...`.'
33
+ MSG_STARTS_WITH_EMPTY_COMMENT_LINE =
34
+ 'Description should not start with an empty comment line.'
25
35
 
26
36
  SPECIAL_WORDS = %w[is can could should will would must may].freeze
27
37
  COP_DESC_OFFENSE_REGEX =
28
38
  /^\s+# This cop (?<special>#{SPECIAL_WORDS.join('|')})?\s*(?<word>.+?) .*/.freeze
29
39
  REPLACEMENT_REGEX = /^\s+# This cop (#{SPECIAL_WORDS.join('|')})?\s*(.+?) /.freeze
40
+ EMPTY_COMMENT_LINE_REGEX = /\A\s*#\s*\n\z/.freeze
30
41
 
31
- # rubocop:disable Metrics/CyclomaticComplexity
32
42
  def on_class(node)
33
43
  return unless (module_node = node.parent) && node.parent_class
34
44
 
35
45
  description_beginning = first_comment_line(module_node)
36
46
  return unless description_beginning
37
47
 
38
- start_with_subject = description_beginning.match(COP_DESC_OFFENSE_REGEX)
39
- return unless start_with_subject
48
+ if description_beginning.match?(EMPTY_COMMENT_LINE_REGEX)
49
+ register_offense_for_empty_comment_line(module_node, description_beginning)
50
+ else
51
+ start_with_subject = description_beginning.match(COP_DESC_OFFENSE_REGEX)
52
+ return unless start_with_subject
53
+
54
+ register_offense_for_wrong_word(module_node, description_beginning, start_with_subject)
55
+ end
56
+ end
57
+
58
+ private
40
59
 
60
+ def register_offense_for_empty_comment_line(module_node, description_beginning)
61
+ range = range(module_node, description_beginning)
62
+ add_offense(range, message: MSG_STARTS_WITH_EMPTY_COMMENT_LINE) do |corrector|
63
+ corrector.remove(range)
64
+ end
65
+ end
66
+
67
+ def register_offense_for_wrong_word(module_node, description_beginning, start_with_subject)
41
68
  suggestion = start_with_subject['word']&.capitalize
42
69
  range = range(module_node, description_beginning)
43
70
  suggestion_for_message = suggestion_for_message(suggestion, start_with_subject)
44
- message = format(MSG, suggestion: suggestion_for_message)
71
+ message = format(MSG_STARTS_WITH_WRONG_WORD, suggestion: suggestion_for_message)
45
72
 
46
73
  add_offense(range, message: message) do |corrector|
47
74
  if suggestion && !start_with_subject['special']
@@ -49,9 +76,6 @@ module RuboCop
49
76
  end
50
77
  end
51
78
  end
52
- # rubocop:enable Metrics/CyclomaticComplexity
53
-
54
- private
55
79
 
56
80
  def replace_with_suggestion(corrector, range, suggestion, description_beginning)
57
81
  replacement = description_beginning.gsub(REPLACEMENT_REGEX, "#{suggestion} ")
@@ -51,7 +51,9 @@ module RuboCop
51
51
 
52
52
  def extract_receiver(node)
53
53
  receiver = node.receiver
54
- receiver = receiver.receiver if receiver.method?(:loc) || receiver.method?(:source_range)
54
+ if receiver.send_type? && (receiver.method?(:loc) || receiver.method?(:source_range))
55
+ receiver = receiver.receiver
56
+ end
55
57
  receiver.source
56
58
  end
57
59
  end
@@ -117,11 +117,11 @@ module RuboCop
117
117
  def add_newline?(node)
118
118
  # Determine if a blank line should be inserted before the new directive
119
119
  # in order to spread out pattern matchers
120
- return if node.sibling_index&.zero?
121
- return unless node.parent
120
+ return false if node.sibling_index&.zero?
121
+ return false unless node.parent
122
122
 
123
123
  prev_sibling = node.parent.child_nodes[node.sibling_index - 1]
124
- return unless prev_sibling && pattern_matcher?(prev_sibling)
124
+ return false unless prev_sibling && pattern_matcher?(prev_sibling)
125
125
 
126
126
  node.loc.line == last_line(prev_sibling) + 1
127
127
  end
@@ -68,6 +68,13 @@ module RuboCop
68
68
  # - extend
69
69
  # ----
70
70
  #
71
+ # @safety
72
+ # Autocorrection is unsafe because class methods and module inclusion
73
+ # can behave differently, based on which methods or constants have
74
+ # already been defined.
75
+ #
76
+ # Constants will only be moved when they are assigned with literals.
77
+ #
71
78
  # @example
72
79
  # # bad
73
80
  # # Expect extend be before constant
@@ -3,7 +3,6 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Layout
6
- #
7
6
  # Checks the indentation of here document closings.
8
7
  #
9
8
  # @example
@@ -73,7 +72,7 @@ module RuboCop
73
72
  end
74
73
 
75
74
  def argument_indentation_correct?(node)
76
- return unless node.argument? || node.chained?
75
+ return false unless node.argument? || node.chained?
77
76
 
78
77
  opening_indentation(
79
78
  find_node_used_heredoc_argument(node.parent)
@@ -135,7 +135,8 @@ module RuboCop
135
135
  return if nodes.all?(&:single_line?) && cop_config['AllowAdjacentOneLineDefs']
136
136
 
137
137
  correction_node = nodes.last
138
- location = correction_node.loc.keyword.join(correction_node.loc.name)
138
+
139
+ location = def_location(correction_node)
139
140
  add_offense(location, message: message(correction_node, count: count)) do |corrector|
140
141
  autocorrect(corrector, *nodes, count)
141
142
  end
@@ -159,10 +160,28 @@ module RuboCop
159
160
 
160
161
  private
161
162
 
163
+ def def_location(correction_node)
164
+ if correction_node.block_type?
165
+ correction_node.source_range.join(correction_node.children.first.source_range)
166
+ else
167
+ correction_node.loc.keyword.join(correction_node.loc.name)
168
+ end
169
+ end
170
+
162
171
  def candidate?(node)
163
- return unless node
172
+ return false unless node
164
173
 
165
- method_candidate?(node) || class_candidate?(node) || module_candidate?(node)
174
+ method_candidate?(node) || class_candidate?(node) || module_candidate?(node) ||
175
+ macro_candidate?(node)
176
+ end
177
+
178
+ def empty_line_between_macros
179
+ cop_config.fetch('DefLikeMacros', []).map(&:to_sym)
180
+ end
181
+
182
+ def macro_candidate?(node)
183
+ node.block_type? && node.children.first.macro? &&
184
+ empty_line_between_macros.include?(node.children.first.method_name)
166
185
  end
167
186
 
168
187
  def method_candidate?(node)
@@ -226,7 +245,11 @@ module RuboCop
226
245
  end
227
246
 
228
247
  def def_start(node)
229
- node.loc.keyword.line
248
+ if node.block_type? && node.children.first.send_type?
249
+ node.source_range.line
250
+ else
251
+ node.loc.keyword.line
252
+ end
230
253
  end
231
254
 
232
255
  def def_end(node)
@@ -68,6 +68,8 @@ module RuboCop
68
68
  check_body(node.body, node.loc.line)
69
69
  end
70
70
  alias on_defs on_def
71
+ alias on_block on_def
72
+ alias on_numblock on_def
71
73
 
72
74
  def on_kwbegin(node)
73
75
  body, = *node
@@ -76,7 +76,7 @@ module RuboCop
76
76
 
77
77
  def autocorrect_lambda_for_tabs(corrector, range)
78
78
  spaces = ' ' * configured_indentation_width
79
- corrector.replace(range, range.source.gsub(/\t/, spaces))
79
+ corrector.replace(range, range.source.gsub("\t", spaces))
80
80
  end
81
81
 
82
82
  def autocorrect_lambda_for_spaces(corrector, range)
@@ -366,10 +366,10 @@ module RuboCop
366
366
  end
367
367
 
368
368
  def starts_with_access_modifier?(body_node)
369
- return unless body_node.begin_type?
369
+ return false unless body_node.begin_type?
370
370
 
371
371
  starting_node = body_node.children.first
372
- return unless starting_node
372
+ return false unless starting_node
373
373
 
374
374
  starting_node.send_type? && starting_node.bare_access_modifier?
375
375
  end
@@ -84,6 +84,8 @@ module RuboCop
84
84
  return unless strings_concatenated_with_backslash?(node)
85
85
 
86
86
  children = node.children
87
+ return if children.empty?
88
+
87
89
  if style == :aligned && !always_indented?(node)
88
90
  check_aligned(children, 1)
89
91
  else
@@ -99,7 +99,7 @@ module RuboCop
99
99
  def suitable_as_single_line?(node)
100
100
  !comment_within?(node) &&
101
101
  node.each_descendant(:if, :case, :kwbegin, :def).none? &&
102
- node.each_descendant(:dstr, :str).none?(&:heredoc?) &&
102
+ node.each_descendant(:dstr, :str).none? { |n| n.heredoc? || n.value.include?("\n") } &&
103
103
  node.each_descendant(:begin).none? { |b| !b.single_line? }
104
104
  end
105
105
 
@@ -24,7 +24,15 @@ module RuboCop
24
24
  end
25
25
 
26
26
  def kind(token)
27
- 'comma' if token.comma?
27
+ 'comma' if token.comma? && !before_semicolon?(token)
28
+ end
29
+
30
+ private
31
+
32
+ def before_semicolon?(token)
33
+ tokens = processed_source.tokens
34
+
35
+ tokens[tokens.index(token) + 1].semicolon?
28
36
  end
29
37
  end
30
38
  end
@@ -153,7 +153,9 @@ module RuboCop
153
153
  private
154
154
 
155
155
  def regular_operator?(send_node)
156
- !send_node.unary_operation? && !send_node.dot? && operator_with_regular_syntax?(send_node)
156
+ return false if send_node.unary_operation? || send_node.dot? || send_node.double_colon?
157
+
158
+ operator_with_regular_syntax?(send_node)
157
159
  end
158
160
 
159
161
  def operator_with_regular_syntax?(send_node)
@@ -35,7 +35,7 @@ module RuboCop
35
35
  def check(node)
36
36
  expression = node.source
37
37
  op = node.loc.operator.source
38
- escaped_op = op.gsub(/\./, '\.')
38
+ escaped_op = op.gsub('.', '\.')
39
39
 
40
40
  # account for multiline range literals
41
41
  expression.sub!(/#{escaped_op}\n\s*/, op)
@@ -97,7 +97,8 @@ module RuboCop
97
97
  def wrap_in_parentheses(corrector, node)
98
98
  range = node.loc.selector.end.join(node.first_argument.source_range.begin)
99
99
 
100
- corrector.replace(range, '(')
100
+ corrector.remove(range)
101
+ corrector.insert_before(range, '(')
101
102
  corrector.insert_after(node.last_argument, ')')
102
103
  end
103
104
  end
@@ -68,10 +68,7 @@ module RuboCop
68
68
  MSG = 'Remove debugger entry point `%<source>s`.'
69
69
 
70
70
  def on_send(node)
71
- return unless debugger_method?(node)
72
-
73
- # Basically, debugger methods are not used as a method argument without arguments.
74
- return if node.arguments.empty? && node.each_ancestor(:send, :csend).any?
71
+ return if !debugger_method?(node) || assumed_usage_context?(node)
75
72
 
76
73
  add_offense(node)
77
74
  end
@@ -90,11 +87,18 @@ module RuboCop
90
87
  end
91
88
 
92
89
  def debugger_method?(send_node)
93
- return if send_node.parent&.send_type? && send_node.parent.receiver == send_node
90
+ return false if send_node.parent&.send_type? && send_node.parent.receiver == send_node
94
91
 
95
92
  debugger_methods.include?(chained_method_name(send_node))
96
93
  end
97
94
 
95
+ def assumed_usage_context?(node)
96
+ # Basically, debugger methods are not used as a method argument without arguments.
97
+ return false unless node.arguments.empty? && node.each_ancestor(:send, :csend).any?
98
+
99
+ node.each_ancestor.none?(&:lambda_or_proc?)
100
+ end
101
+
98
102
  def chained_method_name(send_node)
99
103
  chained_method_name = send_node.method_name.to_s
100
104
  receiver = send_node.receiver