rubocop 1.43.0 → 1.45.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/default.yml +64 -29
  4. data/lib/rubocop/cli.rb +54 -8
  5. data/lib/rubocop/config_loader.rb +12 -15
  6. data/lib/rubocop/config_loader_resolver.rb +3 -4
  7. data/lib/rubocop/cop/base.rb +27 -9
  8. data/lib/rubocop/cop/commissioner.rb +8 -2
  9. data/lib/rubocop/cop/cop.rb +23 -3
  10. data/lib/rubocop/cop/corrector.rb +10 -2
  11. data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -6
  12. data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
  13. data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +11 -3
  14. data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
  15. data/lib/rubocop/cop/layout/block_end_newline.rb +7 -1
  16. data/lib/rubocop/cop/layout/class_structure.rb +2 -16
  17. data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +2 -6
  18. data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
  19. data/lib/rubocop/cop/layout/heredoc_indentation.rb +6 -9
  20. data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
  21. data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
  22. data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +11 -13
  23. data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +4 -4
  24. data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +5 -4
  25. data/lib/rubocop/cop/lint/ambiguous_operator.rb +4 -0
  26. data/lib/rubocop/cop/lint/debugger.rb +8 -27
  27. data/lib/rubocop/cop/lint/deprecated_class_methods.rb +62 -112
  28. data/lib/rubocop/cop/lint/else_layout.rb +2 -6
  29. data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -7
  30. data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +15 -17
  31. data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
  32. data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
  33. data/lib/rubocop/cop/lint/nested_method_definition.rb +8 -5
  34. data/lib/rubocop/cop/lint/redundant_require_statement.rb +11 -1
  35. data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -4
  36. data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -3
  37. data/lib/rubocop/cop/lint/useless_rescue.rb +15 -1
  38. data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +9 -1
  39. data/lib/rubocop/cop/lint/void.rb +19 -10
  40. data/lib/rubocop/cop/metrics/block_length.rb +1 -1
  41. data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
  42. data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
  43. data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +2 -5
  44. data/lib/rubocop/cop/mixin/alignment.rb +1 -1
  45. data/lib/rubocop/cop/mixin/allowed_methods.rb +3 -1
  46. data/lib/rubocop/cop/mixin/comments_help.rb +5 -3
  47. data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +51 -25
  48. data/lib/rubocop/cop/mixin/line_length_help.rb +3 -1
  49. data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
  50. data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
  51. data/lib/rubocop/cop/naming/block_forwarding.rb +4 -0
  52. data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
  53. data/lib/rubocop/cop/registry.rb +12 -7
  54. data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -11
  55. data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -0
  56. data/lib/rubocop/cop/style/block_delimiters.rb +8 -2
  57. data/lib/rubocop/cop/style/class_and_module_children.rb +3 -10
  58. data/lib/rubocop/cop/style/command_literal.rb +1 -1
  59. data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
  60. data/lib/rubocop/cop/style/conditional_assignment.rb +0 -6
  61. data/lib/rubocop/cop/style/documentation.rb +1 -1
  62. data/lib/rubocop/cop/style/documentation_method.rb +6 -0
  63. data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
  64. data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
  65. data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +11 -5
  66. data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -0
  67. data/lib/rubocop/cop/style/min_max_comparison.rb +11 -1
  68. data/lib/rubocop/cop/style/multiline_if_modifier.rb +0 -4
  69. data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
  70. data/lib/rubocop/cop/style/multiline_ternary_operator.rb +18 -3
  71. data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -5
  72. data/lib/rubocop/cop/style/numbered_parameters_limit.rb +11 -3
  73. data/lib/rubocop/cop/style/one_line_conditional.rb +3 -6
  74. data/lib/rubocop/cop/style/operator_method_call.rb +2 -2
  75. data/lib/rubocop/cop/style/parallel_assignment.rb +3 -1
  76. data/lib/rubocop/cop/style/redundant_condition.rb +16 -1
  77. data/lib/rubocop/cop/style/redundant_conditional.rb +0 -4
  78. data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +16 -10
  79. data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +58 -0
  80. data/lib/rubocop/cop/style/require_order.rb +2 -9
  81. data/lib/rubocop/cop/style/self_assignment.rb +2 -2
  82. data/lib/rubocop/cop/style/semicolon.rb +24 -2
  83. data/lib/rubocop/cop/style/symbol_array.rb +1 -1
  84. data/lib/rubocop/cop/style/word_array.rb +1 -1
  85. data/lib/rubocop/cop/style/yoda_condition.rb +12 -5
  86. data/lib/rubocop/cop/style/yoda_expression.rb +11 -2
  87. data/lib/rubocop/cop/team.rb +19 -14
  88. data/lib/rubocop/cop/variable_force/scope.rb +3 -3
  89. data/lib/rubocop/cop/variable_force/variable_table.rb +3 -1
  90. data/lib/rubocop/cop/variable_force.rb +1 -1
  91. data/lib/rubocop/formatter.rb +0 -1
  92. data/lib/rubocop/options.rb +22 -1
  93. data/lib/rubocop/rspec/expect_offense.rb +6 -4
  94. data/lib/rubocop/runner.rb +40 -4
  95. data/lib/rubocop/server/cache.rb +10 -3
  96. data/lib/rubocop/server/cli.rb +37 -18
  97. data/lib/rubocop/server/client_command/exec.rb +1 -1
  98. data/lib/rubocop/server/client_command/start.rb +6 -1
  99. data/lib/rubocop/server/core.rb +23 -8
  100. data/lib/rubocop/version.rb +1 -1
  101. data/lib/rubocop.rb +4 -0
  102. metadata +11 -27
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: adbae8e6d8a8ad828f302b9d3bba545a8b7fe54727480077a1878d4c2109dbf3
4
- data.tar.gz: fb30d554306879712c48bec3d389b4232ee8f8dde6d410dd139b73796a076d3d
3
+ metadata.gz: 648a8aa3156aebf0c078bc0b600f301d3aa0abaf92d6b3643c343718575f7302
4
+ data.tar.gz: 21b185886d70b70a163c6e7f3ea47e04ee0a0afcf11217e0d7bcb31e25440880
5
5
  SHA512:
