rubocop 0.85.0 → 0.88.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +25 -17
- data/bin/rubocop-profile +31 -0
- data/config/default.yml +132 -11
- data/lib/rubocop.rb +17 -1
- data/lib/rubocop/cli.rb +2 -4
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +42 -7
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/cli/command/show_cops.rb +1 -1
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/config_loader.rb +39 -67
- data/lib/rubocop/config_loader_resolver.rb +1 -1
- data/lib/rubocop/config_obsoletion.rb +0 -1
- data/lib/rubocop/config_store.rb +4 -0
- data/lib/rubocop/cop/autocorrect_logic.rb +14 -24
- data/lib/rubocop/cop/badge.rb +1 -1
- data/lib/rubocop/cop/base.rb +407 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
- data/lib/rubocop/cop/commissioner.rb +48 -50
- data/lib/rubocop/cop/cop.rb +91 -235
- data/lib/rubocop/cop/corrector.rb +38 -115
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +26 -0
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +7 -2
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/generator.rb +1 -1
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
- data/lib/rubocop/cop/layout/case_indentation.rb +18 -19
- data/lib/rubocop/cop/layout/class_structure.rb +2 -37
- data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +1 -0
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +3 -8
- data/lib/rubocop/cop/layout/end_alignment.rb +3 -2
- data/lib/rubocop/cop/layout/end_of_line.rb +1 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +5 -1
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +2 -3
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +17 -7
- data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +22 -27
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +27 -68
- data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +4 -3
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
- data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
- data/lib/rubocop/cop/legacy/corrector.rb +29 -0
- data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -4
- data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +8 -2
- data/lib/rubocop/cop/lint/duplicate_elsif_condition.rb +39 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -2
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +3 -2
- data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
- data/lib/rubocop/cop/lint/literal_as_condition.rb +11 -1
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +8 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +14 -20
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +69 -2
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -3
- data/lib/rubocop/cop/lint/percent_string_array.rb +1 -1
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
- data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
- data/lib/rubocop/cop/lint/rand_one.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +31 -25
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +9 -1
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/syntax.rb +11 -26
- data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +1 -1
- data/lib/rubocop/cop/metrics/block_length.rb +22 -0
- data/lib/rubocop/cop/metrics/class_length.rb +25 -2
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
- data/lib/rubocop/cop/metrics/method_length.rb +23 -0
- data/lib/rubocop/cop/metrics/module_length.rb +25 -2
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
- data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
- data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
- data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
- data/lib/rubocop/cop/mixin/code_length.rb +4 -0
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -1
- data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -1
- data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +1 -1
- data/lib/rubocop/cop/mixin/regexp_literal_help.rb +27 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -3
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -5
- data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +6 -4
- data/lib/rubocop/cop/mixin/visibility_help.rb +50 -0
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +3 -3
- data/lib/rubocop/cop/naming/file_name.rb +1 -3
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +3 -5
- data/lib/rubocop/cop/naming/variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/variable_number.rb +1 -1
- data/lib/rubocop/cop/offense.rb +16 -2
- data/lib/rubocop/cop/registry.rb +62 -7
- data/lib/rubocop/cop/style/accessor_grouping.rb +147 -0
- data/lib/rubocop/cop/style/array_coercion.rb +63 -0
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +3 -2
- data/lib/rubocop/cop/style/bare_percent_literals.rb +1 -1
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +146 -0
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -4
- data/lib/rubocop/cop/style/case_like_if.rb +217 -0
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_vars.rb +21 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -2
- data/lib/rubocop/cop/style/copyright.rb +3 -3
- data/lib/rubocop/cop/style/date_time.rb +1 -1
- data/lib/rubocop/cop/style/dir.rb +2 -2
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +2 -2
- data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
- data/lib/rubocop/cop/style/empty_literal.rb +5 -5
- data/lib/rubocop/cop/style/encoding.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
- data/lib/rubocop/cop/style/exponential_notation.rb +8 -10
- data/lib/rubocop/cop/style/float_division.rb +7 -10
- data/lib/rubocop/cop/style/format_string_token.rb +5 -5
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +62 -0
- data/lib/rubocop/cop/style/hash_like_case.rb +76 -0
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
- data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -11
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
- data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
- data/lib/rubocop/cop/style/missing_else.rb +1 -11
- data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
- data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
- data/lib/rubocop/cop/style/next.rb +2 -2
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
- data/lib/rubocop/cop/style/numeric_predicate.rb +3 -4
- data/lib/rubocop/cop/style/parallel_assignment.rb +3 -3
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
- data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
- data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
- data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +122 -0
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +50 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +8 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +4 -3
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +14 -23
- data/lib/rubocop/cop/style/redundant_self.rb +6 -9
- data/lib/rubocop/cop/style/redundant_sort.rb +3 -2
- data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
- data/lib/rubocop/cop/style/sample.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/signal_exception.rb +1 -1
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +3 -2
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +23 -2
- data/lib/rubocop/cop/style/symbol_array.rb +5 -5
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +9 -32
- data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
- data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
- data/lib/rubocop/cop/team.rb +105 -81
- data/lib/rubocop/cop/util.rb +2 -2
- data/lib/rubocop/cop/utils/format_string.rb +19 -2
- data/lib/rubocop/cop/variable_force/variable.rb +5 -3
- data/lib/rubocop/file_finder.rb +12 -12
- data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -2
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/name_similarity.rb +7 -3
- data/lib/rubocop/options.rb +15 -8
- data/lib/rubocop/path_util.rb +4 -19
- data/lib/rubocop/platform.rb +1 -1
- data/lib/rubocop/rake_task.rb +6 -9
- data/lib/rubocop/result_cache.rb +12 -8
- data/lib/rubocop/rspec/cop_helper.rb +4 -4
- data/lib/rubocop/rspec/expect_offense.rb +65 -21
- data/lib/rubocop/rspec/shared_contexts.rb +19 -16
- data/lib/rubocop/runner.rb +34 -33
- data/lib/rubocop/target_finder.rb +3 -3
- data/lib/rubocop/target_ruby.rb +2 -2
- data/lib/rubocop/version.rb +1 -1
- metadata +34 -9
- data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
@@ -134,7 +134,7 @@ module RuboCop
|
|
134
134
|
|
135
135
|
def autocorrect(range)
|
136
136
|
lambda do |corrector|
|
137
|
-
if range.source
|
137
|
+
if /\*\*/.match?(range.source) && !space_around_exponent_operator?
|
138
138
|
corrector.replace(range, '**')
|
139
139
|
elsif range.source.end_with?("\n")
|
140
140
|
corrector.replace(range, " #{range.source.strip}\n")
|
@@ -27,6 +27,20 @@ module RuboCop
|
|
27
27
|
# foo.map{ |a|
|
28
28
|
# a.bar.to_s
|
29
29
|
# }
|
30
|
+
#
|
31
|
+
# @example EnforcedStyleForEmptyBraces: space (default)
|
32
|
+
# # bad
|
33
|
+
# 7.times{}
|
34
|
+
#
|
35
|
+
# # good
|
36
|
+
# 7.times {}
|
37
|
+
#
|
38
|
+
# @example EnforcedStyleForEmptyBraces: no_space
|
39
|
+
# # bad
|
40
|
+
# 7.times {}
|
41
|
+
#
|
42
|
+
# # good
|
43
|
+
# 7.times{}
|
30
44
|
class SpaceBeforeBlockBraces < Cop
|
31
45
|
include ConfigurableEnforcedStyle
|
32
46
|
include RangeHelp
|
@@ -130,7 +130,7 @@ module RuboCop
|
|
130
130
|
line, col = line_and_column_for(token)
|
131
131
|
return true if col == -1
|
132
132
|
|
133
|
-
processed_source.lines[line][0..col]
|
133
|
+
!/\S/.match?(processed_source.lines[line][0..col])
|
134
134
|
end
|
135
135
|
|
136
136
|
def index_for(node, token)
|
@@ -142,11 +142,12 @@ module RuboCop
|
|
142
142
|
end
|
143
143
|
|
144
144
|
def issue_offenses(node, left, right, start_ok, end_ok)
|
145
|
-
|
145
|
+
case style
|
146
|
+
when :no_space
|
146
147
|
start_ok = next_to_comment?(node, left)
|
147
148
|
no_space_offenses(node, left, right, MSG, start_ok: start_ok,
|
148
149
|
end_ok: end_ok)
|
149
|
-
|
150
|
+
when :space
|
150
151
|
space_offenses(node, left, right, MSG, start_ok: start_ok,
|
151
152
|
end_ok: end_ok)
|
152
153
|
else
|
@@ -150,7 +150,7 @@ module RuboCop
|
|
150
150
|
end
|
151
151
|
|
152
152
|
def check_right_brace(inner, left_brace, right_brace, single_line)
|
153
|
-
if single_line &&
|
153
|
+
if single_line && /\S$/.match?(inner)
|
154
154
|
no_space(right_brace.begin_pos, right_brace.end_pos,
|
155
155
|
'Space missing inside }.')
|
156
156
|
else
|
@@ -182,7 +182,7 @@ module RuboCop
|
|
182
182
|
def range_of_space_to_the_right(range)
|
183
183
|
src = range.source_buffer.source
|
184
184
|
end_pos = range.end_pos
|
185
|
-
end_pos += 1 while
|
185
|
+
end_pos += 1 while /[ \t]/.match?(src[end_pos])
|
186
186
|
|
187
187
|
range_between(range.begin_pos + 1, end_pos)
|
188
188
|
end
|
@@ -190,7 +190,7 @@ module RuboCop
|
|
190
190
|
def range_of_space_to_the_left(range)
|
191
191
|
src = range.source_buffer.source
|
192
192
|
begin_pos = range.begin_pos
|
193
|
-
begin_pos -= 1 while src[begin_pos - 1]
|
193
|
+
begin_pos -= 1 while /[ \t]/.match?(src[begin_pos - 1])
|
194
194
|
|
195
195
|
range_between(begin_pos, range.end_pos - 1)
|
196
196
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Legacy
|
6
|
+
# Legacy support for Corrector#corrections
|
7
|
+
# See https://docs.rubocop.org/rubocop/cop_api_v1_changelog.html
|
8
|
+
class CorrectionsProxy
|
9
|
+
def initialize(corrector)
|
10
|
+
@corrector = corrector
|
11
|
+
end
|
12
|
+
|
13
|
+
def <<(callable)
|
14
|
+
suppress_clobbering do
|
15
|
+
@corrector.transaction do
|
16
|
+
callable.call(@corrector)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def empty?
|
22
|
+
@corrector.empty?
|
23
|
+
end
|
24
|
+
|
25
|
+
def concat(corrections)
|
26
|
+
if corrections.is_a?(CorrectionsProxy)
|
27
|
+
suppress_clobbering do
|
28
|
+
corrector.merge!(corrections.corrector)
|
29
|
+
end
|
30
|
+
else
|
31
|
+
corrections.each { |correction| self << correction }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
attr_reader :corrector
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def suppress_clobbering
|
42
|
+
yield
|
43
|
+
rescue ::Parser::ClobberingError
|
44
|
+
# ignore Clobbering errors
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Legacy
|
6
|
+
# Legacy Corrector for v0 API support.
|
7
|
+
# See https://docs.rubocop.org/rubocop/cop_api_v1_changelog.html
|
8
|
+
class Corrector < RuboCop::Cop::Corrector
|
9
|
+
# Support legacy second argument
|
10
|
+
def initialize(source, corr = [])
|
11
|
+
super(source)
|
12
|
+
if corr.is_a?(CorrectionsProxy)
|
13
|
+
merge!(corr.send(:corrector))
|
14
|
+
else
|
15
|
+
# warn "Corrector.new with corrections is deprecated." unless corr.empty? TODO
|
16
|
+
corr.each do |c|
|
17
|
+
corrections << c
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def corrections
|
23
|
+
# warn "#corrections is deprecated. Open an issue if you have a valid usecase." TODO
|
24
|
+
CorrectionsProxy.new(self)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# Check that certain constants are fully qualified.
|
7
|
+
#
|
8
|
+
# This is not enabled by default because it would mark a lot of offenses
|
9
|
+
# unnecessarily.
|
10
|
+
#
|
11
|
+
# Generally, gems should fully qualify all constants to avoid conflicts with
|
12
|
+
# the code that uses the gem. Enable this cop without using `Only`/`Ignore`
|
13
|
+
#
|
14
|
+
# Large projects will over time end up with one or two constant names that
|
15
|
+
# are problematic because of a conflict with a library or just internally
|
16
|
+
# using the same name a namespace and a class. To avoid too many unnecessary
|
17
|
+
# offenses, Enable this cop with `Only: [The, Constant, Names, Causing, Issues]`
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
# # By default checks every constant
|
21
|
+
#
|
22
|
+
# # bad
|
23
|
+
# User
|
24
|
+
#
|
25
|
+
# # bad
|
26
|
+
# User::Login
|
27
|
+
#
|
28
|
+
# # good
|
29
|
+
# ::User
|
30
|
+
#
|
31
|
+
# # good
|
32
|
+
# ::User::Login
|
33
|
+
#
|
34
|
+
# @example Only: ['Login']
|
35
|
+
# # Restrict this cop to only being concerned about certain constants
|
36
|
+
#
|
37
|
+
# # bad
|
38
|
+
# Login
|
39
|
+
#
|
40
|
+
# # good
|
41
|
+
# ::Login
|
42
|
+
#
|
43
|
+
# # good
|
44
|
+
# User::Login
|
45
|
+
#
|
46
|
+
# @example Ignore: ['Login']
|
47
|
+
# # Restrict this cop not being concerned about certain constants
|
48
|
+
#
|
49
|
+
# # bad
|
50
|
+
# User
|
51
|
+
#
|
52
|
+
# # good
|
53
|
+
# ::User::Login
|
54
|
+
#
|
55
|
+
# # good
|
56
|
+
# Login
|
57
|
+
#
|
58
|
+
class ConstantResolution < Cop
|
59
|
+
MSG = 'Fully qualify this constant to avoid possibly ambiguous resolution.'
|
60
|
+
|
61
|
+
def_node_matcher :unqualified_const?, <<~PATTERN
|
62
|
+
(const nil? #const_name?)
|
63
|
+
PATTERN
|
64
|
+
|
65
|
+
def on_const(node)
|
66
|
+
return unless unqualified_const?(node)
|
67
|
+
|
68
|
+
add_offense(node)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def const_name?(name)
|
74
|
+
name = name.to_s
|
75
|
+
(allowed_names.empty? || allowed_names.include?(name)) &&
|
76
|
+
!ignored_names.include?(name)
|
77
|
+
end
|
78
|
+
|
79
|
+
def allowed_names
|
80
|
+
cop_config['Only']
|
81
|
+
end
|
82
|
+
|
83
|
+
def ignored_names
|
84
|
+
cop_config['Ignore']
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -15,7 +15,7 @@ module RuboCop
|
|
15
15
|
# OpenSSL::Cipher::AES.new(128, :GCM)
|
16
16
|
#
|
17
17
|
# # good
|
18
|
-
# OpenSSL::Cipher.new('
|
18
|
+
# OpenSSL::Cipher.new('aes-128-gcm')
|
19
19
|
#
|
20
20
|
# @example
|
21
21
|
#
|
@@ -127,9 +127,9 @@ module RuboCop
|
|
127
127
|
end
|
128
128
|
|
129
129
|
def build_cipher_arguments(node, algorithm_name)
|
130
|
-
algorithm_parts = algorithm_name.split('-')
|
131
|
-
size_and_mode = sanitize_arguments(node.arguments)
|
132
|
-
"'#{(algorithm_parts + size_and_mode + ['
|
130
|
+
algorithm_parts = algorithm_name.downcase.split('-')
|
131
|
+
size_and_mode = sanitize_arguments(node.arguments).map(&:downcase)
|
132
|
+
"'#{(algorithm_parts + size_and_mode + ['cbc']).take(3).join('-')}'"
|
133
133
|
end
|
134
134
|
end
|
135
135
|
end
|
@@ -22,7 +22,9 @@ module RuboCop
|
|
22
22
|
# def initialize
|
23
23
|
# @x = 1
|
24
24
|
# end
|
25
|
-
class DisjunctiveAssignmentInConstructor <
|
25
|
+
class DisjunctiveAssignmentInConstructor < Base
|
26
|
+
extend AutoCorrector
|
27
|
+
|
26
28
|
MSG = 'Unnecessary disjunctive assignment. Use plain assignment.'
|
27
29
|
|
28
30
|
def on_def(node)
|
@@ -73,7 +75,11 @@ module RuboCop
|
|
73
75
|
# @param [Node] node a disjunctive assignment
|
74
76
|
def check_disjunctive_assignment(node)
|
75
77
|
lhs = node.child_nodes.first
|
76
|
-
|
78
|
+
return unless lhs.ivasgn_type?
|
79
|
+
|
80
|
+
add_offense(node.loc.operator) do |corrector|
|
81
|
+
corrector.replace(node.loc.operator, '=')
|
82
|
+
end
|
77
83
|
end
|
78
84
|
end
|
79
85
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks that there are no repeated conditions used in if 'elsif'.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# if x == 1
|
11
|
+
# do_something
|
12
|
+
# elsif x == 1
|
13
|
+
# do_something_else
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# if x == 1
|
18
|
+
# do_something
|
19
|
+
# elsif x == 2
|
20
|
+
# do_something_else
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
class DuplicateElsifCondition < Base
|
24
|
+
MSG = 'Duplicate `elsif` condition detected.'
|
25
|
+
|
26
|
+
def on_if(node)
|
27
|
+
previous = []
|
28
|
+
while node.if? || node.elsif?
|
29
|
+
condition = node.condition
|
30
|
+
add_offense(condition) if previous.include?(condition)
|
31
|
+
previous << condition
|
32
|
+
node = node.else_branch
|
33
|
+
break unless node&.if_type?
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -126,7 +126,7 @@ module RuboCop
|
|
126
126
|
end
|
127
127
|
|
128
128
|
def message_for_dup(node, method_name)
|
129
|
-
format(MSG, method: method_name, defined: @definitions[method_name],
|
129
|
+
format(MSG, method: method_name, defined: source_location(@definitions[method_name]),
|
130
130
|
current: source_location(node))
|
131
131
|
end
|
132
132
|
|
@@ -152,7 +152,7 @@ module RuboCop
|
|
152
152
|
|
153
153
|
add_offense(node, location: loc, message: message)
|
154
154
|
else
|
155
|
-
@definitions[method_name] =
|
155
|
+
@definitions[method_name] = node
|
156
156
|
end
|
157
157
|
end
|
158
158
|
|
@@ -7,6 +7,10 @@ module RuboCop
|
|
7
7
|
# expected fields for format/sprintf/#% and what is actually
|
8
8
|
# passed as arguments.
|
9
9
|
#
|
10
|
+
# In addition it checks whether different formats are used in the same
|
11
|
+
# format string. Do not mix numbered, unnumbered, and named formats in
|
12
|
+
# the same format string.
|
13
|
+
#
|
10
14
|
# @example
|
11
15
|
#
|
12
16
|
# # bad
|
@@ -18,16 +22,37 @@ module RuboCop
|
|
18
22
|
# # good
|
19
23
|
#
|
20
24
|
# format('A value: %s and another: %i', a_value, another)
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
#
|
28
|
+
# # bad
|
29
|
+
#
|
30
|
+
# format('Unnumbered format: %s and numbered: %2$s', a_value, another)
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
#
|
34
|
+
# # good
|
35
|
+
#
|
36
|
+
# format('Numbered format: %1$s and numbered %2$s', a_value, another)
|
21
37
|
class FormatParameterMismatch < Cop
|
22
38
|
# http://rubular.com/r/CvpbxkcTzy
|
23
39
|
MSG = "Number of arguments (%<arg_num>i) to `%<method>s` doesn't " \
|
24
40
|
'match the number of fields (%<field_num>i).'
|
41
|
+
MSG_INVALID = 'Format string is invalid because formatting sequence types ' \
|
42
|
+
'(numbered, named or unnumbered) are mixed.'
|
25
43
|
|
26
44
|
KERNEL = 'Kernel'
|
27
45
|
SHOVEL = '<<'
|
28
46
|
STRING_TYPES = %i[str dstr].freeze
|
29
47
|
|
30
48
|
def on_send(node)
|
49
|
+
return unless format_string?(node)
|
50
|
+
|
51
|
+
if invalid_format_string?(node)
|
52
|
+
add_offense(node, location: :selector, message: MSG_INVALID)
|
53
|
+
return
|
54
|
+
end
|
55
|
+
|
31
56
|
return unless offending_node?(node)
|
32
57
|
|
33
58
|
add_offense(node, location: :selector)
|
@@ -35,9 +60,20 @@ module RuboCop
|
|
35
60
|
|
36
61
|
private
|
37
62
|
|
63
|
+
def format_string?(node)
|
64
|
+
called_on_string?(node) && method_with_format_args?(node)
|
65
|
+
end
|
66
|
+
|
67
|
+
def invalid_format_string?(node)
|
68
|
+
string = if sprintf?(node) || format?(node)
|
69
|
+
node.first_argument.source
|
70
|
+
else
|
71
|
+
node.receiver.source
|
72
|
+
end
|
73
|
+
!RuboCop::Cop::Utils::FormatString.new(string).valid?
|
74
|
+
end
|
75
|
+
|
38
76
|
def offending_node?(node)
|
39
|
-
return false unless called_on_string?(node)
|
40
|
-
return false unless method_with_format_args?(node)
|
41
77
|
return false if splat_args?(node)
|
42
78
|
|
43
79
|
num_of_format_args, num_of_expected_fields = count_matches(node)
|