rubocop 1.80.2 → 1.86.1
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/LICENSE.txt +1 -1
- data/README.md +2 -2
- data/config/default.yml +170 -19
- data/config/obsoletion.yml +9 -0
- data/lib/rubocop/cache_config.rb +29 -0
- data/lib/rubocop/cli/command/auto_generate_config.rb +3 -3
- data/lib/rubocop/cli/command/lsp.rb +1 -1
- data/lib/rubocop/cli/command/mcp.rb +19 -0
- data/lib/rubocop/cli/command/show_cops.rb +2 -2
- data/lib/rubocop/cli/command/show_docs_url.rb +1 -1
- data/lib/rubocop/cli.rb +28 -6
- data/lib/rubocop/comment_config.rb +62 -17
- data/lib/rubocop/config.rb +14 -10
- data/lib/rubocop/config_finder.rb +1 -1
- data/lib/rubocop/config_loader.rb +20 -21
- data/lib/rubocop/config_loader_resolver.rb +9 -7
- data/lib/rubocop/config_obsoletion/extracted_cop.rb +4 -2
- data/lib/rubocop/config_store.rb +6 -1
- data/lib/rubocop/config_validator.rb +1 -1
- data/lib/rubocop/cop/autocorrect_logic.rb +8 -4
- data/lib/rubocop/cop/bundler/gem_version.rb +28 -28
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -2
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +22 -6
- data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +2 -2
- data/lib/rubocop/cop/documentation.rb +2 -3
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -2
- data/lib/rubocop/cop/gemspec/require_mfa.rb +1 -1
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +10 -5
- data/lib/rubocop/cop/internal_affairs/example_heredoc_delimiter.rb +8 -8
- data/lib/rubocop/cop/internal_affairs/itblock_handler.rb +69 -0
- data/lib/rubocop/cop/internal_affairs/location_exists.rb +28 -2
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +9 -9
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups/ast_processor.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +3 -1
- data/lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +4 -4
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/case_indentation.rb +3 -1
- data/lib/rubocop/cop/layout/class_structure.rb +13 -6
- data/lib/rubocop/cop/layout/dot_position.rb +2 -2
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +12 -2
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +31 -13
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -0
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +12 -2
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +16 -2
- data/lib/rubocop/cop/layout/empty_lines_around_module_body.rb +16 -2
- data/lib/rubocop/cop/layout/end_alignment.rb +8 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +34 -1
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +26 -0
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +7 -1
- data/lib/rubocop/cop/layout/first_hash_element_line_break.rb +25 -25
- data/lib/rubocop/cop/layout/hash_alignment.rb +3 -6
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +33 -3
- data/lib/rubocop/cop/layout/indentation_style.rb +1 -1
- data/lib/rubocop/cop/layout/indentation_width.rb +111 -7
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +26 -9
- data/lib/rubocop/cop/layout/multiline_array_brace_layout.rb +57 -57
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +9 -2
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -0
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +56 -56
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +204 -39
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -4
- data/lib/rubocop/cop/layout/parameter_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +13 -3
- data/lib/rubocop/cop/layout/space_after_comma.rb +2 -10
- data/lib/rubocop/cop/layout/space_after_semicolon.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +4 -2
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -8
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +1 -1
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +47 -3
- data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +3 -2
- data/lib/rubocop/cop/lint/constant_reassignment.rb +59 -9
- data/lib/rubocop/cop/lint/constant_resolution.rb +1 -1
- data/lib/rubocop/cop/lint/cop_directive_syntax.rb +14 -8
- data/lib/rubocop/cop/lint/data_define_override.rb +63 -0
- data/lib/rubocop/cop/lint/debugger.rb +0 -2
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +4 -1
- data/lib/rubocop/cop/lint/duplicate_match_pattern.rb +4 -4
- data/lib/rubocop/cop/lint/duplicate_methods.rb +111 -12
- data/lib/rubocop/cop/lint/else_layout.rb +19 -0
- data/lib/rubocop/cop/lint/empty_block.rb +1 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +6 -1
- data/lib/rubocop/cop/lint/empty_in_pattern.rb +8 -1
- data/lib/rubocop/cop/lint/empty_interpolation.rb +11 -0
- data/lib/rubocop/cop/lint/empty_when.rb +8 -1
- data/lib/rubocop/cop/lint/float_comparison.rb +1 -1
- data/lib/rubocop/cop/lint/interpolation_check.rb +7 -2
- data/lib/rubocop/cop/lint/literal_as_condition.rb +5 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +16 -6
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +2 -0
- data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +4 -0
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -1
- data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +23 -9
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +0 -9
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +4 -2
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +23 -6
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +8 -2
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -4
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -0
- data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +7 -1
- data/lib/rubocop/cop/lint/self_assignment.rb +10 -2
- data/lib/rubocop/cop/lint/shadowed_argument.rb +7 -7
- data/lib/rubocop/cop/lint/struct_new_override.rb +17 -1
- data/lib/rubocop/cop/lint/syntax.rb +25 -1
- data/lib/rubocop/cop/lint/to_json.rb +12 -16
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -0
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +1 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +5 -3
- data/lib/rubocop/cop/lint/unreachable_pattern_branch.rb +113 -0
- data/lib/rubocop/cop/lint/unused_method_argument.rb +10 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +45 -17
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +4 -4
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +2 -0
- data/lib/rubocop/cop/lint/useless_or.rb +15 -2
- data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +24 -9
- data/lib/rubocop/cop/lint/void.rb +39 -12
- data/lib/rubocop/cop/message_annotator.rb +1 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +23 -0
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +4 -3
- data/lib/rubocop/cop/migration/department_name.rb +12 -1
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
- data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +4 -6
- data/lib/rubocop/cop/mixin/code_length.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +5 -5
- data/lib/rubocop/cop/mixin/hash_transform_method/autocorrection.rb +63 -0
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +10 -60
- data/lib/rubocop/cop/mixin/line_length_help.rb +21 -2
- data/lib/rubocop/cop/mixin/method_complexity.rb +1 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +1 -1
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +5 -4
- data/lib/rubocop/cop/mixin/statement_modifier.rb +0 -6
- data/lib/rubocop/cop/mixin/trailing_comma.rb +8 -5
- data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +4 -2
- data/lib/rubocop/cop/naming/predicate_method.rb +27 -4
- data/lib/rubocop/cop/naming/predicate_prefix.rb +11 -11
- data/lib/rubocop/cop/offense.rb +9 -1
- data/lib/rubocop/cop/registry.rb +20 -13
- data/lib/rubocop/cop/security/eval.rb +15 -2
- data/lib/rubocop/cop/security/json_load.rb +33 -11
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +15 -4
- data/lib/rubocop/cop/style/accessor_grouping.rb +4 -2
- data/lib/rubocop/cop/style/alias.rb +4 -1
- data/lib/rubocop/cop/style/and_or.rb +1 -0
- data/lib/rubocop/cop/style/arguments_forwarding.rb +25 -7
- data/lib/rubocop/cop/style/array_intersect.rb +2 -2
- data/lib/rubocop/cop/style/array_intersect_with_single_element.rb +47 -0
- data/lib/rubocop/cop/style/array_join.rb +4 -2
- data/lib/rubocop/cop/style/ascii_comments.rb +6 -3
- data/lib/rubocop/cop/style/attr.rb +5 -2
- data/lib/rubocop/cop/style/bare_percent_literals.rb +4 -3
- data/lib/rubocop/cop/style/begin_block.rb +3 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +27 -34
- data/lib/rubocop/cop/style/case_equality.rb +15 -13
- data/lib/rubocop/cop/style/class_and_module_children.rb +11 -2
- data/lib/rubocop/cop/style/collection_compact.rb +36 -16
- data/lib/rubocop/cop/style/colon_method_call.rb +3 -1
- data/lib/rubocop/cop/style/concat_array_literals.rb +2 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +8 -18
- data/lib/rubocop/cop/style/constant_visibility.rb +17 -12
- data/lib/rubocop/cop/style/copyright.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +6 -6
- data/lib/rubocop/cop/style/documentation_method.rb +8 -8
- data/lib/rubocop/cop/style/double_negation.rb +1 -1
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
- data/lib/rubocop/cop/style/each_with_object.rb +2 -0
- data/lib/rubocop/cop/style/empty_block_parameter.rb +1 -1
- data/lib/rubocop/cop/style/empty_class_definition.rb +119 -0
- data/lib/rubocop/cop/style/empty_lambda_parameter.rb +1 -1
- data/lib/rubocop/cop/style/empty_method.rb +0 -6
- data/lib/rubocop/cop/style/encoding.rb +7 -1
- data/lib/rubocop/cop/style/end_block.rb +3 -1
- data/lib/rubocop/cop/style/endless_method.rb +23 -5
- data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
- data/lib/rubocop/cop/style/file_open.rb +84 -0
- data/lib/rubocop/cop/style/float_division.rb +15 -1
- data/lib/rubocop/cop/style/for.rb +3 -0
- data/lib/rubocop/cop/style/format_string_token.rb +49 -5
- data/lib/rubocop/cop/style/global_vars.rb +5 -2
- data/lib/rubocop/cop/style/guard_clause.rb +27 -22
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +27 -9
- data/lib/rubocop/cop/style/hash_lookup_method.rb +101 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
- data/lib/rubocop/cop/style/hash_transform_keys.rb +17 -7
- data/lib/rubocop/cop/style/hash_transform_values.rb +17 -7
- data/lib/rubocop/cop/style/if_inside_else.rb +1 -5
- data/lib/rubocop/cop/style/if_unless_modifier.rb +57 -17
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -12
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +4 -1
- data/lib/rubocop/cop/style/if_with_semicolon.rb +7 -5
- data/lib/rubocop/cop/style/inline_comment.rb +4 -1
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -2
- data/lib/rubocop/cop/style/lambda_call.rb +8 -8
- data/lib/rubocop/cop/style/magic_comment_format.rb +2 -2
- data/lib/rubocop/cop/style/map_join.rb +123 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +15 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +17 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +2 -4
- data/lib/rubocop/cop/style/module_member_existence_check.rb +107 -0
- data/lib/rubocop/cop/style/multiline_if_then.rb +4 -4
- data/lib/rubocop/cop/style/multiline_method_signature.rb +2 -4
- data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
- data/lib/rubocop/cop/style/negative_array_index.rb +220 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +11 -10
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -1
- data/lib/rubocop/cop/style/non_nil_check.rb +5 -11
- data/lib/rubocop/cop/style/not.rb +2 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +3 -2
- data/lib/rubocop/cop/style/one_class_per_file.rb +115 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +21 -12
- data/lib/rubocop/cop/style/operator_method_call.rb +11 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +6 -2
- data/lib/rubocop/cop/style/partition_instead_of_double_select.rb +270 -0
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +2 -0
- data/lib/rubocop/cop/style/predicate_with_kind.rb +84 -0
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +12 -12
- data/lib/rubocop/cop/style/proc.rb +3 -2
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/reduce_to_hash.rb +184 -0
- data/lib/rubocop/cop/style/redundant_argument.rb +2 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +3 -3
- data/lib/rubocop/cop/style/redundant_condition.rb +5 -2
- data/lib/rubocop/cop/style/redundant_each.rb +3 -3
- data/lib/rubocop/cop/style/redundant_exception.rb +1 -1
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +1 -1
- data/lib/rubocop/cop/style/redundant_format.rb +26 -5
- data/lib/rubocop/cop/style/redundant_interpolation.rb +11 -2
- data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +26 -10
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +16 -0
- data/lib/rubocop/cop/style/redundant_min_max_by.rb +93 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +26 -22
- data/lib/rubocop/cop/style/redundant_percent_q.rb +5 -3
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +9 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +8 -0
- data/lib/rubocop/cop/style/redundant_return.rb +3 -1
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +0 -5
- data/lib/rubocop/cop/style/redundant_sort.rb +7 -7
- data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +114 -0
- data/lib/rubocop/cop/style/reverse_find.rb +51 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +7 -7
- data/lib/rubocop/cop/style/select_by_kind.rb +158 -0
- data/lib/rubocop/cop/style/select_by_range.rb +197 -0
- data/lib/rubocop/cop/style/select_by_regexp.rb +51 -21
- data/lib/rubocop/cop/style/semicolon.rb +25 -7
- data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +1 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +3 -1
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +8 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +6 -1
- data/lib/rubocop/cop/style/super_arguments.rb +2 -2
- data/lib/rubocop/cop/style/symbol_proc.rb +4 -3
- data/lib/rubocop/cop/style/tally_method.rb +181 -0
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +45 -0
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +1 -1
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -0
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +11 -11
- data/lib/rubocop/cop/style/unless_else.rb +10 -9
- data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
- data/lib/rubocop/cop/team.rb +4 -4
- data/lib/rubocop/cop/util.rb +2 -3
- data/lib/rubocop/cop/utils/format_string.rb +10 -0
- data/lib/rubocop/cop/variable_force/branch.rb +30 -6
- data/lib/rubocop/cops_documentation_generator.rb +4 -4
- data/lib/rubocop/directive_comment.rb +48 -4
- data/lib/rubocop/formatter/clang_style_formatter.rb +5 -2
- data/lib/rubocop/formatter/disabled_config_formatter.rb +2 -1
- data/lib/rubocop/formatter/formatter_set.rb +2 -2
- data/lib/rubocop/formatter/junit_formatter.rb +1 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +0 -2
- data/lib/rubocop/formatter/tap_formatter.rb +5 -2
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
- data/lib/rubocop/formatter.rb +22 -21
- data/lib/rubocop/lsp/diagnostic.rb +18 -33
- data/lib/rubocop/lsp/disable_comment_edits.rb +135 -0
- data/lib/rubocop/lsp/routes.rb +12 -5
- data/lib/rubocop/lsp/runtime.rb +13 -3
- data/lib/rubocop/lsp/stdin_runner.rb +8 -17
- data/lib/rubocop/magic_comment.rb +20 -0
- data/lib/rubocop/mcp/server.rb +200 -0
- data/lib/rubocop/options.rb +10 -1
- data/lib/rubocop/path_util.rb +14 -2
- data/lib/rubocop/plugin/loader.rb +1 -1
- data/lib/rubocop/rake_task.rb +1 -1
- data/lib/rubocop/remote_config.rb +10 -8
- data/lib/rubocop/result_cache.rb +60 -37
- data/lib/rubocop/rspec/cop_helper.rb +8 -0
- data/lib/rubocop/rspec/shared_contexts.rb +18 -5
- data/lib/rubocop/rspec/support.rb +2 -1
- data/lib/rubocop/runner.rb +12 -3
- data/lib/rubocop/server/cache.rb +6 -29
- data/lib/rubocop/server/core.rb +2 -0
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/target_ruby.rb +31 -14
- data/lib/rubocop/version.rb +2 -2
- data/lib/rubocop.rb +20 -0
- data/lib/ruby_lsp/rubocop/addon.rb +23 -8
- data/lib/ruby_lsp/rubocop/runtime_adapter.rb +49 -15
- metadata +33 -9
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative 'hash_transform_method/autocorrection'
|
|
4
|
+
|
|
3
5
|
module RuboCop
|
|
4
6
|
module Cop
|
|
5
7
|
# Common functionality for Style/HashTransformKeys and
|
|
@@ -27,68 +29,16 @@ module RuboCop
|
|
|
27
29
|
end
|
|
28
30
|
end
|
|
29
31
|
|
|
30
|
-
#
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
new(match, node.children.last, 'Hash['.length, ']'.length)
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def self.from_map_to_h(node, match)
|
|
41
|
-
if node.parent&.block_type? && node.parent.send_node == node
|
|
42
|
-
strip_trailing_chars = 0
|
|
43
|
-
else
|
|
44
|
-
map_range = node.children.first.source_range
|
|
45
|
-
node_range = node.source_range
|
|
46
|
-
strip_trailing_chars = node_range.end_pos - map_range.end_pos
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
new(match, node.children.first, 0, strip_trailing_chars)
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
def self.from_to_h(node, match)
|
|
53
|
-
new(match, node, 0, 0)
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def strip_prefix_and_suffix(node, corrector)
|
|
57
|
-
expression = node.source_range
|
|
58
|
-
corrector.remove_leading(expression, leading)
|
|
59
|
-
corrector.remove_trailing(expression, trailing)
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def set_new_method_name(new_method_name, corrector)
|
|
63
|
-
range = block_node.send_node.loc.selector
|
|
64
|
-
if (send_end = block_node.send_node.loc.end)
|
|
65
|
-
# If there are arguments (only true in the `each_with_object`
|
|
66
|
-
# case)
|
|
67
|
-
range = range.begin.join(send_end)
|
|
68
|
-
end
|
|
69
|
-
corrector.replace(range, new_method_name)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def set_new_arg_name(transformed_argname, corrector)
|
|
73
|
-
corrector.replace(block_node.arguments, "|#{transformed_argname}|")
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
def set_new_body_expression(transforming_body_expr, corrector)
|
|
77
|
-
body_source = transforming_body_expr.source
|
|
78
|
-
if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
|
|
79
|
-
body_source = "{ #{body_source} }"
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
corrector.replace(block_node.body, body_source)
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
# @!method array_receiver?(node)
|
|
87
|
-
def_node_matcher :array_receiver?, <<~PATTERN
|
|
88
|
-
{(array ...) (send _ :each_with_index) (send _ :with_index _ ?) (send _ :zip ...)}
|
|
32
|
+
# @!method hash_receiver?(node)
|
|
33
|
+
def_node_matcher :hash_receiver?, <<~PATTERN
|
|
34
|
+
{(hash ...)
|
|
35
|
+
(send _ {:to_h :to_hash :merge :merge! :update :invert :except :tally} ...)
|
|
36
|
+
(block (send _ {:group_by :to_h :tally :transform_keys :transform_keys!
|
|
37
|
+
:transform_values :transform_values!}) ...)
|
|
38
|
+
(block (send _ :each_with_object (hash)) ...)}
|
|
89
39
|
PATTERN
|
|
90
40
|
|
|
91
|
-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
|
|
41
|
+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
|
|
92
42
|
on_bad_each_with_object(node) do |*match|
|
|
93
43
|
handle_possible_offense(node, match, 'each_with_object')
|
|
94
44
|
end
|
|
@@ -8,8 +8,27 @@ module RuboCop
|
|
|
8
8
|
|
|
9
9
|
private
|
|
10
10
|
|
|
11
|
-
def
|
|
12
|
-
config.for_cop('Layout/LineLength')['
|
|
11
|
+
def allow_rbs_inline_annotation?
|
|
12
|
+
config.for_cop('Layout/LineLength')['AllowRBSInlineAnnotation']
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def rbs_inline_annotation_on_source_line?(line_index)
|
|
16
|
+
source_line_number = line_index + processed_source.buffer.first_line
|
|
17
|
+
comment = processed_source.comment_at_line(source_line_number)
|
|
18
|
+
|
|
19
|
+
return false unless comment
|
|
20
|
+
|
|
21
|
+
comment.text.start_with?(/#:|#\[.+\]|#\|/)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def allow_cop_directives?
|
|
25
|
+
# TODO: This logic for backward compatibility with deprecated `IgnoreCopDirectives` option.
|
|
26
|
+
# The following three lines will be removed in RuboCop 2.0.
|
|
27
|
+
ignore_cop_directives = config.for_cop('Layout/LineLength')['IgnoreCopDirectives']
|
|
28
|
+
return true if ignore_cop_directives
|
|
29
|
+
return false if ignore_cop_directives == false
|
|
30
|
+
|
|
31
|
+
config.for_cop('Layout/LineLength')['AllowCopDirectives']
|
|
13
32
|
end
|
|
14
33
|
|
|
15
34
|
def directive_on_source_line?(line_index)
|
|
@@ -73,7 +73,7 @@ module RuboCop
|
|
|
73
73
|
|
|
74
74
|
def location(node)
|
|
75
75
|
if LSP.enabled?
|
|
76
|
-
end_range = node.loc
|
|
76
|
+
end_range = node.loc?(:name) ? node.loc.name : node.loc.begin
|
|
77
77
|
node.source_range.begin.join(end_range)
|
|
78
78
|
else
|
|
79
79
|
node.source_range
|
|
@@ -200,7 +200,7 @@ module RuboCop
|
|
|
200
200
|
end
|
|
201
201
|
|
|
202
202
|
def grouped_expression?(node)
|
|
203
|
-
node.begin_type? && node.loc
|
|
203
|
+
node.begin_type? && node.loc?(:begin) && node.loc.begin
|
|
204
204
|
end
|
|
205
205
|
|
|
206
206
|
def inside_arg_list_parentheses?(node, ancestor)
|
|
@@ -8,8 +8,8 @@ module RuboCop
|
|
|
8
8
|
MSG = 'Space missing after %<token>s.'
|
|
9
9
|
|
|
10
10
|
def on_new_investigation
|
|
11
|
-
each_missing_space(processed_source.tokens) do |token|
|
|
12
|
-
add_offense(token.pos, message: format(MSG, token: kind
|
|
11
|
+
each_missing_space(processed_source.tokens) do |token, kind|
|
|
12
|
+
add_offense(token.pos, message: format(MSG, token: kind)) do |corrector|
|
|
13
13
|
PunctuationCorrector.add_space(corrector, token)
|
|
14
14
|
end
|
|
15
15
|
end
|
|
@@ -19,11 +19,12 @@ module RuboCop
|
|
|
19
19
|
|
|
20
20
|
def each_missing_space(tokens)
|
|
21
21
|
tokens.each_cons(2) do |token1, token2|
|
|
22
|
-
|
|
22
|
+
kind = kind(token1, token2)
|
|
23
|
+
next unless kind
|
|
23
24
|
next unless space_missing?(token1, token2)
|
|
24
25
|
next unless space_required_before?(token2)
|
|
25
26
|
|
|
26
|
-
yield token1
|
|
27
|
+
yield token1, kind
|
|
27
28
|
end
|
|
28
29
|
end
|
|
29
30
|
|
|
@@ -100,12 +100,6 @@ module RuboCop
|
|
|
100
100
|
node.parent.send_type?
|
|
101
101
|
end
|
|
102
102
|
|
|
103
|
-
def max_line_length
|
|
104
|
-
return unless config.cop_enabled?('Layout/LineLength')
|
|
105
|
-
|
|
106
|
-
config.for_cop('Layout/LineLength')['Max']
|
|
107
|
-
end
|
|
108
|
-
|
|
109
103
|
def comment_disables_cop?(comment)
|
|
110
104
|
regexp_pattern = "# rubocop : (disable|todo) ([^,],)* (all|#{cop_name})"
|
|
111
105
|
Regexp.new(regexp_pattern.gsub(' ', '\s*')).match?(comment)
|
|
@@ -95,13 +95,16 @@ module RuboCop
|
|
|
95
95
|
node.multiline? && !allowed_multiline_argument?(node)
|
|
96
96
|
end
|
|
97
97
|
|
|
98
|
+
# rubocop:disable Metrics/AbcSize
|
|
98
99
|
def method_name_and_arguments_on_same_line?(node)
|
|
99
|
-
return false
|
|
100
|
+
return false if !node.call_type? || node.last_line != node.last_argument.last_line
|
|
101
|
+
return true if node.last_argument.hash_type? && node.last_argument.braces?
|
|
100
102
|
|
|
101
|
-
line = node.loc.selector
|
|
103
|
+
line = node.loc.selector&.line || node.loc.line
|
|
102
104
|
|
|
103
|
-
line == node.last_argument.last_line
|
|
105
|
+
line == node.last_argument.last_line
|
|
104
106
|
end
|
|
107
|
+
# rubocop:enable Metrics/AbcSize
|
|
105
108
|
|
|
106
109
|
# A single argument with the closing bracket on the same line as the end
|
|
107
110
|
# of the argument is not considered multiline, even if the argument
|
|
@@ -140,7 +143,7 @@ module RuboCop
|
|
|
140
143
|
end
|
|
141
144
|
|
|
142
145
|
def last_item_precedes_newline?(node)
|
|
143
|
-
after_last_item = node.children.last.source_range.end.join(node.
|
|
146
|
+
after_last_item = node.children.last.source_range.end.join(node.source_range.end)
|
|
144
147
|
|
|
145
148
|
after_last_item.source.start_with?(/,?\s*(#.*)?\n/)
|
|
146
149
|
end
|
|
@@ -185,7 +188,7 @@ module RuboCop
|
|
|
185
188
|
|
|
186
189
|
def heredoc?(node)
|
|
187
190
|
return false unless node.is_a?(RuboCop::AST::Node)
|
|
188
|
-
return true if node.loc
|
|
191
|
+
return true if node.loc?(:heredoc_body)
|
|
189
192
|
|
|
190
193
|
return heredoc_send?(node) if node.send_type?
|
|
191
194
|
|
|
@@ -38,7 +38,7 @@ module RuboCop
|
|
|
38
38
|
class BlockParameterName < Base
|
|
39
39
|
include UncommunicativeName
|
|
40
40
|
|
|
41
|
-
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
|
|
41
|
+
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler, InternalAffairs/ItblockHandler
|
|
42
42
|
return unless node.arguments?
|
|
43
43
|
|
|
44
44
|
check(node, node.arguments)
|
|
@@ -147,7 +147,9 @@ module RuboCop
|
|
|
147
147
|
alias on_defs on_def
|
|
148
148
|
|
|
149
149
|
def on_alias(node)
|
|
150
|
-
|
|
150
|
+
return unless (new_identifier = node.new_identifier).sym_type?
|
|
151
|
+
|
|
152
|
+
handle_method_name(new_identifier, new_identifier.value)
|
|
151
153
|
end
|
|
152
154
|
|
|
153
155
|
private
|
|
@@ -232,7 +234,7 @@ module RuboCop
|
|
|
232
234
|
end
|
|
233
235
|
|
|
234
236
|
def range_position(node)
|
|
235
|
-
if node.loc
|
|
237
|
+
if node.loc?(:selector)
|
|
236
238
|
selector_end_pos = node.loc.selector.end_pos + 1
|
|
237
239
|
expr_end_pos = node.source_range.end_pos
|
|
238
240
|
|
|
@@ -113,6 +113,18 @@ module RuboCop
|
|
|
113
113
|
# true
|
|
114
114
|
# end
|
|
115
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
|
+
#
|
|
116
128
|
# @example AllowBangMethods: false (default)
|
|
117
129
|
# # bad
|
|
118
130
|
# def save!
|
|
@@ -125,6 +137,17 @@ module RuboCop
|
|
|
125
137
|
# true
|
|
126
138
|
# end
|
|
127
139
|
#
|
|
140
|
+
# @example WaywardPredicates: ['infinite?', 'nonzero?'] (default)
|
|
141
|
+
# # good
|
|
142
|
+
# def non_predicate_method(num)
|
|
143
|
+
# num.infinite?
|
|
144
|
+
# end
|
|
145
|
+
#
|
|
146
|
+
# # good
|
|
147
|
+
# def non_predicate_method(num)
|
|
148
|
+
# num.nonzero?
|
|
149
|
+
# end
|
|
150
|
+
#
|
|
128
151
|
class PredicateMethod < Base
|
|
129
152
|
include AllowedMethods
|
|
130
153
|
include AllowedPattern
|
|
@@ -181,8 +204,7 @@ module RuboCop
|
|
|
181
204
|
return_values << extract_return_value(return_node)
|
|
182
205
|
end
|
|
183
206
|
|
|
184
|
-
|
|
185
|
-
return_values << last_value if last_value
|
|
207
|
+
return_values << last_value(node)
|
|
186
208
|
|
|
187
209
|
process_return_values(return_values)
|
|
188
210
|
end
|
|
@@ -235,8 +257,9 @@ module RuboCop
|
|
|
235
257
|
end
|
|
236
258
|
|
|
237
259
|
def last_value(node)
|
|
238
|
-
value = node.begin_type? ? node.children.last : node
|
|
239
|
-
|
|
260
|
+
value = node.begin_type? ? node.children.last || s(:nil) : node
|
|
261
|
+
|
|
262
|
+
value.return_type? ? extract_return_value(value) : value
|
|
240
263
|
end
|
|
241
264
|
|
|
242
265
|
def process_return_values(return_values)
|
|
@@ -63,17 +63,17 @@ module RuboCop
|
|
|
63
63
|
# end
|
|
64
64
|
#
|
|
65
65
|
# @example UseSorbetSigs: false (default)
|
|
66
|
-
#
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
#
|
|
70
|
-
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
#
|
|
74
|
-
#
|
|
75
|
-
#
|
|
76
|
-
#
|
|
66
|
+
# # bad
|
|
67
|
+
# sig { returns(String) }
|
|
68
|
+
# def is_this_thing_on
|
|
69
|
+
# "yes"
|
|
70
|
+
# end
|
|
71
|
+
#
|
|
72
|
+
# # good - Sorbet signature is not evaluated
|
|
73
|
+
# sig { returns(String) }
|
|
74
|
+
# def is_this_thing_on?
|
|
75
|
+
# "yes"
|
|
76
|
+
# end
|
|
77
77
|
#
|
|
78
78
|
# @example UseSorbetSigs: true
|
|
79
79
|
# # bad
|
data/lib/rubocop/cop/offense.rb
CHANGED
|
@@ -66,6 +66,13 @@ module RuboCop
|
|
|
66
66
|
alias_method :last_line, :line
|
|
67
67
|
alias_method :last_column, :column
|
|
68
68
|
|
|
69
|
+
attr_reader :source_buffer
|
|
70
|
+
|
|
71
|
+
def initialize(line, column, source_line, begin_pos, end_pos)
|
|
72
|
+
super
|
|
73
|
+
@source_buffer = Parser::Source::Buffer.new('(pseudo)', source: source_line)
|
|
74
|
+
end
|
|
75
|
+
|
|
69
76
|
def column_range
|
|
70
77
|
column...last_column
|
|
71
78
|
end
|
|
@@ -139,7 +146,8 @@ module RuboCop
|
|
|
139
146
|
# @return [Parser::Source::Range]
|
|
140
147
|
# the range of the code that is highlighted
|
|
141
148
|
def highlighted_area
|
|
142
|
-
Parser::Source::
|
|
149
|
+
source_buffer = Parser::Source::Buffer.new(location.source_buffer.name, source: source_line)
|
|
150
|
+
Parser::Source::Range.new(source_buffer, column, column + column_length)
|
|
143
151
|
end
|
|
144
152
|
|
|
145
153
|
# @api private
|
data/lib/rubocop/cop/registry.rb
CHANGED
|
@@ -16,7 +16,7 @@ module RuboCop
|
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
# Registry that tracks all cops by their badge and department.
|
|
19
|
-
class Registry
|
|
19
|
+
class Registry # rubocop:disable Metrics/ClassLength
|
|
20
20
|
include Enumerable
|
|
21
21
|
|
|
22
22
|
def self.all
|
|
@@ -46,7 +46,7 @@ module RuboCop
|
|
|
46
46
|
global.qualify_badge(badge).first == badge
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
-
attr_reader :options
|
|
49
|
+
attr_reader :options, :warnings
|
|
50
50
|
|
|
51
51
|
def initialize(cops = [], options = {})
|
|
52
52
|
@registry = {}
|
|
@@ -58,6 +58,7 @@ module RuboCop
|
|
|
58
58
|
|
|
59
59
|
@enabled_cache = {}.compare_by_identity
|
|
60
60
|
@disabled_cache = {}.compare_by_identity
|
|
61
|
+
@warnings = {}
|
|
61
62
|
end
|
|
62
63
|
|
|
63
64
|
def enlist(cop)
|
|
@@ -132,7 +133,7 @@ module RuboCop
|
|
|
132
133
|
# @return [String] Qualified cop name
|
|
133
134
|
def qualified_cop_name(name, path, warn: true)
|
|
134
135
|
badge = Badge.parse(name)
|
|
135
|
-
|
|
136
|
+
print_department_missing_warning(name, path) if warn && department_missing?(badge, name)
|
|
136
137
|
return name if registered?(badge)
|
|
137
138
|
|
|
138
139
|
potential_badges = qualify_badge(badge)
|
|
@@ -148,12 +149,12 @@ module RuboCop
|
|
|
148
149
|
!badge.qualified? && unqualified_cop_names.include?(name)
|
|
149
150
|
end
|
|
150
151
|
|
|
151
|
-
def
|
|
152
|
-
message = "
|
|
152
|
+
def print_department_missing_warning(name, path)
|
|
153
|
+
message = "no department given for #{name}."
|
|
153
154
|
if path.end_with?('.rb')
|
|
154
155
|
message += ' Run `rubocop -a --only Migration/DepartmentName` to fix.'
|
|
155
156
|
end
|
|
156
|
-
|
|
157
|
+
emit_warning(path, message)
|
|
157
158
|
end
|
|
158
159
|
|
|
159
160
|
def unqualified_cop_names
|
|
@@ -274,6 +275,10 @@ module RuboCop
|
|
|
274
275
|
attr_reader :global
|
|
275
276
|
end
|
|
276
277
|
|
|
278
|
+
def warnings?(path)
|
|
279
|
+
@warnings[path]
|
|
280
|
+
end
|
|
281
|
+
|
|
277
282
|
private
|
|
278
283
|
|
|
279
284
|
def initialize_copy(reg)
|
|
@@ -297,18 +302,20 @@ module RuboCop
|
|
|
297
302
|
end
|
|
298
303
|
|
|
299
304
|
def resolve_badge(given_badge, real_badge, source_path, warn: true)
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
warn("#{path}: #{given_badge} has the wrong namespace - " \
|
|
305
|
-
"replace it with #{given_badge.with_department(real_badge.department)}")
|
|
306
|
-
end
|
|
305
|
+
if warn && !given_badge.match?(real_badge)
|
|
306
|
+
emit_warning(source_path,
|
|
307
|
+
"#{given_badge} has the wrong namespace - " \
|
|
308
|
+
"replace it with #{given_badge.with_department(real_badge.department)}")
|
|
307
309
|
end
|
|
308
310
|
|
|
309
311
|
real_badge.to_s
|
|
310
312
|
end
|
|
311
313
|
|
|
314
|
+
def emit_warning(path, message)
|
|
315
|
+
Registry.global.warnings[path] = true
|
|
316
|
+
warn "#{PathUtil.smart_path(path)}: Warning: #{message}"
|
|
317
|
+
end
|
|
318
|
+
|
|
312
319
|
def registered?(badge)
|
|
313
320
|
clear_enrollment_queue
|
|
314
321
|
@registry.key?(badge)
|
|
@@ -3,15 +3,28 @@
|
|
|
3
3
|
module RuboCop
|
|
4
4
|
module Cop
|
|
5
5
|
module Security
|
|
6
|
-
# Checks for the use of `Kernel#eval` and `Binding#eval
|
|
6
|
+
# Checks for the use of `Kernel#eval` and `Binding#eval` with
|
|
7
|
+
# dynamic strings as arguments. Evaluating non-literal strings
|
|
8
|
+
# can enable code injection attacks and makes it difficult to
|
|
9
|
+
# reason about what code will actually be executed.
|
|
10
|
+
#
|
|
11
|
+
# Calls to `eval` with literal strings are not flagged by this cop,
|
|
12
|
+
# as they do not pose the same injection risk.
|
|
7
13
|
#
|
|
8
14
|
# @example
|
|
9
15
|
#
|
|
10
16
|
# # bad
|
|
11
|
-
#
|
|
12
17
|
# eval(something)
|
|
13
18
|
# binding.eval(something)
|
|
14
19
|
# Kernel.eval(something)
|
|
20
|
+
#
|
|
21
|
+
# # good - use safer alternatives
|
|
22
|
+
# obj.public_send(method_name)
|
|
23
|
+
# obj.send(method_name, *args)
|
|
24
|
+
#
|
|
25
|
+
# # good - literal strings are allowed
|
|
26
|
+
# eval("1 + 1")
|
|
27
|
+
# binding.eval("foo")
|
|
15
28
|
class Eval < Base
|
|
16
29
|
MSG = 'The use of `eval` is a serious security risk.'
|
|
17
30
|
RESTRICT_ON_SEND = %i[eval].freeze
|
|
@@ -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
|
+
!`(pair (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
|
|
@@ -326,8 +326,7 @@ module RuboCop
|
|
|
326
326
|
argument_less_modifier_node = find_argument_less_modifier_node(node)
|
|
327
327
|
if argument_less_modifier_node
|
|
328
328
|
corrector.insert_after(argument_less_modifier_node, "\n\n#{source}")
|
|
329
|
-
elsif (ancestor = node.each_ancestor(:class, :module).first)
|
|
330
|
-
|
|
329
|
+
elsif (ancestor = node.each_ancestor(:class, :module, :sclass).first)
|
|
331
330
|
corrector.insert_before(ancestor.loc.end, "#{node.method_name}\n\n#{source}\n")
|
|
332
331
|
else
|
|
333
332
|
corrector.replace(node, "#{node.method_name}\n\n#{source}")
|
|
@@ -349,8 +348,20 @@ module RuboCop
|
|
|
349
348
|
|
|
350
349
|
def remove_modifier_node_within_begin(corrector, modifier_node, begin_node)
|
|
351
350
|
def_node = begin_node.children[begin_node.children.index(modifier_node) + 1]
|
|
352
|
-
range
|
|
353
|
-
|
|
351
|
+
# Stop the removal range at the first comment that precedes the def, if
|
|
352
|
+
# any exist. Without this, comments between the modifier and the def are
|
|
353
|
+
# dropped because they fall inside the removed range.
|
|
354
|
+
end_pos = first_comment_or_node_start(def_node)
|
|
355
|
+
corrector.remove(modifier_node.source_range.begin.join(end_pos))
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
def first_comment_or_node_start(node)
|
|
359
|
+
preceding = processed_source.ast_with_comments[node].select do |comment|
|
|
360
|
+
comment.loc.line < node.loc.line
|
|
361
|
+
end
|
|
362
|
+
return node.source_range.begin if preceding.empty?
|
|
363
|
+
|
|
364
|
+
preceding.first.source_range.begin
|
|
354
365
|
end
|
|
355
366
|
|
|
356
367
|
def def_source(node, def_nodes)
|
|
@@ -4,8 +4,10 @@ module RuboCop
|
|
|
4
4
|
module Cop
|
|
5
5
|
module Style
|
|
6
6
|
# Checks for grouping of accessors in `class` and `module` bodies.
|
|
7
|
-
# By default it enforces accessors to be placed in grouped
|
|
8
|
-
#
|
|
7
|
+
# By default it enforces accessors to be placed in grouped
|
|
8
|
+
# declarations, reducing boilerplate. It can also be configured
|
|
9
|
+
# to enforce separating them into individual declarations for
|
|
10
|
+
# easier diffing and per-attribute documentation.
|
|
9
11
|
#
|
|
10
12
|
# NOTE: If there is a method call before the accessor method it is always allowed
|
|
11
13
|
# as it might be intended like Sorbet.
|
|
@@ -4,7 +4,10 @@ module RuboCop
|
|
|
4
4
|
module Cop
|
|
5
5
|
module Style
|
|
6
6
|
# Enforces the use of either `#alias` or `#alias_method`
|
|
7
|
-
# depending on configuration.
|
|
7
|
+
# depending on configuration. Consistent use of one or the
|
|
8
|
+
# other prevents confusion about their different semantics
|
|
9
|
+
# (e.g., `alias` is resolved at parse time, while `alias_method`
|
|
10
|
+
# is resolved at runtime).
|
|
8
11
|
# It also flags uses of `alias :symbol` rather than `alias bareword`.
|
|
9
12
|
#
|
|
10
13
|
# However, it will always enforce `method_alias` when used `alias`
|