6
- metadata.gz: 7aa85cf4d34b4994422b5aa921d746b68bc282c97f7563affa2b2574a9b1102eefce4e10bed58801bb6ef9fb2ee2d3d563a63f5b114d754a69c872f6830a793e
7
- data.tar.gz: 1e088db8ccb3cdcf2ab7b9e624b68bfc4513681e4cc8ee6bd69084dfa91fda775fc28483a4764962829230d6d2fd5726855299ba1ae622bf85cc66f55451c8c9
6
+ metadata.gz: ff5a5e947001ba9507f3e806896559baaf0e4bccc6b2cc56316dffc3ae334f62046040be389518139d2979c4d09900600f9b233bc1f4dbd2d1d37a2f8fe99037
7
+ data.tar.gz: 8f89f55198c9d206c5b8d0c32cbeb5033491046c0ea0f4311f6c1a23595bcce852ec732606ca1fd2bc499b31ecb2265dcf6d85edd352f6400d01bd9385993a44
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.43', require: false
56
+ gem 'rubocop', '~> 1.45', 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
@@ -262,6 +262,21 @@ Gemspec/DeprecatedAttributeAssignment:
262
262
  Include:
263
263
  - '**/*.gemspec'
264
264
 
265
+ Gemspec/DevelopmentDependencies:
266
+ Description: Checks that development dependencies are specified in Gemfile rather than gemspec.
267
+ Enabled: pending
268
+ VersionAdded: '1.44'
269
+ EnforcedStyle: Gemfile
270
+ SupportedStyles:
271
+ - Gemfile
272
+ - gems.rb
273
+ - gemspec
274
+ AllowedGems: []
275
+ Include:
276
+ - '**/*.gemspec'
277
+ - '**/Gemfile'
278
+ - '**/gems.rb'
279
+
265
280
  Gemspec/DuplicatedAssignment:
266
281
  Description: 'An attribute assignment method calls should be listed only once in a gemspec.'
267
282
  Enabled: true
@@ -391,9 +406,8 @@ Layout/AssignmentIndentation:
391
406
  Checks the indentation of the first line of the
392
407
  right-hand-side of a multi-line assignment.
393
408
  Enabled: true
394
- SafeAutoCorrect: false
395
409
  VersionAdded: '0.49'
396
- VersionChanged: '1.40'
410
+ VersionChanged: '1.45'
397
411
  # By default the indentation width from `Layout/IndentationWidth` is used,
