rubocop 0.52.1 → 0.53.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +2 -2
- data/config/default.yml +118 -46
- data/config/disabled.yml +8 -8
- data/config/enabled.yml +84 -28
- data/lib/rubocop.rb +28 -8
- data/lib/rubocop/ast/builder.rb +35 -37
- data/lib/rubocop/ast/node.rb +16 -1
- data/lib/rubocop/ast/node/and_node.rb +0 -8
- data/lib/rubocop/ast/node/block_node.rb +1 -9
- data/lib/rubocop/ast/node/case_node.rb +0 -8
- data/lib/rubocop/ast/node/ensure_node.rb +0 -8
- data/lib/rubocop/ast/node/for_node.rb +0 -8
- data/lib/rubocop/ast/node/or_node.rb +0 -8
- data/lib/rubocop/ast/node/pair_node.rb +0 -8
- data/lib/rubocop/ast/node/resbody_node.rb +0 -8
- data/lib/rubocop/ast/node/send_node.rb +0 -8
- data/lib/rubocop/ast/node/symbol_node.rb +0 -8
- data/lib/rubocop/ast/node/until_node.rb +0 -8
- data/lib/rubocop/ast/node/when_node.rb +0 -8
- data/lib/rubocop/ast/node/while_node.rb +0 -8
- data/lib/rubocop/cli.rb +17 -7
- data/lib/rubocop/comment_config.rb +24 -3
- data/lib/rubocop/config.rb +75 -6
- data/lib/rubocop/config_loader.rb +18 -28
- data/lib/rubocop/config_loader_resolver.rb +61 -9
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +3 -1
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +4 -2
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
- data/lib/rubocop/cop/commissioner.rb +2 -2
- data/lib/rubocop/cop/cop.rb +4 -0
- data/lib/rubocop/cop/corrector.rb +11 -1
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +59 -0
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/space_corrector.rb +13 -0
- data/lib/rubocop/cop/correctors/unused_arg_corrector.rb +1 -1
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +3 -1
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +3 -5
- data/lib/rubocop/cop/generator.rb +29 -8
- data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +2 -0
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +2 -0
- data/lib/rubocop/cop/layout/align_hash.rb +106 -37
- data/lib/rubocop/cop/{lint → layout}/block_alignment.rb +8 -5
- data/lib/rubocop/cop/layout/block_end_newline.rb +7 -17
- data/lib/rubocop/cop/layout/case_indentation.rb +1 -0
- data/lib/rubocop/cop/layout/class_structure.rb +6 -7
- data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
- data/lib/rubocop/cop/{lint → layout}/condition_position.rb +3 -3
- data/lib/rubocop/cop/{lint → layout}/def_end_alignment.rb +2 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_comment.rb +140 -0
- data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -0
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -0
- data/lib/rubocop/cop/layout/empty_lines.rb +3 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +7 -5
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +20 -10
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +20 -0
- data/lib/rubocop/cop/{lint → layout}/end_alignment.rb +37 -6
- data/lib/rubocop/cop/layout/end_of_line.rb +1 -0
- data/lib/rubocop/cop/layout/extra_spacing.rb +30 -37
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -0
- data/lib/rubocop/cop/layout/indent_heredoc.rb +38 -2
- data/lib/rubocop/cop/layout/indentation_consistency.rb +105 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +4 -3
- data/lib/rubocop/cop/layout/initial_indentation.rb +15 -1
- data/lib/rubocop/cop/layout/leading_comment_space.rb +4 -2
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +62 -29
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +74 -33
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +16 -2
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -1
- data/lib/rubocop/cop/layout/space_after_method_name.rb +2 -0
- data/lib/rubocop/cop/layout/space_after_not.rb +2 -0
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -0
- data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +15 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +15 -13
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +13 -1
- data/lib/rubocop/cop/layout/space_before_comment.rb +6 -4
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +1 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -0
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +30 -45
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +3 -2
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +48 -18
- data/lib/rubocop/cop/layout/space_inside_parens.rb +8 -7
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +57 -11
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +1 -0
- data/lib/rubocop/cop/layout/tab.rb +42 -16
- data/lib/rubocop/cop/layout/trailing_blank_lines.rb +46 -13
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +12 -0
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +5 -3
- data/lib/rubocop/cop/lint/big_decimal_new.rb +44 -0
- data/lib/rubocop/cop/lint/boolean_symbol.rb +2 -2
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -2
- data/lib/rubocop/cop/lint/debugger.rb +2 -2
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +5 -4
- data/lib/rubocop/cop/lint/duplicate_methods.rb +20 -9
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +4 -3
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +16 -10
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +5 -4
- data/lib/rubocop/cop/lint/inherit_exception.rb +2 -2
- data/lib/rubocop/cop/lint/interpolation_check.rb +4 -3
- data/lib/rubocop/cop/lint/literal_as_condition.rb +2 -2
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +2 -0
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +7 -5
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
- data/lib/rubocop/cop/lint/number_conversion.rb +59 -0
- data/lib/rubocop/cop/lint/ordered_magic_comments.rb +86 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +2 -0
- data/lib/rubocop/cop/lint/percent_string_array.rb +0 -2
- data/lib/rubocop/cop/lint/rand_one.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -0
- data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -0
- data/lib/rubocop/cop/lint/require_parentheses.rb +2 -0
- data/lib/rubocop/cop/lint/rescue_type.rb +6 -3
- data/lib/rubocop/cop/lint/return_in_void_context.rb +2 -2
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -21
- data/lib/rubocop/cop/lint/script_permission.rb +30 -10
- data/lib/rubocop/cop/lint/shadowed_argument.rb +3 -3
- data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -2
- data/lib/rubocop/cop/lint/unified_integer.rb +2 -2
- data/lib/rubocop/cop/lint/{unneeded_disable.rb → unneeded_cop_disable_directive.rb} +13 -7
- data/lib/rubocop/cop/lint/unneeded_cop_enable_directive.rb +97 -0
- data/lib/rubocop/cop/lint/unneeded_require_statement.rb +1 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +3 -3
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +11 -10
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -5
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -2
- data/lib/rubocop/cop/lint/useless_setter_call.rb +2 -2
- data/lib/rubocop/cop/lint/void.rb +49 -10
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/line_length.rb +5 -2
- data/lib/rubocop/cop/mixin/alignment.rb +4 -0
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -1
- data/lib/rubocop/cop/mixin/def_node.rb +4 -0
- data/lib/rubocop/cop/mixin/documentation_comment.rb +11 -3
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +12 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +20 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +4 -0
- data/lib/rubocop/cop/mixin/hash_alignment.rb +2 -2
- data/lib/rubocop/cop/mixin/match_range.rb +2 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +6 -0
- data/lib/rubocop/cop/mixin/nil_methods.rb +19 -0
- data/lib/rubocop/cop/mixin/percent_literal.rb +57 -9
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +6 -5
- data/lib/rubocop/cop/mixin/range_help.rb +102 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +1 -1
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +8 -7
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +11 -9
- data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -10
- data/lib/rubocop/cop/mixin/surrounding_space.rb +38 -8
- data/lib/rubocop/cop/mixin/trailing_body.rb +26 -0
- data/lib/rubocop/cop/mixin/trailing_comma.rb +15 -3
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +104 -0
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +3 -1
- data/lib/rubocop/cop/naming/file_name.rb +5 -10
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +76 -0
- data/lib/rubocop/cop/naming/uncommunicative_block_param_name.rb +48 -0
- data/lib/rubocop/cop/naming/uncommunicative_method_param_name.rb +57 -0
- data/lib/rubocop/cop/offense.rb +3 -2
- data/lib/rubocop/cop/performance/case_when_splat.rb +1 -0
- data/lib/rubocop/cop/performance/casecmp.rb +17 -8
- data/lib/rubocop/cop/performance/compare_with_block.rb +2 -0
- data/lib/rubocop/cop/performance/count.rb +1 -0
- data/lib/rubocop/cop/performance/fixed_size.rb +41 -0
- data/lib/rubocop/cop/performance/flat_map.rb +2 -0
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -0
- data/lib/rubocop/cop/performance/redundant_merge.rb +1 -1
- data/lib/rubocop/cop/performance/redundant_sort_by.rb +2 -0
- data/lib/rubocop/cop/performance/regexp_match.rb +4 -0
- data/lib/rubocop/cop/performance/reverse_each.rb +2 -0
- data/lib/rubocop/cop/performance/string_replacement.rb +2 -0
- data/lib/rubocop/cop/rails/active_record_aliases.rb +46 -0
- data/lib/rubocop/cop/rails/blank.rb +3 -3
- data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +6 -0
- data/lib/rubocop/cop/rails/delegate.rb +6 -6
- data/lib/rubocop/cop/rails/file_path.rb +7 -1
- data/lib/rubocop/cop/rails/find_by.rb +2 -0
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +17 -5
- data/lib/rubocop/cop/rails/inverse_of.rb +21 -2
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +45 -9
- data/lib/rubocop/cop/rails/presence.rb +8 -2
- data/lib/rubocop/cop/rails/present.rb +5 -5
- data/lib/rubocop/cop/rails/read_write_attribute.rb +4 -3
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +1 -0
- data/lib/rubocop/cop/rails/relative_date_constant.rb +4 -3
- data/lib/rubocop/cop/rails/request_referer.rb +3 -2
- data/lib/rubocop/cop/rails/reversible_migration.rb +9 -8
- data/lib/rubocop/cop/rails/safe_navigation.rb +3 -2
- data/lib/rubocop/cop/rails/save_bang.rb +11 -12
- data/lib/rubocop/cop/rails/skips_model_validations.rb +2 -2
- data/lib/rubocop/cop/rails/time_zone.rb +38 -16
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +26 -16
- data/lib/rubocop/cop/rails/validation.rb +30 -2
- data/lib/rubocop/cop/security/open.rb +48 -0
- data/lib/rubocop/cop/style/and_or.rb +1 -0
- data/lib/rubocop/cop/style/ascii_comments.rb +3 -1
- data/lib/rubocop/cop/style/attr.rb +2 -0
- data/lib/rubocop/cop/style/block_comments.rb +3 -1
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +2 -5
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -0
- data/lib/rubocop/cop/style/class_vars.rb +23 -0
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -2
- data/lib/rubocop/cop/style/comment_annotation.rb +6 -4
- data/lib/rubocop/cop/style/commented_keyword.rb +3 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +1 -1
- data/lib/rubocop/cop/style/copyright.rb +3 -1
- data/lib/rubocop/cop/style/each_with_object.rb +15 -1
- data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -0
- data/lib/rubocop/cop/style/empty_case_condition.rb +2 -0
- data/lib/rubocop/cop/style/empty_else.rb +9 -5
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -0
- data/lib/rubocop/cop/style/empty_line_after_guard_clause.rb +80 -0
- data/lib/rubocop/cop/style/empty_literal.rb +1 -0
- data/lib/rubocop/cop/style/encoding.rb +2 -0
- data/lib/rubocop/cop/style/expand_path_arguments.rb +194 -0
- data/lib/rubocop/cop/style/for.rb +33 -0
- data/lib/rubocop/cop/style/format_string.rb +1 -1
- data/lib/rubocop/cop/style/format_string_token.rb +4 -5
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +2 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +1 -1
- data/lib/rubocop/cop/style/inline_comment.rb +1 -1
- data/lib/rubocop/cop/style/lambda.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +2 -0
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -0
- data/lib/rubocop/cop/style/missing_else.rb +72 -7
- data/lib/rubocop/cop/style/mixin_usage.rb +3 -5
- data/lib/rubocop/cop/style/module_function.rb +10 -0
- data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -0
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -0
- data/lib/rubocop/cop/style/nested_modifier.rb +2 -0
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -0
- data/lib/rubocop/cop/style/next.rb +1 -0
- data/lib/rubocop/cop/style/not.rb +2 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
- data/lib/rubocop/cop/style/redundant_exception.rb +8 -3
- data/lib/rubocop/cop/style/redundant_return.rb +37 -3
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +74 -32
- data/lib/rubocop/cop/style/semicolon.rb +3 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +14 -23
- data/lib/rubocop/cop/style/stderr_puts.rb +2 -0
- data/lib/rubocop/cop/style/string_hash_keys.rb +12 -0
- data/lib/rubocop/cop/style/string_literals.rb +1 -1
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/symbol_array.rb +29 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +2 -0
- data/lib/rubocop/cop/style/trailing_body_on_class.rb +43 -0
- data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +7 -54
- data/lib/rubocop/cop/style/trailing_body_on_module.rb +43 -0
- data/lib/rubocop/cop/style/{trailing_comma_in_literal.rb → trailing_comma_in_array_literal.rb} +2 -20
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +56 -0
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +17 -20
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -0
- data/lib/rubocop/cop/style/unless_else.rb +2 -0
- data/lib/rubocop/cop/style/word_array.rb +0 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
- data/lib/rubocop/cop/team.rb +5 -5
- data/lib/rubocop/cop/util.rb +23 -188
- data/lib/rubocop/cop/variable_force.rb +1 -1
- data/lib/rubocop/file_finder.rb +45 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +23 -14
- data/lib/rubocop/formatter/fuubar_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +12 -5
- data/lib/rubocop/formatter/json_formatter.rb +1 -1
- data/lib/rubocop/node_pattern.rb +8 -5
- data/lib/rubocop/options.rb +40 -33
- data/lib/rubocop/path_util.rb +5 -8
- data/lib/rubocop/processed_source.rb +53 -0
- data/lib/rubocop/remote_config.rb +1 -1
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/rspec/cop_helper.rb +0 -4
- data/lib/rubocop/rspec/host_environment_simulation_helper.rb +0 -4
- data/lib/rubocop/rspec/shared_contexts.rb +3 -1
- data/lib/rubocop/rspec/shared_examples.rb +23 -25
- data/lib/rubocop/rspec/support.rb +5 -0
- data/lib/rubocop/runner.rb +3 -2
- data/lib/rubocop/string_util.rb +10 -9
- data/lib/rubocop/target_finder.rb +4 -1
- data/lib/rubocop/token.rb +26 -16
- data/lib/rubocop/version.rb +6 -4
- metadata +31 -17
- data/lib/rubocop/cop/performance/hash_each_methods.rb +0 -129
@@ -7,8 +7,41 @@ module RuboCop
|
|
7
7
|
# preferred alternative is set in the EnforcedStyle configuration
|
8
8
|
# parameter. An *each* call with a block on a single line is always
|
9
9
|
# allowed, however.
|
10
|
+
#
|
11
|
+
# @example EnforcedStyle: each (default)
|
12
|
+
# # bad
|
13
|
+
# def foo
|
14
|
+
# for n in [1, 2, 3] do
|
15
|
+
# puts n
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# # good
|
20
|
+
# def foo
|
21
|
+
# [1, 2, 3].each do |n|
|
22
|
+
# puts n
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# @example EnforcedStyle: for
|
27
|
+
# # bad
|
28
|
+
# def foo
|
29
|
+
# [1, 2, 3].each do |n|
|
30
|
+
# puts n
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# # good
|
35
|
+
# def foo
|
36
|
+
# for n in [1, 2, 3] do
|
37
|
+
# puts n
|
38
|
+
# end
|
39
|
+
# end
|
40
|
+
#
|
10
41
|
class For < Cop
|
11
42
|
include ConfigurableEnforcedStyle
|
43
|
+
include RangeHelp
|
44
|
+
|
12
45
|
EACH_LENGTH = 'each'.length
|
13
46
|
|
14
47
|
def on_for(node)
|
@@ -11,7 +11,7 @@ module RuboCop
|
|
11
11
|
# if the first argument is a string literal and if the second
|
12
12
|
# argument is an array literal.
|
13
13
|
#
|
14
|
-
# @example EnforcedStyle: format(default)
|
14
|
+
# @example EnforcedStyle: format (default)
|
15
15
|
# # bad
|
16
16
|
# puts sprintf('%10s', 'hoge')
|
17
17
|
# puts '%10s' % 'hoge'
|
@@ -68,11 +68,8 @@ module RuboCop
|
|
68
68
|
private
|
69
69
|
|
70
70
|
def includes_format_methods?(node)
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
root_node.descendants.any? do |desc_node|
|
75
|
-
FORMAT_STRING_METHODS.include?(desc_node.method_name)
|
71
|
+
node.each_ancestor.any? do |ancestor|
|
72
|
+
FORMAT_STRING_METHODS.include?(ancestor.method_name)
|
76
73
|
end
|
77
74
|
end
|
78
75
|
|
@@ -84,6 +81,7 @@ module RuboCop
|
|
84
81
|
"Prefer #{message_text(style)} over #{message_text(detected_style)}."
|
85
82
|
end
|
86
83
|
|
84
|
+
# rubocop:disable Style/FormatStringToken
|
87
85
|
def message_text(style)
|
88
86
|
case style
|
89
87
|
when :annotated then 'annotated tokens (like `%<foo>s`)'
|
@@ -91,6 +89,7 @@ module RuboCop
|
|
91
89
|
when :unannotated then 'unannotated tokens (like `%s`)'
|
92
90
|
end
|
93
91
|
end
|
92
|
+
# rubocop:enable Style/FormatStringToken
|
94
93
|
|
95
94
|
def tokens(str_node, &block)
|
96
95
|
return if str_node.source == '__FILE__'
|
@@ -57,6 +57,7 @@ module RuboCop
|
|
57
57
|
class FrozenStringLiteralComment < Cop
|
58
58
|
include ConfigurableEnforcedStyle
|
59
59
|
include FrozenStringLiteral
|
60
|
+
include RangeHelp
|
60
61
|
|
61
62
|
MSG = 'Missing magic comment `# frozen_string_literal: true`.'.freeze
|
62
63
|
MSG_UNNECESSARY = 'Unnecessary frozen string literal comment.'.freeze
|
@@ -117,7 +118,7 @@ module RuboCop
|
|
117
118
|
end
|
118
119
|
|
119
120
|
def frozen_string_literal_comment(processed_source)
|
120
|
-
processed_source.
|
121
|
+
processed_source.find_token do |token|
|
121
122
|
token.text.start_with?(FrozenStringLiteral::FROZEN_STRING_LITERAL)
|
122
123
|
end
|
123
124
|
end
|
@@ -85,7 +85,7 @@ module RuboCop
|
|
85
85
|
|
86
86
|
def first_line_comment(node)
|
87
87
|
comment =
|
88
|
-
processed_source.
|
88
|
+
processed_source.find_comment { |c| c.loc.line == node.loc.line }
|
89
89
|
|
90
90
|
comment ? comment.loc.expression.source : nil
|
91
91
|
end
|
@@ -21,7 +21,7 @@ module RuboCop
|
|
21
21
|
MSG = 'Avoid trailing inline comments.'.freeze
|
22
22
|
|
23
23
|
def investigate(processed_source)
|
24
|
-
processed_source.
|
24
|
+
processed_source.each_comment do |comment|
|
25
25
|
next if comment_line?(processed_source[comment.loc.line - 1])
|
26
26
|
add_offense(comment)
|
27
27
|
end
|
@@ -4,29 +4,94 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
6
|
# Checks for `if` expressions that do not have an `else` branch.
|
7
|
-
# SupportedStyles
|
8
7
|
#
|
9
|
-
# if
|
10
|
-
#
|
8
|
+
# Supported styles are: if, case, both.
|
9
|
+
#
|
10
|
+
# @example EnforcedStyle: if
|
11
|
+
# # warn when an `if` expression is missing an `else` branch.
|
12
|
+
#
|
11
13
|
# # bad
|
12
14
|
# if condition
|
13
15
|
# statement
|
14
16
|
# end
|
15
17
|
#
|
16
|
-
#
|
17
|
-
#
|
18
|
+
# # good
|
19
|
+
# if condition
|
20
|
+
# statement
|
21
|
+
# else
|
22
|
+
# # the content of `else` branch will be determined by Style/EmptyElse
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# case var
|
27
|
+
# when condition
|
28
|
+
# statement
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# # good
|
32
|
+
# case var
|
33
|
+
# when condition
|
34
|
+
# statement
|
35
|
+
# else
|
36
|
+
# # the content of `else` branch will be determined by Style/EmptyElse
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# @example EnforcedStyle: case
|
40
|
+
# # warn when a `case` expression is missing an `else` branch.
|
41
|
+
#
|
18
42
|
# # bad
|
19
43
|
# case var
|
20
44
|
# when condition
|
21
45
|
# statement
|
22
46
|
# end
|
23
47
|
#
|
24
|
-
#
|
48
|
+
# # good
|
49
|
+
# case var
|
50
|
+
# when condition
|
51
|
+
# statement
|
52
|
+
# else
|
53
|
+
# # the content of `else` branch will be determined by Style/EmptyElse
|
54
|
+
# end
|
55
|
+
#
|
25
56
|
# # good
|
26
57
|
# if condition
|
27
58
|
# statement
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# # good
|
62
|
+
# if condition
|
63
|
+
# statement
|
64
|
+
# else
|
65
|
+
# # the content of `else` branch will be determined by Style/EmptyElse
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# @example EnforcedStyle: both (default)
|
69
|
+
# # warn when an `if` or `case` expression is missing an `else` branch.
|
70
|
+
#
|
71
|
+
# # bad
|
72
|
+
# if condition
|
73
|
+
# statement
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
# # bad
|
77
|
+
# case var
|
78
|
+
# when condition
|
79
|
+
# statement
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# # good
|
83
|
+
# if condition
|
84
|
+
# statement
|
85
|
+
# else
|
86
|
+
# # the content of `else` branch will be determined by Style/EmptyElse
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# # good
|
90
|
+
# case var
|
91
|
+
# when condition
|
92
|
+
# statement
|
28
93
|
# else
|
29
|
-
#
|
94
|
+
# # the content of `else` branch will be determined by Style/EmptyElse
|
30
95
|
# end
|
31
96
|
class MissingElse < Cop
|
32
97
|
include OnNormalIfUnless
|
@@ -3,11 +3,9 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# This cop checks that `include`, `extend` and `prepend`
|
7
|
-
# the top level
|
8
|
-
#
|
9
|
-
# There will not be using `include`, `extend` and `prepend` at
|
10
|
-
# the top level. Let's use it inside `class` or `module`.
|
6
|
+
# This cop checks that `include`, `extend` and `prepend` statements appear
|
7
|
+
# inside classes and modules, not at the top level, so as to not affect
|
8
|
+
# the behavior of `Object`.
|
11
9
|
#
|
12
10
|
# @example
|
13
11
|
# # bad
|
@@ -56,6 +56,16 @@ module RuboCop
|
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
+
def autocorrect(node)
|
60
|
+
lambda do |corrector|
|
61
|
+
if extend_self_node?(node)
|
62
|
+
corrector.replace(node.source_range, 'module_function')
|
63
|
+
else
|
64
|
+
corrector.replace(node.source_range, 'extend self')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
59
69
|
private
|
60
70
|
|
61
71
|
def each_wrong_style(nodes)
|
@@ -63,7 +63,7 @@ module RuboCop
|
|
63
63
|
|
64
64
|
def method_call_with_changed_precedence?(node)
|
65
65
|
return false unless node.send_type? && node.arguments?
|
66
|
-
return false if parenthesized_call?
|
66
|
+
return false if node.parenthesized_call?
|
67
67
|
|
68
68
|
!operator?(node.method_name)
|
69
69
|
end
|
@@ -72,7 +72,7 @@ module RuboCop
|
|
72
72
|
return false unless node.keyword?
|
73
73
|
return true if node.keyword_not?
|
74
74
|
|
75
|
-
!parenthesized_call?
|
75
|
+
!node.parenthesized_call?
|
76
76
|
end
|
77
77
|
end
|
78
78
|
end
|
@@ -28,11 +28,16 @@ module RuboCop
|
|
28
28
|
|
29
29
|
# Switch `raise RuntimeError, 'message'` to `raise 'message'`, and
|
30
30
|
# `raise RuntimeError.new('message')` to `raise 'message'`.
|
31
|
-
def autocorrect(node)
|
31
|
+
def autocorrect(node) # rubocop:disable Metrics/MethodLength
|
32
32
|
exploded?(node) do |command, message|
|
33
33
|
return lambda do |corrector|
|
34
|
-
|
35
|
-
|
34
|
+
if node.parenthesized?
|
35
|
+
corrector.replace(node.source_range,
|
36
|
+
"#{command}(#{message.source})")
|
37
|
+
else
|
38
|
+
corrector.replace(node.source_range,
|
39
|
+
"#{command} #{message.source}")
|
40
|
+
end
|
36
41
|
end
|
37
42
|
end
|
38
43
|
compact?(node) do |new_call, message|
|
@@ -6,11 +6,15 @@ module RuboCop
|
|
6
6
|
# This cop checks for redundant `return` expressions.
|
7
7
|
#
|
8
8
|
# @example
|
9
|
+
# # These bad cases should be extended to handle methods whose body is
|
10
|
+
# # if/else or a case expression with a default branch.
|
9
11
|
#
|
12
|
+
# # bad
|
10
13
|
# def test
|
11
14
|
# return something
|
12
15
|
# end
|
13
16
|
#
|
17
|
+
# # bad
|
14
18
|
# def test
|
15
19
|
# one
|
16
20
|
# two
|
@@ -18,9 +22,22 @@ module RuboCop
|
|
18
22
|
# return something
|
19
23
|
# end
|
20
24
|
#
|
21
|
-
#
|
22
|
-
#
|
25
|
+
# # good
|
26
|
+
# def test
|
27
|
+
# return something if something_else
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# # good
|
31
|
+
# def test
|
32
|
+
# if x
|
33
|
+
# elsif y
|
34
|
+
# else
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
#
|
23
38
|
class RedundantReturn < Cop
|
39
|
+
include RangeHelp
|
40
|
+
|
24
41
|
MSG = 'Redundant `return` detected.'.freeze
|
25
42
|
MULTI_RETURN_MSG = 'To return multiple values, use an array.'.freeze
|
26
43
|
|
@@ -71,14 +88,20 @@ module RuboCop
|
|
71
88
|
!args.first.begin_type? || !args.first.children.empty?
|
72
89
|
end
|
73
90
|
|
91
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
74
92
|
def check_branch(node)
|
75
93
|
case node.type
|
76
94
|
when :return then check_return_node(node)
|
77
95
|
when :case then check_case_node(node)
|
78
96
|
when :if then check_if_node(node)
|
79
|
-
when :
|
97
|
+
when :rescue, :resbody
|
98
|
+
check_rescue_node(node)
|
99
|
+
when :ensure then check_ensure_node(node)
|
100
|
+
when :begin, :kwbegin
|
101
|
+
check_begin_node(node)
|
80
102
|
end
|
81
103
|
end
|
104
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
82
105
|
|
83
106
|
def check_return_node(node)
|
84
107
|
return if cop_config['AllowMultipleReturnValues'] &&
|
@@ -108,6 +131,17 @@ module RuboCop
|
|
108
131
|
check_branch(else_node) if else_node
|
109
132
|
end
|
110
133
|
|
134
|
+
def check_rescue_node(node)
|
135
|
+
node.child_nodes.each do |child_node|
|
136
|
+
check_branch(child_node)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def check_ensure_node(node)
|
141
|
+
rescue_node = node.node_parts[0]
|
142
|
+
check_branch(rescue_node)
|
143
|
+
end
|
144
|
+
|
111
145
|
def check_begin_node(node)
|
112
146
|
expressions = *node
|
113
147
|
last_expr = expressions.last
|