rubocop 1.48.0 → 1.50.2
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 +22 -7
- data/lib/rubocop/cli/command/execute_runner.rb +7 -2
- data/lib/rubocop/cli.rb +6 -6
- data/lib/rubocop/config.rb +3 -3
- data/lib/rubocop/config_loader.rb +8 -8
- data/lib/rubocop/cop/autocorrect_logic.rb +28 -12
- data/lib/rubocop/cop/cop.rb +2 -2
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
- data/lib/rubocop/cop/gemspec/dependency_version.rb +1 -1
- data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/cop_description.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +2 -2
- data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +29 -2
- data/lib/rubocop/cop/layout/block_end_newline.rb +7 -21
- data/lib/rubocop/cop/layout/class_structure.rb +1 -0
- data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +5 -1
- data/lib/rubocop/cop/layout/extra_spacing.rb +6 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -1
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +25 -34
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +7 -19
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +42 -52
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +38 -55
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
- data/lib/rubocop/cop/layout/initial_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_array_line_breaks.rb +8 -27
- data/lib/rubocop/cop/layout/multiline_hash_key_line_breaks.rb +7 -26
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +4 -21
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +6 -30
- data/lib/rubocop/cop/layout/redundant_line_break.rb +6 -7
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_parens.rb +2 -2
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +3 -3
- data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +122 -0
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +35 -15
- data/lib/rubocop/cop/lint/syntax.rb +4 -0
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +7 -1
- data/lib/rubocop/cop/lint/unreachable_loop.rb +3 -3
- data/lib/rubocop/cop/lint/useless_method_definition.rb +10 -2
- data/lib/rubocop/cop/lint/useless_rescue.rb +4 -1
- data/lib/rubocop/cop/lint/void.rb +7 -3
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/class_length.rb +1 -0
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +1 -0
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +1 -1
- data/lib/rubocop/cop/naming/inclusive_language.rb +23 -4
- data/lib/rubocop/cop/naming/method_name.rb +2 -2
- data/lib/rubocop/cop/style/accessor_grouping.rb +4 -4
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_equality_comparison.rb +42 -9
- data/lib/rubocop/cop/style/collection_compact.rb +3 -0
- data/lib/rubocop/cop/style/copyright.rb +1 -1
- data/lib/rubocop/cop/style/data_inheritance.rb +75 -0
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
- data/lib/rubocop/cop/style/double_negation.rb +2 -2
- data/lib/rubocop/cop/style/file_empty.rb +3 -3
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/hash_except.rb +4 -4
- data/lib/rubocop/cop/style/hash_syntax.rb +4 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +38 -12
- data/lib/rubocop/cop/style/map_to_hash.rb +4 -1
- data/lib/rubocop/cop/style/map_to_set.rb +4 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +3 -7
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +43 -36
- data/lib/rubocop/cop/style/multiline_method_signature.rb +6 -3
- data/lib/rubocop/cop/style/parallel_assignment.rb +26 -18
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -3
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +6 -4
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +179 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +1 -1
- data/lib/rubocop/cop/style/redundant_string_escape.rb +2 -3
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -2
- data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
- data/lib/rubocop/cop/style/trailing_body_on_class.rb +1 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unless_logical_operators.rb +1 -0
- data/lib/rubocop/cops_documentation_generator.rb +10 -3
- data/lib/rubocop/ext/regexp_node.rb +1 -1
- data/lib/rubocop/ext/regexp_parser.rb +1 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
- data/lib/rubocop/options.rb +4 -1
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/rspec/support.rb +1 -0
- data/lib/rubocop/server/cache.rb +1 -1
- data/lib/rubocop/server/helper.rb +1 -1
- data/lib/rubocop/server/server_command/exec.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +3 -0
- metadata +12 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67accbc10c4c2ae80033ab2a3713fce8a190d772234cbd52c5584e5c69895915
|
4
|
+
data.tar.gz: 69e7f99ee66c192619b2dc26df6d0ac73c794bb088bade8182f3eceb1cb8d8c0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21005d0d16a207a998640686ee7dcf251683899ab8e746baa861888fd26f2e58791733b48869a085b1b365ce2b67e64b216b4cfe6c1a2e26a2ecc699da0ea085
|
7
|
+
data.tar.gz: 461a588bb95269c194efee4ee8570c166ce009695664aa31181f1f0806220f451e4685d4feff8751d4135f196d3cb42ec100c171b905526e06ed968cd9bb47f9
|
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.50', 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
@@ -1016,8 +1016,6 @@ Layout/LineContinuationLeadingSpace:
|
|
1016
1016
|
Layout/LineContinuationSpacing:
|
1017
1017
|
Description: 'Checks the spacing in front of backslash in line continuations.'
|
1018
1018
|
Enabled: pending
|
1019
|
-
AutoCorrect: true
|
1020
|
-
SafeAutoCorrect: true
|
1021
1019
|
VersionAdded: '1.31'
|
1022
1020
|
EnforcedStyle: space
|
1023
1021
|
SupportedStyles:
|
@@ -1659,6 +1657,7 @@ Lint/Debugger:
|
|
1659
1657
|
- Kernel.binding.remote_pry
|
1660
1658
|
- Kernel.binding.pry_remote
|
1661
1659
|
- Pry.rescue
|
1660
|
+
- pry
|
1662
1661
|
Rails:
|
1663
1662
|
- debugger
|
1664
1663
|
- Kernel.debugger
|
@@ -1751,6 +1750,11 @@ Lint/DuplicateMagicComment:
|
|
1751
1750
|
Enabled: pending
|
1752
1751
|
VersionAdded: '1.37'
|
1753
1752
|
|
1753
|
+
Lint/DuplicateMatchPattern:
|
1754
|
+
Description: 'Do not repeat patterns in `in` keywords.'
|
1755
|
+
Enabled: pending
|
1756
|
+
VersionAdded: '1.50'
|
1757
|
+
|
1754
1758
|
Lint/DuplicateMethods:
|
1755
1759
|
Description: 'Check for duplicate method definitions.'
|
1756
1760
|
Enabled: true
|
@@ -2778,7 +2782,7 @@ Naming/InclusiveLanguage:
|
|
2778
2782
|
Description: 'Recommend the use of inclusive language instead of problematic terms.'
|
2779
2783
|
Enabled: false
|
2780
2784
|
VersionAdded: '1.18'
|
2781
|
-
VersionChanged: '1.
|
2785
|
+
VersionChanged: '1.49'
|
2782
2786
|
CheckIdentifiers: true
|
2783
2787
|
CheckConstants: true
|
2784
2788
|
CheckVariables: true
|
@@ -3276,7 +3280,7 @@ Style/ClassAndModuleChildren:
|
|
3276
3280
|
StyleGuide: '#namespace-definition'
|
3277
3281
|
# Moving from compact to nested children requires knowledge of whether the
|
3278
3282
|
# outer parent is a module or a class. Moving from nested to compact requires
|
3279
|
-
# verification that the outer parent is defined elsewhere.
|
3283
|
+
# verification that the outer parent is defined elsewhere. RuboCop does not
|
3280
3284
|
# have the knowledge to perform either operation safely and thus requires
|
3281
3285
|
# manual oversight.
|
3282
3286
|
SafeAutoCorrect: false
|
@@ -3507,6 +3511,12 @@ Style/Copyright:
|
|
3507
3511
|
Notice: '^Copyright (\(c\) )?2[0-9]{3} .+'
|
3508
3512
|
AutocorrectNotice: ''
|
3509
3513
|
|
3514
|
+
Style/DataInheritance:
|
3515
|
+
Description: 'Checks for inheritance from Data.define.'
|
3516
|
+
StyleGuide: '#no-extend-data-define'
|
3517
|
+
Enabled: pending
|
3518
|
+
VersionAdded: '1.49'
|
3519
|
+
|
3510
3520
|
Style/DateTime:
|
3511
3521
|
Description: 'Use Time over DateTime.'
|
3512
3522
|
StyleGuide: '#date-time'
|
@@ -3736,7 +3746,6 @@ Style/FileEmpty:
|
|
3736
3746
|
Prefer to use `File.empty?('path/to/file')` when checking if a file is empty.
|
3737
3747
|
Enabled: pending
|
3738
3748
|
Safe: false
|
3739
|
-
SafeAutoCorrect: false
|
3740
3749
|
VersionAdded: '1.48'
|
3741
3750
|
|
3742
3751
|
Style/FileRead:
|
@@ -4058,7 +4067,9 @@ Style/InverseMethods:
|
|
4058
4067
|
Style/InvertibleUnlessCondition:
|
4059
4068
|
Description: 'Favor `if` with inverted condition over `unless`.'
|
4060
4069
|
Enabled: false
|
4070
|
+
Safe: false
|
4061
4071
|
VersionAdded: '1.44'
|
4072
|
+
VersionChanged: '1.50'
|
4062
4073
|
# `InverseMethods` are methods that can be inverted in a `unless` condition.
|
4063
4074
|
# The relationship of inverse methods needs to be defined in both directions.
|
4064
4075
|
# Keys and values both need to be defined as symbols.
|
@@ -4564,11 +4575,10 @@ Style/NumericPredicate:
|
|
4564
4575
|
Checks for the use of predicate- or comparison methods for
|
4565
4576
|
numeric comparisons.
|
4566
4577
|
StyleGuide: '#predicate-methods'
|
4567
|
-
Safe: false
|
4568
4578
|
# This will change to a new method call which isn't guaranteed to be on the
|
4569
4579
|
# object. Switching these methods has to be done with knowledge of the types
|
4570
4580
|
# of the variables which rubocop doesn't have.
|
4571
|
-
|
4581
|
+
Safe: false
|
4572
4582
|
Enabled: true
|
4573
4583
|
VersionAdded: '0.42'
|
4574
4584
|
VersionChanged: '0.59'
|
@@ -4877,6 +4887,11 @@ Style/RedundantInterpolation:
|
|
4877
4887
|
VersionAdded: '0.76'
|
4878
4888
|
VersionChanged: '1.30'
|
4879
4889
|
|
4890
|
+
Style/RedundantLineContinuation:
|
4891
|
+
Description: 'Check for redundant line continuation.'
|
4892
|
+
Enabled: pending
|
4893
|
+
VersionAdded: '1.49'
|
4894
|
+
|
4880
4895
|
Style/RedundantParentheses:
|
4881
4896
|
Description: "Checks for parentheses that seem not to serve any purpose."
|
4882
4897
|
Enabled: true
|
@@ -76,13 +76,18 @@ module RuboCop
|
|
76
76
|
warn <<~WARNING
|
77
77
|
Errors are usually caused by RuboCop bugs.
|
78
78
|
Please, report your problems to RuboCop's issue tracker.
|
79
|
-
#{
|
80
|
-
|
79
|
+
#{bug_tracker_uri}
|
81
80
|
Mention the following information in the issue report:
|
82
81
|
#{RuboCop::Version.version(debug: true)}
|
83
82
|
WARNING
|
84
83
|
end
|
85
84
|
|
85
|
+
def bug_tracker_uri
|
86
|
+
return unless Gem.loaded_specs.key?('rubocop')
|
87
|
+
|
88
|
+
"#{Gem.loaded_specs['rubocop'].metadata['bug_tracker_uri']}\n"
|
89
|
+
end
|
90
|
+
|
86
91
|
def maybe_print_corrected_source
|
87
92
|
# Integration tools (like RubyMine) expect to have only the JSON result
|
88
93
|
# when specifying JSON format. Similar HTML and JUnit are targeted as well.
|
data/lib/rubocop/cli.rb
CHANGED
@@ -87,20 +87,20 @@ module RuboCop
|
|
87
87
|
|
88
88
|
tmp_dir = File.join(ConfigFinder.project_root, 'tmp')
|
89
89
|
FileUtils.mkdir_p(tmp_dir)
|
90
|
+
cpu_profile_file = File.join(tmp_dir, 'rubocop-stackprof.dump')
|
90
91
|
status = nil
|
91
92
|
|
92
|
-
StackProf.run(out:
|
93
|
+
StackProf.run(out: cpu_profile_file) do
|
93
94
|
status = yield
|
94
95
|
end
|
95
|
-
puts
|
96
|
+
puts "Profile report generated at #{cpu_profile_file}"
|
96
97
|
|
97
98
|
if with_memory
|
98
99
|
puts 'Building memory report...'
|
99
100
|
report = MemoryProfiler.stop
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
)
|
101
|
+
memory_profile_file = File.join(tmp_dir, 'rubocop-memory_profiler.txt')
|
102
|
+
report.pretty_print(to_file: memory_profile_file, scale_bytes: true)
|
103
|
+
puts "Memory report generated at #{memory_profile_file}"
|
104
104
|
end
|
105
105
|
status
|
106
106
|
end
|
data/lib/rubocop/config.rb
CHANGED
@@ -29,7 +29,7 @@ module RuboCop
|
|
29
29
|
end
|
30
30
|
|
31
31
|
# rubocop:disable Metrics/AbcSize
|
32
|
-
def initialize(hash =
|
32
|
+
def initialize(hash = RuboCop::ConfigLoader.default_configuration, loaded_path = nil)
|
33
33
|
@loaded_path = loaded_path
|
34
34
|
@for_cop = Hash.new do |h, cop|
|
35
35
|
cop_name = cop.respond_to?(:cop_name) ? cop.cop_name : cop
|
@@ -304,8 +304,8 @@ module RuboCop
|
|
304
304
|
end
|
305
305
|
|
306
306
|
def enable_cop?(qualified_cop_name, cop_options)
|
307
|
-
# If the cop is explicitly enabled
|
308
|
-
return true if cop_options['Enabled'] == true
|
307
|
+
# If the cop is explicitly enabled or `Lint/Syntax`, the other checks can be skipped.
|
308
|
+
return true if cop_options['Enabled'] == true || qualified_cop_name == 'Lint/Syntax'
|
309
309
|
|
310
310
|
department = department_of(qualified_cop_name)
|
311
311
|
cop_enabled = cop_options.fetch('Enabled') { !for_all_cops['DisabledByDefault'] }
|
@@ -23,6 +23,14 @@ module RuboCop
|
|
23
23
|
class << self
|
24
24
|
include FileFinder
|
25
25
|
|
26
|
+
PENDING_BANNER = <<~BANNER
|
27
|
+
The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.
|
28
|
+
|
29
|
+
Please also note that you can opt-in to new cops by default by adding this to your config:
|
30
|
+
AllCops:
|
31
|
+
NewCops: enable
|
32
|
+
BANNER
|
33
|
+
|
26
34
|
attr_accessor :debug, :ignore_parent_exclusion, :disable_pending_cops, :enable_pending_cops,
|
27
35
|
:ignore_unrecognized_cops
|
28
36
|
attr_writer :default_configuration
|
@@ -165,14 +173,6 @@ module RuboCop
|
|
165
173
|
ConfigFinder.project_root
|
166
174
|
end
|
167
175
|
|
168
|
-
PENDING_BANNER = <<~BANNER
|
169
|
-
The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.
|
170
|
-
|
171
|
-
Please also note that you can opt-in to new cops by default by adding this to your config:
|
172
|
-
AllCops:
|
173
|
-
NewCops: enable
|
174
|
-
BANNER
|
175
|
-
|
176
176
|
def warn_on_pending_cops(pending_cops)
|
177
177
|
warn Rainbow(PENDING_BANNER).yellow
|
178
178
|
|
@@ -35,7 +35,7 @@ module RuboCop
|
|
35
35
|
return false if cop_config['AutoCorrect'] == false
|
36
36
|
|
37
37
|
# :safe_autocorrect is a derived option based on several command-line
|
38
|
-
# arguments - see
|
38
|
+
# arguments - see RuboCop::Options#add_autocorrection_options
|
39
39
|
return safe_autocorrect? if @options.fetch(:safe_autocorrect, false)
|
40
40
|
|
41
41
|
true
|
@@ -43,18 +43,24 @@ module RuboCop
|
|
43
43
|
|
44
44
|
private
|
45
45
|
|
46
|
-
def disable_offense(
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
def disable_offense(offense_range)
|
47
|
+
range = surrounding_heredoc(offense_range) || surrounding_percent_array(offense_range)
|
48
|
+
|
49
|
+
if range
|
50
|
+
disable_offense_before_and_after(range_by_lines(range))
|
51
|
+
else
|
52
|
+
disable_offense_with_eol_or_surround_comment(offense_range)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def disable_offense_with_eol_or_surround_comment(range)
|
57
|
+
eol_comment = " # rubocop:todo #{cop_name}"
|
58
|
+
needed_line_length = (range.source_line + eol_comment).length
|
59
|
+
|
60
|
+
if needed_line_length <= max_line_length
|
61
|
+
disable_offense_at_end_of_line(range_of_first_line(range), eol_comment)
|
50
62
|
else
|
51
|
-
|
52
|
-
needed_line_length = (range.source_line + eol_comment).length
|
53
|
-
if needed_line_length <= max_line_length
|
54
|
-
disable_offense_at_end_of_line(range_of_first_line(range), eol_comment)
|
55
|
-
else
|
56
|
-
disable_offense_before_and_after(range_by_lines(range))
|
57
|
-
end
|
63
|
+
disable_offense_before_and_after(range_by_lines(range))
|
58
64
|
end
|
59
65
|
end
|
60
66
|
|
@@ -69,6 +75,16 @@ module RuboCop
|
|
69
75
|
.find { |range| range.contains?(offense_range) }
|
70
76
|
end
|
71
77
|
|
78
|
+
def surrounding_percent_array(offense_range)
|
79
|
+
return nil if offense_range.empty?
|
80
|
+
|
81
|
+
percent_array = processed_source.ast.each_descendant.select do |node|
|
82
|
+
node.array_type? && node.percent_literal?
|
83
|
+
end
|
84
|
+
|
85
|
+
percent_array.map(&:source_range).find { |range| range.overlaps?(offense_range) }
|
86
|
+
end
|
87
|
+
|
72
88
|
def range_of_first_line(range)
|
73
89
|
begin_of_first_line = range.begin_pos - range.column
|
74
90
|
end_of_first_line = begin_of_first_line + range.source_line.length
|
data/lib/rubocop/cop/cop.rb
CHANGED
@@ -138,10 +138,10 @@ module RuboCop
|
|
138
138
|
def correction_lambda
|
139
139
|
return unless support_autocorrect?
|
140
140
|
|
141
|
-
|
141
|
+
dedupe_on_node(@v0_argument) { autocorrect(@v0_argument) }
|
142
142
|
end
|
143
143
|
|
144
|
-
def
|
144
|
+
def dedupe_on_node(node)
|
145
145
|
@corrected_nodes ||= {}.compare_by_identity
|
146
146
|
yield unless @corrected_nodes.key?(node)
|
147
147
|
ensure
|
@@ -66,10 +66,10 @@ module RuboCop
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
def line_breaks(node, source, previous_line_num, base_line_num,
|
69
|
+
def line_breaks(node, source, previous_line_num, base_line_num, node_index)
|
70
70
|
source_in_lines = source.split("\n")
|
71
71
|
if first_line?(node, previous_line_num)
|
72
|
-
|
72
|
+
node_index.zero? && node.first_line == base_line_num ? '' : ' '
|
73
73
|
else
|
74
74
|
process_lines(node, previous_line_num, base_line_num, source_in_lines)
|
75
75
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Gemspec
|
6
|
-
# Checks that deprecated
|
6
|
+
# Checks that deprecated attributes are not set in a gemspec file.
|
7
7
|
# Removing deprecated attributes allows the user to receive smaller packed gems.
|
8
8
|
#
|
9
9
|
# @example
|
@@ -24,7 +24,7 @@ module RuboCop
|
|
24
24
|
MSG = 'Description should be started with %<suggestion>s instead of `This cop ...`.'
|
25
25
|
|
26
26
|
SPECIAL_WORDS = %w[is can could should will would must may].freeze
|
27
|
-
COP_DESC_OFFENSE_REGEX =
|
27
|
+
COP_DESC_OFFENSE_REGEX =
|
28
28
|
/^\s+# This cop (?<special>#{SPECIAL_WORDS.join('|')})?\s*(?<word>.+?) .*/.freeze
|
29
29
|
REPLACEMENT_REGEX = /^\s+# This cop (#{SPECIAL_WORDS.join('|')})?\s*(.+?) /.freeze
|
30
30
|
|
@@ -52,7 +52,7 @@ module RuboCop
|
|
52
52
|
# @return [void]
|
53
53
|
def autocorrect(corrector, node)
|
54
54
|
[
|
55
|
-
|
55
|
+
heredoc_opening_delimiter_range_from(node),
|
56
56
|
heredoc_closing_delimiter_range_from(node)
|
57
57
|
].each do |range|
|
58
58
|
corrector.replace(range, EXPECTED_HEREDOC_DELIMITER)
|
@@ -90,7 +90,7 @@ module RuboCop
|
|
90
90
|
|
91
91
|
# @param node [RuboCop::AST::StrNode]
|
92
92
|
# @return [Parser::Source::Range]
|
93
|
-
def
|
93
|
+
def heredoc_opening_delimiter_range_from(node)
|
94
94
|
match_data = node.source.match(Heredoc::OPENING_DELIMITER)
|
95
95
|
node.source_range.begin.adjust(
|
96
96
|
begin_pos: match_data.begin(2),
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module InternalAffairs
|
6
|
-
# `RuboCop::Cop::Cop` is deprecated and will be removed in
|
6
|
+
# `RuboCop::Cop::Cop` is deprecated and will be removed in RuboCop 2.0.
|
7
7
|
# Your custom cop class should inherit from `RuboCop::Cop::Base` instead of
|
8
8
|
# `RuboCop::Cop::Cop`.
|
9
9
|
#
|
@@ -13,19 +13,46 @@ module RuboCop
|
|
13
13
|
# # good
|
14
14
|
# node.source
|
15
15
|
#
|
16
|
+
# # bad
|
17
|
+
# add_offense(node) { |corrector| corrector.replace(node.source_range, prefer) }
|
18
|
+
# add_offense(node) { |corrector| corrector.insert_before(node.source_range, prefer) }
|
19
|
+
# add_offense(node) { |corrector| corrector.insert_before_multi(node.source_range, prefer) }
|
20
|
+
# add_offense(node) { |corrector| corrector.insert_after(node.source_range, prefer) }
|
21
|
+
# add_offense(node) { |corrector| corrector.insert_after_multi(node.source_range, prefer) }
|
22
|
+
# add_offense(node) { |corrector| corrector.swap(node.source_range, before, after) }
|
23
|
+
#
|
24
|
+
# # good
|
25
|
+
# add_offense(node) { |corrector| corrector.replace(node, prefer) }
|
26
|
+
# add_offense(node) { |corrector| corrector.insert_before(node, prefer) }
|
27
|
+
# add_offense(node) { |corrector| corrector.insert_before_multi(node, prefer) }
|
28
|
+
# add_offense(node) { |corrector| corrector.insert_after(node, prefer) }
|
29
|
+
# add_offense(node) { |corrector| corrector.insert_after_multi(node, prefer) }
|
30
|
+
# add_offense(node) { |corrector| corrector.swap(node, before, after) }
|
31
|
+
#
|
16
32
|
class RedundantSourceRange < Base
|
17
33
|
extend AutoCorrector
|
18
34
|
|
19
35
|
MSG = 'Remove the redundant `source_range`.'
|
20
|
-
RESTRICT_ON_SEND = %i[
|
36
|
+
RESTRICT_ON_SEND = %i[
|
37
|
+
source
|
38
|
+
replace remove insert_before insert_before_multi insert_after insert_after_multi swap
|
39
|
+
].freeze
|
21
40
|
|
22
41
|
# @!method redundant_source_range(node)
|
23
42
|
def_node_matcher :redundant_source_range, <<~PATTERN
|
24
|
-
|
43
|
+
{
|
44
|
+
(send $(send _ :source_range) :source)
|
45
|
+
(send _ {
|
46
|
+
:replace :insert_before :insert_before_multi :insert_after :insert_after_multi
|
47
|
+
} $(send _ :source_range) _)
|
48
|
+
(send _ :remove $(send _ :source_range))
|
49
|
+
(send _ :swap $(send _ :source_range) _ _)
|
50
|
+
}
|
25
51
|
PATTERN
|
26
52
|
|
27
53
|
def on_send(node)
|
28
54
|
return unless (source_range = redundant_source_range(node))
|
55
|
+
return if source_range.receiver.send_type? && source_range.receiver.method?(:buffer)
|
29
56
|
|
30
57
|
selector = source_range.loc.selector
|
31
58
|
|
@@ -36,17 +36,19 @@ module RuboCop
|
|
36
36
|
# If the end is on its own line, there is no offense
|
37
37
|
return if begins_its_line?(node.loc.end)
|
38
38
|
|
39
|
-
|
39
|
+
offense_range = offense_range(node)
|
40
|
+
return if offense_range.source.lstrip.start_with?(';')
|
41
|
+
|
42
|
+
register_offense(node, offense_range)
|
40
43
|
end
|
41
44
|
|
42
45
|
alias on_numblock on_block
|
43
46
|
|
44
47
|
private
|
45
48
|
|
46
|
-
def register_offense(node)
|
49
|
+
def register_offense(node, offense_range)
|
47
50
|
add_offense(node.loc.end, message: message(node)) do |corrector|
|
48
|
-
|
49
|
-
replacement = replacement(node)
|
51
|
+
replacement = "\n#{offense_range.source.lstrip}"
|
50
52
|
|
51
53
|
if (heredoc = last_heredoc_argument(node.body))
|
52
54
|
corrector.remove(offense_range)
|
@@ -72,23 +74,7 @@ module RuboCop
|
|
72
74
|
end
|
73
75
|
|
74
76
|
def offense_range(node)
|
75
|
-
|
76
|
-
node.source_range.source_buffer,
|
77
|
-
node.children.compact.last.source_range.end_pos,
|
78
|
-
end_of_method_chain(node).source_range.end_pos
|
79
|
-
)
|
80
|
-
end
|
81
|
-
|
82
|
-
def replacement(node)
|
83
|
-
end_with_method_chain = node.loc.end.join(end_of_method_chain(node).source_range.end)
|
84
|
-
|
85
|
-
"\n#{end_with_method_chain.source.strip}"
|
86
|
-
end
|
87
|
-
|
88
|
-
def end_of_method_chain(node)
|
89
|
-
return node unless node.parent&.call_type?
|
90
|
-
|
91
|
-
end_of_method_chain(node.parent)
|
77
|
+
node.children.compact.last.source_range.end.join(node.loc.end)
|
92
78
|
end
|
93
79
|
end
|
94
80
|
end
|
@@ -137,7 +137,7 @@ module RuboCop
|
|
137
137
|
end
|
138
138
|
|
139
139
|
def current_token(comment)
|
140
|
-
processed_source.
|
140
|
+
processed_source.tokens.find { |token| token.pos == comment.source_range }
|
141
141
|
end
|
142
142
|
|
143
143
|
def previous_token(node)
|
@@ -31,7 +31,7 @@ module RuboCop
|
|
31
31
|
return unless processed_source.raw_source.include?("\n\n\n")
|
32
32
|
|
33
33
|
lines = Set.new
|
34
|
-
processed_source.
|
34
|
+
processed_source.tokens.each { |token| lines << token.line }
|
35
35
|
|
36
36
|
each_extra_empty_line(lines.sort) do |range|
|
37
37
|
add_offense(range) do |corrector|
|
@@ -51,6 +51,8 @@ module RuboCop
|
|
51
51
|
MSG_BEFORE_FOR_ONLY_BEFORE = 'Keep a blank line before `%<modifier>s`.'
|
52
52
|
MSG_AFTER_FOR_ONLY_BEFORE = 'Remove a blank line after `%<modifier>s`.'
|
53
53
|
|
54
|
+
RESTRICT_ON_SEND = %i[public protected private module_function].freeze
|
55
|
+
|
54
56
|
def initialize(config = nil, options = nil)
|
55
57
|
super
|
56
58
|
|
@@ -109,6 +109,7 @@ module RuboCop
|
|
109
109
|
check_other_alignment(node)
|
110
110
|
end
|
111
111
|
end
|
112
|
+
alias on_case_match on_case
|
112
113
|
|
113
114
|
private
|
114
115
|
|
@@ -169,7 +170,10 @@ module RuboCop
|
|
169
170
|
end
|
170
171
|
|
171
172
|
def alignment_node_for_variable_style(node)
|
172
|
-
|
173
|
+
if (node.case_type? || node.case_match_type?) && node.argument? &&
|
174
|
+
same_line?(node, node.parent)
|
175
|
+
return node.parent
|
176
|
+
end
|
173
177
|
|
174
178
|
assignment = assignment_or_operator_method(node)
|
175
179
|
|
@@ -49,7 +49,7 @@ module RuboCop
|
|
49
49
|
|
50
50
|
private
|
51
51
|
|
52
|
-
def aligned_locations(locs)
|
52
|
+
def aligned_locations(locs) # rubocop:disable Metrics/AbcSize
|
53
53
|
return [] if locs.empty?
|
54
54
|
|
55
55
|
aligned = Set[locs.first.line, locs.last.line]
|
@@ -57,6 +57,11 @@ module RuboCop
|
|
57
57
|
col = loc.column
|
58
58
|
aligned << loc.line if col == before.column || col == after.column
|
59
59
|
end
|
60
|
+
|
61
|
+
# if locs.size > 2 and the size of variable `aligned`
|
62
|
+
# has not increased from its initial value, there are not aligned lines.
|
63
|
+
return [] if locs.size > 2 && aligned.size == 2
|
64
|
+
|
60
65
|
aligned
|
61
66
|
end
|
62
67
|
|
@@ -153,9 +153,10 @@ module RuboCop
|
|
153
153
|
MSG = 'Indent the first argument one step more than %<base>s.'
|
154
154
|
|
155
155
|
def on_send(node)
|
156
|
+
return unless should_check?(node)
|
157
|
+
return if same_line?(node, node.first_argument)
|
156
158
|
return if style != :consistent && enforce_first_argument_with_fixed_indentation? &&
|
157
159
|
!enable_layout_first_method_argument_line_break?
|
158
|
-
return if !node.arguments? || bare_operator?(node) || node.setter_method?
|
159
160
|
|
160
161
|
indent = base_indentation(node) + configured_indentation_width
|
161
162
|
|
@@ -166,6 +167,10 @@ module RuboCop
|
|
166
167
|
|
167
168
|
private
|
168
169
|
|
170
|
+
def should_check?(node)
|
171
|
+
node.arguments? && !bare_operator?(node) && !node.setter_method?
|
172
|
+
end
|
173
|
+
|
169
174
|
def autocorrect(corrector, node)
|
170
175
|
AlignmentCorrector.correct(corrector, processed_source, node, column_delta)
|
171
176
|
end
|
@@ -6,49 +6,40 @@ module RuboCop
|
|
6
6
|
# Checks for a line break before the first element in a
|
7
7
|
# multi-line array.
|
8
8
|
#
|
9
|
-
# @example
|
9
|
+
# @example
|
10
10
|
#
|
11
|
-
#
|
12
|
-
#
|
13
|
-
#
|
11
|
+
# # bad
|
12
|
+
# [ :a,
|
13
|
+
# :b]
|
14
14
|
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
15
|
+
# # good
|
16
|
+
# [
|
17
|
+
# :a,
|
18
|
+
# :b]
|
19
19
|
#
|
20
|
-
#
|
21
|
-
#
|
20
|
+
# # good
|
21
|
+
# [:a, :b]
|
22
22
|
#
|
23
|
-
#
|
24
|
-
# [
|
25
|
-
# :a,
|
26
|
-
# :b]
|
23
|
+
# @example AllowMultilineFinalElement: false (default)
|
27
24
|
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
# }]
|
25
|
+
# # bad
|
26
|
+
# [ :a, {
|
27
|
+
# :b => :c
|
28
|
+
# }]
|
33
29
|
#
|
34
|
-
#
|
30
|
+
# # good
|
31
|
+
# [
|
32
|
+
# :a, {
|
33
|
+
# :b => :c
|
34
|
+
# }]
|
35
35
|
#
|
36
|
-
#
|
37
|
-
# [ :a,
|
38
|
-
# :b]
|
39
|
-
#
|
40
|
-
# # good
|
41
|
-
# [ :a, {
|
42
|
-
# :b => :c
|
43
|
-
# }]
|
36
|
+
# @example AllowMultilineFinalElement: true
|
44
37
|
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
#
|
38
|
+
# # good
|
39
|
+
# [:a, {
|
40
|
+
# :b => :c
|
41
|
+
# }]
|
49
42
|
#
|
50
|
-
# # good
|
51
|
-
# [:a, :b]
|
52
43
|
class FirstArrayElementLineBreak < Base
|
53
44
|
include FirstElementLineBreak
|
54
45
|
extend AutoCorrector
|