398
412
  # but it can be overridden by setting this parameter.
399
413
  IndentationWidth: ~
@@ -964,7 +978,6 @@ Layout/IndentationWidth:
964
978
  # Number of spaces for each indentation level.
965
979
  Width: 2
966
980
  AllowedPatterns: []
967
- IgnoredPatterns: [] # deprecated
968
981
 
969
982
  Layout/InitialIndentation:
970
983
  Description: >-
@@ -992,10 +1005,8 @@ Layout/LineContinuationLeadingSpace:
992
1005
  Use trailing spaces instead of leading spaces in strings
993
1006
  broken over multiple lines (by a backslash).
994
1007
  Enabled: pending
995
- AutoCorrect: false
996
- SafeAutoCorrect: false
997
1008
  VersionAdded: '1.31'
998
- VersionChanged: '1.32'
1009
+ VersionChanged: '1.45'
999
1010
  EnforcedStyle: trailing
1000
1011
  SupportedStyles:
1001
1012
  - leading
@@ -1047,7 +1058,6 @@ Layout/LineLength:
1047
1058
  # elements. Strings will be converted to Regexp objects. A line that matches
1048
1059
  # any regular expression listed in this option will be ignored by LineLength.
1049
1060
  AllowedPatterns: []
1050
- IgnoredPatterns: [] # deprecated
1051
1061
 
1052
1062
  Layout/MultilineArrayBraceLayout:
1053
1063
  Description: >-
@@ -1526,7 +1536,6 @@ Lint/AmbiguousBlockAssociation:
1526
1536
  VersionChanged: '1.13'
1527
1537
  AllowedMethods: []
1528
1538
  AllowedPatterns: []
1529
- IgnoredMethods: [] # deprecated
1530
1539
 
1531
1540
  Lint/AmbiguousOperator:
1532
1541
  Description: >-
@@ -1563,7 +1572,9 @@ Lint/AssignmentInCondition:
1563
1572
  Description: "Don't use assignment in conditions."
1564
1573
  StyleGuide: '#safe-assignment-in-condition'
1565
1574
  Enabled: true
1575
+ SafeAutoCorrect: false
1566
1576
  VersionAdded: '0.9'
1577
+ VersionChanged: '1.45'
1567
1578
  AllowSafeAssignment: true
1568
1579
 
1569
1580
  Lint/BigDecimalNew:
@@ -1620,12 +1631,12 @@ Lint/Debugger:
1620
1631
  Enabled: true
1621
1632
  VersionAdded: '0.14'
1622
1633
  VersionChanged: '1.10'
1623
- DebuggerReceivers: [] # deprecated
1624
1634
  DebuggerMethods:
1625
1635
  # Groups are available so that a specific group can be disabled in
1626
1636
  # a user's configuration, but are otherwise not significant.
1627
1637
  Kernel:
1628
1638
  - binding.irb
1639
+ - p
1629
1640
  - Kernel.binding.irb
1630
1641
  Byebug:
1631
1642
  - byebug
@@ -1635,6 +1646,9 @@ Lint/Debugger:
1635
1646
  Capybara:
1636
1647
  - save_and_open_page
1637
1648
  - save_and_open_screenshot
1649
+ PP:
1650
+ - PP.pp
1651
+ - pp
1638
1652
  debug.rb:
1639
1653
  - binding.b
1640
1654
  - binding.break
@@ -2037,7 +2051,6 @@ Lint/NumberConversion:
2037
2051
  SafeAutoCorrect: false
2038
2052
  AllowedMethods: []
2039
2053
  AllowedPatterns: []
2040
- IgnoredMethods: [] # deprecated
2041
2054
  IgnoredClasses:
2042
2055
  - Time
2043
2056
  - DateTime
@@ -2388,7 +2401,6 @@ Lint/UnreachableLoop:
2388
2401
  # RSpec uses `times` in its message expectations
2389
2402
  # eg. `exactly(2).times`
2390
2403
  - !ruby/regexp /(exactly|at_least|at_most)\(\d+\)\.times/
2391
- IgnoredPatterns: [] # deprecated
2392
2404
 
2393
2405
  Lint/UnusedBlockArgument:
2394
2406
  Description: 'Checks for unused block arguments.'
