rubocop 1.33.0 → 1.35.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +29 -1
- data/lib/rubocop/cli/command/suggest_extensions.rb +53 -15
- data/lib/rubocop/config_loader.rb +12 -0
- data/lib/rubocop/config_loader_resolver.rb +1 -5
- data/lib/rubocop/cop/cop.rb +1 -1
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +58 -0
- data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +69 -0
- data/lib/rubocop/cop/internal_affairs/single_line_comparison.rb +61 -0
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/block_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/block_end_newline.rb +4 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +5 -2
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +2 -0
- data/lib/rubocop/cop/layout/indentation_width.rb +2 -0
- data/lib/rubocop/cop/layout/line_length.rb +4 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -0
- data/lib/rubocop/cop/legacy/corrections_proxy.rb +1 -1
- data/lib/rubocop/cop/legacy/corrector.rb +1 -1
- data/lib/rubocop/cop/lint/debugger.rb +15 -15
- data/lib/rubocop/cop/lint/empty_block.rb +1 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +5 -0
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +9 -9
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +25 -6
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +12 -0
- data/lib/rubocop/cop/lint/redundant_with_index.rb +13 -10
- data/lib/rubocop/cop/lint/redundant_with_object.rb +12 -11
- data/lib/rubocop/cop/lint/shadowed_exception.rb +15 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +10 -1
- data/lib/rubocop/cop/lint/unreachable_loop.rb +7 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +6 -4
- data/lib/rubocop/cop/lint/void.rb +2 -0
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +76 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
- data/lib/rubocop/cop/mixin/method_complexity.rb +4 -4
- data/lib/rubocop/cop/mixin/range_help.rb +4 -5
- data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
- data/lib/rubocop/cop/style/arguments_forwarding.rb +2 -2
- data/lib/rubocop/cop/style/class_methods_definitions.rb +2 -1
- data/lib/rubocop/cop/style/collection_methods.rb +2 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +3 -1
- data/lib/rubocop/cop/style/double_negation.rb +2 -0
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
- data/lib/rubocop/cop/style/each_with_object.rb +39 -8
- data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
- data/lib/rubocop/cop/style/for.rb +2 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +3 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +17 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +8 -6
- data/lib/rubocop/cop/style/magic_comment_format.rb +307 -0
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
- data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -1
- data/lib/rubocop/cop/style/next.rb +2 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +16 -1
- data/lib/rubocop/cop/style/object_then.rb +2 -0
- data/lib/rubocop/cop/style/proc.rb +4 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +2 -0
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +15 -22
- data/lib/rubocop/cop/style/redundant_self.rb +2 -0
- data/lib/rubocop/cop/style/redundant_sort_by.rb +24 -8
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +14 -3
- data/lib/rubocop/cop/style/symbol_proc.rb +5 -0
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -13
- data/lib/rubocop/cop/style/top_level_method_definition.rb +3 -1
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
- data/lib/rubocop/ext/range.rb +15 -0
- data/lib/rubocop/feature_loader.rb +92 -0
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +1 -1
- data/lib/rubocop/formatter/markdown_formatter.rb +1 -1
- data/lib/rubocop/formatter/tap_formatter.rb +1 -1
- data/lib/rubocop/server/cache.rb +4 -2
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +4 -1
- metadata +13 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6533c308e8ecd9193b09cd13fb1f967d1721cbfe4e4cb88bcb453861362b5152
|
4
|
+
data.tar.gz: a8ebb7e167336289eb9adf7e05cbf8bfe59ccc9669b9177b363a7d54b4d597fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d49c12af4f602e15bf19903272b4ac0e834004f9eb07eecbca829a619559b6326dd95e2e9e10121763c73ff844968853e59dd89f84e9eded8d48bef0149cb379
|
7
|
+
data.tar.gz: 440d3e163691372ef95aa49847a58e611e669faf0d950f24b0135264d234c4bc4e35ef5c5a6d92394725cf3947295fd7b107dd48bfb7937df9a7fbac7ba70e90
|
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.
|
56
|
+
gem 'rubocop', '~> 1.35', 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
@@ -1604,6 +1604,7 @@ Lint/Debugger:
|
|
1604
1604
|
# a user's configuration, but are otherwise not significant.
|
1605
1605
|
Kernel:
|
1606
1606
|
- binding.irb
|
1607
|
+
- Kernel.binding.irb
|
1607
1608
|
Byebug:
|
1608
1609
|
- byebug
|
1609
1610
|
- remote_byebug
|
@@ -1621,6 +1622,9 @@ Lint/Debugger:
|
|
1621
1622
|
- binding.pry
|
1622
1623
|
- binding.remote_pry
|
1623
1624
|
- binding.pry_remote
|
1625
|
+
- Kernel.binding.pry
|
1626
|
+
- Kernel.binding.remote_pry
|
1627
|
+
- Kernel.binding.pry_remote
|
1624
1628
|
- Pry.rescue
|
1625
1629
|
Rails:
|
1626
1630
|
- debugger
|
@@ -1753,9 +1757,10 @@ Lint/EmptyClass:
|
|
1753
1757
|
Lint/EmptyConditionalBody:
|
1754
1758
|
Description: 'Checks for the presence of `if`, `elsif` and `unless` branches without a body.'
|
1755
1759
|
Enabled: true
|
1760
|
+
SafeAutoCorrect: false
|
1756
1761
|
AllowComments: true
|
1757
1762
|
VersionAdded: '0.89'
|
1758
|
-
VersionChanged: '1.
|
1763
|
+
VersionChanged: '1.34'
|
1759
1764
|
|
1760
1765
|
Lint/EmptyEnsure:
|
1761
1766
|
Description: 'Checks for empty ensure block.'
|
@@ -3834,6 +3839,8 @@ Style/HashSyntax:
|
|
3834
3839
|
- never
|
3835
3840
|
# accepts both shorthand and explicit use of hash literal value.
|
3836
3841
|
- either
|
3842
|
+
# like "always", but will avoid mixing styles in a single hash
|
3843
|
+
- consistent
|
3837
3844
|
# Force hashes that have a symbol value to use hash rockets
|
3838
3845
|
UseHashRocketsWithSymbolValues: false
|
3839
3846
|
# Do not suggest { a?: 1 } over { :a? => 1 } in ruby19 style
|
@@ -4010,6 +4017,26 @@ Style/LineEndConcatenation:
|
|
4010
4017
|
VersionAdded: '0.18'
|
4011
4018
|
VersionChanged: '0.64'
|
4012
4019
|
|
4020
|
+
Style/MagicCommentFormat:
|
4021
|
+
Description: 'Use a consistent style for magic comments.'
|
4022
|
+
Enabled: pending
|
4023
|
+
VersionAdded: '1.35'
|
4024
|
+
EnforcedStyle: snake_case
|
4025
|
+
SupportedStyles:
|
4026
|
+
# `snake` will enforce the magic comment is written
|
4027
|
+
# in snake case (words separated by underscores).
|
4028
|
+
# Eg: froze_string_literal: true
|
4029
|
+
- snake_case
|
4030
|
+
# `kebab` will enforce the magic comment is written
|
4031
|
+
# in kebab case (words separated by hyphens).
|
4032
|
+
# Eg: froze-string-literal: true
|
4033
|
+
- kebab_case
|
4034
|
+
DirectiveCapitalization: lowercase
|
4035
|
+
ValueCapitalization: ~
|
4036
|
+
SupportedCapitalizations:
|
4037
|
+
- lowercase
|
4038
|
+
- uppercase
|
4039
|
+
|
4013
4040
|
Style/MapCompactWithConditionalBlock:
|
4014
4041
|
Description: 'Prefer `select` or `reject` over `map { ... }.compact`.'
|
4015
4042
|
Enabled: pending
|
@@ -4400,6 +4427,7 @@ Style/NumericLiterals:
|
|
4400
4427
|
Strict: false
|
4401
4428
|
# You can specify allowed numbers. (e.g. port number 3000, 8080, and etc)
|
4402
4429
|
AllowedNumbers: []
|
4430
|
+
AllowedPatterns: []
|
4403
4431
|
|
4404
4432
|
Style/NumericPredicate:
|
4405
4433
|
Description: >-
|
@@ -17,20 +17,10 @@ module RuboCop
|
|
17
17
|
def run
|
18
18
|
return if skip? || extensions.none?
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
'RuboCop extension libraries might be helpful:'
|
23
|
-
|
24
|
-
extensions.sort.each do |extension|
|
25
|
-
puts " * #{extension} (https://rubygems.org/gems/#{extension})"
|
26
|
-
end
|
20
|
+
print_install_suggestions if not_installed_extensions.any?
|
21
|
+
print_load_suggestions if installed_and_not_loaded_extensions.any?
|
27
22
|
|
28
|
-
|
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
|
-
'for more options):'
|
32
|
-
puts ' AllCops:'
|
33
|
-
puts ' SuggestExtensions: false'
|
23
|
+
print_opt_out_instruction
|
34
24
|
|
35
25
|
puts if @options[:display_time]
|
36
26
|
end
|
@@ -48,15 +38,63 @@ module RuboCop
|
|
48
38
|
!INCLUDED_FORMATTERS.include?(current_formatter)
|
49
39
|
end
|
50
40
|
|
41
|
+
def print_install_suggestions
|
42
|
+
puts
|
43
|
+
puts 'Tip: Based on detected gems, the following ' \
|
44
|
+
'RuboCop extension libraries might be helpful:'
|
45
|
+
|
46
|
+
not_installed_extensions.sort.each do |extension|
|
47
|
+
puts " * #{extension} (https://rubygems.org/gems/#{extension})"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def print_load_suggestions
|
52
|
+
puts
|
53
|
+
puts 'The following RuboCop extension libraries are installed but not loaded in config:'
|
54
|
+
|
55
|
+
installed_and_not_loaded_extensions.sort.each do |extension|
|
56
|
+
puts " * #{extension}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def print_opt_out_instruction
|
61
|
+
puts
|
62
|
+
puts 'You can opt out of this message by adding the following to your config ' \
|
63
|
+
'(see https://docs.rubocop.org/rubocop/extensions.html#extension-suggestions ' \
|
64
|
+
'for more options):'
|
65
|
+
puts ' AllCops:'
|
66
|
+
puts ' SuggestExtensions: false'
|
67
|
+
end
|
68
|
+
|
51
69
|
def current_formatter
|
52
70
|
@options[:format] || @config_store.for_pwd.for_all_cops['DefaultFormatter'] || 'p'
|
53
71
|
end
|
54
72
|
|
55
|
-
def
|
73
|
+
def all_extensions
|
56
74
|
return [] unless lockfile.dependencies.any?
|
57
75
|
|
58
76
|
extensions = @config_store.for_pwd.for_all_cops['SuggestExtensions'] || {}
|
59
|
-
extensions.select { |_, v| (Array(v) & dependent_gems).any? }.keys
|
77
|
+
extensions.select { |_, v| (Array(v) & dependent_gems).any? }.keys
|
78
|
+
end
|
79
|
+
|
80
|
+
def extensions
|
81
|
+
not_installed_extensions + installed_and_not_loaded_extensions
|
82
|
+
end
|
83
|
+
|
84
|
+
def installed_extensions
|
85
|
+
all_extensions & installed_gems
|
86
|
+
end
|
87
|
+
|
88
|
+
def not_installed_extensions
|
89
|
+
all_extensions - installed_gems
|
90
|
+
end
|
91
|
+
|
92
|
+
def loaded_extensions
|
93
|
+
@config_store.for_pwd.loaded_features.to_a
|
94
|
+
end
|
95
|
+
|
96
|
+
def installed_and_not_loaded_extensions
|
97
|
+
installed_extensions - loaded_extensions
|
60
98
|
end
|
61
99
|
|
62
100
|
def lockfile
|
@@ -137,6 +137,18 @@ module RuboCop
|
|
137
137
|
end
|
138
138
|
end
|
139
139
|
|
140
|
+
# Returns the path RuboCop inferred as the root of the project. No file
|
141
|
+
# searches will go past this directory.
|
142
|
+
# @deprecated Use `RuboCop::ConfigFinder.project_root` instead.
|
143
|
+
def project_root
|
144
|
+
warn Rainbow(<<~WARNING).yellow
|
145
|
+
`RuboCop::ConfigLoader.project_root` is deprecated and will be removed in RuboCop 2.0. \
|
146
|
+
Use `RuboCop::ConfigFinder.project_root` instead.
|
147
|
+
WARNING
|
148
|
+
|
149
|
+
ConfigFinder.project_root
|
150
|
+
end
|
151
|
+
|
140
152
|
PENDING_BANNER = <<~BANNER
|
141
153
|
The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.
|
142
154
|
|
@@ -11,11 +11,7 @@ module RuboCop
|
|
11
11
|
config_dir = File.dirname(path)
|
12
12
|
hash.delete('require').tap do |loaded_features|
|
13
13
|
Array(loaded_features).each do |feature|
|
14
|
-
|
15
|
-
require(File.join(config_dir, feature))
|
16
|
-
else
|
17
|
-
require(feature)
|
18
|
-
end
|
14
|
+
FeatureLoader.load(config_directory_path: config_dir, feature: feature)
|
19
15
|
end
|
20
16
|
end
|
21
17
|
end
|
data/lib/rubocop/cop/cop.rb
CHANGED
@@ -7,7 +7,7 @@ module RuboCop
|
|
7
7
|
module Cop
|
8
8
|
# @deprecated Use Cop::Base instead
|
9
9
|
# Legacy scaffold for Cops.
|
10
|
-
# See https://docs.rubocop.org/rubocop/
|
10
|
+
# See https://docs.rubocop.org/rubocop/v1_upgrade_notes.html
|
11
11
|
class Cop < Base
|
12
12
|
attr_reader :offenses
|
13
13
|
|
@@ -5,9 +5,15 @@ module RuboCop
|
|
5
5
|
# This autocorrects parentheses
|
6
6
|
class ParenthesesCorrector
|
7
7
|
class << self
|
8
|
+
include RangeHelp
|
9
|
+
|
10
|
+
COMMA_REGEXP = /(?<=\))\s*,/.freeze
|
11
|
+
|
8
12
|
def correct(corrector, node)
|
9
13
|
corrector.remove(node.loc.begin)
|
10
14
|
corrector.remove(node.loc.end)
|
15
|
+
handle_orphaned_comma(corrector, node)
|
16
|
+
|
11
17
|
return unless ternary_condition?(node) && next_char_is_question_mark?(node)
|
12
18
|
|
13
19
|
corrector.insert_after(node.loc.end, ' ')
|
@@ -22,6 +28,58 @@ module RuboCop
|
|
22
28
|
def next_char_is_question_mark?(node)
|
23
29
|
node.loc.last_column == node.parent.loc.question.column
|
24
30
|
end
|
31
|
+
|
32
|
+
def only_closing_paren_before_comma?(node)
|
33
|
+
source_buffer = node.source_range.source_buffer
|
34
|
+
line_range = source_buffer.line_range(node.loc.end.line)
|
35
|
+
|
36
|
+
line_range.source.start_with?(/\s*\)\s*,/)
|
37
|
+
end
|
38
|
+
|
39
|
+
# If removing parentheses leaves a comma on its own line, remove all the whitespace
|
40
|
+
# preceding it to prevent a syntax error.
|
41
|
+
def handle_orphaned_comma(corrector, node)
|
42
|
+
return unless only_closing_paren_before_comma?(node)
|
43
|
+
|
44
|
+
range = extend_range_for_heredoc(node, parens_range(node))
|
45
|
+
corrector.remove(range)
|
46
|
+
|
47
|
+
add_heredoc_comma(corrector, node)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Get a range for the closing parenthesis and all whitespace to the left of it
|
51
|
+
def parens_range(node)
|
52
|
+
range_with_surrounding_space(
|
53
|
+
range: node.loc.end,
|
54
|
+
buffer: node.source_range.source_buffer,
|
55
|
+
side: :left,
|
56
|
+
newlines: true,
|
57
|
+
whitespace: true,
|
58
|
+
continuations: true
|
59
|
+
)
|
60
|
+
end
|
61
|
+
|
62
|
+
# If the node contains a heredoc, remove the comma too
|
63
|
+
# It'll be added back in the right place later
|
64
|
+
def extend_range_for_heredoc(node, range)
|
65
|
+
return range unless heredoc?(node)
|
66
|
+
|
67
|
+
comma_line = range_by_whole_lines(node.loc.end, buffer: node.source_range.source_buffer)
|
68
|
+
offset = comma_line.source.match(COMMA_REGEXP)[0]&.size || 0
|
69
|
+
|
70
|
+
range.adjust(end_pos: offset)
|
71
|
+
end
|
72
|
+
|
73
|
+
# Add a comma back after the heredoc identifier
|
74
|
+
def add_heredoc_comma(corrector, node)
|
75
|
+
return unless heredoc?(node)
|
76
|
+
|
77
|
+
corrector.insert_after(node.child_nodes.last.loc.expression, ',')
|
78
|
+
end
|
79
|
+
|
80
|
+
def heredoc?(node)
|
81
|
+
node.child_nodes.last.loc.is_a?(Parser::Source::Map::Heredoc)
|
82
|
+
end
|
25
83
|
end
|
26
84
|
end
|
27
85
|
end
|
@@ -84,7 +84,7 @@ module RuboCop
|
|
84
84
|
(str "true")
|
85
85
|
PATTERN
|
86
86
|
|
87
|
-
def on_block(node) # rubocop:disable Metrics/MethodLength
|
87
|
+
def on_block(node) # rubocop:disable Metrics/MethodLength, InternalAffairs/NumblockHandler
|
88
88
|
gem_specification(node) do |block_var|
|
89
89
|
metadata_value = metadata(node)
|
90
90
|
mfa_value = mfa_value(metadata_value)
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module InternalAffairs
|
6
|
+
# Checks for missing `numblock handlers. The blocks with numbered
|
7
|
+
# arguments introduced in Ruby 2.7 are parsed with a node type of
|
8
|
+
# `numblock` instead of block. Cops that define `block` handlers
|
9
|
+
# need to define `numblock` handlers or disable this cope for them.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
#
|
13
|
+
# # bad
|
14
|
+
# class BlockRelatedCop < Base
|
15
|
+
# def on_block(node)
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# # good
|
20
|
+
# class BlockRelatedCop < Base
|
21
|
+
# def on_block(node)
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# alias on_numblock on_block
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# class BlockRelatedCop < Base
|
28
|
+
# def on_block(node)
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# alias_method :on_numblock, :on_block
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# class BlockRelatedCop < Base
|
35
|
+
# def on_block(node)
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# def on_numblock(node)
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
class NumblockHandler < Base
|
42
|
+
MSG = 'Define on_numblock to handle blocks with numbered arguments.'
|
43
|
+
|
44
|
+
def on_def(node)
|
45
|
+
return unless block_handler?(node)
|
46
|
+
return unless node.parent
|
47
|
+
|
48
|
+
add_offense(node) unless numblock_handler?(node.parent)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
# @!method block_handler?(node)
|
54
|
+
def_node_matcher :block_handler?, <<~PATTERN
|
55
|
+
(def :on_block (args (arg :node)) ...)
|
56
|
+
PATTERN
|
57
|
+
|
58
|
+
# @!method numblock_handler?(node)
|
59
|
+
def_node_matcher :numblock_handler?, <<~PATTERN
|
60
|
+
{
|
61
|
+
`(def :on_numblock (args (arg :node)) ...)
|
62
|
+
`(alias (sym :on_numblock) (sym :on_block))
|
63
|
+
`(send nil? :alias_method (sym :on_numblock) (sym :on_block))
|
64
|
+
}
|
65
|
+
PATTERN
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module InternalAffairs
|
6
|
+
# Enforces the use of `node.single_line?` instead of
|
7
|
+
# comparing `first_line` and `last_line` for equality.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # bad
|
11
|
+
# node.loc.first_line == node.loc.last_line
|
12
|
+
#
|
13
|
+
# # bad
|
14
|
+
# node.loc.last_line == node.loc.first_line
|
15
|
+
#
|
16
|
+
# # bad
|
17
|
+
# node.loc.line == node.loc.last_line
|
18
|
+
#
|
19
|
+
# # bad
|
20
|
+
# node.loc.last_line == node.loc.line
|
21
|
+
#
|
22
|
+
# # bad
|
23
|
+
# node.first_line == node.last_line
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# node.single_line?
|
27
|
+
#
|
28
|
+
class SingleLineComparison < Base
|
29
|
+
extend AutoCorrector
|
30
|
+
|
31
|
+
MSG = 'Use `%<preferred>s`.'
|
32
|
+
RESTRICT_ON_SEND = %i[==].freeze
|
33
|
+
|
34
|
+
# @!method single_line_comparison(node)
|
35
|
+
def_node_matcher :single_line_comparison, <<~PATTERN
|
36
|
+
{
|
37
|
+
(send (send $_receiver {:line :first_line}) :== (send _receiver :last_line))
|
38
|
+
(send (send $_receiver :last_line) :== (send _receiver {:line :first_line}))
|
39
|
+
}
|
40
|
+
PATTERN
|
41
|
+
|
42
|
+
def on_send(node)
|
43
|
+
return unless (receiver = single_line_comparison(node))
|
44
|
+
|
45
|
+
preferred = "#{extract_receiver(receiver)}.single_line?"
|
46
|
+
|
47
|
+
add_offense(node, message: format(MSG, preferred: preferred)) do |corrector|
|
48
|
+
corrector.replace(node, preferred)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def extract_receiver(node)
|
55
|
+
node = node.receiver if node.send_type? && %i[loc source_range].include?(node.method_name)
|
56
|
+
node.source
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -10,6 +10,7 @@ require_relative 'internal_affairs/method_name_equal'
|
|
10
10
|
require_relative 'internal_affairs/node_destructuring'
|
11
11
|
require_relative 'internal_affairs/node_matcher_directive'
|
12
12
|
require_relative 'internal_affairs/node_type_predicate'
|
13
|
+
require_relative 'internal_affairs/numblock_handler'
|
13
14
|
require_relative 'internal_affairs/offense_location_keyword'
|
14
15
|
require_relative 'internal_affairs/redundant_context_config_parameter'
|
15
16
|
require_relative 'internal_affairs/redundant_described_class_as_subject'
|
@@ -17,6 +18,7 @@ require_relative 'internal_affairs/redundant_let_rubocop_config_new'
|
|
17
18
|
require_relative 'internal_affairs/redundant_location_argument'
|
18
19
|
require_relative 'internal_affairs/redundant_message_argument'
|
19
20
|
require_relative 'internal_affairs/redundant_method_dispatch_node'
|
21
|
+
require_relative 'internal_affairs/single_line_comparison'
|
20
22
|
require_relative 'internal_affairs/style_detected_api_use'
|
21
23
|
require_relative 'internal_affairs/undefined_config'
|
22
24
|
require_relative 'internal_affairs/useless_message_assertion'
|
@@ -39,6 +39,8 @@ module RuboCop
|
|
39
39
|
register_offense(node)
|
40
40
|
end
|
41
41
|
|
42
|
+
alias on_numblock on_block
|
43
|
+
|
42
44
|
private
|
43
45
|
|
44
46
|
def register_offense(node)
|
@@ -60,9 +62,10 @@ module RuboCop
|
|
60
62
|
end
|
61
63
|
|
62
64
|
def last_heredoc_argument(node)
|
65
|
+
return unless node&.call_type?
|
63
66
|
return unless (arguments = node&.arguments)
|
64
67
|
|
65
|
-
heredoc = arguments.reverse.detect
|
68
|
+
heredoc = arguments.reverse.detect { |arg| arg.str_type? && arg.heredoc? }
|
66
69
|
return heredoc if heredoc
|
67
70
|
|
68
71
|
last_heredoc_argument(node.children.first)
|
@@ -80,8 +80,11 @@ module RuboCop
|
|
80
80
|
@block_line = node.source_range.first_line
|
81
81
|
end
|
82
82
|
|
83
|
-
|
84
|
-
|
83
|
+
alias on_numblock on_block
|
84
|
+
|
85
|
+
def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity
|
86
|
+
return unless node.bare_access_modifier? &&
|
87
|
+
!(node.parent&.block_type? || node.parent&.numblock_type?)
|
85
88
|
return if expected_empty_lines?(node)
|
86
89
|
|
87
90
|
message = message(node)
|
@@ -22,6 +22,7 @@ module RuboCop
|
|
22
22
|
# (Many of these are enabled by default.)
|
23
23
|
#
|
24
24
|
# * ArgumentAlignment
|
25
|
+
# * ArrayAlignment
|
25
26
|
# * BlockAlignment
|
26
27
|
# * BlockDelimiters
|
27
28
|
# * BlockEndNewline
|
@@ -74,6 +75,8 @@ module RuboCop
|
|
74
75
|
check_for_breakable_block(node)
|
75
76
|
end
|
76
77
|
|
78
|
+
alias on_numblock on_block
|
79
|
+
|
77
80
|
def on_potential_breakable_node(node)
|
78
81
|
check_for_breakable_node(node)
|
79
82
|
end
|
@@ -131,7 +134,7 @@ module RuboCop
|
|
131
134
|
if block_node.arguments? && !block_node.lambda?
|
132
135
|
block_node.arguments.loc.end
|
133
136
|
else
|
134
|
-
block_node.loc.begin
|
137
|
+
block_node.braces? ? block_node.loc.begin : block_node.loc.begin.adjust(begin_pos: 1)
|
135
138
|
end
|
136
139
|
end
|
137
140
|
|
@@ -73,7 +73,7 @@ module RuboCop
|
|
73
73
|
return if node.send_type? && node.loc.operator&.source != '='
|
74
74
|
return unless rhs
|
75
75
|
return unless supported_types.include?(rhs.type)
|
76
|
-
return if rhs.
|
76
|
+
return if rhs.single_line?
|
77
77
|
|
78
78
|
check_by_enforced_style(node, rhs)
|
79
79
|
end
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Legacy
|
6
6
|
# Legacy support for Corrector#corrections
|
7
|
-
# See https://docs.rubocop.org/rubocop/
|
7
|
+
# See https://docs.rubocop.org/rubocop/v1_upgrade_notes.html
|
8
8
|
class CorrectionsProxy
|
9
9
|
def initialize(corrector)
|
10
10
|
@corrector = corrector
|
@@ -4,7 +4,7 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Legacy
|
6
6
|
# Legacy Corrector for v0 API support.
|
7
|
-
# See https://docs.rubocop.org/rubocop/
|
7
|
+
# See https://docs.rubocop.org/rubocop/v1_upgrade_notes.html
|
8
8
|
class Corrector < RuboCop::Cop::Corrector
|
9
9
|
# Support legacy second argument
|
10
10
|
def initialize(source, corr = [])
|