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
data/lib/rubocop/config.rb
CHANGED
data/lib/rubocop/cop/base.rb
CHANGED
@@ -30,7 +30,7 @@ module RuboCop
|
|
30
30
|
# if Dir.exist?(local)
|
31
31
|
# gem 'rubocop', path: local
|
32
32
|
# elsif ENV['RUBOCOP_VERSION'] == 'master'
|
33
|
-
# gem 'rubocop', git: 'https://github.com/rubocop
|
33
|
+
# gem 'rubocop', git: 'https://github.com/rubocop/rubocop.git'
|
34
34
|
# else
|
35
35
|
# gem 'rubocop', '~> 0.90.0'
|
36
36
|
# end
|
@@ -57,6 +57,7 @@ module RuboCop
|
|
57
57
|
|
58
58
|
private
|
59
59
|
|
60
|
+
# @!method gem_declarations(node)
|
60
61
|
def_node_search :gem_declarations, '(send nil? :gem str ...)'
|
61
62
|
|
62
63
|
def duplicated_gem_nodes
|
@@ -12,7 +12,7 @@ module RuboCop
|
|
12
12
|
class << self
|
13
13
|
attr_reader :processed_source
|
14
14
|
|
15
|
-
def correct(processed_source, node, column_delta)
|
15
|
+
def correct(corrector, processed_source, node, column_delta)
|
16
16
|
return unless node
|
17
17
|
|
18
18
|
@processed_source = processed_source
|
@@ -21,11 +21,8 @@ module RuboCop
|
|
21
21
|
|
22
22
|
taboo_ranges = inside_string_ranges(node)
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
autocorrect_line(corrector, line_begin_pos, expr, column_delta,
|
27
|
-
taboo_ranges)
|
28
|
-
end
|
24
|
+
each_line(expr) do |line_begin_pos|
|
25
|
+
autocorrect_line(corrector, line_begin_pos, expr, column_delta, taboo_ranges)
|
29
26
|
end
|
30
27
|
end
|
31
28
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
# Allows specified configuration options to have an exclude limit
|
5
|
+
# ie. a maximum value tracked that it can be used by `--auto-gen-config`.
|
6
|
+
module ExcludeLimit
|
7
|
+
# Sets up a configuration option to have an exclude limit tracked.
|
8
|
+
# The parameter name given is transformed into a method name (eg. `Max`
|
9
|
+
# becomes `self.max=` and `MinDigits` becomes `self.min_digits=`).
|
10
|
+
def exclude_limit(parameter_name, method_name: transform(parameter_name))
|
11
|
+
define_method("#{method_name}=") do |value|
|
12
|
+
cfg = config_to_allow_offenses
|
13
|
+
cfg[:exclude_limit] ||= {}
|
14
|
+
current_max = cfg[:exclude_limit][parameter_name]
|
15
|
+
value = [current_max, value].max if current_max
|
16
|
+
cfg[:exclude_limit][parameter_name] = value
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def transform(parameter_name)
|
23
|
+
parameter_name.gsub(/(?<!\A)(?=[A-Z])/, '_').downcase
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Gemspec
|
6
|
+
# This cop checks that `date =` is not used in gemspec file.
|
7
|
+
# It is set automatically when the gem is packaged.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# # bad
|
12
|
+
# Gem::Specification.new do |spec|
|
13
|
+
# s.name = 'your_cool_gem_name'
|
14
|
+
# spec.date = Time.now.strftime('%Y-%m-%d')
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# Gem::Specification.new do |spec|
|
19
|
+
# s.name = 'your_cool_gem_name'
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
class DateAssignment < Base
|
23
|
+
include RangeHelp
|
24
|
+
extend AutoCorrector
|
25
|
+
|
26
|
+
MSG = 'Do not use `date =` in gemspec, it is set automatically when the gem is packaged.'
|
27
|
+
|
28
|
+
# @!method gem_specification(node)
|
29
|
+
def_node_matcher :gem_specification, <<~PATTERN
|
30
|
+
(block
|
31
|
+
(send
|
32
|
+
(const
|
33
|
+
(const {cbase nil?} :Gem) :Specification) :new)
|
34
|
+
...)
|
35
|
+
PATTERN
|
36
|
+
|
37
|
+
def on_block(block_node)
|
38
|
+
return unless gem_specification(block_node)
|
39
|
+
|
40
|
+
block_parameter = block_node.arguments.first.source
|
41
|
+
|
42
|
+
date_assignment = block_node.descendants.detect do |node|
|
43
|
+
node.send_type? && node.receiver&.source == block_parameter && node.method?(:date=)
|
44
|
+
end
|
45
|
+
|
46
|
+
return unless date_assignment
|
47
|
+
|
48
|
+
add_offense(date_assignment) do |corrector|
|
49
|
+
range = range_by_whole_lines(date_assignment.source_range, include_final_newline: true)
|
50
|
+
|
51
|
+
corrector.remove(range)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -40,6 +40,7 @@ module RuboCop
|
|
40
40
|
MSG = '`%<assignment>s` method calls already given on line '\
|
41
41
|
'%<line_of_first_occurrence>d of the gemspec.'
|
42
42
|
|
43
|
+
# @!method gem_specification(node)
|
43
44
|
def_node_search :gem_specification, <<~PATTERN
|
44
45
|
(block
|
45
46
|
(send
|
@@ -49,6 +50,7 @@ module RuboCop
|
|
49
50
|
(arg $_)) ...)
|
50
51
|
PATTERN
|
51
52
|
|
53
|
+
# @!method assignment_method_declarations(node)
|
52
54
|
def_node_search :assignment_method_declarations, <<~PATTERN
|
53
55
|
(send
|
54
56
|
(lvar #match_block_variable_name?) #assignment_method? ...)
|
@@ -55,10 +55,12 @@ module RuboCop
|
|
55
55
|
'.rubocop.yml) should be equal.'
|
56
56
|
MISSING_MSG = '`required_ruby_version` should be specified.'
|
57
57
|
|
58
|
+
# @!method required_ruby_version(node)
|
58
59
|
def_node_search :required_ruby_version, <<~PATTERN
|
59
60
|
(send _ :required_ruby_version= $_)
|
60
61
|
PATTERN
|
61
62
|
|
63
|
+
# @!method defined_ruby_version(node)
|
62
64
|
def_node_matcher :defined_ruby_version, <<~PATTERN
|
63
65
|
{$(str _) $(array (str _) (str _))
|
64
66
|
(send (const (const nil? :Gem) :Requirement) :new $(str _))}
|
@@ -28,8 +28,10 @@ module RuboCop
|
|
28
28
|
class RubyVersionGlobalsUsage < Base
|
29
29
|
MSG = 'Do not use `RUBY_VERSION` in gemspec file.'
|
30
30
|
|
31
|
+
# @!method ruby_version?(node)
|
31
32
|
def_node_matcher :ruby_version?, '(const {cbase nil?} :RUBY_VERSION)'
|
32
33
|
|
34
|
+
# @!method gem_specification?(node)
|
33
35
|
def_node_search :gem_specification?, <<~PATTERN
|
34
36
|
(block
|
35
37
|
(send
|
@@ -9,7 +9,7 @@ module RuboCop
|
|
9
9
|
# @api private
|
10
10
|
class Generator
|
11
11
|
# NOTE: RDoc 5.1.0 or lower has the following issue.
|
12
|
-
# https://github.com/rubocop
|
12
|
+
# https://github.com/rubocop/rubocop/issues/7043
|
13
13
|
#
|
14
14
|
# The following `String#gsub` can be replaced with
|
15
15
|
# squiggly heredoc when RuboCop supports Ruby 2.5 or higher
|
@@ -58,7 +58,7 @@ module RuboCop
|
|
58
58
|
# TODO: Implement the cop in here.
|
59
59
|
#
|
60
60
|
# In many cases, you can use a node matcher for matching node pattern.
|
61
|
-
# See https://github.com/rubocop
|
61
|
+
# See https://github.com/rubocop/rubocop-ast/blob/master/lib/rubocop/ast/node_pattern.rb
|
62
62
|
#
|
63
63
|
# For example
|
64
64
|
MSG = 'Use `#good_method` instead of `#bad_method`.'
|
@@ -4,6 +4,7 @@ require_relative 'internal_affairs/empty_line_between_expect_offense_and_correct
|
|
4
4
|
require_relative 'internal_affairs/example_description'
|
5
5
|
require_relative 'internal_affairs/method_name_equal'
|
6
6
|
require_relative 'internal_affairs/node_destructuring'
|
7
|
+
require_relative 'internal_affairs/node_matcher_directive'
|
7
8
|
require_relative 'internal_affairs/node_type_predicate'
|
8
9
|
require_relative 'internal_affairs/offense_location_keyword'
|
9
10
|
require_relative 'internal_affairs/redundant_described_class_as_subject'
|
@@ -19,10 +19,12 @@ module RuboCop
|
|
19
19
|
MSG = 'Use the methods provided with the node extensions instead ' \
|
20
20
|
'of manually destructuring nodes.'
|
21
21
|
|
22
|
+
# @!method node_variable?(node)
|
22
23
|
def_node_matcher :node_variable?, <<~PATTERN
|
23
24
|
{(lvar [#node_suffix? _]) (send nil? [#node_suffix? _])}
|
24
25
|
PATTERN
|
25
26
|
|
27
|
+
# @!method node_destructuring?(node)
|
26
28
|
def_node_matcher :node_destructuring?, <<~PATTERN
|
27
29
|
{(masgn (mlhs ...) {(send #node_variable? :children) (array (splat #node_variable?))})}
|
28
30
|
PATTERN
|
@@ -0,0 +1,151 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module InternalAffairs
|
6
|
+
# Checks that node matcher definitions are tagged with a YARD `@!method`
|
7
|
+
# directive so that editors are able to find the dynamically defined
|
8
|
+
# method.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# # bad
|
12
|
+
# def_node_matcher :foo?, <<~PATTERN
|
13
|
+
# ...
|
14
|
+
# PATTERN
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# # @!method foo?(node)
|
18
|
+
# def_node_matcher :foo?, <<~PATTERN
|
19
|
+
# ...
|
20
|
+
# PATTERN
|
21
|
+
#
|
22
|
+
class NodeMatcherDirective < Base
|
23
|
+
extend AutoCorrector
|
24
|
+
include RangeHelp
|
25
|
+
|
26
|
+
MSG = 'Preceed `%<method>s` with a `@!method` YARD directive.'
|
27
|
+
MSG_WRONG_NAME = '`@!method` YARD directive has invalid method name, ' \
|
28
|
+
'use `%<expected>s` instead of `%<actual>s`.'
|
29
|
+
MSG_TOO_MANY = 'Multiple `@!method` YARD directives found for this matcher.'
|
30
|
+
|
31
|
+
RESTRICT_ON_SEND = %i[def_node_matcher def_node_search].to_set.freeze
|
32
|
+
REGEXP = /^\s*#\s*@!method\s+(?<method_name>[a-z0-9_]+[?!]?)(?:\((?<args>.*)\))?/.freeze
|
33
|
+
|
34
|
+
# @!method pattern_matcher?(node)
|
35
|
+
def_node_matcher :pattern_matcher?, <<~PATTERN
|
36
|
+
(send _ %RESTRICT_ON_SEND {str sym} {str dstr})
|
37
|
+
PATTERN
|
38
|
+
|
39
|
+
def on_send(node)
|
40
|
+
return if node.arguments.none?
|
41
|
+
return unless valid_method_name?(node)
|
42
|
+
|
43
|
+
actual_name = node.arguments.first.value
|
44
|
+
directives = method_directives(node)
|
45
|
+
return too_many_directives(node) if directives.size > 1
|
46
|
+
|
47
|
+
directive = directives.first
|
48
|
+
return if directive_correct?(directive, actual_name)
|
49
|
+
|
50
|
+
register_offense(node, directive, actual_name)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def valid_method_name?(node)
|
56
|
+
node.arguments.first.str_type? || node.arguments.first.sym_type?
|
57
|
+
end
|
58
|
+
|
59
|
+
def method_directives(node)
|
60
|
+
comments = processed_source.ast_with_comments[node]
|
61
|
+
|
62
|
+
comments.map do |comment|
|
63
|
+
match = comment.text.match(REGEXP)
|
64
|
+
next unless match
|
65
|
+
|
66
|
+
{ node: comment, method_name: match[:method_name], args: match[:args] }
|
67
|
+
end.compact
|
68
|
+
end
|
69
|
+
|
70
|
+
def too_many_directives(node)
|
71
|
+
add_offense(node, message: MSG_TOO_MANY)
|
72
|
+
end
|
73
|
+
|
74
|
+
def directive_correct?(directive, actual_name)
|
75
|
+
directive && directive[:method_name] == actual_name.to_s
|
76
|
+
end
|
77
|
+
|
78
|
+
def register_offense(node, directive, actual_name)
|
79
|
+
message = formatted_message(directive, actual_name, node.method_name)
|
80
|
+
|
81
|
+
add_offense(node, message: message) do |corrector|
|
82
|
+
if directive
|
83
|
+
correct_directive(corrector, directive, actual_name)
|
84
|
+
else
|
85
|
+
insert_directive(corrector, node, actual_name)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def formatted_message(directive, actual_name, method_name)
|
91
|
+
if directive
|
92
|
+
format(MSG_WRONG_NAME, expected: actual_name, actual: directive[:method_name])
|
93
|
+
else
|
94
|
+
format(MSG, method: method_name)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def insert_directive(corrector, node, actual_name)
|
99
|
+
# If the pattern matcher uses arguments (`%1`, `%2`, etc.), include them in the directive
|
100
|
+
arguments = pattern_arguments(node.arguments[1].source)
|
101
|
+
|
102
|
+
range = range_with_surrounding_space(
|
103
|
+
range: node.loc.expression,
|
104
|
+
side: :left,
|
105
|
+
newlines: false
|
106
|
+
)
|
107
|
+
indentation = range.source.match(/^\s*/)[0]
|
108
|
+
directive = "#{indentation}# @!method #{actual_name}(#{arguments.join(', ')})\n"
|
109
|
+
directive = "\n#{directive}" if add_newline?(node)
|
110
|
+
|
111
|
+
corrector.insert_before(range, directive)
|
112
|
+
end
|
113
|
+
|
114
|
+
def pattern_arguments(pattern)
|
115
|
+
arguments = %w[node]
|
116
|
+
max_pattern_var = pattern.scan(/(?<=%)\d+/).map(&:to_i).max
|
117
|
+
max_pattern_var&.times { |i| arguments << "arg#{i + 1}" }
|
118
|
+
arguments
|
119
|
+
end
|
120
|
+
|
121
|
+
def add_newline?(node)
|
122
|
+
# Determine if a blank line should be inserted before the new directive
|
123
|
+
# in order to spread out pattern matchers
|
124
|
+
return if node.sibling_index&.zero?
|
125
|
+
return unless node.parent
|
126
|
+
|
127
|
+
prev_sibling = node.parent.child_nodes[node.sibling_index - 1]
|
128
|
+
return unless prev_sibling && pattern_matcher?(prev_sibling)
|
129
|
+
|
130
|
+
node.loc.line == last_line(prev_sibling) + 1
|
131
|
+
end
|
132
|
+
|
133
|
+
def last_line(node)
|
134
|
+
if node.last_argument.heredoc?
|
135
|
+
node.last_argument.loc.heredoc_end.line
|
136
|
+
else
|
137
|
+
node.loc.last_line
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def correct_directive(corrector, directive, actual_name)
|
142
|
+
correct = "@!method #{actual_name}"
|
143
|
+
regexp = /@!method\s+#{Regexp.escape(directive[:method_name])}/
|
144
|
+
|
145
|
+
replacement = directive[:node].text.gsub(regexp, correct)
|
146
|
+
corrector.replace(directive[:node], replacement)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|