@@ -2498,7 +2510,6 @@ Metrics/AbcSize:
2498
2510
  # a Float.
2499
2511
  AllowedMethods: []
2500
2512
  AllowedPatterns: []
2501
- IgnoredMethods: [] # deprecated
2502
2513
  CountRepeatedAttributes: true
2503
2514
  Max: 17
2504
2515
 
@@ -2510,13 +2521,11 @@ Metrics/BlockLength:
2510
2521
  CountComments: false # count full line comments?
2511
2522
  Max: 25
2512
2523
  CountAsOne: []
2513
- ExcludedMethods: [] # deprecated, retained for backwards compatibility
2514
2524
  AllowedMethods:
2515
2525
  # By default, exclude the `#refine` method, as it tends to have larger
2516
2526
  # associated blocks.
2517
2527
  - refine
2518
2528
  AllowedPatterns: []
2519
- IgnoredMethods: [] # deprecated
2520
2529
  Exclude:
2521
2530
  - '**/*.gemspec'
2522
2531
 
@@ -2548,7 +2557,6 @@ Metrics/CyclomaticComplexity:
2548
2557
  VersionChanged: '0.81'
2549
2558
  AllowedMethods: []
2550
2559
  AllowedPatterns: []
2551
- IgnoredMethods: [] # deprecated
2552
2560
  Max: 7
2553
2561
 
2554
2562
  Metrics/MethodLength:
@@ -2560,10 +2568,8 @@ Metrics/MethodLength:
2560
2568
  CountComments: false # count full line comments?
2561
2569
  Max: 10
2562
2570
  CountAsOne: []
2563
- ExcludedMethods: [] # deprecated, retained for backwards compatibility
2564
2571
  AllowedMethods: []
2565
2572
  AllowedPatterns: []
2566
- IgnoredMethods: [] # deprecated
2567
2573
 
2568
2574
  Metrics/ModuleLength:
2569
2575
  Description: 'Avoid modules longer than 100 lines of code.'
@@ -2593,7 +2599,6 @@ Metrics/PerceivedComplexity:
2593
2599
  VersionChanged: '0.81'
2594
2600
  AllowedMethods: []
2595
2601
  AllowedPatterns: []
2596
- IgnoredMethods: [] # deprecated
2597
2602
  Max: 8
2598
2603
 
2599
2604
  ################## Migration #############################
@@ -2764,7 +2769,7 @@ Naming/HeredocDelimiterNaming:
2764
2769
  Enabled: true
2765
2770
  VersionAdded: '0.50'
2766
2771
  ForbiddenDelimiters:
2767
- - !ruby/regexp '/(^|\s)(EO[A-Z]{1}|END)(\s|$)/'
2772
+ - !ruby/regexp '/(^|\s)(EO[A-Z]{1}|END)(\s|$)/i'
2768
2773
 
2769
2774
  Naming/InclusiveLanguage:
2770
2775
  Description: 'Recommend the use of inclusive language instead of problematic terms.'
@@ -2822,7 +2827,6 @@ Naming/MethodName:
2822
2827
  # - '\A\s*onSelectionCleared\s*'
2823
2828
  #
2824
2829
  AllowedPatterns: []
2825
- IgnoredPatterns: [] # deprecated
2826
2830
 
2827
2831
  Naming/MethodParameterName:
2828
2832
  Description: >-
@@ -3192,7 +3196,6 @@ Style/BlockDelimiters:
3192
3196
  - proc
3193
3197
  - it
3194
3198
  AllowedPatterns: []
3195
- IgnoredMethods: [] # deprecated
3196
3199
  # The AllowBracesOnProceduralOneLiners option is ignored unless the
3197
3200
  # EnforcedStyle is set to `semantic`. If so:
3198
3201
  #
@@ -3216,7 +3219,7 @@ Style/BlockDelimiters:
3216
3219
  # collection.each do |element| puts element end
3217
3220
  AllowBracesOnProceduralOneLiners: false
3218
3221
  # The BracesRequiredMethods overrides all other configurations except
3219
- # IgnoredMethods. It can be used to enforce that all blocks for specific
3222
+ # AllowedMethods. It can be used to enforce that all blocks for specific
3220
3223
  # methods use braces. For example, you can use this to enforce Sorbet
3221
3224
  # signatures use braces even when the rest of your codebase enforces
