-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_table_force.rb
89 lines (80 loc) · 2.18 KB
/
create_table_force.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true
module RuboCop
module Cop
module Migration
# Create tables without `force: true` option.
#
# The `force: true` option can drop an existing table.
# If you indend to drop an existing table, explicitly call `drop_table` first.
#
# @example
# # bad
# class CreateUsers < ActiveRecord::Migration[7.0]
# def change
# create_table :users, force: true
# end
# end
#
# # good
# class CreateUsers < ActiveRecord::Migration[7.0]
# def change
# create_table :users
# end
# end
class CreateTableForce < RuboCop::Cop::Base
extend AutoCorrector
include RangeHelp
MSG = 'Create tables without `force: true` option.'
RESTRICT_ON_SEND = %i[
create_table
].freeze
# @param node [RuboCop::AST::SendNode]
# @return [void]
def on_send(node)
option_node = option_force_true_from_create_table(node)
return unless option_node
add_offense(option_node) do |corrector|
autocorrect(corrector, option_node)
end
end
private
# @!method option_force_true_from_create_table(node)
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::PairNode, nil]
def_node_matcher :option_force_true_from_create_table, <<~PATTERN
(send
nil?
:create_table
_
(hash
<
$(pair
(sym :force)
true
)
...
>
)
)
PATTERN
# @param corrector [RuboCop::Cop::Corrector]
# @param node [RuboCop::AST::PairNode]
# @return [void]
def autocorrect(
corrector,
node
)
corrector.remove(
range_with_surrounding_comma(
range_with_surrounding_space(
node.source_range,
side: :left
),
:left
)
)
end
end
end
end
end