rubocop 0.47.1 → 0.48.0
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.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +28 -16
- data/config/default.yml +203 -115
- data/config/disabled.yml +0 -5
- data/config/enabled.yml +92 -8
- data/lib/rubocop.rb +24 -1
- data/lib/rubocop/ast/builder.rb +7 -2
- data/lib/rubocop/ast/node.rb +23 -20
- data/lib/rubocop/ast/node/and_node.rb +37 -0
- data/lib/rubocop/ast/node/array_node.rb +4 -1
- data/lib/rubocop/ast/node/case_node.rb +1 -0
- data/lib/rubocop/ast/node/ensure_node.rb +25 -0
- data/lib/rubocop/ast/node/hash_node.rb +10 -3
- data/lib/rubocop/ast/node/if_node.rb +2 -0
- data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +23 -0
- data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +35 -0
- data/lib/rubocop/ast/node/or_node.rb +37 -0
- data/lib/rubocop/ast/node/resbody_node.rb +25 -0
- data/lib/rubocop/ast/node/send_node.rb +190 -0
- data/lib/rubocop/ast/node/when_node.rb +1 -1
- data/lib/rubocop/ast/traversal.rb +15 -15
- data/lib/rubocop/comment_config.rb +1 -1
- data/lib/rubocop/config.rb +39 -15
- data/lib/rubocop/config_loader.rb +34 -13
- data/lib/rubocop/cop/bundler/ordered_gems.rb +23 -4
- data/lib/rubocop/cop/commissioner.rb +4 -0
- data/lib/rubocop/cop/cop.rb +5 -0
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +53 -0
- data/lib/rubocop/cop/lint/debugger.rb +8 -1
- data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +2 -4
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +3 -1
- data/lib/rubocop/cop/lint/empty_ensure.rb +6 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +20 -18
- data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -3
- data/lib/rubocop/cop/lint/literal_in_condition.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/multiple_compare.rb +5 -3
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +26 -18
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +7 -8
- data/lib/rubocop/cop/lint/require_parentheses.rb +7 -13
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +7 -3
- data/lib/rubocop/cop/lint/shadowed_exception.rb +2 -6
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +7 -8
- data/lib/rubocop/cop/lint/unneeded_disable.rb +35 -11
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +5 -2
- data/lib/rubocop/cop/lint/unused_block_argument.rb +6 -6
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -1
- data/lib/rubocop/cop/lint/useless_comparison.rb +5 -4
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/message_annotator.rb +7 -3
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +4 -4
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +2 -2
- data/lib/rubocop/cop/metrics/line_length.rb +2 -9
- data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -3
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +2 -2
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +1 -1
- data/lib/rubocop/cop/mixin/array_hash_indentation.rb +2 -2
- data/lib/rubocop/cop/mixin/check_assignment.rb +6 -6
- data/lib/rubocop/cop/mixin/duplication.rb +1 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/ignored_pattern.rb +27 -0
- data/lib/rubocop/cop/mixin/method_preference.rb +2 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +17 -29
- data/lib/rubocop/cop/mixin/on_method_def.rb +3 -3
- data/lib/rubocop/cop/mixin/percent_literal.rb +27 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +21 -0
- data/lib/rubocop/cop/mixin/safe_mode.rb +1 -1
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -1
- data/lib/rubocop/cop/mixin/target_rails_version.rb +16 -0
- data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
- data/lib/rubocop/cop/offense.rb +3 -3
- data/lib/rubocop/cop/performance/casecmp.rb +1 -1
- data/lib/rubocop/cop/performance/detect.rb +2 -1
- data/lib/rubocop/cop/performance/double_start_end_with.rb +35 -1
- data/lib/rubocop/cop/performance/end_with.rb +3 -1
- data/lib/rubocop/cop/performance/flat_map.rb +6 -6
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
- data/lib/rubocop/cop/performance/range_include.rb +3 -1
- data/lib/rubocop/cop/performance/redundant_match.rb +6 -5
- data/lib/rubocop/cop/performance/regexp_match.rb +10 -3
- data/lib/rubocop/cop/performance/reverse_each.rb +2 -1
- data/lib/rubocop/cop/performance/size.rb +6 -11
- data/lib/rubocop/cop/performance/start_with.rb +3 -1
- data/lib/rubocop/cop/performance/string_replacement.rb +13 -18
- data/lib/rubocop/cop/performance/times_map.rb +4 -4
- data/lib/rubocop/cop/rails/action_filter.rb +42 -42
- data/lib/rubocop/cop/rails/active_support_aliases.rb +68 -0
- data/lib/rubocop/cop/rails/blank.rb +131 -0
- data/lib/rubocop/cop/rails/date.rb +25 -28
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +5 -7
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +7 -3
- data/lib/rubocop/cop/rails/exit.rb +9 -9
- data/lib/rubocop/cop/rails/file_path.rb +5 -14
- data/lib/rubocop/cop/rails/find_by.rb +8 -10
- data/lib/rubocop/cop/rails/find_each.rb +6 -9
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -0
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +15 -7
- data/lib/rubocop/cop/rails/output.rb +3 -5
- data/lib/rubocop/cop/rails/output_safety.rb +4 -8
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +25 -24
- data/lib/rubocop/cop/rails/present.rb +137 -0
- data/lib/rubocop/cop/rails/read_write_attribute.rb +9 -18
- data/lib/rubocop/cop/rails/relative_date_constant.rb +53 -0
- data/lib/rubocop/cop/rails/request_referer.rb +7 -4
- data/lib/rubocop/cop/rails/reversible_migration.rb +1 -2
- data/lib/rubocop/cop/rails/safe_navigation.rb +2 -1
- data/lib/rubocop/cop/rails/save_bang.rb +10 -10
- data/lib/rubocop/cop/rails/skips_model_validations.rb +23 -6
- data/lib/rubocop/cop/rails/time_zone.rb +20 -18
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -2
- data/lib/rubocop/cop/rails/validation.rb +8 -11
- data/lib/rubocop/cop/registry.rb +3 -3
- data/lib/rubocop/cop/security/json_load.rb +1 -1
- data/lib/rubocop/cop/security/marshal_load.rb +5 -1
- data/lib/rubocop/cop/security/yaml_load.rb +3 -3
- data/lib/rubocop/cop/severity.rb +1 -1
- data/lib/rubocop/cop/style/alias.rb +5 -5
- data/lib/rubocop/cop/style/align_hash.rb +1 -1
- data/lib/rubocop/cop/style/align_parameters.rb +5 -5
- data/lib/rubocop/cop/style/and_or.rb +16 -31
- data/lib/rubocop/cop/style/attr.rb +14 -8
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +8 -11
- data/lib/rubocop/cop/style/block_delimiters.rb +11 -13
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +19 -23
- data/lib/rubocop/cop/style/case_indentation.rb +2 -0
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_check.rb +5 -7
- data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +5 -4
- data/lib/rubocop/cop/style/collection_methods.rb +8 -8
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -9
- data/lib/rubocop/cop/style/conditional_assignment.rb +38 -45
- data/lib/rubocop/cop/style/constant_name.rb +1 -1
- data/lib/rubocop/cop/style/documentation_method.rb +1 -0
- data/lib/rubocop/cop/style/dot_position.rb +3 -7
- data/lib/rubocop/cop/style/double_negation.rb +2 -1
- data/lib/rubocop/cop/style/each_with_object.rb +1 -1
- data/lib/rubocop/cop/style/empty_else.rb +2 -2
- data/lib/rubocop/cop/style/empty_line_after_magic_comment.rb +63 -0
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +74 -4
- data/lib/rubocop/cop/style/empty_lines_around_begin_body.rb +42 -0
- data/lib/rubocop/cop/style/empty_lines_around_exception_handling_keywords.rb +127 -0
- data/lib/rubocop/cop/style/empty_literal.rb +17 -9
- data/lib/rubocop/cop/style/end_of_line.rb +25 -3
- data/lib/rubocop/cop/style/file_name.rb +1 -1
- data/lib/rubocop/cop/style/first_method_argument_line_break.rb +1 -1
- data/lib/rubocop/cop/style/first_parameter_indentation.rb +17 -19
- data/lib/rubocop/cop/style/for.rb +2 -4
- data/lib/rubocop/cop/style/format_string.rb +5 -4
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +27 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
- data/lib/rubocop/cop/style/indent_assignment.rb +2 -2
- data/lib/rubocop/cop/style/indent_hash.rb +2 -1
- data/lib/rubocop/cop/style/indent_heredoc.rb +173 -0
- data/lib/rubocop/cop/style/indentation_width.rb +61 -29
- data/lib/rubocop/cop/style/inverse_methods.rb +130 -0
- data/lib/rubocop/cop/style/lambda_call.rb +15 -11
- data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -4
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +26 -14
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -16
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
- data/lib/rubocop/cop/style/missing_else.rb +4 -3
- data/lib/rubocop/cop/style/mixin_grouping.rb +97 -0
- data/lib/rubocop/cop/style/multiline_memoization.rb +38 -5
- data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +2 -3
- data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +38 -19
- data/lib/rubocop/cop/style/mutable_constant.rb +5 -1
- data/lib/rubocop/cop/style/negated_if.rb +73 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +21 -19
- data/lib/rubocop/cop/style/next.rb +5 -5
- data/lib/rubocop/cop/style/non_nil_check.rb +7 -10
- data/lib/rubocop/cop/style/not.rb +3 -4
- data/lib/rubocop/cop/style/numeric_literals.rb +25 -3
- data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
- data/lib/rubocop/cop/style/op_method.rb +2 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +6 -3
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +52 -6
- data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -9
- data/lib/rubocop/cop/style/raise_args.rb +28 -24
- data/lib/rubocop/cop/style/redundant_freeze.rb +5 -7
- data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -3
- data/lib/rubocop/cop/style/redundant_self.rb +17 -35
- data/lib/rubocop/cop/style/rescue_modifier.rb +2 -14
- data/lib/rubocop/cop/style/self_assignment.rb +3 -3
- data/lib/rubocop/cop/style/send.rb +4 -5
- data/lib/rubocop/cop/style/space_after_not.rb +7 -8
- data/lib/rubocop/cop/style/space_around_keyword.rb +8 -9
- data/lib/rubocop/cop/style/space_around_operators.rb +19 -15
- data/lib/rubocop/cop/style/space_before_first_arg.rb +17 -14
- data/lib/rubocop/cop/style/space_inside_brackets.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +3 -3
- data/lib/rubocop/cop/style/space_inside_parens.rb +1 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +14 -14
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/string_literals.rb +1 -1
- data/lib/rubocop/cop/style/string_methods.rb +10 -5
- data/lib/rubocop/cop/style/struct_inheritance.rb +4 -15
- data/lib/rubocop/cop/style/symbol_array.rb +31 -35
- data/lib/rubocop/cop/style/symbol_proc.rb +2 -2
- data/lib/rubocop/cop/style/ternary_parentheses.rb +41 -13
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +6 -9
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +4 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -2
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +12 -34
- data/lib/rubocop/cop/style/zero_length_predicate.rb +11 -4
- data/lib/rubocop/cop/team.rb +4 -1
- data/lib/rubocop/cop/util.rb +33 -26
- data/lib/rubocop/cop/variable_force.rb +13 -13
- data/lib/rubocop/cop/variable_force/assignment.rb +1 -8
- data/lib/rubocop/cop/variable_force/branch.rb +318 -0
- data/lib/rubocop/cop/variable_force/branchable.rb +21 -0
- data/lib/rubocop/cop/variable_force/reference.rb +1 -3
- data/lib/rubocop/cop/variable_force/scope.rb +36 -20
- data/lib/rubocop/cop/variable_force/variable.rb +9 -8
- data/lib/rubocop/formatter/colorizable.rb +10 -10
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +2 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +4 -2
- data/lib/rubocop/magic_comment.rb +20 -6
- data/lib/rubocop/options.rb +1 -1
- data/lib/rubocop/platform.rb +11 -0
- data/lib/rubocop/processed_source.rb +1 -1
- data/lib/rubocop/remote_config.rb +18 -6
- data/lib/rubocop/result_cache.rb +8 -8
- data/lib/rubocop/rspec/cop_helper.rb +2 -0
- data/lib/rubocop/rspec/shared_contexts.rb +20 -0
- data/lib/rubocop/rspec/shared_examples.rb +1 -1
- data/lib/rubocop/runner.rb +2 -2
- data/lib/rubocop/target_finder.rb +64 -6
- data/lib/rubocop/version.rb +2 -4
- metadata +27 -4
- data/lib/rubocop/cop/variable_force/locatable.rb +0 -200
@@ -26,15 +26,15 @@ module RuboCop
|
|
26
26
|
PATTERN
|
27
27
|
|
28
28
|
def on_send(node)
|
29
|
-
read_write_attribute?(node)
|
30
|
-
|
31
|
-
|
29
|
+
return unless read_write_attribute?(node)
|
30
|
+
|
31
|
+
add_offense(node, :selector)
|
32
32
|
end
|
33
33
|
|
34
|
-
|
35
|
-
_receiver, method_name, *_args = *node
|
34
|
+
private
|
36
35
|
|
37
|
-
|
36
|
+
def message(node)
|
37
|
+
if node.method?(:read_attribute)
|
38
38
|
format(MSG, 'self[:attr]', 'read_attribute(:attr)')
|
39
39
|
else
|
40
40
|
format(MSG, 'self[:attr] = val', 'write_attribute(:attr, val)')
|
@@ -42,9 +42,7 @@ module RuboCop
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def autocorrect(node)
|
45
|
-
|
46
|
-
|
47
|
-
case method_name
|
45
|
+
case node.method_name
|
48
46
|
when :read_attribute
|
49
47
|
replacement = read_attribute_replacement(node)
|
50
48
|
when :write_attribute
|
@@ -54,19 +52,12 @@ module RuboCop
|
|
54
52
|
->(corrector) { corrector.replace(node.source_range, replacement) }
|
55
53
|
end
|
56
54
|
|
57
|
-
private
|
58
|
-
|
59
55
|
def read_attribute_replacement(node)
|
60
|
-
|
61
|
-
|
62
|
-
"self[#{body.source}]"
|
56
|
+
"self[#{node.first_argument.source}]"
|
63
57
|
end
|
64
58
|
|
65
59
|
def write_attribute_replacement(node)
|
66
|
-
|
67
|
-
name, value = *args
|
68
|
-
|
69
|
-
"self[#{name.source}] = #{value.source}"
|
60
|
+
"self[#{node.first_argument.source}] = #{node.last_argument.source}"
|
70
61
|
end
|
71
62
|
end
|
72
63
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# This cop checks whether constant value isn't relative date.
|
7
|
+
# Because the relative date will be evaluated only once.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# # bad
|
11
|
+
# class SomeClass
|
12
|
+
# EXPIRED_AT = 1.week.since
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # good
|
16
|
+
# class SomeClass
|
17
|
+
# def self.expired_at
|
18
|
+
# 1.week.since
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
class RelativeDateConstant < Cop
|
22
|
+
MSG = 'Do not assign %s to constants as it will be evaluated only ' \
|
23
|
+
'once.'.freeze
|
24
|
+
|
25
|
+
RELATIVE_DATE_METHODS = %i(ago from_now since until).freeze
|
26
|
+
|
27
|
+
def on_casgn(node)
|
28
|
+
bad_node = node.descendants.find { |n| bad_method?(n) }
|
29
|
+
return unless bad_node
|
30
|
+
|
31
|
+
add_offense(node, :expression, format(MSG, bad_node.method_name))
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def bad_method?(node)
|
37
|
+
node.send_type? &&
|
38
|
+
RELATIVE_DATE_METHODS.include?(node.method_name) &&
|
39
|
+
node.method_args.empty?
|
40
|
+
end
|
41
|
+
|
42
|
+
def autocorrect(node)
|
43
|
+
_scope, const_name, value = *node
|
44
|
+
indent = ' ' * node.loc.column
|
45
|
+
new_code = ["def self.#{const_name.downcase}",
|
46
|
+
"#{indent}#{value.source}",
|
47
|
+
'end'].join("\n#{indent}")
|
48
|
+
->(corrector) { corrector.replace(node.source_range, new_code) }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -8,13 +8,16 @@ module RuboCop
|
|
8
8
|
class RequestReferer < Cop
|
9
9
|
include ConfigurableEnforcedStyle
|
10
10
|
|
11
|
+
MSG = 'Use `request.%s` instead of `request.%s`.'.freeze
|
12
|
+
|
11
13
|
def_node_matcher :referer?, <<-PATTERN
|
12
|
-
(send (send nil :request)
|
14
|
+
(send (send nil :request) {:referer :referrer})
|
13
15
|
PATTERN
|
14
16
|
|
15
17
|
def on_send(node)
|
16
|
-
referer?(node) do
|
17
|
-
return unless
|
18
|
+
referer?(node) do
|
19
|
+
return unless node.method?(wrong_method_name)
|
20
|
+
|
18
21
|
add_offense(node.source_range, node.source_range, message)
|
19
22
|
end
|
20
23
|
end
|
@@ -26,7 +29,7 @@ module RuboCop
|
|
26
29
|
private
|
27
30
|
|
28
31
|
def message
|
29
|
-
|
32
|
+
format(MSG, style, wrong_method_name)
|
30
33
|
end
|
31
34
|
|
32
35
|
def wrong_method_name
|
@@ -202,8 +202,7 @@ module RuboCop
|
|
202
202
|
end
|
203
203
|
|
204
204
|
def all_hash_key?(args, *keys)
|
205
|
-
return false unless args
|
206
|
-
return false unless args.hash_type?
|
205
|
+
return false unless args && args.hash_type?
|
207
206
|
|
208
207
|
hash_keys = args.to_a.map do |arg|
|
209
208
|
arg.to_a.first.children.first.to_sym
|
@@ -63,8 +63,9 @@ module RuboCop
|
|
63
63
|
private
|
64
64
|
|
65
65
|
def autocorrect(node)
|
66
|
-
|
66
|
+
method_node, *params = *node.arguments
|
67
67
|
method = method_node.source[1..-1]
|
68
|
+
|
68
69
|
range = range_between(node.loc.dot.begin_pos,
|
69
70
|
node.loc.expression.end_pos)
|
70
71
|
|
@@ -44,9 +44,10 @@ module RuboCop
|
|
44
44
|
CREATE_CONDITIONAL_MSG = '`%s` returns a model which is always truthy.'
|
45
45
|
.freeze
|
46
46
|
|
47
|
-
CREATE_PERSIST_METHODS =
|
48
|
-
|
49
|
-
MODIFY_PERSIST_METHODS =
|
47
|
+
CREATE_PERSIST_METHODS = %i(create
|
48
|
+
first_or_create find_or_create_by).freeze
|
49
|
+
MODIFY_PERSIST_METHODS = %i(save
|
50
|
+
update update_attributes destroy).freeze
|
50
51
|
PERSIST_METHODS = (CREATE_PERSIST_METHODS +
|
51
52
|
MODIFY_PERSIST_METHODS).freeze
|
52
53
|
|
@@ -107,7 +108,7 @@ module RuboCop
|
|
107
108
|
def persisted_referenced?(assignment)
|
108
109
|
return unless assignment.referenced?
|
109
110
|
assignment.variable.references.any? do |reference|
|
110
|
-
reference.node.parent.
|
111
|
+
reference.node.parent.method?(:persisted?)
|
111
112
|
end
|
112
113
|
end
|
113
114
|
|
@@ -129,8 +130,7 @@ module RuboCop
|
|
129
130
|
end
|
130
131
|
|
131
132
|
def last_call_of_method?(node)
|
132
|
-
|
133
|
-
node.parent.children.count == node.sibling_index + 1
|
133
|
+
node.parent && node.parent.children.count == node.sibling_index + 1
|
134
134
|
end
|
135
135
|
|
136
136
|
# Ignore simple assignment or if condition
|
@@ -143,11 +143,11 @@ module RuboCop
|
|
143
143
|
|
144
144
|
# Check argument signature as no arguments or one hash
|
145
145
|
def expected_signature?(node)
|
146
|
-
node.
|
147
|
-
(node.
|
146
|
+
!node.arguments? ||
|
147
|
+
(node.arguments.one? &&
|
148
148
|
node.method_name != :destroy &&
|
149
|
-
(node.
|
150
|
-
!node.
|
149
|
+
(node.first_argument.hash_type? ||
|
150
|
+
!node.first_argument.literal?))
|
151
151
|
end
|
152
152
|
end
|
153
153
|
end
|
@@ -25,20 +25,37 @@ module RuboCop
|
|
25
25
|
class SkipsModelValidations < Cop
|
26
26
|
MSG = 'Avoid using `%s` because it skips validations.'.freeze
|
27
27
|
|
28
|
+
METHODS_WITH_ARGUMENTS = %w(decrement!
|
29
|
+
decrement_counter
|
30
|
+
increment!
|
31
|
+
increment_counter
|
32
|
+
toggle!
|
33
|
+
update_all
|
34
|
+
update_attribute
|
35
|
+
update_column
|
36
|
+
update_columns
|
37
|
+
update_counters).freeze
|
38
|
+
|
28
39
|
def on_send(node)
|
29
|
-
|
40
|
+
return unless blacklist.include?(node.method_name.to_s)
|
41
|
+
|
42
|
+
_receiver, method_name, *args = *node
|
30
43
|
|
31
|
-
|
44
|
+
if METHODS_WITH_ARGUMENTS.include?(method_name.to_s) && args.empty?
|
45
|
+
return
|
46
|
+
end
|
32
47
|
|
33
|
-
add_offense(node,
|
34
|
-
node.loc.selector,
|
35
|
-
format(MSG, method_name))
|
48
|
+
add_offense(node, :selector)
|
36
49
|
end
|
37
50
|
|
38
51
|
private
|
39
52
|
|
53
|
+
def message(node)
|
54
|
+
format(MSG, node.method_name)
|
55
|
+
end
|
56
|
+
|
40
57
|
def blacklist
|
41
|
-
cop_config['Blacklist']
|
58
|
+
cop_config['Blacklist'] || []
|
42
59
|
end
|
43
60
|
end
|
44
61
|
end
|
@@ -23,7 +23,7 @@ module RuboCop
|
|
23
23
|
# Time.zone.now
|
24
24
|
# Time.zone.parse('2015-03-02 19:05:37')
|
25
25
|
#
|
26
|
-
# # no offense only if style is '
|
26
|
+
# # no offense only if style is 'flexible'
|
27
27
|
# Time.current
|
28
28
|
# DateTime.strptime(str, "%Y-%m-%d %H:%M %Z").in_time_zone
|
29
29
|
# Time.at(timestamp).in_time_zone
|
@@ -40,14 +40,16 @@ module RuboCop
|
|
40
40
|
|
41
41
|
MSG_CURRENT = 'Do not use `%s`. Use `Time.zone.now` instead.'.freeze
|
42
42
|
|
43
|
-
TIMECLASS =
|
43
|
+
TIMECLASS = %i(Time DateTime).freeze
|
44
44
|
|
45
|
-
|
46
|
-
:parse, :at, :current].freeze
|
45
|
+
GOOD_METHODS = %i(zone zone_default find_zone find_zone!).freeze
|
47
46
|
|
48
|
-
|
49
|
-
|
50
|
-
|
47
|
+
DANGEROUS_METHODS = %i(now local new strftime
|
48
|
+
parse at current).freeze
|
49
|
+
|
50
|
+
ACCEPTED_METHODS = %i(in_time_zone utc getlocal
|
51
|
+
iso8601 jisx0301 rfc3339
|
52
|
+
to_i to_f).freeze
|
51
53
|
|
52
54
|
def on_const(node)
|
53
55
|
mod, klass = *node
|
@@ -127,25 +129,26 @@ module RuboCop
|
|
127
129
|
end
|
128
130
|
|
129
131
|
def safe_method(method_name, node)
|
130
|
-
_receiver, _method_name, *args = *node
|
131
132
|
return method_name unless method_name == 'new'
|
132
133
|
|
133
|
-
if
|
134
|
-
'now'
|
135
|
-
else
|
134
|
+
if node.arguments?
|
136
135
|
'local'
|
136
|
+
else
|
137
|
+
'now'
|
137
138
|
end
|
138
139
|
end
|
139
140
|
|
140
141
|
def check_localtime(node)
|
141
142
|
selector_node = node
|
142
|
-
|
143
|
+
|
144
|
+
while node && node.send_type?
|
143
145
|
break if extract_method(node) == :localtime
|
144
146
|
node = node.parent
|
145
147
|
end
|
146
|
-
_receiver, _method, args = *node
|
147
148
|
|
148
|
-
|
149
|
+
return if node.arguments?
|
150
|
+
|
151
|
+
add_offense(selector_node, :selector, MSG_LOCALTIME)
|
149
152
|
end
|
150
153
|
|
151
154
|
def danger_chain?(chain)
|
@@ -162,9 +165,9 @@ module RuboCop
|
|
162
165
|
|
163
166
|
def good_methods
|
164
167
|
if style == :strict
|
165
|
-
|
168
|
+
GOOD_METHODS
|
166
169
|
else
|
167
|
-
|
170
|
+
GOOD_METHODS + [:current] + ACCEPTED_METHODS
|
168
171
|
end
|
169
172
|
end
|
170
173
|
|
@@ -186,8 +189,7 @@ module RuboCop
|
|
186
189
|
# Example:
|
187
190
|
# Time.new(1988, 3, 15, 3, 0, 0, "-05:00")
|
188
191
|
def offset_provided?(node)
|
189
|
-
|
190
|
-
args.length >= 7
|
192
|
+
node.arguments.size >= 7
|
191
193
|
end
|
192
194
|
end
|
193
195
|
end
|
@@ -59,10 +59,11 @@ module RuboCop
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def autocorrect(node)
|
62
|
-
send_pluck, method, *_args = *node
|
63
62
|
lambda do |corrector|
|
63
|
+
method = node.method_name
|
64
|
+
|
64
65
|
corrector.remove(dot_method_with_whitespace(method, node))
|
65
|
-
corrector.insert_before(
|
66
|
+
corrector.insert_before(node.receiver.loc.dot.begin, ".#{method}")
|
66
67
|
end
|
67
68
|
end
|
68
69
|
|
@@ -21,22 +21,20 @@ module RuboCop
|
|
21
21
|
).freeze
|
22
22
|
|
23
23
|
BLACKLIST = TYPES.map { |p| "validates_#{p}_of".to_sym }.freeze
|
24
|
-
|
25
24
|
WHITELIST = TYPES.map { |p| "validates :column, #{p}: value" }.freeze
|
26
25
|
|
27
26
|
def on_send(node)
|
28
|
-
|
29
|
-
return unless receiver.nil? && BLACKLIST.include?(method_name)
|
27
|
+
return unless !node.receiver && BLACKLIST.include?(node.method_name)
|
30
28
|
|
31
|
-
add_offense(node,
|
32
|
-
:selector,
|
33
|
-
format(MSG,
|
34
|
-
preferred_method(method_name),
|
35
|
-
method_name))
|
29
|
+
add_offense(node, :selector)
|
36
30
|
end
|
37
31
|
|
38
32
|
private
|
39
33
|
|
34
|
+
def message(node)
|
35
|
+
format(MSG, preferred_method(node.method_name), node.method_name)
|
36
|
+
end
|
37
|
+
|
40
38
|
def preferred_method(method)
|
41
39
|
WHITELIST[BLACKLIST.index(method.to_sym)]
|
42
40
|
end
|
@@ -49,9 +47,8 @@ module RuboCop
|
|
49
47
|
end
|
50
48
|
|
51
49
|
def correct_validate_type(corrector, node)
|
52
|
-
|
53
|
-
|
54
|
-
validate_type = method_name.to_s.split('_')[1]
|
50
|
+
options = node.arguments.find { |arg| !arg.sym_type? }
|
51
|
+
validate_type = node.method_name.to_s.split('_')[1]
|
55
52
|
|
56
53
|
if options
|
57
54
|
corrector.replace(options.loc.expression,
|
data/lib/rubocop/cop/registry.rb
CHANGED
@@ -72,7 +72,7 @@ module RuboCop
|
|
72
72
|
# @example namespaces bare cop identifiers
|
73
73
|
#
|
74
74
|
# cops = RuboCop::Cop::Cop.all
|
75
|
-
# cops.qualified_cop_name('IndentArray') # => 'IndentArray'
|
75
|
+
# cops.qualified_cop_name('IndentArray') # => 'Style/IndentArray'
|
76
76
|
#
|
77
77
|
# @example passes back unrecognized cop names
|
78
78
|
#
|
@@ -80,7 +80,7 @@ module RuboCop
|
|
80
80
|
# cops.qualified_cop_name('NotACop') # => 'NotACop'
|
81
81
|
#
|
82
82
|
# @param name [String] Cop name extracted from config
|
83
|
-
# @param path [String] Path of file that `name` was extracted from
|
83
|
+
# @param path [String, nil] Path of file that `name` was extracted from
|
84
84
|
#
|
85
85
|
# @raise [AmbiguousCopName]
|
86
86
|
# if a bare identifier with two possible namespaces is provided
|
@@ -115,7 +115,7 @@ module RuboCop
|
|
115
115
|
|
116
116
|
def enabled(config, only)
|
117
117
|
select do |cop|
|
118
|
-
config.
|
118
|
+
config.for_cop(cop).fetch('Enabled') || only.include?(cop.cop_name)
|
119
119
|
end
|
120
120
|
end
|
121
121
|
|
@@ -15,11 +15,15 @@ module RuboCop
|
|
15
15
|
# # good
|
16
16
|
# Marshal.dump("{}")
|
17
17
|
#
|
18
|
+
# # okish - deep copy hack
|
19
|
+
# Marshal.load(Marshal.dump({}))
|
20
|
+
#
|
18
21
|
class MarshalLoad < Cop
|
19
22
|
MSG = 'Avoid using `Marshal.%s`.'.freeze
|
20
23
|
|
21
24
|
def_node_matcher :marshal_load, <<-END
|
22
|
-
(send (const nil :Marshal) ${:load :restore}
|
25
|
+
(send (const {nil cbase} :Marshal) ${:load :restore}
|
26
|
+
!(send (const {nil cbase} :Marshal) :dump ...))
|
23
27
|
END
|
24
28
|
|
25
29
|
def on_send(node)
|