3222
3225
  # the `line_count_based` style.
@@ -3310,7 +3313,6 @@ Style/ClassEqualityComparison:
3310
3313
  - equal?
3311
3314
  - eql?
3312
3315
  AllowedPatterns: []
3313
- IgnoredMethods: [] # deprecated
3314
3316
 
3315
3317
  Style/ClassMethods:
3316
3318
  Description: 'Use self when defining module/class methods.'
@@ -3431,6 +3433,11 @@ Style/CommentedKeyword:
3431
3433
  VersionAdded: '0.51'
3432
3434
  VersionChanged: '1.19'
3433
3435
 
3436
+ Style/ComparableClamp:
3437
+ Description: 'Enforces the use of `Comparable#clamp` instead of comparison by minimum and maximum.'
3438
+ Enabled: pending
3439
+ VersionAdded: '1.44'
3440
+
3434
3441
  Style/ConcatArrayLiterals:
3435
3442
  Description: 'Enforces the use of `Array#push(item)` instead of `Array#concat([item])` to avoid redundant array literals.'
3436
3443
  Enabled: pending
@@ -3780,7 +3787,6 @@ Style/FormatStringToken:
3780
3787
  VersionChanged: '1.0'
3781
3788
  AllowedMethods: []
3782
3789
  AllowedPatterns: []
3783
- IgnoredMethods: [] # deprecated
3784
3790
 
3785
3791
  Style/FrozenStringLiteralComment:
3786
3792
  Description: >-
@@ -4031,6 +4037,35 @@ Style/InverseMethods:
4031
4037
  :select: :reject
4032
4038
  :select!: :reject!
4033
4039
 
4040
+ Style/InvertibleUnlessCondition:
4041
+ Description: 'Favor `if` with inverted condition over `unless`.'
4042
+ Enabled: false
4043
+ VersionAdded: '1.44'
4044
+ # `InverseMethods` are methods that can be inverted in a `unless` condition.
4045
+ # The relationship of inverse methods needs to be defined in both directions.
4046
+ # Keys and values both need to be defined as symbols.
4047
+ InverseMethods:
4048
+ :!=: :==
4049
+ :>: :<=
4050
+ :<=: :>
4051
+ :<: :>=
4052
+ :>=: :<
4053
+ :!~: :=~
4054
+ :zero?: :nonzero?
4055
+ :nonzero?: :zero?
4056
+ :any?: :none?
4057
+ :none?: :any?
4058
+ :even?: :odd?
4059
+ :odd?: :even?
4060
+ # `ActiveSupport` defines some common inverse methods. They are listed below,
4061
+ # and not enabled by default.
4062
+ # :present?: :blank?
4063
+ # :blank?: :present?
4064
+ # :include?: :exclude?
4065
+ # :exclude?: :include?
4066
+ # :one?: :many?
4067
+ # :many?: :one?
4068
+
4034
4069
  Style/IpAddresses:
4035
4070
  Description: "Don't include literal IP addresses in code."
4036
4071
  Enabled: false
@@ -4130,9 +4165,7 @@ Style/MethodCallWithArgsParentheses:
4130
4165
  VersionChanged: '1.7'
4131
4166
  IgnoreMacros: true
4132
4167
  AllowedMethods: []
4133
- IgnoredMethods: [] # deprecated
4134
4168
  AllowedPatterns: []
4135
- IgnoredPatterns: [] # deprecated
4136
4169
  IncludedMacros: []
4137
4170
  AllowParenthesesInMultilineCall: false
4138
4171
  AllowParenthesesInChaining: false
@@ -4149,7 +4182,6 @@ Style/MethodCallWithoutArgsParentheses:
4149
4182
  Enabled: true
4150
4183
  AllowedMethods: []
4151
4184
  AllowedPatterns: []
4152
- IgnoredMethods: [] # deprecated
4153
4185
  VersionAdded: '0.47'
4154
4186
  VersionChanged: '0.55'
4155
4187
 
@@ -4528,7 +4560,6 @@ Style/NumericPredicate:
4528
4560
  - comparison
4529
4561
  AllowedMethods: []
4530
4562
  AllowedPatterns: []
4531
- IgnoredMethods: [] # deprecated
4532
4563
  # Exclude RSpec specs because assertions like `expect(1).to be > 0` cause
