rubocop 1.9.0 → 1.12.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/README.md +14 -14
- data/assets/output.html.erb +1 -1
- data/config/default.yml +70 -17
- data/config/obsoletion.yml +4 -0
- data/lib/rubocop.rb +5 -0
- data/lib/rubocop/cli/command/execute_runner.rb +1 -1
- data/lib/rubocop/cli/command/suggest_extensions.rb +3 -2
- data/lib/rubocop/comment_config.rb +43 -94
- data/lib/rubocop/config.rb +4 -1
- data/lib/rubocop/cop/base.rb +1 -0
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
- data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
- data/lib/rubocop/cop/exclude_limit.rb +26 -0
- data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
- data/lib/rubocop/cop/generator.rb +2 -2
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
- data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
- data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +11 -8
- data/lib/rubocop/cop/layout/argument_alignment.rb +6 -5
- data/lib/rubocop/cop/layout/array_alignment.rb +7 -6
- data/lib/rubocop/cop/layout/assignment_indentation.rb +6 -3
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
- data/lib/rubocop/cop/layout/block_end_newline.rb +4 -8
- data/lib/rubocop/cop/layout/class_structure.rb +1 -0
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +14 -15
- data/lib/rubocop/cop/layout/comment_indentation.rb +16 -16
- data/lib/rubocop/cop/layout/else_alignment.rb +9 -6
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +20 -3
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
- data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +27 -7
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +9 -6
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +22 -15
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +6 -5
- data/lib/rubocop/cop/layout/indentation_consistency.rb +9 -6
- data/lib/rubocop/cop/layout/indentation_style.rb +27 -30
- data/lib/rubocop/cop/layout/indentation_width.rb +20 -9
- data/lib/rubocop/cop/layout/line_length.rb +2 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +18 -5
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +10 -5
- data/lib/rubocop/cop/layout/parameter_alignment.rb +6 -5
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
- data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
- data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
- data/lib/rubocop/cop/lint/debugger.rb +60 -14
- data/lib/rubocop/cop/lint/deprecated_constants.rb +5 -0
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
- data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
- data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
- data/lib/rubocop/cop/lint/else_layout.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
- data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
- data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
- data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +11 -2
- data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
- data/lib/rubocop/cop/lint/rand_one.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -2
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
- data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
- data/lib/rubocop/cop/lint/suppressed_exception.rb +44 -1
- data/lib/rubocop/cop/lint/symbol_conversion.rb +91 -3
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
- data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
- data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
- data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
- data/lib/rubocop/cop/lint/useless_times.rb +3 -0
- data/lib/rubocop/cop/message_annotator.rb +4 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
- data/lib/rubocop/cop/metrics/module_length.rb +1 -0
- data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
- data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
- data/lib/rubocop/cop/mixin/alignment.rb +10 -3
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
- data/lib/rubocop/cop/mixin/code_length.rb +3 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +5 -1
- data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
- data/lib/rubocop/cop/mixin/def_node.rb +1 -0
- data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
- data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
- data/lib/rubocop/cop/mixin/line_length_help.rb +11 -6
- data/lib/rubocop/cop/mixin/method_complexity.rb +4 -1
- data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +3 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +4 -23
- data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
- data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
- data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
- data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -6
- data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
- data/lib/rubocop/cop/naming/constant_name.rb +2 -0
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +6 -0
- data/lib/rubocop/cop/naming/method_name.rb +3 -0
- data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -0
- data/lib/rubocop/cop/registry.rb +10 -1
- data/lib/rubocop/cop/security/eval.rb +1 -0
- data/lib/rubocop/cop/security/json_load.rb +1 -0
- data/lib/rubocop/cop/security/marshal_load.rb +1 -0
- data/lib/rubocop/cop/security/open.rb +1 -0
- data/lib/rubocop/cop/security/yaml_load.rb +1 -0
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +3 -2
- data/lib/rubocop/cop/style/alias.rb +1 -0
- data/lib/rubocop/cop/style/and_or.rb +3 -1
- data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
- data/lib/rubocop/cop/style/array_coercion.rb +2 -0
- data/lib/rubocop/cop/style/array_join.rb +1 -0
- data/lib/rubocop/cop/style/attr.rb +1 -0
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +59 -71
- data/lib/rubocop/cop/style/bisected_attr_accessor/macro.rb +62 -0
- data/lib/rubocop/cop/style/case_equality.rb +2 -1
- data/lib/rubocop/cop/style/case_like_if.rb +15 -4
- data/lib/rubocop/cop/style/class_equality_comparison.rb +3 -0
- data/lib/rubocop/cop/style/collection_compact.rb +2 -0
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +10 -10
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
- data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
- data/lib/rubocop/cop/style/date_time.rb +3 -0
- data/lib/rubocop/cop/style/dir.rb +1 -0
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
- data/lib/rubocop/cop/style/documentation.rb +30 -3
- data/lib/rubocop/cop/style/documentation_method.rb +1 -0
- data/lib/rubocop/cop/style/double_negation.rb +3 -2
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
- data/lib/rubocop/cop/style/each_with_object.rb +1 -0
- data/lib/rubocop/cop/style/empty_literal.rb +9 -0
- data/lib/rubocop/cop/style/endless_method.rb +1 -0
- data/lib/rubocop/cop/style/eval_with_location.rb +90 -28
- data/lib/rubocop/cop/style/even_odd.rb +1 -0
- data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -1
- data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
- data/lib/rubocop/cop/style/float_division.rb +4 -0
- data/lib/rubocop/cop/style/format_string.rb +2 -0
- data/lib/rubocop/cop/style/format_string_token.rb +1 -0
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +0 -3
- data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
- data/lib/rubocop/cop/style/hash_conversion.rb +108 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
- data/lib/rubocop/cop/style/hash_except.rb +1 -0
- data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +16 -15
- data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
- data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +37 -11
- data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
- data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +46 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +19 -3
- data/lib/rubocop/cop/style/min_max.rb +1 -0
- data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
- data/lib/rubocop/cop/style/module_function.rb +5 -0
- data/lib/rubocop/cop/style/multiline_method_signature.rb +10 -3
- data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
- data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +16 -2
- data/lib/rubocop/cop/style/nil_comparison.rb +6 -1
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
- data/lib/rubocop/cop/style/non_nil_check.rb +7 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
- data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
- data/lib/rubocop/cop/style/option_hash.rb +1 -0
- data/lib/rubocop/cop/style/or_assignment.rb +2 -0
- data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
- data/lib/rubocop/cop/style/proc.rb +1 -0
- data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
- data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +44 -4
- data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
- data/lib/rubocop/cop/style/redundant_return.rb +4 -0
- data/lib/rubocop/cop/style/redundant_self.rb +7 -3
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
- data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +17 -14
- data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
- data/lib/rubocop/cop/style/return_nil.rb +6 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
- data/lib/rubocop/cop/style/sample.rb +1 -0
- data/lib/rubocop/cop/style/signal_exception.rb +3 -0
- data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
- data/lib/rubocop/cop/style/single_line_methods.rb +4 -1
- data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +20 -4
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
- data/lib/rubocop/cop/style/string_chars.rb +38 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +1 -0
- data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
- data/lib/rubocop/cop/style/strip.rb +1 -0
- data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -0
- data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +5 -0
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
- data/lib/rubocop/cop/style/unless_logical_operators.rb +105 -0
- data/lib/rubocop/cop/style/unpack_first.rb +1 -0
- data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
- data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
- data/lib/rubocop/cop/util.rb +4 -1
- data/lib/rubocop/directive_comment.rb +69 -9
- data/lib/rubocop/ext/regexp_parser.rb +3 -6
- data/lib/rubocop/formatter/clang_style_formatter.rb +4 -2
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
- data/lib/rubocop/formatter/tap_formatter.rb +4 -2
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
- data/lib/rubocop/magic_comment.rb +1 -1
- data/lib/rubocop/name_similarity.rb +1 -1
- data/lib/rubocop/target_finder.rb +1 -0
- data/lib/rubocop/target_ruby.rb +21 -13
- data/lib/rubocop/version.rb +1 -1
- metadata +14 -7
@@ -6,6 +6,12 @@ module RuboCop
|
|
6
6
|
# This cop checks for uses of literal strings converted to
|
7
7
|
# a symbol where a literal symbol could be used instead.
|
8
8
|
#
|
9
|
+
# There are two possible styles for this cop.
|
10
|
+
# `strict` (default) will register an offense for any incorrect usage.
|
11
|
+
# `consistent` additionally requires hashes to use the same style for
|
12
|
+
# every symbol key (ie. if any symbol key needs to be quoted it requires
|
13
|
+
# all keys to be quoted).
|
14
|
+
#
|
9
15
|
# @example
|
10
16
|
# # bad
|
11
17
|
# 'string'.to_sym
|
@@ -21,20 +27,60 @@ module RuboCop
|
|
21
27
|
# :underscored_symbol
|
22
28
|
# :'hyphenated-string'
|
23
29
|
#
|
30
|
+
# @example EnforcedStyle: strict (default)
|
31
|
+
#
|
32
|
+
# # bad
|
33
|
+
# {
|
34
|
+
# 'a': 1,
|
35
|
+
# "b": 2,
|
36
|
+
# 'c-d': 3
|
37
|
+
# }
|
38
|
+
#
|
39
|
+
# # good (don't quote keys that don't require quoting)
|
40
|
+
# {
|
41
|
+
# a: 1,
|
42
|
+
# b: 2,
|
43
|
+
# 'c-d': 3
|
44
|
+
# }
|
45
|
+
#
|
46
|
+
# @example EnforcedStyle: consistent
|
47
|
+
#
|
48
|
+
# # bad
|
49
|
+
# {
|
50
|
+
# a: 1,
|
51
|
+
# 'b-c': 2
|
52
|
+
# }
|
53
|
+
#
|
54
|
+
# # good (quote all keys if any need quoting)
|
55
|
+
# {
|
56
|
+
# 'a': 1,
|
57
|
+
# 'b-c': 2
|
58
|
+
# }
|
59
|
+
#
|
60
|
+
# # good (no quoting required)
|
61
|
+
# {
|
62
|
+
# a: 1,
|
63
|
+
# b: 2
|
64
|
+
# }
|
65
|
+
#
|
24
66
|
class SymbolConversion < Base
|
25
67
|
extend AutoCorrector
|
68
|
+
include ConfigurableEnforcedStyle
|
26
69
|
|
27
70
|
MSG = 'Unnecessary symbol conversion; use `%<correction>s` instead.'
|
71
|
+
MSG_CONSISTENCY = 'Symbol hash key should be quoted for consistency; ' \
|
72
|
+
'use `%<correction>s` instead.'
|
28
73
|
RESTRICT_ON_SEND = %i[to_sym intern].freeze
|
29
74
|
|
30
75
|
def on_send(node)
|
76
|
+
return unless node.receiver
|
31
77
|
return unless node.receiver.str_type? || node.receiver.sym_type?
|
32
78
|
|
33
79
|
register_offense(node, correction: node.receiver.value.to_sym.inspect)
|
34
80
|
end
|
35
81
|
|
36
82
|
def on_sym(node)
|
37
|
-
return if properly_quoted?(node.source, node.value.inspect)
|
83
|
+
return if ignored_node?(node) || properly_quoted?(node.source, node.value.inspect)
|
38
84
|
|
39
85
|
# `alias` arguments are symbols but since a symbol that requires
|
40
86
|
# being quoted is not a valid method identifier, it can be ignored
|
@@ -50,6 +96,21 @@ module RuboCop
|
|
50
96
|
register_offense(node, correction: node.value.inspect)
|
51
97
|
end
|
52
98
|
|
99
|
+
def on_hash(node)
|
100
|
+
# For `EnforcedStyle: strict`, hash keys are evaluated in `on_sym`
|
101
|
+
return unless style == :consistent
|
102
|
+
|
103
|
+
keys = node.keys.select(&:sym_type?)
|
104
|
+
|
105
|
+
if keys.any? { |key| requires_quotes?(key) }
|
106
|
+
correct_inconsistent_hash_keys(keys)
|
107
|
+
else
|
108
|
+
# If there are no symbol keys requiring quoting,
|
109
|
+
# treat the hash like `EnforcedStyle: strict`.
|
110
|
+
keys.each { |key| correct_hash_key(key) }
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
53
114
|
private
|
54
115
|
|
55
116
|
def register_offense(node, correction:, message: format(MSG, correction: correction))
|
@@ -59,7 +120,7 @@ module RuboCop
|
|
59
120
|
end
|
60
121
|
|
61
122
|
def properly_quoted?(source, value)
|
62
|
-
return true
|
123
|
+
return true if style == :strict && (!source.match?(/['"]/) || value.end_with?('='))
|
63
124
|
|
64
125
|
source == value ||
|
65
126
|
# `Symbol#inspect` uses double quotes, but allow single-quoted
|
@@ -67,6 +128,10 @@ module RuboCop
|
|
67
128
|
source.tr("'", '"') == value
|
68
129
|
end
|
69
130
|
|
131
|
+
def requires_quotes?(sym_node)
|
132
|
+
sym_node.value.inspect.match?(/^:".*?"|=$/)
|
133
|
+
end
|
134
|
+
|
70
135
|
def in_alias?(node)
|
71
136
|
node.parent&.alias_type?
|
72
137
|
end
|
@@ -87,7 +152,7 @@ module RuboCop
|
|
87
152
|
# will be ignored.
|
88
153
|
return unless node.value.to_s.match?(/\A[a-z0-9_]/i)
|
89
154
|
|
90
|
-
correction = node.value.inspect.
|
155
|
+
correction = node.value.inspect.gsub(/\A:/, '')
|
91
156
|
return if properly_quoted?(node.source, correction)
|
92
157
|
|
93
158
|
register_offense(
|
@@ -96,6 +161,29 @@ module RuboCop
|
|
96
161
|
message: format(MSG, correction: "#{correction}:")
|
97
162
|
)
|
98
163
|
end
|
164
|
+
|
165
|
+
def correct_inconsistent_hash_keys(keys)
|
166
|
+
keys.each do |key|
|
167
|
+
ignore_node(key)
|
168
|
+
|
169
|
+
next if requires_quotes?(key)
|
170
|
+
next if properly_quoted?(key.source, %("#{key.value}"))
|
171
|
+
|
172
|
+
correction = "#{quote_type}#{key.value}#{quote_type}"
|
173
|
+
register_offense(
|
174
|
+
key,
|
175
|
+
correction: correction,
|
176
|
+
message: format(MSG_CONSISTENCY, correction: "#{correction}:")
|
177
|
+
)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def quote_type
|
182
|
+
# Use the `Style/StringLiterals` configuration for quoting symbols
|
183
|
+
return '"' unless config.for_cop('Style/StringLiterals')['Enabled']
|
184
|
+
|
185
|
+
config.for_cop('Style/StringLiterals')['EnforcedStyle'] == 'single_quotes' ? "'" : '"'
|
186
|
+
end
|
99
187
|
end
|
100
188
|
end
|
101
189
|
end
|
@@ -23,14 +23,17 @@ module RuboCop
|
|
23
23
|
|
24
24
|
RESTRICT_ON_SEND = %i[to_enum enum_for].freeze
|
25
25
|
|
26
|
+
# @!method enum_conversion_call?(node)
|
26
27
|
def_node_matcher :enum_conversion_call?, <<~PATTERN
|
27
28
|
(send {nil? self} {:to_enum :enum_for} $_ $...)
|
28
29
|
PATTERN
|
29
30
|
|
31
|
+
# @!method method_name?(node, name)
|
30
32
|
def_node_matcher :method_name?, <<~PATTERN
|
31
33
|
{(send nil? {:__method__ :__callee__}) (sym %1)}
|
32
34
|
PATTERN
|
33
35
|
|
36
|
+
# @!method passing_keyword_arg?(node, name)
|
34
37
|
def_node_matcher :passing_keyword_arg?, <<~PATTERN
|
35
38
|
(pair (sym %1) (lvar %1))
|
36
39
|
PATTERN
|
@@ -66,6 +66,7 @@ module RuboCop
|
|
66
66
|
MSG = 'Ensure the accumulator `%<accum>s` will be modified by `%<method>s`.'
|
67
67
|
MSG_INDEX = 'Do not return an element of the accumulator in `%<method>s`.'
|
68
68
|
|
69
|
+
# @!method reduce_with_block?(node)
|
69
70
|
def_node_matcher :reduce_with_block?, <<~PATTERN
|
70
71
|
{
|
71
72
|
(block (send _recv {:reduce :inject} ...) args ...)
|
@@ -73,10 +74,12 @@ module RuboCop
|
|
73
74
|
}
|
74
75
|
PATTERN
|
75
76
|
|
77
|
+
# @!method accumulator_index?(node, accumulator_name)
|
76
78
|
def_node_matcher :accumulator_index?, <<~PATTERN
|
77
79
|
(send (lvar %1) {:[] :[]=} ...)
|
78
80
|
PATTERN
|
79
81
|
|
82
|
+
# @!method element_modified?(node, element_name)
|
80
83
|
def_node_search :element_modified?, <<~PATTERN
|
81
84
|
{
|
82
85
|
(send _receiver !{:[] :[]=} <`(lvar %1) `_ ...>) # method(el, ...)
|
@@ -86,6 +89,7 @@ module RuboCop
|
|
86
89
|
}
|
87
90
|
PATTERN
|
88
91
|
|
92
|
+
# @!method lvar_used?(node, name)
|
89
93
|
def_node_matcher :lvar_used?, <<~PATTERN
|
90
94
|
{
|
91
95
|
(lvar %1)
|
@@ -96,6 +100,7 @@ module RuboCop
|
|
96
100
|
}
|
97
101
|
PATTERN
|
98
102
|
|
103
|
+
# @!method expression_values(node)
|
99
104
|
def_node_search :expression_values, <<~PATTERN
|
100
105
|
{
|
101
106
|
(%RuboCop::AST::Node::VARIABLES $_)
|
@@ -47,6 +47,7 @@ module RuboCop
|
|
47
47
|
METHOD_NAMES = %i[escape encode unescape decode].freeze
|
48
48
|
RESTRICT_ON_SEND = METHOD_NAMES
|
49
49
|
|
50
|
+
# @!method uri_escape_unescape?(node)
|
50
51
|
def_node_matcher :uri_escape_unescape?, <<~PATTERN
|
51
52
|
(send
|
52
53
|
(const ${nil? cbase} :URI) ${:#{METHOD_NAMES.join(' :')}}
|
@@ -150,18 +150,22 @@ module RuboCop
|
|
150
150
|
corrector.remove(range)
|
151
151
|
end
|
152
152
|
|
153
|
+
# @!method static_method_definition?(node)
|
153
154
|
def_node_matcher :static_method_definition?, <<~PATTERN
|
154
155
|
{def (send nil? {:attr :attr_reader :attr_writer :attr_accessor} ...)}
|
155
156
|
PATTERN
|
156
157
|
|
158
|
+
# @!method dynamic_method_definition?(node)
|
157
159
|
def_node_matcher :dynamic_method_definition?, <<~PATTERN
|
158
160
|
{(send nil? :define_method ...) (block (send nil? :define_method ...) ...)}
|
159
161
|
PATTERN
|
160
162
|
|
163
|
+
# @!method class_or_instance_eval?(node)
|
161
164
|
def_node_matcher :class_or_instance_eval?, <<~PATTERN
|
162
165
|
(block (send _ {:class_eval :instance_eval}) ...)
|
163
166
|
PATTERN
|
164
167
|
|
168
|
+
# @!method class_or_module_or_struct_new_call?(node)
|
165
169
|
def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
|
166
170
|
(block (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
|
167
171
|
PATTERN
|
@@ -27,14 +27,17 @@ module RuboCop
|
|
27
27
|
MSG = 'Useless call to `%<count>i.times` detected.'
|
28
28
|
RESTRICT_ON_SEND = %i[times].freeze
|
29
29
|
|
30
|
+
# @!method times_call?(node)
|
30
31
|
def_node_matcher :times_call?, <<~PATTERN
|
31
32
|
(send (int $_) :times (block-pass (sym $_))?)
|
32
33
|
PATTERN
|
33
34
|
|
35
|
+
# @!method block_arg(node)
|
34
36
|
def_node_matcher :block_arg, <<~PATTERN
|
35
37
|
(block _ (args (arg $_)) ...)
|
36
38
|
PATTERN
|
37
39
|
|
40
|
+
# @!method block_reassigns_arg?(node)
|
38
41
|
def_node_search :block_reassigns_arg?, <<~PATTERN
|
39
42
|
(lvasgn %)
|
40
43
|
PATTERN
|
@@ -85,8 +85,11 @@ module RuboCop
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
+
# Returns the base style guide URL from AllCops or the specific department
|
89
|
+
#
|
90
|
+
# @return [String] style guide URL
|
88
91
|
def style_guide_base_url
|
89
|
-
department_name = cop_name.split('/').
|
92
|
+
department_name = cop_name.split('/')[0..-2].join('/')
|
90
93
|
|
91
94
|
config.for_department(department_name)['StyleGuideBaseURL'] ||
|
92
95
|
config.for_all_cops['StyleGuideBaseURL']
|
@@ -12,13 +12,13 @@ module RuboCop
|
|
12
12
|
#
|
13
13
|
# The maximum level of nesting allowed is configurable.
|
14
14
|
class BlockNesting < Base
|
15
|
-
include ConfigurableMax
|
16
|
-
|
17
15
|
NESTING_BLOCKS = %i[
|
18
16
|
case if while while_post
|
19
17
|
until until_post for resbody
|
20
18
|
].freeze
|
21
19
|
|
20
|
+
exclude_limit 'Max'
|
21
|
+
|
22
22
|
def on_new_investigation
|
23
23
|
return if processed_source.blank?
|
24
24
|
|
@@ -51,7 +51,8 @@ module RuboCop
|
|
51
51
|
# end
|
52
52
|
#
|
53
53
|
class ParameterLists < Base
|
54
|
-
|
54
|
+
exclude_limit 'Max'
|
55
|
+
exclude_limit 'MaxOptionalParameters'
|
55
56
|
|
56
57
|
MSG = 'Avoid parameter lists longer than %<max>d parameters. ' \
|
57
58
|
'[%<count>d/%<max>d]'
|
@@ -70,7 +71,9 @@ module RuboCop
|
|
70
71
|
count: optargs.count
|
71
72
|
)
|
72
73
|
|
73
|
-
add_offense(node, message: message)
|
74
|
+
add_offense(node, message: message) do
|
75
|
+
self.max_optional_parameters = optargs.count
|
76
|
+
end
|
74
77
|
end
|
75
78
|
alias on_defs on_def
|
76
79
|
|
@@ -87,6 +90,7 @@ module RuboCop
|
|
87
90
|
|
88
91
|
private
|
89
92
|
|
93
|
+
# @!method argument_to_lambda_or_proc?(node)
|
90
94
|
def_node_matcher :argument_to_lambda_or_proc?, <<~PATTERN
|
91
95
|
^lambda_or_proc?
|
92
96
|
PATTERN
|
@@ -26,10 +26,12 @@ module RuboCop
|
|
26
26
|
return length if @foldable_types.empty?
|
27
27
|
|
28
28
|
each_top_level_descendant(@node, @foldable_types) do |descendant|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
next unless foldable_node?(descendant)
|
30
|
+
|
31
|
+
descendant_length = code_length(descendant)
|
32
|
+
length = length - descendant_length + 1
|
33
|
+
# Subtract 2 length of opening and closing brace if method argument omits hash braces.
|
34
|
+
length -= 2 if descendant.hash_type? && !descendant.braces?
|
33
35
|
end
|
34
36
|
|
35
37
|
length
|
@@ -60,6 +60,7 @@ module RuboCop
|
|
60
60
|
|
61
61
|
private
|
62
62
|
|
63
|
+
# @!method attribute_call?(node)
|
63
64
|
def_node_matcher :attribute_call?, <<~PATTERN
|
64
65
|
( {csend send} _receiver _method # and no parameters
|
65
66
|
)
|
@@ -90,6 +91,7 @@ module RuboCop
|
|
90
91
|
end
|
91
92
|
end
|
92
93
|
|
94
|
+
# @!method root_node?(node)
|
93
95
|
def_node_matcher :root_node?, <<~PATTERN
|
94
96
|
{ nil? | self # e.g. receiver of `my_method` or `self.my_attr`
|
95
97
|
| lvar | ivar | cvar | gvar # e.g. receiver of `var.my_method`
|
@@ -29,15 +29,15 @@ module RuboCop
|
|
29
29
|
|
30
30
|
each_bad_alignment(items, base_column) do |current|
|
31
31
|
expr = current.source_range
|
32
|
-
if
|
32
|
+
if @current_offenses.any? { |o| within?(expr, o.location) }
|
33
33
|
# If this offense is within a line range that is already being
|
34
34
|
# realigned by autocorrect, we report the offense without
|
35
35
|
# autocorrecting it. Two rewrites in the same area by the same
|
36
36
|
# cop cannot be handled. The next iteration will find the
|
37
37
|
# offense again and correct it.
|
38
|
-
|
38
|
+
register_offense(expr, nil)
|
39
39
|
else
|
40
|
-
|
40
|
+
register_offense(current, current)
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -71,6 +71,13 @@ module RuboCop
|
|
71
71
|
def end_of_line_comment(line)
|
72
72
|
processed_source.line_with_comment?(line)
|
73
73
|
end
|
74
|
+
|
75
|
+
# @api private
|
76
|
+
def register_offense(offense_node, message_node)
|
77
|
+
add_offense(offense_node, message: message(message_node)) do |corrector|
|
78
|
+
autocorrect(corrector, message_node)
|
79
|
+
end
|
80
|
+
end
|
74
81
|
end
|
75
82
|
end
|
76
83
|
end
|