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
@@ -37,10 +37,12 @@ module RuboCop
|
|
37
37
|
# params[:width_gteq]
|
38
38
|
#
|
39
39
|
class AsciiIdentifiers < Cop
|
40
|
+
include RangeHelp
|
41
|
+
|
40
42
|
MSG = 'Use only ascii symbols in identifiers.'.freeze
|
41
43
|
|
42
44
|
def investigate(processed_source)
|
43
|
-
processed_source.
|
45
|
+
processed_source.each_token do |token|
|
44
46
|
next unless token.type == :tIDENTIFIER && !token.text.ascii_only?
|
45
47
|
add_offense(token, location: first_offense_range(token))
|
46
48
|
end
|
@@ -20,6 +20,8 @@ module RuboCop
|
|
20
20
|
#
|
21
21
|
# anything/using_snake_case.rake
|
22
22
|
class FileName < Cop
|
23
|
+
include RangeHelp
|
24
|
+
|
23
25
|
MSG_SNAKE_CASE = 'The name of this source file (`%<basename>s`) ' \
|
24
26
|
'should use snake_case.'.freeze
|
25
27
|
MSG_NO_DEFINITION = '%<basename>s should define a class or module ' \
|
@@ -29,7 +31,7 @@ module RuboCop
|
|
29
31
|
SNAKE_CASE = /^[\da-z_.?!]+$/
|
30
32
|
|
31
33
|
def investigate(processed_source)
|
32
|
-
file_path = processed_source.
|
34
|
+
file_path = processed_source.file_path
|
33
35
|
return if config.file_to_include?(file_path)
|
34
36
|
|
35
37
|
for_bad_filename(file_path) do |range, msg|
|
@@ -48,17 +50,14 @@ module RuboCop
|
|
48
50
|
|
49
51
|
no_definition_message(basename, file_path)
|
50
52
|
else
|
51
|
-
return if ignore_executable_scripts? &&
|
53
|
+
return if ignore_executable_scripts? &&
|
54
|
+
processed_source.start_with?('#!')
|
52
55
|
other_message(basename)
|
53
56
|
end
|
54
57
|
|
55
58
|
yield source_range(processed_source.buffer, 1, 0), msg
|
56
59
|
end
|
57
60
|
|
58
|
-
def first_line
|
59
|
-
processed_source.lines.first
|
60
|
-
end
|
61
|
-
|
62
61
|
def no_definition_message(basename, file_path)
|
63
62
|
format(MSG_NO_DEFINITION,
|
64
63
|
basename: basename,
|
@@ -73,10 +72,6 @@ module RuboCop
|
|
73
72
|
end
|
74
73
|
end
|
75
74
|
|
76
|
-
def shebang?(line)
|
77
|
-
line && line.start_with?('#!')
|
78
|
-
end
|
79
|
-
|
80
75
|
def ignore_executable_scripts?
|
81
76
|
cop_config['IgnoreExecutableScripts']
|
82
77
|
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Naming
|
6
|
+
# This cop checks for memoized methods whose instance variable name
|
7
|
+
# does not match the method name.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # bad
|
11
|
+
# # Method foo is memoized using an instance variable that is
|
12
|
+
# # not `@foo`. This can cause confusion and bugs.
|
13
|
+
# def foo
|
14
|
+
# @something ||= calculate_expensive_thing
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# def foo
|
19
|
+
# @foo ||= calculate_expensive_thing
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# # good
|
23
|
+
# def foo
|
24
|
+
# @foo ||= begin
|
25
|
+
# calculate_expensive_thing
|
26
|
+
# end
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# def foo
|
31
|
+
# helper_variable = something_we_need_to_calculate_foo
|
32
|
+
# @foo ||= calculate_expensive_thing(helper_variable)
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
class MemoizedInstanceVariableName < Cop
|
36
|
+
MSG = 'Memoized variable `%<var>s` does not match ' \
|
37
|
+
'method name `%<method>s`. Use `@%<method>s` instead.'.freeze
|
38
|
+
|
39
|
+
def self.node_pattern
|
40
|
+
memo_assign = '(or_asgn $(ivasgn _) _)'
|
41
|
+
memoized_at_end_of_method = "(begin ... #{memo_assign})"
|
42
|
+
instance_method =
|
43
|
+
"(def $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
|
44
|
+
class_method =
|
45
|
+
"(defs self $_ _ {#{memo_assign} #{memoized_at_end_of_method}})"
|
46
|
+
"{#{instance_method} #{class_method}}"
|
47
|
+
end
|
48
|
+
|
49
|
+
private_class_method :node_pattern
|
50
|
+
def_node_matcher :memoized?, node_pattern
|
51
|
+
|
52
|
+
def on_def(node)
|
53
|
+
(method_name, ivar_assign) = memoized?(node)
|
54
|
+
return if matches?(method_name, ivar_assign)
|
55
|
+
msg = format(
|
56
|
+
MSG,
|
57
|
+
var: ivar_assign.children.first.to_s,
|
58
|
+
method: method_name
|
59
|
+
)
|
60
|
+
add_offense(node, location: ivar_assign.source_range, message: msg)
|
61
|
+
end
|
62
|
+
alias on_defs on_def
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def matches?(method_name, ivar_assign)
|
67
|
+
return true if ivar_assign.nil? || method_name == :initialize
|
68
|
+
method_name = method_name.to_s.sub('?', '')
|
69
|
+
variable = ivar_assign.children.first
|
70
|
+
variable_name = variable.to_s.sub('@', '')
|
71
|
+
variable_name == method_name
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Naming
|
6
|
+
# This cop checks block parameter names for how descriptive they
|
7
|
+
# are. It is highly configurable.
|
8
|
+
#
|
9
|
+
# The `MinNameLength` config option takes an integer. It represents
|
10
|
+
# the minimum amount of characters the name must be. Its default is 1.
|
11
|
+
# The `AllowNamesEndingInNumbers` config option takes a boolean. When
|
12
|
+
# set to false, this cop will register offenses for names ending with
|
13
|
+
# numbers. Its default is false. The `AllowedNames` config option
|
14
|
+
# takes an array of whitelisted names that will never register an
|
15
|
+
# offense. The `ForbiddenNames` config option takes an array of
|
16
|
+
# blacklisted names that will always register an offense.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# # bad
|
20
|
+
# bar do |varOne, varTwo|
|
21
|
+
# varOne + varTwo
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# # With `AllowNamesEndingInNumbers` set to false
|
25
|
+
# foo { |num1, num2| num1 * num2 }
|
26
|
+
#
|
27
|
+
# # With `MinParamNameLength` set to number greater than 1
|
28
|
+
# baz { |a, b, c| do_stuff(a, b, c) }
|
29
|
+
#
|
30
|
+
# # good
|
31
|
+
# bar do |thud, fred|
|
32
|
+
# thud + fred
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# foo { |speed, distance| speed * distance }
|
36
|
+
#
|
37
|
+
# baz { |age, height, gender| do_stuff(age, height, gender) }
|
38
|
+
class UncommunicativeBlockParamName < Cop
|
39
|
+
include UncommunicativeName
|
40
|
+
|
41
|
+
def on_block(node)
|
42
|
+
return unless node.arguments?
|
43
|
+
check(node, node.arguments)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Naming
|
6
|
+
# This cop checks method parameter names for how descriptive they
|
7
|
+
# are. It is highly configurable.
|
8
|
+
#
|
9
|
+
# The `MinNameLength` config option takes an integer. It represents
|
10
|
+
# the minimum amount of characters the name must be. Its default is 3.
|
11
|
+
# The `AllowNamesEndingInNumbers` config option takes a boolean. When
|
12
|
+
# set to false, this cop will register offenses for names ending with
|
13
|
+
# numbers. Its default is false. The `AllowedNames` config option
|
14
|
+
# takes an array of whitelisted names that will never register an
|
15
|
+
# offense. The `ForbiddenNames` config option takes an array of
|
16
|
+
# blacklisted names that will always register an offense.
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# # bad
|
20
|
+
# def bar(varOne, varTwo)
|
21
|
+
# varOne + varTwo
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# # With `AllowNamesEndingInNumbers` set to false
|
25
|
+
# def foo(num1, num2)
|
26
|
+
# num1 * num2
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# # With `MinArgNameLength` set to number greater than 1
|
30
|
+
# def baz(a, b, c)
|
31
|
+
# do_stuff(a, b, c)
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# # good
|
35
|
+
# def bar(thud, fred)
|
36
|
+
# thud + fred
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# def foo(speed, distance)
|
40
|
+
# speed * distance
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# def baz(age_a, height_b, gender_c)
|
44
|
+
# do_stuff(age_a, height_b, gender_c)
|
45
|
+
# end
|
46
|
+
class UncommunicativeMethodParamName < Cop
|
47
|
+
include UncommunicativeName
|
48
|
+
|
49
|
+
def on_def(node)
|
50
|
+
return unless node.arguments?
|
51
|
+
check(node, node.arguments)
|
52
|
+
end
|
53
|
+
alias on_defs on_def
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/rubocop/cop/offense.rb
CHANGED
@@ -99,8 +99,9 @@ module RuboCop
|
|
99
99
|
# @api private
|
100
100
|
# This is just for debugging purpose.
|
101
101
|
def to_s
|
102
|
-
format('
|
103
|
-
severity.code, line
|
102
|
+
format('%<severity>s:%3<line>d:%3<column>d: %<message>s',
|
103
|
+
severity: severity.code, line: line,
|
104
|
+
column: real_column, message: message)
|
104
105
|
end
|
105
106
|
|
106
107
|
# @api private
|
@@ -35,6 +35,13 @@ module RuboCop
|
|
35
35
|
$(send _ ${:downcase :upcase}))
|
36
36
|
PATTERN
|
37
37
|
|
38
|
+
def_node_matcher :downcase_downcase, <<-PATTERN
|
39
|
+
(send
|
40
|
+
$(send _ ${:downcase :upcase})
|
41
|
+
${:== :eql? :!=}
|
42
|
+
$(send _ ${:downcase :upcase}))
|
43
|
+
PATTERN
|
44
|
+
|
38
45
|
def on_send(node)
|
39
46
|
return if part_of_ignored_node?(node)
|
40
47
|
|
@@ -46,17 +53,19 @@ module RuboCop
|
|
46
53
|
end
|
47
54
|
|
48
55
|
def autocorrect(node)
|
49
|
-
|
56
|
+
if downcase_downcase(node)
|
57
|
+
receiver, method, rhs = *node
|
58
|
+
arg, = *rhs
|
59
|
+
elsif downcase_eq(node)
|
50
60
|
receiver, method, arg = *node
|
51
|
-
|
52
|
-
return correction(node, receiver, method, arg, variable)
|
53
|
-
end
|
54
|
-
|
55
|
-
eq_downcase(node) do
|
61
|
+
elsif eq_downcase(node)
|
56
62
|
arg, method, receiver = *node
|
57
|
-
|
58
|
-
return
|
63
|
+
else
|
64
|
+
return
|
59
65
|
end
|
66
|
+
|
67
|
+
variable, = *receiver
|
68
|
+
correction(node, receiver, method, arg, variable)
|
60
69
|
end
|
61
70
|
|
62
71
|
private
|
@@ -24,6 +24,8 @@ module RuboCop
|
|
24
24
|
# array.min_by(&:foo)
|
25
25
|
# array.sort_by { |a| a[:foo] }
|
26
26
|
class CompareWithBlock < Cop
|
27
|
+
include RangeHelp
|
28
|
+
|
27
29
|
MSG = 'Use `%<compare_method>s_by%<instead>s` instead of ' \
|
28
30
|
'`%<compare_method>s { |%<var_a>s, %<var_b>s| %<str_a>s ' \
|
29
31
|
'<=> %<str_b>s }`.'.freeze
|
@@ -4,6 +4,47 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Performance
|
6
6
|
# Do not compute the size of statically sized objects.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # String methods
|
10
|
+
# # bad
|
11
|
+
# 'foo'.size
|
12
|
+
# %q[bar].count
|
13
|
+
# %(qux).length
|
14
|
+
#
|
15
|
+
# # Symbol methods
|
16
|
+
# # bad
|
17
|
+
# :fred.size
|
18
|
+
# :'baz'.length
|
19
|
+
#
|
20
|
+
# # Array methods
|
21
|
+
# # bad
|
22
|
+
# [1, 2, thud].count
|
23
|
+
# %W(1, 2, bar).size
|
24
|
+
#
|
25
|
+
# # Hash methods
|
26
|
+
# # bad
|
27
|
+
# { a: corge, b: grault }.length
|
28
|
+
#
|
29
|
+
# # good
|
30
|
+
# foo.size
|
31
|
+
# bar.count
|
32
|
+
# qux.length
|
33
|
+
#
|
34
|
+
# # good
|
35
|
+
# :"#{fred}".size
|
36
|
+
# CONST = :baz.length
|
37
|
+
#
|
38
|
+
# # good
|
39
|
+
# [1, 2, *thud].count
|
40
|
+
# garply = [1, 2, 3]
|
41
|
+
# garly.size
|
42
|
+
#
|
43
|
+
# # good
|
44
|
+
# { a: corge, **grault }.length
|
45
|
+
# waldo = { a: corge, b: grault }
|
46
|
+
# waldo.size
|
47
|
+
#
|
7
48
|
class FixedSize < Cop
|
8
49
|
MSG = 'Do not compute the size of statically sized objects.'.freeze
|
9
50
|
|
@@ -15,6 +15,8 @@ module RuboCop
|
|
15
15
|
# [1, 2, 3, 4].map { |e| [e, e] }.flatten
|
16
16
|
# [1, 2, 3, 4].collect { |e| [e, e] }.flatten
|
17
17
|
class FlatMap < Cop
|
18
|
+
include RangeHelp
|
19
|
+
|
18
20
|
MSG = 'Use `flat_map` instead of `%<method>s...%<flatten>s`.'.freeze
|
19
21
|
FLATTEN_MULTIPLE_LEVELS = ' Beware, `flat_map` only flattens 1 level ' \
|
20
22
|
'and `flatten` can be used to flatten ' \
|
@@ -154,6 +154,10 @@ module RuboCop
|
|
154
154
|
def last_match_used?(match_node)
|
155
155
|
scope_root = scope_root(match_node)
|
156
156
|
body = scope_root ? scope_body(scope_root) : match_node.ancestors.last
|
157
|
+
|
158
|
+
return true if match_node.parent.if_type? &&
|
159
|
+
match_node.parent.modifier_form?
|
160
|
+
|
157
161
|
match_node_pos = match_node.loc.expression.begin_pos
|
158
162
|
|
159
163
|
next_match_pos = next_match_pos(body, match_node_pos, scope_root)
|
@@ -19,6 +19,8 @@ module RuboCop
|
|
19
19
|
# 'abc'.tr('b', 'd')
|
20
20
|
# 'a b c'.delete(' ')
|
21
21
|
class StringReplacement < Cop
|
22
|
+
include RangeHelp
|
23
|
+
|
22
24
|
MSG = 'Use `%<prefer>s` instead of `%<current>s`.'.freeze
|
23
25
|
DETERMINISTIC_REGEX = /\A(?:#{LITERAL_REGEX})+\Z/
|
24
26
|
DELETE = 'delete'.freeze
|