4533
4564
  # false positives.
4534
4565
  Exclude:
@@ -4808,6 +4839,11 @@ Style/RedundantFreeze:
4808
4839
  VersionAdded: '0.34'
4809
4840
  VersionChanged: '0.66'
4810
4841
 
4842
+ Style/RedundantHeredocDelimiterQuotes:
4843
+ Description: 'Checks for redundant heredoc delimiter quotes.'
4844
+ Enabled: pending
4845
+ VersionAdded: '1.45'
4846
+
4811
4847
  Style/RedundantInitialize:
4812
4848
  Description: 'Checks for redundant `initialize` methods.'
4813
4849
  Enabled: pending
@@ -5205,7 +5241,6 @@ Style/SymbolProc:
5205
5241
  AllowedMethods:
5206
5242
  - define_method
5207
5243
  AllowedPatterns: []
5208
- IgnoredMethods: [] # deprecated
5209
5244
  AllowComments: false
5210
5245
 
5211
5246
  Style/TernaryParentheses:
data/lib/rubocop/cli.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'fileutils'
4
+
3
5
  module RuboCop
4
6
  # The CLI is a class responsible of handling all the command line interface
5
7
  # logic.
@@ -38,14 +40,16 @@ module RuboCop
38
40
  @options, paths = Options.new.parse(args)
39
41
  @env = Environment.new(@options, @config_store, paths)
40
42
 
41
- if @options[:init]
42
- run_command(:init)
43
- else
44
- act_on_options
45
- validate_options_vs_config
46
- parallel_by_default!
47
- apply_default_formatter
48
- execute_runners
43
+ profile_if_needed do
44
+ if @options[:init]
45
+ run_command(:init)
46
+ else
47
+ act_on_options
48
+ validate_options_vs_config
49
+ parallel_by_default!
50
+ apply_default_formatter
51
+ execute_runners
52
+ end
49
53
  end
50
54
  rescue ConfigNotFoundError, IncorrectCopNameError, OptionArgumentError => e
51
55
  warn e.message
@@ -68,6 +72,48 @@ module RuboCop
68
72
 
69
73
  private
70
74
 
75
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
76
+ def profile_if_needed
77
+ return yield unless @options[:profile]
78
+
79
+ return STATUS_ERROR unless require_gem('stackprof')
80
+
81
+ with_memory = @options[:memory]
82
+ if with_memory
83
+ return STATUS_ERROR unless require_gem('memory_profiler')
84
+
85
+ MemoryProfiler.start
86
+ end
87
+
88
+ tmp_dir = File.join(ConfigFinder.project_root, 'tmp')
89
+ FileUtils.mkdir_p(tmp_dir)
90
+ status = nil
91
+
92
+ StackProf.run(out: File.join(tmp_dir, 'rubocop-stackprof.dump')) do
93
+ status = yield
94
+ end
95
+ puts 'Profile report generated'
96
+
97
+ if with_memory
98
+ puts 'Building memory report...'
99
+ report = MemoryProfiler.stop
100
+ report.pretty_print(
101
+ to_file: File.join(tmp_dir, 'rubocop-memory_profiler.txt'),
102
+ scale_bytes: true
103
+ )
104
+ end
105
+ status
106
+ end
107
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
108
+
109
+ def require_gem(name)
110
+ require name
111
+ true
112
+ rescue LoadError
113
+ warn("You don't have #{name} installed. Add it to your Gemfile and run `bundle install`")
114
+ false
115
+ end
116
+
71
117
  def run_command(name)
72
118
  @env.run(name)
73
119
  end
@@ -111,10 +111,17 @@ module RuboCop
111
111
  end
112
112
 
113
113
  merge_with_default(config, config_file).tap do |merged_config|
114
- warn_on_pending_cops(merged_config.pending_cops) unless possible_new_cops?(merged_config)
114
+ unless possible_new_cops?(merged_config)
115
+ pending_cops = pending_cops_only_qualified(merged_config.pending_cops)
116
+ warn_on_pending_cops(pending_cops) unless pending_cops.empty?
117
+ end
115
118
  end
116
119
  end
117
120
 
121
+ def pending_cops_only_qualified(pending_cops)
122
+ pending_cops.select { |cop| Cop::Registry.qualified_cop?(cop.name) }
123
+ end
124
+
118
125
  def possible_new_cops?(config)
