-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
reserved_word_mysql.rb
232 lines (213 loc) · 6.24 KB
/
reserved_word_mysql.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# frozen_string_literal: true
require 'set'
module RuboCop
module Cop
module Migration
# Avoid using MySQL reserved words as identifiers.
#
# @example
# # bad
# # NOTE: `role` is a reserved word in MySQL.
# add_column :users, :role, :string
#
# # good
# add_column :users, :some_other_good_name, :string
class ReservedWordMysql < RuboCop::Cop::Base
include ::RuboCop::Migration::CopConcerns::ColumnTypeMethod
MSG = 'Avoid using MySQL reserved words as identifiers.'
# Obtained from https://dev.mysql.com/doc/refman/8.0/en/keywords.html.
PATH_TO_RESERVED_WORDS_FILE = File.expand_path(
'../../../../data/reserved_words_mysql.txt',
__dir__
).freeze
RESTRICT_ON_SEND = [
:add_column,
:add_index,
:add_reference,
:create_join_table,
:create_table,
:rename,
:rename_column,
:rename_index,
:rename_table,
*COLUMN_TYPE_METHOD_NAMES
].freeze
class << self
# @return [Array<String>]
def reserved_words
@reserved_words ||= ::Set.new(
::File.read(PATH_TO_RESERVED_WORDS_FILE).split("\n")
).freeze
end
end
# @param node [RuboCop::AST::DefNode]
# @return [void]
def on_send(node)
offended_identifier_nodes_from(node).each do |identifier_node|
add_offense(identifier_node)
end
end
private
# @!method index_name_option_from_add_index(node)
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::Node, nil]
def_node_matcher :index_name_option_from_add_index, <<~PATTERN
(send
nil?
:add_index
_
_
(hash
<
(pair
(sym :name)
$_
)
>
...
)
)
PATTERN
# @!method index_name_option_from_add_reference(node)
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::Node, nil]
def_node_matcher :index_name_option_from_add_reference, <<~PATTERN
(send
nil?
:add_reference
_
_
(hash
<
(pair
(sym :index)
(hash
<
(pair
(sym :name)
$_
)
>
...
)
)
>
...
)
)
PATTERN
# @!method index_name_option_from_column_type(node)
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::Node, nil]
def_node_matcher :index_name_option_from_column_type, <<~PATTERN
(send
lvar
COLUMN_TYPE_METHOD_NAMES
_
(hash
<
(pair
(sym :index)
(hash
<
(pair
(sym :name)
$_
)
...
>
)
)
...
>
)
)
PATTERN
# @!method table_name_option_from(node)
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::Node, nil]
def_node_matcher :table_name_option_from, <<~PATTERN
(send
nil?
:create_join_table
_
_
(hash
<
(pair
(sym :table_name)
$_
)
...
>
)
)
PATTERN
# @param node [RuboCop::AST::SendNode]
# @return [Array<RuboCop::AST::Node>]
def identifier_column_name_nodes_from(node)
case node.method_name
when :add_column, :rename
[node.arguments[1]]
when :rename_column
[node.arguments[2]]
when *COLUMN_TYPE_METHOD_NAMES
[node.first_argument]
else
[]
end
end
# @param node [RuboCop::AST::SendNode]
# @return [Array<RuboCop::AST::Node>]
def identifier_index_name_nodes_from(node)
case node.method_name
when :add_index
[index_name_option_from_add_index(node)].compact
when :add_reference
[index_name_option_from_add_reference(node)].compact
when :rename_index
[node.arguments[2]]
when *COLUMN_TYPE_METHOD_NAMES
[index_name_option_from_column_type(node)].compact
else
[]
end
end
# @param node [RuboCop::AST::SendNode]
# @return [Array<RuboCop::AST::Node>]
def identifier_nodes_from(node)
identifier_table_name_nodes_from(node) +
identifier_column_name_nodes_from(node) +
identifier_index_name_nodes_from(node)
end
# @param node [RuboCop::AST::SendNode]
# @return [Array<RuboCop::AST::Node>]
def identifier_table_name_nodes_from(node)
case node.method_name
when :create_join_table
[table_name_option_from(node)].compact
when :create_table
[node.first_argument]
when :rename_table
[node.arguments[1]]
else
[]
end
end
# @param node [RuboCop::AST::Node]
# @return [Array<RuboCop::AST::Node>]
def offended_identifier_nodes_from(node)
identifier_nodes_from(node).select do |identifier_node|
reserved_word_identifier_node?(identifier_node)
end
end
# @param node [RuboCop::AST::Node]
# @return [Boolean]
def reserved_word_identifier_node?(node)
return false unless node.respond_to?(:value)
self.class.reserved_words.include?(node.value.to_s)
end
end
end
end
end