rubocop 0.70.0 → 0.72.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -10
- data/config/default.yml +50 -491
- data/lib/rubocop.rb +5 -53
- data/lib/rubocop/ast/builder.rb +2 -0
- data/lib/rubocop/ast/node.rb +1 -1
- data/lib/rubocop/ast/node/float_node.rb +12 -0
- data/lib/rubocop/ast/node/int_node.rb +12 -0
- data/lib/rubocop/ast/node/mixin/numeric_node.rb +21 -0
- data/lib/rubocop/ast/node/resbody_node.rb +1 -6
- data/lib/rubocop/cached_data.rb +1 -1
- data/lib/rubocop/config.rb +35 -6
- data/lib/rubocop/config_loader.rb +2 -2
- data/lib/rubocop/config_loader_resolver.rb +0 -6
- data/lib/rubocop/cop/cop.rb +0 -4
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +55 -0
- data/lib/rubocop/cop/layout/class_structure.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_block_body.rb +3 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +4 -0
- data/lib/rubocop/cop/layout/indent_first_argument.rb +6 -2
- data/lib/rubocop/cop/layout/indent_first_parameter.rb +7 -3
- data/lib/rubocop/cop/layout/indent_heredoc.rb +0 -1
- data/lib/rubocop/cop/layout/indentation_consistency.rb +13 -12
- data/lib/rubocop/cop/layout/indentation_width.rb +8 -4
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +2 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
- data/lib/rubocop/cop/mixin/hash_alignment.rb +4 -0
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +20 -22
- data/lib/rubocop/cop/style/commented_keyword.rb +1 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -1
- data/lib/rubocop/cop/style/float_division.rb +94 -0
- data/lib/rubocop/cop/style/format_string.rb +7 -3
- data/lib/rubocop/cop/style/if_inside_else.rb +42 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +12 -2
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -0
- data/lib/rubocop/cop/style/word_array.rb +2 -2
- data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
- data/lib/rubocop/node_pattern.rb +84 -5
- data/lib/rubocop/options.rb +0 -2
- data/lib/rubocop/processed_source.rb +5 -1
- data/lib/rubocop/rspec/cop_helper.rb +0 -1
- data/lib/rubocop/rspec/shared_contexts.rb +0 -17
- data/lib/rubocop/rspec/support.rb +0 -1
- data/lib/rubocop/runner.rb +6 -7
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop/yaml_duplication_checker.rb +8 -2
- metadata +7 -69
- data/lib/rubocop/cop/mixin/target_rails_version.rb +0 -16
- data/lib/rubocop/cop/rails/action_filter.rb +0 -117
- data/lib/rubocop/cop/rails/active_record_aliases.rb +0 -48
- data/lib/rubocop/cop/rails/active_record_override.rb +0 -82
- data/lib/rubocop/cop/rails/active_support_aliases.rb +0 -69
- data/lib/rubocop/cop/rails/application_job.rb +0 -40
- data/lib/rubocop/cop/rails/application_record.rb +0 -40
- data/lib/rubocop/cop/rails/assert_not.rb +0 -44
- data/lib/rubocop/cop/rails/belongs_to.rb +0 -102
- data/lib/rubocop/cop/rails/blank.rb +0 -164
- data/lib/rubocop/cop/rails/bulk_change_table.rb +0 -289
- data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +0 -91
- data/lib/rubocop/cop/rails/date.rb +0 -161
- data/lib/rubocop/cop/rails/delegate.rb +0 -132
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +0 -37
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +0 -91
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +0 -45
- data/lib/rubocop/cop/rails/environment_comparison.rb +0 -68
- data/lib/rubocop/cop/rails/exit.rb +0 -67
- data/lib/rubocop/cop/rails/file_path.rb +0 -108
- data/lib/rubocop/cop/rails/find_by.rb +0 -55
- data/lib/rubocop/cop/rails/find_each.rb +0 -51
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +0 -25
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +0 -106
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +0 -117
- data/lib/rubocop/cop/rails/http_status.rb +0 -179
- data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +0 -94
- data/lib/rubocop/cop/rails/inverse_of.rb +0 -246
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +0 -175
- data/lib/rubocop/cop/rails/link_to_blank.rb +0 -98
- data/lib/rubocop/cop/rails/not_null_column.rb +0 -67
- data/lib/rubocop/cop/rails/output.rb +0 -49
- data/lib/rubocop/cop/rails/output_safety.rb +0 -99
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +0 -107
- data/lib/rubocop/cop/rails/presence.rb +0 -124
- data/lib/rubocop/cop/rails/present.rb +0 -153
- data/lib/rubocop/cop/rails/read_write_attribute.rb +0 -74
- data/lib/rubocop/cop/rails/redundant_allow_nil.rb +0 -111
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +0 -136
- data/lib/rubocop/cop/rails/reflection_class_name.rb +0 -37
- data/lib/rubocop/cop/rails/refute_methods.rb +0 -76
- data/lib/rubocop/cop/rails/relative_date_constant.rb +0 -93
- data/lib/rubocop/cop/rails/request_referer.rb +0 -56
- data/lib/rubocop/cop/rails/reversible_migration.rb +0 -286
- data/lib/rubocop/cop/rails/safe_navigation.rb +0 -87
- data/lib/rubocop/cop/rails/save_bang.rb +0 -316
- data/lib/rubocop/cop/rails/scope_args.rb +0 -29
- data/lib/rubocop/cop/rails/skips_model_validations.rb +0 -87
- data/lib/rubocop/cop/rails/time_zone.rb +0 -238
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +0 -105
- data/lib/rubocop/cop/rails/unknown_env.rb +0 -63
- data/lib/rubocop/cop/rails/validation.rb +0 -109
- data/lib/rubocop/rspec/shared_examples.rb +0 -59
@@ -1,37 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Rails
|
6
|
-
# This cop checks if the value of the option `class_name`, in
|
7
|
-
# the definition of a reflection is a string.
|
8
|
-
#
|
9
|
-
# @example
|
10
|
-
# # bad
|
11
|
-
# has_many :accounts, class_name: Account
|
12
|
-
# has_many :accounts, class_name: Account.name
|
13
|
-
#
|
14
|
-
# # good
|
15
|
-
# has_many :accounts, class_name: 'Account'
|
16
|
-
class ReflectionClassName < Cop
|
17
|
-
MSG = 'Use a string value for `class_name`.'
|
18
|
-
|
19
|
-
def_node_matcher :association_with_reflection, <<-PATTERN
|
20
|
-
(send nil? {:has_many :has_one :belongs_to} _
|
21
|
-
(hash <$#reflection_class_name ...>)
|
22
|
-
)
|
23
|
-
PATTERN
|
24
|
-
|
25
|
-
def_node_matcher :reflection_class_name, <<-PATTERN
|
26
|
-
(pair (sym :class_name) [!dstr !str !sym])
|
27
|
-
PATTERN
|
28
|
-
|
29
|
-
def on_send(node)
|
30
|
-
association_with_reflection(node) do |reflection_class_name|
|
31
|
-
add_offense(node, location: reflection_class_name.loc.expression)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,76 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Rails
|
6
|
-
#
|
7
|
-
# Use `assert_not` methods instead of `refute` methods.
|
8
|
-
#
|
9
|
-
# @example
|
10
|
-
# # bad
|
11
|
-
# refute false
|
12
|
-
# refute_empty [1, 2, 3]
|
13
|
-
# refute_equal true, false
|
14
|
-
#
|
15
|
-
# # good
|
16
|
-
# assert_not false
|
17
|
-
# assert_not_empty [1, 2, 3]
|
18
|
-
# assert_not_equal true, false
|
19
|
-
#
|
20
|
-
class RefuteMethods < Cop
|
21
|
-
MSG = 'Prefer `%<assert_method>s` over `%<refute_method>s`.'
|
22
|
-
|
23
|
-
CORRECTIONS = {
|
24
|
-
refute: 'assert_not',
|
25
|
-
refute_empty: 'assert_not_empty',
|
26
|
-
refute_equal: 'assert_not_equal',
|
27
|
-
refute_in_delta: 'assert_not_in_delta',
|
28
|
-
refute_in_epsilon: 'assert_not_in_epsilon',
|
29
|
-
refute_includes: 'assert_not_includes',
|
30
|
-
refute_instance_of: 'assert_not_instance_of',
|
31
|
-
refute_kind_of: 'assert_not_kind_of',
|
32
|
-
refute_nil: 'assert_not_nil',
|
33
|
-
refute_operator: 'assert_not_operator',
|
34
|
-
refute_predicate: 'assert_not_predicate',
|
35
|
-
refute_respond_to: 'assert_not_respond_to',
|
36
|
-
refute_same: 'assert_not_same',
|
37
|
-
refute_match: 'assert_no_match'
|
38
|
-
}.freeze
|
39
|
-
|
40
|
-
OFFENSIVE_METHODS = CORRECTIONS.keys.freeze
|
41
|
-
|
42
|
-
def_node_matcher :offensive?, '(send nil? #refute_method? ...)'
|
43
|
-
|
44
|
-
def on_send(node)
|
45
|
-
return unless offensive?(node)
|
46
|
-
|
47
|
-
message = offense_message(node.method_name)
|
48
|
-
add_offense(node, location: :selector, message: message)
|
49
|
-
end
|
50
|
-
|
51
|
-
def autocorrect(node)
|
52
|
-
lambda do |corrector|
|
53
|
-
corrector.replace(
|
54
|
-
node.loc.selector,
|
55
|
-
CORRECTIONS[node.method_name]
|
56
|
-
)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
private
|
61
|
-
|
62
|
-
def refute_method?(method_name)
|
63
|
-
OFFENSIVE_METHODS.include?(method_name)
|
64
|
-
end
|
65
|
-
|
66
|
-
def offense_message(method_name)
|
67
|
-
format(
|
68
|
-
MSG,
|
69
|
-
refute_method: method_name,
|
70
|
-
assert_method: CORRECTIONS[method_name]
|
71
|
-
)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
@@ -1,93 +0,0 @@
|
|
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
|
-
include RangeHelp
|
23
|
-
|
24
|
-
MSG = 'Do not assign %<method_name>s to constants as it ' \
|
25
|
-
'will be evaluated only once.'
|
26
|
-
|
27
|
-
def on_casgn(node)
|
28
|
-
relative_date_assignment?(node) do |method_name|
|
29
|
-
add_offense(node, message: format(MSG, method_name: method_name))
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def on_masgn(node)
|
34
|
-
lhs, rhs = *node
|
35
|
-
|
36
|
-
return unless rhs&.array_type?
|
37
|
-
|
38
|
-
lhs.children.zip(rhs.children).each do |(name, value)|
|
39
|
-
next unless name.casgn_type?
|
40
|
-
|
41
|
-
relative_date?(value) do |method_name|
|
42
|
-
add_offense(node,
|
43
|
-
location: range_between(name.loc.expression.begin_pos,
|
44
|
-
value.loc.expression.end_pos),
|
45
|
-
message: format(MSG, method_name: method_name))
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def on_or_asgn(node)
|
51
|
-
relative_date_or_assignment?(node) do |method_name|
|
52
|
-
add_offense(node, message: format(MSG, method_name: method_name))
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def autocorrect(node)
|
57
|
-
return unless node.casgn_type?
|
58
|
-
|
59
|
-
scope, const_name, value = *node
|
60
|
-
return unless scope.nil?
|
61
|
-
|
62
|
-
indent = ' ' * node.loc.column
|
63
|
-
new_code = ["def self.#{const_name.downcase}",
|
64
|
-
"#{indent}#{value.source}",
|
65
|
-
'end'].join("\n#{indent}")
|
66
|
-
->(corrector) { corrector.replace(node.source_range, new_code) }
|
67
|
-
end
|
68
|
-
|
69
|
-
private
|
70
|
-
|
71
|
-
def_node_matcher :relative_date_assignment?, <<-PATTERN
|
72
|
-
{
|
73
|
-
(casgn _ _ (send _ ${:since :from_now :after :ago :until :before}))
|
74
|
-
(casgn _ _ ({erange irange} _ (send _ ${:since :from_now :after :ago :until :before})))
|
75
|
-
(casgn _ _ ({erange irange} (send _ ${:since :from_now :after :ago :until :before}) _))
|
76
|
-
}
|
77
|
-
PATTERN
|
78
|
-
|
79
|
-
def_node_matcher :relative_date_or_assignment?, <<-PATTERN
|
80
|
-
(:or_asgn (casgn _ _) (send _ ${:since :from_now :after :ago :until :before}))
|
81
|
-
PATTERN
|
82
|
-
|
83
|
-
def_node_matcher :relative_date?, <<-PATTERN
|
84
|
-
{
|
85
|
-
({erange irange} _ (send _ ${:since :from_now :after :ago :until :before}))
|
86
|
-
({erange irange} (send _ ${:since :from_now :after :ago :until :before}) _)
|
87
|
-
(send _ ${:since :from_now :after :ago :until :before})
|
88
|
-
}
|
89
|
-
PATTERN
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Rails
|
6
|
-
# This cop checks for consistent uses of `request.referer` or
|
7
|
-
# `request.referrer`, depending on the cop's configuration.
|
8
|
-
#
|
9
|
-
# @example EnforcedStyle: referer (default)
|
10
|
-
# # bad
|
11
|
-
# request.referrer
|
12
|
-
#
|
13
|
-
# # good
|
14
|
-
# request.referer
|
15
|
-
#
|
16
|
-
# @example EnforcedStyle: referrer
|
17
|
-
# # bad
|
18
|
-
# request.referer
|
19
|
-
#
|
20
|
-
# # good
|
21
|
-
# request.referrer
|
22
|
-
class RequestReferer < Cop
|
23
|
-
include ConfigurableEnforcedStyle
|
24
|
-
|
25
|
-
MSG = 'Use `request.%<prefer>s` instead of ' \
|
26
|
-
'`request.%<current>s`.'
|
27
|
-
|
28
|
-
def_node_matcher :referer?, <<-PATTERN
|
29
|
-
(send (send nil? :request) {:referer :referrer})
|
30
|
-
PATTERN
|
31
|
-
|
32
|
-
def on_send(node)
|
33
|
-
referer?(node) do
|
34
|
-
return unless node.method?(wrong_method_name)
|
35
|
-
|
36
|
-
add_offense(node.source_range, location: node.source_range)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def autocorrect(node)
|
41
|
-
->(corrector) { corrector.replace(node, "request.#{style}") }
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
def message(_node)
|
47
|
-
format(MSG, prefer: style, current: wrong_method_name)
|
48
|
-
end
|
49
|
-
|
50
|
-
def wrong_method_name
|
51
|
-
style == :referer ? :referrer : :referer
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
@@ -1,286 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
module Cop
|
5
|
-
module Rails
|
6
|
-
# This cop checks whether the change method of the migration file is
|
7
|
-
# reversible.
|
8
|
-
#
|
9
|
-
# @example
|
10
|
-
# # bad
|
11
|
-
# def change
|
12
|
-
# change_table :users do |t|
|
13
|
-
# t.remove :name
|
14
|
-
# end
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# # good
|
18
|
-
# def change
|
19
|
-
# create_table :users do |t|
|
20
|
-
# t.string :name
|
21
|
-
# end
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# # good
|
25
|
-
# def change
|
26
|
-
# reversible do |dir|
|
27
|
-
# change_table :users do |t|
|
28
|
-
# dir.up do
|
29
|
-
# t.column :name, :string
|
30
|
-
# end
|
31
|
-
#
|
32
|
-
# dir.down do
|
33
|
-
# t.remove :name
|
34
|
-
# end
|
35
|
-
# end
|
36
|
-
# end
|
37
|
-
# end
|
38
|
-
#
|
39
|
-
# @example
|
40
|
-
# # drop_table
|
41
|
-
#
|
42
|
-
# # bad
|
43
|
-
# def change
|
44
|
-
# drop_table :users
|
45
|
-
# end
|
46
|
-
#
|
47
|
-
# # good
|
48
|
-
# def change
|
49
|
-
# drop_table :users do |t|
|
50
|
-
# t.string :name
|
51
|
-
# end
|
52
|
-
# end
|
53
|
-
#
|
54
|
-
# @example
|
55
|
-
# # change_column_default
|
56
|
-
#
|
57
|
-
# # bad
|
58
|
-
# def change
|
59
|
-
# change_column_default(:suppliers, :qualification, 'new')
|
60
|
-
# end
|
61
|
-
#
|
62
|
-
# # good
|
63
|
-
# def change
|
64
|
-
# change_column_default(:posts, :state, from: nil, to: "draft")
|
65
|
-
# end
|
66
|
-
#
|
67
|
-
# @example
|
68
|
-
# # remove_column
|
69
|
-
#
|
70
|
-
# # bad
|
71
|
-
# def change
|
72
|
-
# remove_column(:suppliers, :qualification)
|
73
|
-
# end
|
74
|
-
#
|
75
|
-
# # good
|
76
|
-
# def change
|
77
|
-
# remove_column(:suppliers, :qualification, :string)
|
78
|
-
# end
|
79
|
-
#
|
80
|
-
# @example
|
81
|
-
# # remove_foreign_key
|
82
|
-
#
|
83
|
-
# # bad
|
84
|
-
# def change
|
85
|
-
# remove_foreign_key :accounts, column: :owner_id
|
86
|
-
# end
|
87
|
-
#
|
88
|
-
# # good
|
89
|
-
# def change
|
90
|
-
# remove_foreign_key :accounts, :branches
|
91
|
-
# end
|
92
|
-
#
|
93
|
-
# @example
|
94
|
-
# # change_table
|
95
|
-
#
|
96
|
-
# # bad
|
97
|
-
# def change
|
98
|
-
# change_table :users do |t|
|
99
|
-
# t.remove :name
|
100
|
-
# t.change_default :authorized, 1
|
101
|
-
# t.change :price, :string
|
102
|
-
# end
|
103
|
-
# end
|
104
|
-
#
|
105
|
-
# # good
|
106
|
-
# def change
|
107
|
-
# change_table :users do |t|
|
108
|
-
# t.string :name
|
109
|
-
# end
|
110
|
-
# end
|
111
|
-
#
|
112
|
-
# # good
|
113
|
-
# def change
|
114
|
-
# reversible do |dir|
|
115
|
-
# change_table :users do |t|
|
116
|
-
# dir.up do
|
117
|
-
# t.change :price, :string
|
118
|
-
# end
|
119
|
-
#
|
120
|
-
# dir.down do
|
121
|
-
# t.change :price, :integer
|
122
|
-
# end
|
123
|
-
# end
|
124
|
-
# end
|
125
|
-
# end
|
126
|
-
#
|
127
|
-
# @see https://api.rubyonrails.org/classes/ActiveRecord/Migration/CommandRecorder.html
|
128
|
-
class ReversibleMigration < Cop
|
129
|
-
MSG = '%<action>s is not reversible.'
|
130
|
-
IRREVERSIBLE_CHANGE_TABLE_CALLS = %i[
|
131
|
-
change change_default remove
|
132
|
-
].freeze
|
133
|
-
|
134
|
-
def_node_matcher :irreversible_schema_statement_call, <<-PATTERN
|
135
|
-
(send nil? ${:change_table_comment :execute :remove_belongs_to} ...)
|
136
|
-
PATTERN
|
137
|
-
|
138
|
-
def_node_matcher :drop_table_call, <<-PATTERN
|
139
|
-
(send nil? :drop_table ...)
|
140
|
-
PATTERN
|
141
|
-
|
142
|
-
def_node_matcher :change_column_default_call, <<-PATTERN
|
143
|
-
(send nil? :change_column_default {[(sym _) (sym _)] (splat _)} $...)
|
144
|
-
PATTERN
|
145
|
-
|
146
|
-
def_node_matcher :remove_column_call, <<-PATTERN
|
147
|
-
(send nil? :remove_column $...)
|
148
|
-
PATTERN
|
149
|
-
|
150
|
-
def_node_matcher :remove_foreign_key_call, <<-PATTERN
|
151
|
-
(send nil? :remove_foreign_key _ $_)
|
152
|
-
PATTERN
|
153
|
-
|
154
|
-
def_node_matcher :change_table_call, <<-PATTERN
|
155
|
-
(send nil? :change_table $_ ...)
|
156
|
-
PATTERN
|
157
|
-
|
158
|
-
def on_send(node)
|
159
|
-
return unless within_change_method?(node)
|
160
|
-
return if within_reversible_or_up_only_block?(node)
|
161
|
-
|
162
|
-
check_irreversible_schema_statement_node(node)
|
163
|
-
check_drop_table_node(node)
|
164
|
-
check_change_column_default_node(node)
|
165
|
-
check_remove_column_node(node)
|
166
|
-
check_remove_foreign_key_node(node)
|
167
|
-
end
|
168
|
-
|
169
|
-
def on_block(node)
|
170
|
-
return unless within_change_method?(node)
|
171
|
-
return if within_reversible_or_up_only_block?(node)
|
172
|
-
return if node.body.nil?
|
173
|
-
|
174
|
-
check_change_table_node(node.send_node, node.body)
|
175
|
-
end
|
176
|
-
|
177
|
-
private
|
178
|
-
|
179
|
-
def check_irreversible_schema_statement_node(node)
|
180
|
-
irreversible_schema_statement_call(node) do |method_name|
|
181
|
-
add_offense(node, message: format(MSG, action: method_name))
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
def check_drop_table_node(node)
|
186
|
-
drop_table_call(node) do
|
187
|
-
unless node.parent.block_type?
|
188
|
-
add_offense(
|
189
|
-
node,
|
190
|
-
message: format(MSG, action: 'drop_table(without block)')
|
191
|
-
)
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
def check_change_column_default_node(node)
|
197
|
-
change_column_default_call(node) do |args|
|
198
|
-
unless all_hash_key?(args.last, :from, :to)
|
199
|
-
add_offense(
|
200
|
-
node,
|
201
|
-
message: format(
|
202
|
-
MSG, action: 'change_column_default(without :from and :to)'
|
203
|
-
)
|
204
|
-
)
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
def check_remove_column_node(node)
|
210
|
-
remove_column_call(node) do |args|
|
211
|
-
if args.to_a.size < 3
|
212
|
-
add_offense(
|
213
|
-
node,
|
214
|
-
message: format(MSG, action: 'remove_column(without type)')
|
215
|
-
)
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
def check_remove_foreign_key_node(node)
|
221
|
-
remove_foreign_key_call(node) do |arg|
|
222
|
-
if arg.hash_type?
|
223
|
-
add_offense(
|
224
|
-
node,
|
225
|
-
message: format(MSG,
|
226
|
-
action: 'remove_foreign_key(without table)')
|
227
|
-
)
|
228
|
-
end
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
def check_change_table_node(node, block)
|
233
|
-
change_table_call(node) do |arg|
|
234
|
-
if target_rails_version < 4.0
|
235
|
-
add_offense(
|
236
|
-
node,
|
237
|
-
message: format(MSG, action: 'change_table')
|
238
|
-
)
|
239
|
-
elsif block.send_type?
|
240
|
-
check_change_table_offense(arg, block)
|
241
|
-
else
|
242
|
-
block.each_child_node(:send) do |child_node|
|
243
|
-
check_change_table_offense(arg, child_node)
|
244
|
-
end
|
245
|
-
end
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
def check_change_table_offense(receiver, node)
|
250
|
-
method_name = node.method_name
|
251
|
-
return if receiver != node.receiver &&
|
252
|
-
!IRREVERSIBLE_CHANGE_TABLE_CALLS.include?(method_name)
|
253
|
-
|
254
|
-
add_offense(
|
255
|
-
node,
|
256
|
-
message: format(MSG, action: "change_table(with #{method_name})")
|
257
|
-
)
|
258
|
-
end
|
259
|
-
|
260
|
-
def within_change_method?(node)
|
261
|
-
node.each_ancestor(:def).any? do |ancestor|
|
262
|
-
ancestor.method?(:change)
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
def within_reversible_or_up_only_block?(node)
|
267
|
-
node.each_ancestor(:block).any? do |ancestor|
|
268
|
-
ancestor.block_type? &&
|
269
|
-
ancestor.send_node.method?(:reversible) ||
|
270
|
-
ancestor.send_node.method?(:up_only)
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
def all_hash_key?(args, *keys)
|
275
|
-
return false unless args&.hash_type?
|
276
|
-
|
277
|
-
hash_keys = args.keys.map do |key|
|
278
|
-
key.children.first.to_sym
|
279
|
-
end
|
280
|
-
|
281
|
-
hash_keys & keys == keys
|
282
|
-
end
|
283
|
-
end
|
284
|
-
end
|
285
|
-
end
|
286
|
-
end
|