119
126
  disable_pending_cops || enable_pending_cops ||
120
127
  config.disabled_new_cops? || config.enabled_new_cops?
@@ -167,8 +174,6 @@ module RuboCop
167
174
  BANNER
168
175
 
169
176
  def warn_on_pending_cops(pending_cops)
170
- return if pending_cops.empty?
171
-
172
177
  warn Rainbow(PENDING_BANNER).yellow
173
178
 
174
179
  pending_cops.each { |cop| warn_pending_cop cop }
@@ -241,18 +246,10 @@ module RuboCop
241
246
  raise
242
247
  end
243
248
 
244
- if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0')
245
- def yaml_safe_load!(yaml_code, filename)
246
- YAML.safe_load(yaml_code,
247
- permitted_classes: [Regexp, Symbol],
248
- permitted_symbols: [],
249
- aliases: true,
250
- filename: filename)
251
- end
252
- else # Ruby < 2.6
253
- def yaml_safe_load!(yaml_code, filename)
254
- YAML.safe_load(yaml_code, [Regexp, Symbol], [], true, filename)
255
- end
249
+ def yaml_safe_load!(yaml_code, filename)
250
+ YAML.safe_load(
251
+ yaml_code, permitted_classes: [Regexp, Symbol], aliases: true, filename: filename
252
+ )
256
253
  end
257
254
  end
258
255
 
@@ -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, path, k, v) if v.key?('Include')
36
+ fix_include_paths(base_config.loaded_path, hash, k, v) if v.key?('Include')
37
37
  end
38
38
  end
39
39
  end
@@ -42,13 +42,12 @@ 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, path, key, value)
45
+ def fix_include_paths(base_config_path, hash, 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)
50
49
  hash[key]['Include'] = value['Include'].map do |include_path|
51
- PathUtil.relative_path(File.join(base_dir, include_path), derived_dir)
50
+ PathUtil.relative_path(File.join(base_dir, include_path), Dir.pwd)
52
51
  end
53
52
  end
54
53
 
@@ -180,6 +180,10 @@ module RuboCop
180
180
 
181
181
  status, corrector = enabled_line?(range.line) ? correct(range, &block) : :disabled
182
182
 
183
+ # Since this range may be generated from Ruby code embedded in some
184
+ # template file, we convert it to location info in the original file.
185
+ range = range_for_original(range)
186
+
183
187
  current_offenses << Offense.new(severity, range, message, name, status, corrector)
184
188
  end
185
189
 
@@ -286,6 +290,21 @@ module RuboCop
286
290
  end
287
291
  # rubocop:enable Layout/ClassStructure
288
292
 
293
+ # Called before any investigation
294
+ # @api private
295
+ def begin_investigation(processed_source, offset: 0, original: processed_source)
296
+ @current_offenses = nil
297
+ @current_offense_locations = nil
298
+ @currently_disabled_lines = nil
299
+ @processed_source = processed_source
300
+ @current_corrector = nil
301
+
302
+ # We need to keep track of the original source and offset,
303
+ # because `processed_source` here may be an embedded code in it.
304
+ @current_offset = offset
305
+ @current_original = original
306
+ end
307
+
289
308
  private
290
309
 
291
310
  ### Reserved for Cop::Cop
@@ -320,15 +339,6 @@ module RuboCop
320
339
  @restrict_on_send ||= self::RESTRICT_ON_SEND.to_a.freeze
321
340
  end
322
341
 
323
- # Called before any investigation
324
- def begin_investigation(processed_source)
325
- @current_offenses = nil
326
- @current_offense_locations = nil
327
- @currently_disabled_lines = nil
328
- @processed_source = processed_source
329
- @current_corrector = nil
330
- end
331
-
332
342
  EMPTY_OFFENSES = [].freeze
333
343
  private_constant :EMPTY_OFFENSES
334
344
  # Called to complete an investigation
@@ -459,6 +469,14 @@ module RuboCop
459
469
  warn(Rainbow(message).red)
460
470
  end
461
471
  end
472
+
473
+ def range_for_original(range)
474
+ ::Parser::Source::Range.new(
475
+ @current_original.buffer,
476
+ range.begin_pos + @current_offset,
477
+ range.end_pos + @current_offset
478
+ )
479
+ end
462
480
  end
