rubocop 1.72.1 → 1.81.7
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 +22 -18
- data/config/default.yml +240 -65
- data/config/internal_affairs.yml +20 -0
- data/config/obsoletion.yml +8 -3
- data/exe/rubocop +1 -8
- data/lib/rubocop/cli/command/auto_generate_config.rb +2 -2
- data/lib/rubocop/cli.rb +19 -4
- data/lib/rubocop/config.rb +35 -6
- data/lib/rubocop/config_loader.rb +8 -40
- data/lib/rubocop/config_loader_resolver.rb +9 -7
- data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -3
- data/lib/rubocop/config_obsoletion/renamed_cop.rb +18 -3
- data/lib/rubocop/config_obsoletion.rb +46 -2
- data/lib/rubocop/config_store.rb +5 -0
- data/lib/rubocop/config_validator.rb +7 -6
- data/lib/rubocop/cop/autocorrect_logic.rb +22 -14
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +7 -4
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +7 -2
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +5 -2
- data/lib/rubocop/cop/gemspec/attribute_assignment.rb +91 -0
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +37 -15
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/gemspec/require_mfa.rb +15 -1
- data/lib/rubocop/cop/internal_affairs/example_description.rb +9 -5
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +4 -4
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +6 -2
- data/lib/rubocop/cop/internal_affairs/node_type_group.rb +92 -0
- data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +6 -5
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +6 -1
- data/lib/rubocop/cop/internal_affairs/useless_restrict_on_send.rb +1 -1
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/layout/block_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/block_end_newline.rb +1 -0
- data/lib/rubocop/cop/layout/class_structure.rb +36 -1
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +32 -14
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +101 -0
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +34 -4
- data/lib/rubocop/cop/layout/empty_lines_around_arguments.rb +8 -29
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +1 -0
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +1 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +4 -7
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
- data/lib/rubocop/cop/layout/leading_comment_space.rb +13 -1
- data/lib/rubocop/cop/layout/line_length.rb +43 -10
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_method_parameter_line_breaks.rb +1 -0
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +9 -5
- data/lib/rubocop/cop/layout/redundant_line_break.rb +9 -5
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +11 -5
- data/lib/rubocop/cop/layout/space_after_semicolon.rb +10 -0
- data/lib/rubocop/cop/layout/space_around_keyword.rb +6 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +12 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -0
- data/lib/rubocop/cop/layout/space_before_brackets.rb +5 -38
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +12 -3
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -0
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +3 -0
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_range.rb +5 -0
- data/lib/rubocop/cop/lint/array_literal_in_regexp.rb +2 -3
- data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -5
- data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +3 -2
- data/lib/rubocop/cop/lint/cop_directive_syntax.rb +13 -7
- data/lib/rubocop/cop/lint/debugger.rb +2 -4
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +5 -2
- data/lib/rubocop/cop/lint/duplicate_methods.rb +111 -23
- data/lib/rubocop/cop/lint/duplicate_regexp_character_class_element.rb +5 -42
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +14 -64
- data/lib/rubocop/cop/lint/empty_interpolation.rb +14 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +0 -6
- data/lib/rubocop/cop/lint/float_comparison.rb +32 -10
- data/lib/rubocop/cop/lint/identity_comparison.rb +19 -15
- data/lib/rubocop/cop/lint/literal_as_condition.rb +124 -10
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +17 -8
- data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -2
- data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -3
- data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +1 -0
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
- data/lib/rubocop/cop/lint/raise_exception.rb +29 -10
- data/lib/rubocop/cop/lint/redundant_regexp_quantifiers.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +0 -21
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +101 -2
- data/lib/rubocop/cop/lint/redundant_type_conversion.rb +43 -13
- data/lib/rubocop/cop/lint/redundant_with_index.rb +3 -0
- data/lib/rubocop/cop/lint/redundant_with_object.rb +3 -0
- data/lib/rubocop/cop/lint/require_range_parentheses.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -4
- data/lib/rubocop/cop/lint/rescue_type.rb +1 -1
- data/lib/rubocop/cop/lint/return_in_void_context.rb +9 -11
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +4 -4
- data/lib/rubocop/cop/lint/self_assignment.rb +31 -5
- data/lib/rubocop/cop/lint/shadowed_argument.rb +7 -7
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +13 -1
- data/lib/rubocop/cop/lint/shared_mutable_default.rb +12 -1
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +2 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
- data/lib/rubocop/cop/lint/unreachable_loop.rb +5 -5
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +2 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +30 -4
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -0
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +9 -12
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +90 -0
- data/lib/rubocop/cop/lint/useless_numeric_operation.rb +1 -0
- data/lib/rubocop/cop/lint/useless_or.rb +98 -0
- data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +3 -3
- data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +121 -0
- data/lib/rubocop/cop/lint/void.rb +16 -2
- data/lib/rubocop/cop/message_annotator.rb +7 -3
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/block_length.rb +1 -0
- data/lib/rubocop/cop/metrics/method_length.rb +1 -0
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/allowed_pattern.rb +4 -4
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -3
- data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +2 -2
- data/lib/rubocop/cop/mixin/def_node.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -7
- data/lib/rubocop/cop/mixin/forbidden_identifiers.rb +20 -0
- data/lib/rubocop/cop/mixin/forbidden_pattern.rb +16 -0
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -2
- data/lib/rubocop/cop/mixin/gemspec_help.rb +22 -0
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +15 -14
- data/lib/rubocop/cop/mixin/hash_subset.rb +19 -4
- data/lib/rubocop/cop/mixin/line_length_help.rb +24 -8
- data/lib/rubocop/cop/mixin/method_complexity.rb +1 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -0
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +12 -0
- data/lib/rubocop/cop/mixin/target_ruby_version.rb +1 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +18 -2
- data/lib/rubocop/cop/naming/block_forwarding.rb +3 -3
- data/lib/rubocop/cop/naming/file_name.rb +2 -2
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +187 -15
- data/lib/rubocop/cop/naming/predicate_method.rb +319 -0
- data/lib/rubocop/cop/naming/{predicate_name.rb → predicate_prefix.rb} +4 -4
- data/lib/rubocop/cop/naming/variable_name.rb +51 -6
- data/lib/rubocop/cop/registry.rb +9 -6
- data/lib/rubocop/cop/security/eval.rb +2 -1
- data/lib/rubocop/cop/security/json_load.rb +33 -11
- data/lib/rubocop/cop/security/open.rb +1 -0
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +32 -10
- data/lib/rubocop/cop/style/accessor_grouping.rb +32 -6
- data/lib/rubocop/cop/style/arguments_forwarding.rb +21 -24
- data/lib/rubocop/cop/style/array_intersect.rb +113 -38
- data/lib/rubocop/cop/style/array_intersect_with_single_element.rb +47 -0
- data/lib/rubocop/cop/style/bitwise_predicate.rb +8 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +3 -2
- data/lib/rubocop/cop/style/case_like_if.rb +1 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +48 -10
- data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
- data/lib/rubocop/cop/style/collection_methods.rb +1 -0
- data/lib/rubocop/cop/style/collection_querying.rb +167 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +1 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +12 -5
- data/lib/rubocop/cop/style/comparable_between.rb +78 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +26 -8
- data/lib/rubocop/cop/style/constant_visibility.rb +14 -9
- data/lib/rubocop/cop/style/data_inheritance.rb +7 -0
- data/lib/rubocop/cop/style/def_with_parentheses.rb +18 -5
- data/lib/rubocop/cop/style/dig_chain.rb +1 -1
- data/lib/rubocop/cop/style/double_negation.rb +3 -3
- data/lib/rubocop/cop/style/empty_literal.rb +4 -0
- data/lib/rubocop/cop/style/empty_string_inside_interpolation.rb +100 -0
- data/lib/rubocop/cop/style/endless_method.rb +176 -18
- data/lib/rubocop/cop/style/eval_with_location.rb +3 -3
- data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -7
- data/lib/rubocop/cop/style/explicit_block_argument.rb +3 -3
- data/lib/rubocop/cop/style/exponential_notation.rb +5 -4
- data/lib/rubocop/cop/style/fetch_env_var.rb +32 -6
- data/lib/rubocop/cop/style/float_division.rb +15 -1
- data/lib/rubocop/cop/style/for.rb +1 -0
- data/lib/rubocop/cop/style/format_string_token.rb +38 -11
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -2
- data/lib/rubocop/cop/style/global_std_stream.rb +3 -0
- data/lib/rubocop/cop/style/guard_clause.rb +2 -1
- data/lib/rubocop/cop/style/hash_conversion.rb +16 -8
- data/lib/rubocop/cop/style/hash_each_methods.rb +3 -2
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +104 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +4 -1
- data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
- data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +3 -3
- data/lib/rubocop/cop/style/if_inside_else.rb +10 -13
- data/lib/rubocop/cop/style/if_unless_modifier.rb +35 -8
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +4 -7
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
- data/lib/rubocop/cop/style/infinite_loop.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +10 -6
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +2 -2
- data/lib/rubocop/cop/style/ip_addresses.rb +2 -2
- data/lib/rubocop/cop/style/it_assignment.rb +69 -12
- data/lib/rubocop/cop/style/it_block_parameter.rb +121 -0
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +13 -7
- data/lib/rubocop/cop/style/lambda.rb +1 -0
- data/lib/rubocop/cop/style/lambda_call.rb +7 -2
- data/lib/rubocop/cop/style/line_end_concatenation.rb +10 -4
- data/lib/rubocop/cop/style/map_into_array.rb +4 -1
- data/lib/rubocop/cop/style/map_to_hash.rb +12 -3
- data/lib/rubocop/cop/style/map_to_set.rb +1 -3
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +9 -8
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +16 -0
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +2 -1
- data/lib/rubocop/cop/style/min_max_comparison.rb +13 -5
- data/lib/rubocop/cop/style/multiline_block_chain.rb +2 -1
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +2 -0
- data/lib/rubocop/cop/style/multiline_method_signature.rb +1 -9
- data/lib/rubocop/cop/style/next.rb +44 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +9 -7
- data/lib/rubocop/cop/style/object_then.rb +1 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +17 -9
- data/lib/rubocop/cop/style/parallel_assignment.rb +32 -20
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -0
- data/lib/rubocop/cop/style/raise_args.rb +8 -8
- data/lib/rubocop/cop/style/redundant_array_flatten.rb +50 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +35 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +57 -0
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +14 -4
- data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -9
- data/lib/rubocop/cop/style/redundant_format.rb +79 -18
- data/lib/rubocop/cop/style/redundant_freeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_interpolation.rb +12 -3
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +1 -4
- data/lib/rubocop/cop/style/redundant_parentheses.rb +73 -18
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +4 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -0
- data/lib/rubocop/cop/style/redundant_self.rb +9 -5
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/redundant_sort_by.rb +17 -1
- data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +3 -0
- data/lib/rubocop/cop/style/return_nil.rb +2 -2
- data/lib/rubocop/cop/style/safe_navigation.rb +61 -14
- data/lib/rubocop/cop/style/select_by_regexp.rb +4 -1
- data/lib/rubocop/cop/style/semicolon.rb +23 -7
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +3 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +10 -7
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +75 -101
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/string_concatenation.rb +18 -15
- data/lib/rubocop/cop/style/struct_inheritance.rb +8 -1
- data/lib/rubocop/cop/style/super_arguments.rb +1 -2
- data/lib/rubocop/cop/style/symbol_array.rb +1 -1
- data/lib/rubocop/cop/style/symbol_proc.rb +3 -1
- data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -0
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +52 -1
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +47 -6
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +48 -6
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unless_else.rb +10 -9
- data/lib/rubocop/cop/team.rb +1 -1
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/utils/format_string.rb +15 -2
- data/lib/rubocop/cop/variable_force/assignment.rb +7 -3
- data/lib/rubocop/cop/variable_force/scope.rb +1 -1
- data/lib/rubocop/cop/variable_force/variable.rb +3 -8
- data/lib/rubocop/cop/variable_force.rb +26 -9
- data/lib/rubocop/cops_documentation_generator.rb +23 -7
- data/lib/rubocop/directive_comment.rb +1 -1
- data/lib/rubocop/ext/regexp_node.rb +0 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +19 -5
- data/lib/rubocop/formatter/fuubar_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +1 -1
- data/lib/rubocop/formatter/markdown_formatter.rb +1 -0
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/pacman_formatter.rb +2 -1
- data/lib/rubocop/lsp/diagnostic.rb +25 -24
- data/lib/rubocop/lsp/routes.rb +65 -9
- data/lib/rubocop/lsp/runtime.rb +5 -5
- data/lib/rubocop/lsp/server.rb +2 -2
- data/lib/rubocop/lsp/stdin_runner.rb +3 -17
- data/lib/rubocop/magic_comment.rb +8 -0
- data/lib/rubocop/pending_cops_reporter.rb +56 -0
- data/lib/rubocop/plugin/configuration_integrator.rb +2 -0
- data/lib/rubocop/plugin/load_error.rb +1 -1
- data/lib/rubocop/plugin.rb +9 -2
- data/lib/rubocop/result_cache.rb +14 -12
- data/lib/rubocop/rspec/cop_helper.rb +6 -1
- data/lib/rubocop/rspec/expect_offense.rb +9 -3
- data/lib/rubocop/rspec/shared_contexts.rb +34 -0
- data/lib/rubocop/rspec/support.rb +3 -0
- data/lib/rubocop/runner.rb +10 -4
- data/lib/rubocop/server/cache.rb +17 -12
- data/lib/rubocop/server/client_command/base.rb +10 -0
- data/lib/rubocop/server/client_command/exec.rb +2 -1
- data/lib/rubocop/server/client_command/start.rb +11 -1
- data/lib/rubocop/target_finder.rb +13 -9
- data/lib/rubocop/target_ruby.rb +11 -2
- data/lib/rubocop/version.rb +14 -7
- data/lib/rubocop.rb +17 -2
- data/lib/ruby_lsp/rubocop/addon.rb +25 -10
- data/lib/ruby_lsp/rubocop/runtime_adapter.rb +57 -5
- metadata +24 -8
- data/lib/rubocop/cop/utils/regexp_ranges.rb +0 -113
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module Naming
|
|
6
|
+
# Checks that predicate methods end with `?` and non-predicate methods do not.
|
|
7
|
+
#
|
|
8
|
+
# The names of predicate methods (methods that return a boolean value) should end
|
|
9
|
+
# in a question mark. Methods that don't return a boolean, shouldn't
|
|
10
|
+
# end in a question mark.
|
|
11
|
+
#
|
|
12
|
+
# The cop assesses a predicate method as one that returns boolean values. Likewise,
|
|
13
|
+
# a method that only returns literal values is assessed as non-predicate. Other predicate
|
|
14
|
+
# method calls are assumed to return boolean values. The cop does not make an assessment
|
|
15
|
+
# if the return type is unknown (non-predicate method calls, variables, etc.).
|
|
16
|
+
#
|
|
17
|
+
# NOTE: The `initialize` method and operator methods (`def ==`, etc.) are ignored.
|
|
18
|
+
#
|
|
19
|
+
# By default, the cop runs in `conservative` mode, which allows a method to be named
|
|
20
|
+
# with a question mark as long as at least one return value is boolean. In `aggressive`
|
|
21
|
+
# mode, methods with a question mark will register an offense if any known non-boolean
|
|
22
|
+
# return values are detected.
|
|
23
|
+
#
|
|
24
|
+
# The cop also has `AllowedMethods` configuration in order to prevent the cop from
|
|
25
|
+
# registering an offense from a method name that does not confirm to the naming
|
|
26
|
+
# guidelines. By default, `call` is allowed. The cop also has `AllowedPatterns`
|
|
27
|
+
# configuration to allow method names by regular expression.
|
|
28
|
+
#
|
|
29
|
+
# Although returning a call to another predicate method is treated as a boolean value,
|
|
30
|
+
# certain method names can be known to not return a boolean, despite ending in a `?`
|
|
31
|
+
# (for example, `Numeric#nonzero?` returns `self` or `nil`). These methods can be
|
|
32
|
+
# configured using `NonBooleanPredicates`.
|
|
33
|
+
#
|
|
34
|
+
# The cop can furthermore be configured to allow all bang methods (method names
|
|
35
|
+
# ending with `!`), with `AllowBangMethods: true` (default false).
|
|
36
|
+
#
|
|
37
|
+
# @example Mode: conservative (default)
|
|
38
|
+
# # bad
|
|
39
|
+
# def foo
|
|
40
|
+
# bar == baz
|
|
41
|
+
# end
|
|
42
|
+
#
|
|
43
|
+
# # good
|
|
44
|
+
# def foo?
|
|
45
|
+
# bar == baz
|
|
46
|
+
# end
|
|
47
|
+
#
|
|
48
|
+
# # bad
|
|
49
|
+
# def foo?
|
|
50
|
+
# 5
|
|
51
|
+
# end
|
|
52
|
+
#
|
|
53
|
+
# # good
|
|
54
|
+
# def foo
|
|
55
|
+
# 5
|
|
56
|
+
# end
|
|
57
|
+
#
|
|
58
|
+
# # bad
|
|
59
|
+
# def foo
|
|
60
|
+
# x == y
|
|
61
|
+
# end
|
|
62
|
+
#
|
|
63
|
+
# # good
|
|
64
|
+
# def foo?
|
|
65
|
+
# x == y
|
|
66
|
+
# end
|
|
67
|
+
#
|
|
68
|
+
# # bad
|
|
69
|
+
# def foo
|
|
70
|
+
# !x
|
|
71
|
+
# end
|
|
72
|
+
#
|
|
73
|
+
# # good
|
|
74
|
+
# def foo?
|
|
75
|
+
# !x
|
|
76
|
+
# end
|
|
77
|
+
#
|
|
78
|
+
# # bad - returns the value of another predicate method
|
|
79
|
+
# def foo
|
|
80
|
+
# bar?
|
|
81
|
+
# end
|
|
82
|
+
#
|
|
83
|
+
# # good
|
|
84
|
+
# def foo?
|
|
85
|
+
# bar?
|
|
86
|
+
# end
|
|
87
|
+
#
|
|
88
|
+
# # good - operator method
|
|
89
|
+
# def ==(other)
|
|
90
|
+
# hash == other.hash
|
|
91
|
+
# end
|
|
92
|
+
#
|
|
93
|
+
# # good - at least one return value is boolean
|
|
94
|
+
# def foo?
|
|
95
|
+
# return unless bar?
|
|
96
|
+
# true
|
|
97
|
+
# end
|
|
98
|
+
#
|
|
99
|
+
# # ok - return type is not known
|
|
100
|
+
# def foo?
|
|
101
|
+
# bar
|
|
102
|
+
# end
|
|
103
|
+
#
|
|
104
|
+
# # ok - return type is not known
|
|
105
|
+
# def foo
|
|
106
|
+
# bar?
|
|
107
|
+
# end
|
|
108
|
+
#
|
|
109
|
+
# @example Mode: aggressive
|
|
110
|
+
# # bad - the method returns nil in some cases
|
|
111
|
+
# def foo?
|
|
112
|
+
# return unless bar?
|
|
113
|
+
# true
|
|
114
|
+
# end
|
|
115
|
+
#
|
|
116
|
+
# @example AllowedMethods: [call] (default)
|
|
117
|
+
# # good
|
|
118
|
+
# def call
|
|
119
|
+
# foo == bar
|
|
120
|
+
# end
|
|
121
|
+
#
|
|
122
|
+
# @example AllowedPatterns: [\Afoo]
|
|
123
|
+
# # good
|
|
124
|
+
# def foo?
|
|
125
|
+
# 'foo'
|
|
126
|
+
# end
|
|
127
|
+
#
|
|
128
|
+
# @example AllowBangMethods: false (default)
|
|
129
|
+
# # bad
|
|
130
|
+
# def save!
|
|
131
|
+
# true
|
|
132
|
+
# end
|
|
133
|
+
#
|
|
134
|
+
# @example AllowBangMethods: true
|
|
135
|
+
# # good
|
|
136
|
+
# def save!
|
|
137
|
+
# true
|
|
138
|
+
# end
|
|
139
|
+
#
|
|
140
|
+
class PredicateMethod < Base
|
|
141
|
+
include AllowedMethods
|
|
142
|
+
include AllowedPattern
|
|
143
|
+
|
|
144
|
+
MSG_PREDICATE = 'Predicate method names should end with `?`.'
|
|
145
|
+
MSG_NON_PREDICATE = 'Non-predicate method names should not end with `?`.'
|
|
146
|
+
|
|
147
|
+
def on_def(node)
|
|
148
|
+
return if allowed?(node)
|
|
149
|
+
|
|
150
|
+
return_values = return_values(node.body)
|
|
151
|
+
return if acceptable?(return_values)
|
|
152
|
+
|
|
153
|
+
if node.predicate_method? && potential_non_predicate?(return_values)
|
|
154
|
+
add_offense(node.loc.name, message: MSG_NON_PREDICATE)
|
|
155
|
+
elsif !node.predicate_method? && all_return_values_boolean?(return_values)
|
|
156
|
+
add_offense(node.loc.name, message: MSG_PREDICATE)
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
alias on_defs on_def
|
|
160
|
+
|
|
161
|
+
private
|
|
162
|
+
|
|
163
|
+
def allowed?(node)
|
|
164
|
+
node.method?(:initialize) ||
|
|
165
|
+
allowed_method?(node.method_name) ||
|
|
166
|
+
matches_allowed_pattern?(node.method_name) ||
|
|
167
|
+
allowed_bang_method?(node) ||
|
|
168
|
+
node.operator_method? ||
|
|
169
|
+
node.body.nil?
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def acceptable?(return_values)
|
|
173
|
+
# In `conservative` mode, if the method returns `super`, `zsuper`, or a
|
|
174
|
+
# non-comparison method call, the method name is acceptable.
|
|
175
|
+
return false unless conservative?
|
|
176
|
+
|
|
177
|
+
return_values.any? do |value|
|
|
178
|
+
value.type?(:super, :zsuper) || unknown_method_call?(value)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def unknown_method_call?(value)
|
|
183
|
+
return false unless value.call_type?
|
|
184
|
+
|
|
185
|
+
!method_returning_boolean?(value)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def return_values(node)
|
|
189
|
+
# Collect all the (implicit and explicit) return values of a node
|
|
190
|
+
return_values = Set.new(node.begin_type? ? [] : [extract_return_value(node)])
|
|
191
|
+
|
|
192
|
+
node.each_descendant(:return) do |return_node|
|
|
193
|
+
return_values << extract_return_value(return_node)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
return_values << last_value(node)
|
|
197
|
+
|
|
198
|
+
process_return_values(return_values)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
def all_return_values_boolean?(return_values)
|
|
202
|
+
values = return_values.reject { |value| value.type?(:super, :zsuper) }
|
|
203
|
+
return false if values.empty?
|
|
204
|
+
|
|
205
|
+
values.all? { |value| boolean_return?(value) }
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def boolean_return?(value)
|
|
209
|
+
return true if value.boolean_type?
|
|
210
|
+
|
|
211
|
+
method_returning_boolean?(value)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def method_returning_boolean?(value)
|
|
215
|
+
return false unless value.call_type?
|
|
216
|
+
return false if wayward_predicate?(value.method_name)
|
|
217
|
+
|
|
218
|
+
value.comparison_method? || value.predicate_method? || value.negation_method?
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def potential_non_predicate?(return_values)
|
|
222
|
+
# Assumes a method to be non-predicate if all return values are non-boolean literals.
|
|
223
|
+
#
|
|
224
|
+
# In `Mode: conservative`, if any of the return values is a boolean,
|
|
225
|
+
# the method name is acceptable.
|
|
226
|
+
# In `Mode: aggressive`, all return values must be booleans for a predicate
|
|
227
|
+
# method, or else an offense will be registered.
|
|
228
|
+
return false if conservative? && return_values.any? { |value| boolean_return?(value) }
|
|
229
|
+
|
|
230
|
+
return_values.any? do |value|
|
|
231
|
+
value.literal? && !value.boolean_type?
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def extract_return_value(node)
|
|
236
|
+
return node unless node.return_type?
|
|
237
|
+
|
|
238
|
+
# `return` without a value is a `nil` return.
|
|
239
|
+
return s(:nil) if node.arguments.empty?
|
|
240
|
+
|
|
241
|
+
# When there's a multiple return, it cannot be a predicate
|
|
242
|
+
# so just return an `array` sexp for simplicity.
|
|
243
|
+
return s(:array) unless node.arguments.one?
|
|
244
|
+
|
|
245
|
+
node.first_argument
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def last_value(node)
|
|
249
|
+
value = node.begin_type? ? node.children.last || s(:nil) : node
|
|
250
|
+
|
|
251
|
+
value.return_type? ? extract_return_value(value) : value
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
def process_return_values(return_values)
|
|
255
|
+
return_values.flat_map do |value|
|
|
256
|
+
if value.conditional?
|
|
257
|
+
process_return_values(extract_conditional_branches(value))
|
|
258
|
+
elsif and_or?(value)
|
|
259
|
+
process_return_values(extract_and_or_clauses(value))
|
|
260
|
+
else
|
|
261
|
+
value
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def and_or?(node)
|
|
267
|
+
node.type?(:and, :or)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def extract_and_or_clauses(node)
|
|
271
|
+
# Recursively traverse an `and` or `or` node to collect all clauses within
|
|
272
|
+
return node unless and_or?(node)
|
|
273
|
+
|
|
274
|
+
[extract_and_or_clauses(node.lhs), extract_and_or_clauses(node.rhs)].flatten
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def extract_conditional_branches(node)
|
|
278
|
+
return node unless node.conditional?
|
|
279
|
+
|
|
280
|
+
if node.type?(:while, :until)
|
|
281
|
+
# If there is no body, act as implicit `nil`.
|
|
282
|
+
node.body ? [last_value(node.body)] : [s(:nil)]
|
|
283
|
+
else
|
|
284
|
+
# Branches with no value act as an implicit `nil`.
|
|
285
|
+
branches = node.branches.map { |branch| branch ? last_value(branch) : s(:nil) }
|
|
286
|
+
# Missing else branches also act as an implicit `nil`.
|
|
287
|
+
branches.push(s(:nil)) unless node.else_branch
|
|
288
|
+
branches
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def conservative?
|
|
293
|
+
cop_config.fetch('Mode', :conservative).to_sym == :conservative
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def allowed_bang_method?(node)
|
|
297
|
+
return false unless allow_bang_methods?
|
|
298
|
+
|
|
299
|
+
node.bang_method?
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
def allow_bang_methods?
|
|
303
|
+
cop_config.fetch('AllowBangMethods', false)
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
# If a method ending in `?` is known to not return a boolean value,
|
|
307
|
+
# (for example, `Numeric#nonzero?`) it should be treated as a non-boolean
|
|
308
|
+
# value, despite the method naming.
|
|
309
|
+
def wayward_predicate?(name)
|
|
310
|
+
wayward_predicates.include?(name.to_s)
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def wayward_predicates
|
|
314
|
+
Array(cop_config.fetch('WaywardPredicates', []))
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
end
|
|
319
|
+
end
|
|
@@ -100,12 +100,12 @@ module RuboCop
|
|
|
100
100
|
# # good
|
|
101
101
|
# def_node_matcher(:even?) { |value| }
|
|
102
102
|
#
|
|
103
|
-
class
|
|
103
|
+
class PredicatePrefix < Base
|
|
104
104
|
include AllowedMethods
|
|
105
105
|
|
|
106
106
|
# @!method dynamic_method_define(node)
|
|
107
107
|
def_node_matcher :dynamic_method_define, <<~PATTERN
|
|
108
|
-
(send nil? #
|
|
108
|
+
(send nil? #method_definition_macro?
|
|
109
109
|
(sym $_)
|
|
110
110
|
...)
|
|
111
111
|
PATTERN
|
|
@@ -143,7 +143,7 @@ module RuboCop
|
|
|
143
143
|
next if predicate_prefixes.include?(forbidden_prefix)
|
|
144
144
|
|
|
145
145
|
raise ValidationError, <<~MSG.chomp
|
|
146
|
-
The `Naming/
|
|
146
|
+
The `Naming/PredicatePrefix` cop is misconfigured. Prefix #{forbidden_prefix} must be included in NamePrefix because it is included in ForbiddenPrefixes.
|
|
147
147
|
MSG
|
|
148
148
|
end
|
|
149
149
|
end
|
|
@@ -195,7 +195,7 @@ module RuboCop
|
|
|
195
195
|
cop_config['UseSorbetSigs']
|
|
196
196
|
end
|
|
197
197
|
|
|
198
|
-
def
|
|
198
|
+
def method_definition_macro?(macro_name)
|
|
199
199
|
cop_config['MethodDefinitionMacros'].include?(macro_name.to_s)
|
|
200
200
|
end
|
|
201
201
|
end
|
|
@@ -3,8 +3,15 @@
|
|
|
3
3
|
module RuboCop
|
|
4
4
|
module Cop
|
|
5
5
|
module Naming
|
|
6
|
-
#
|
|
7
|
-
#
|
|
6
|
+
# Checks that the configured style (snake_case or camelCase) is used for all variable names.
|
|
7
|
+
# This includes local variables, instance variables, class variables, method arguments
|
|
8
|
+
# (positional, keyword, rest or block), and block arguments.
|
|
9
|
+
#
|
|
10
|
+
# The cop can also be configured to forbid using specific names for variables, using
|
|
11
|
+
# `ForbiddenIdentifiers` or `ForbiddenPatterns`. In addition to the above, this applies
|
|
12
|
+
# to global variables as well.
|
|
13
|
+
#
|
|
14
|
+
# Method definitions and method calls are not affected by this cop.
|
|
8
15
|
#
|
|
9
16
|
# @example EnforcedStyle: snake_case (default)
|
|
10
17
|
# # bad
|
|
@@ -26,24 +33,45 @@ module RuboCop
|
|
|
26
33
|
#
|
|
27
34
|
# @example AllowedPatterns: ['_v\d+\z']
|
|
28
35
|
# # good (with EnforcedStyle: camelCase)
|
|
29
|
-
#
|
|
36
|
+
# release_v1 = true
|
|
37
|
+
#
|
|
38
|
+
# @example ForbiddenIdentifiers: ['fooBar']
|
|
39
|
+
# # bad (in all cases)
|
|
40
|
+
# fooBar = 1
|
|
41
|
+
# @fooBar = 1
|
|
42
|
+
# @@fooBar = 1
|
|
43
|
+
# $fooBar = 1
|
|
44
|
+
#
|
|
45
|
+
# @example ForbiddenPatterns: ['_v\d+\z']
|
|
46
|
+
# # bad (in all cases)
|
|
47
|
+
# release_v1 = true
|
|
48
|
+
# @release_v1 = true
|
|
49
|
+
# @@release_v1 = true
|
|
50
|
+
# $release_v1 = true
|
|
30
51
|
#
|
|
31
52
|
class VariableName < Base
|
|
32
53
|
include AllowedIdentifiers
|
|
33
54
|
include ConfigurableNaming
|
|
34
55
|
include AllowedPattern
|
|
56
|
+
include ForbiddenIdentifiers
|
|
57
|
+
include ForbiddenPattern
|
|
35
58
|
|
|
36
59
|
MSG = 'Use %<style>s for variable names.'
|
|
60
|
+
MSG_FORBIDDEN = '`%<identifier>s` is forbidden, use another name instead.'
|
|
37
61
|
|
|
38
62
|
def valid_name?(node, name, given_style = style)
|
|
39
63
|
super || matches_allowed_pattern?(name)
|
|
40
64
|
end
|
|
41
65
|
|
|
42
66
|
def on_lvasgn(node)
|
|
43
|
-
return unless node.name
|
|
44
|
-
return if allowed_identifier?(
|
|
67
|
+
return unless (name = node.name)
|
|
68
|
+
return if allowed_identifier?(name)
|
|
45
69
|
|
|
46
|
-
|
|
70
|
+
if forbidden_name?(name)
|
|
71
|
+
register_forbidden_name(node)
|
|
72
|
+
else
|
|
73
|
+
check_name(node, name, node.loc.name)
|
|
74
|
+
end
|
|
47
75
|
end
|
|
48
76
|
alias on_ivasgn on_lvasgn
|
|
49
77
|
alias on_cvasgn on_lvasgn
|
|
@@ -56,11 +84,28 @@ module RuboCop
|
|
|
56
84
|
alias on_blockarg on_lvasgn
|
|
57
85
|
alias on_lvar on_lvasgn
|
|
58
86
|
|
|
87
|
+
# Only forbidden names are checked for global variable assignment
|
|
88
|
+
def on_gvasgn(node)
|
|
89
|
+
return unless (name = node.name)
|
|
90
|
+
return unless forbidden_name?(name)
|
|
91
|
+
|
|
92
|
+
register_forbidden_name(node)
|
|
93
|
+
end
|
|
94
|
+
|
|
59
95
|
private
|
|
60
96
|
|
|
97
|
+
def forbidden_name?(name)
|
|
98
|
+
forbidden_identifier?(name) || forbidden_pattern?(name)
|
|
99
|
+
end
|
|
100
|
+
|
|
61
101
|
def message(style)
|
|
62
102
|
format(MSG, style: style)
|
|
63
103
|
end
|
|
104
|
+
|
|
105
|
+
def register_forbidden_name(node)
|
|
106
|
+
message = format(MSG_FORBIDDEN, identifier: node.name)
|
|
107
|
+
add_offense(node.loc.name, message: message)
|
|
108
|
+
end
|
|
64
109
|
end
|
|
65
110
|
end
|
|
66
111
|
end
|
data/lib/rubocop/cop/registry.rb
CHANGED
|
@@ -23,8 +23,8 @@ module RuboCop
|
|
|
23
23
|
global.without_department(:Test).cops
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
def self.qualified_cop_name(name, origin)
|
|
27
|
-
global.qualified_cop_name(name, origin)
|
|
26
|
+
def self.qualified_cop_name(name, origin, warn: true)
|
|
27
|
+
global.qualified_cop_name(name, origin, warn: warn)
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
# Changes momentarily the global registry
|
|
@@ -139,7 +139,7 @@ module RuboCop
|
|
|
139
139
|
|
|
140
140
|
case potential_badges.size
|
|
141
141
|
when 0 then name # No namespace found. Deal with it later in caller.
|
|
142
|
-
when 1 then resolve_badge(badge, potential_badges.first, path)
|
|
142
|
+
when 1 then resolve_badge(badge, potential_badges.first, path, warn: warn)
|
|
143
143
|
else raise AmbiguousCopName.new(badge, path, potential_badges)
|
|
144
144
|
end
|
|
145
145
|
end
|
|
@@ -296,11 +296,14 @@ module RuboCop
|
|
|
296
296
|
self.class.new(cops)
|
|
297
297
|
end
|
|
298
298
|
|
|
299
|
-
def resolve_badge(given_badge, real_badge, source_path)
|
|
299
|
+
def resolve_badge(given_badge, real_badge, source_path, warn: true)
|
|
300
300
|
unless given_badge.match?(real_badge)
|
|
301
301
|
path = PathUtil.smart_path(source_path)
|
|
302
|
-
|
|
303
|
-
|
|
302
|
+
|
|
303
|
+
if warn
|
|
304
|
+
warn("#{path}: #{given_badge} has the wrong namespace - " \
|
|
305
|
+
"replace it with #{given_badge.with_department(real_badge.department)}")
|
|
306
|
+
end
|
|
304
307
|
end
|
|
305
308
|
|
|
306
309
|
real_badge.to_s
|
|
@@ -11,13 +11,14 @@ module RuboCop
|
|
|
11
11
|
#
|
|
12
12
|
# eval(something)
|
|
13
13
|
# binding.eval(something)
|
|
14
|
+
# Kernel.eval(something)
|
|
14
15
|
class Eval < Base
|
|
15
16
|
MSG = 'The use of `eval` is a serious security risk.'
|
|
16
17
|
RESTRICT_ON_SEND = %i[eval].freeze
|
|
17
18
|
|
|
18
19
|
# @!method eval?(node)
|
|
19
20
|
def_node_matcher :eval?, <<~PATTERN
|
|
20
|
-
(send {nil? (send nil? :binding)} :eval $!str ...)
|
|
21
|
+
(send {nil? (send nil? :binding) (const {cbase nil?} :Kernel)} :eval $!str ...)
|
|
21
22
|
PATTERN
|
|
22
23
|
|
|
23
24
|
def on_send(node)
|
|
@@ -6,22 +6,40 @@ module RuboCop
|
|
|
6
6
|
# Checks for the use of JSON class methods which have potential
|
|
7
7
|
# security issues.
|
|
8
8
|
#
|
|
9
|
+
# `JSON.load` and similar methods allow deserialization of arbitrary ruby objects:
|
|
10
|
+
#
|
|
11
|
+
# [source,ruby]
|
|
12
|
+
# ----
|
|
13
|
+
# require 'json/add/string'
|
|
14
|
+
# result = JSON.load('{ "json_class": "String", "raw": [72, 101, 108, 108, 111] }')
|
|
15
|
+
# pp result # => "Hello"
|
|
16
|
+
# ----
|
|
17
|
+
#
|
|
18
|
+
# Never use `JSON.load` for untrusted user input. Prefer `JSON.parse` unless you have
|
|
19
|
+
# a concrete use-case for `JSON.load`.
|
|
20
|
+
#
|
|
21
|
+
# NOTE: Starting with `json` gem version 2.8.0, triggering this behavior without explicitly
|
|
22
|
+
# passing the `create_additions` keyword argument emits a deprecation warning, with the
|
|
23
|
+
# goal of being secure by default in the next major version 3.0.0.
|
|
24
|
+
#
|
|
9
25
|
# @safety
|
|
10
26
|
# This cop's autocorrection is unsafe because it's potentially dangerous.
|
|
11
|
-
# If using a stream, like `JSON.load(open('file'))`,
|
|
27
|
+
# If using a stream, like `JSON.load(open('file'))`, you will need to call
|
|
12
28
|
# `#read` manually, like `JSON.parse(open('file').read)`.
|
|
13
|
-
# If reading single values (rather than proper JSON objects), like
|
|
14
|
-
# `JSON.load('false')`, it will need to pass the `quirks_mode: true`
|
|
15
|
-
# option, like `JSON.parse('false', quirks_mode: true)`.
|
|
16
29
|
# Other similar issues may apply.
|
|
17
30
|
#
|
|
18
31
|
# @example
|
|
19
32
|
# # bad
|
|
20
|
-
# JSON.load(
|
|
21
|
-
# JSON.restore(
|
|
33
|
+
# JSON.load('{}')
|
|
34
|
+
# JSON.restore('{}')
|
|
22
35
|
#
|
|
23
36
|
# # good
|
|
24
|
-
# JSON.parse(
|
|
37
|
+
# JSON.parse('{}')
|
|
38
|
+
# JSON.unsafe_load('{}')
|
|
39
|
+
#
|
|
40
|
+
# # good - explicit use of `create_additions` option
|
|
41
|
+
# JSON.load('{}', create_additions: true)
|
|
42
|
+
# JSON.load('{}', create_additions: false)
|
|
25
43
|
#
|
|
26
44
|
class JSONLoad < Base
|
|
27
45
|
extend AutoCorrector
|
|
@@ -29,13 +47,17 @@ module RuboCop
|
|
|
29
47
|
MSG = 'Prefer `JSON.parse` over `JSON.%<method>s`.'
|
|
30
48
|
RESTRICT_ON_SEND = %i[load restore].freeze
|
|
31
49
|
|
|
32
|
-
# @!method
|
|
33
|
-
def_node_matcher :
|
|
34
|
-
(
|
|
50
|
+
# @!method insecure_json_load(node)
|
|
51
|
+
def_node_matcher :insecure_json_load, <<~PATTERN
|
|
52
|
+
(
|
|
53
|
+
send (const {nil? cbase} :JSON) ${:load :restore}
|
|
54
|
+
...
|
|
55
|
+
!(hash `(sym $:create_additions))
|
|
56
|
+
)
|
|
35
57
|
PATTERN
|
|
36
58
|
|
|
37
59
|
def on_send(node)
|
|
38
|
-
|
|
60
|
+
insecure_json_load(node) do |method|
|
|
39
61
|
add_offense(node.loc.selector, message: format(MSG, method: method)) do |corrector|
|
|
40
62
|
corrector.replace(node.loc.selector, 'parse')
|
|
41
63
|
end
|
|
@@ -195,15 +195,27 @@ module RuboCop
|
|
|
195
195
|
def autocorrect(corrector, node)
|
|
196
196
|
case style
|
|
197
197
|
when :group
|
|
198
|
-
|
|
199
|
-
return unless def_nodes.any?
|
|
200
|
-
|
|
201
|
-
replace_defs(corrector, node, def_nodes)
|
|
198
|
+
autocorrect_group_style(corrector, node)
|
|
202
199
|
when :inline
|
|
200
|
+
autocorrect_inline_style(corrector, node)
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def autocorrect_group_style(corrector, node)
|
|
205
|
+
def_nodes = find_corresponding_def_nodes(node)
|
|
206
|
+
return unless def_nodes.any?
|
|
207
|
+
|
|
208
|
+
replace_defs(corrector, node, def_nodes)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def autocorrect_inline_style(corrector, node)
|
|
212
|
+
if node.parent&.begin_type?
|
|
213
|
+
remove_modifier_node_within_begin(corrector, node, node.parent)
|
|
214
|
+
else
|
|
203
215
|
remove_nodes(corrector, node)
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
216
|
+
end
|
|
217
|
+
select_grouped_def_nodes(node).each do |grouped_def_node|
|
|
218
|
+
insert_inline_modifier(corrector, grouped_def_node, node.method_name)
|
|
207
219
|
end
|
|
208
220
|
end
|
|
209
221
|
|
|
@@ -224,9 +236,13 @@ module RuboCop
|
|
|
224
236
|
end
|
|
225
237
|
|
|
226
238
|
def offense?(node)
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
239
|
+
if group_style?
|
|
240
|
+
return false if node.parent ? node.parent.if_type? : access_modifier_with_symbol?(node)
|
|
241
|
+
|
|
242
|
+
access_modifier_is_inlined?(node) && !right_siblings_same_inline_method?(node)
|
|
243
|
+
else
|
|
244
|
+
access_modifier_is_not_inlined?(node) && select_grouped_def_nodes(node).any?
|
|
245
|
+
end
|
|
230
246
|
end
|
|
231
247
|
|
|
232
248
|
def correctable_group_offense?(node)
|
|
@@ -331,6 +347,12 @@ module RuboCop
|
|
|
331
347
|
end
|
|
332
348
|
end
|
|
333
349
|
|
|
350
|
+
def remove_modifier_node_within_begin(corrector, modifier_node, begin_node)
|
|
351
|
+
def_node = begin_node.children[begin_node.children.index(modifier_node) + 1]
|
|
352
|
+
range = modifier_node.source_range.begin.join(def_node.source_range.begin)
|
|
353
|
+
corrector.remove(range)
|
|
354
|
+
end
|
|
355
|
+
|
|
334
356
|
def def_source(node, def_nodes)
|
|
335
357
|
[
|
|
336
358
|
*processed_source.ast_with_comments[node].map(&:text),
|