463
481
  end
464
482
  end
@@ -76,10 +76,10 @@ module RuboCop
76
76
  end
77
77
 
78
78
  # @return [InvestigationReport]
79
- def investigate(processed_source)
79
+ def investigate(processed_source, offset: 0, original: processed_source)
80
80
  reset
81
81
 
82
- @cops.each { |cop| cop.send :begin_investigation, processed_source }
82
+ begin_investigation(processed_source, offset: offset, original: original)
83
83
  if processed_source.valid_syntax?
84
84
  invoke(:on_new_investigation, @cops)
85
85
  invoke_with_argument(:investigate, @forces, processed_source)
@@ -95,6 +95,12 @@ module RuboCop
95
95
 
96
96
  private
97
97
 
98
+ def begin_investigation(processed_source, offset:, original:)
99
+ @cops.each do |cop|
100
+ cop.begin_investigation(processed_source, offset: offset, original: original)
101
+ end
102
+ end
103
+
98
104
  def trigger_responding_cops(callback, node)
99
105
  @callbacks[callback]&.each do |cop|
100
106
  with_cop_error_handling(cop, node) do
@@ -53,6 +53,11 @@ module RuboCop
53
53
  def add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)
54
54
  @v0_argument = node_or_range
55
55
  range = find_location(node_or_range, location)
56
+
57
+ # Since this range may be generated from Ruby code embedded in some
58
+ # template file, we convert it to location info in the original file.
59
+ range = range_for_original(range)
60
+
56
61
  if block.nil? && !support_autocorrect?
57
62
  super(range, message: message, severity: severity)
58
63
  else
@@ -93,14 +98,21 @@ module RuboCop
93
98
  super
94
99
  end
95
100
 
96
- private
97
-
98
- def begin_investigation(processed_source)
101
+ # Called before any investigation
102
+ # @api private
103
+ def begin_investigation(processed_source, offset: 0, original: processed_source)
99
104
  super
100
105
  @offenses = current_offenses
101
106
  @last_corrector = @current_corrector
107
+
108
+ # We need to keep track of the original source and offset,
109
+ # because `processed_source` here may be an embedded code in it.
110
+ @current_offset = offset
111
+ @current_original = original
102
112
  end
103
113
 
114
+ private
115
+
104
116
  # Override Base
105
117
  def callback_argument(_range)
106
118
  @v0_argument
@@ -141,6 +153,14 @@ module RuboCop
141
153
  rescue ::Parser::ClobberingError
142
154
  # ignore Clobbering errors
143
155
  end
156
+
157
+ def range_for_original(range)
158
+ ::Parser::Source::Range.new(
159
+ @current_original.buffer,
160
+ range.begin_pos + @current_offset,
161
+ range.end_pos + @current_offset
162
+ )
163
+ end
144
164
  end
145
165
  end
146
166
  end
@@ -86,8 +86,16 @@ module RuboCop
86
86
  range1 = to_range(node_or_range1)
87
87
  range2 = to_range(node_or_range2)
88
88
 
89
- replace(range1, range2.source)
90
- replace(range2, range1.source)
89
+ if range1.end_pos == range2.begin_pos
90
+ insert_before(range1, range2.source)
91
+ remove(range2)
92
+ elsif range2.end_pos == range1.begin_pos
93
+ insert_before(range2, range1.source)
94
+ remove(range1)
95
+ else
96
+ replace(range1, range2.source)
97
+ replace(range2, range1.source)
98
+ end
91
99
  end
92
100
 
93
101
  private
@@ -18,7 +18,7 @@ module RuboCop
18
18
  current_range = declaration_with_comment(node)
19
19
  previous_range = declaration_with_comment(previous_declaration)
20
20
 
21
- ->(corrector) { swap_range(corrector, current_range, previous_range) }
21
+ ->(corrector) { corrector.swap(current_range, previous_range) }
22
22
  end
23
23
 
24
24
  private
@@ -32,11 +32,6 @@ module RuboCop
32
32
 
33
33
  range_between(begin_pos, end_pos)
34
34
  end
35
-
36
- def swap_range(corrector, range1, range2)
37
- corrector.insert_before(range2, range1.source)
38
- corrector.remove(range1)
39
- end
40
35
  end
41
36
  end
42
37
  end