-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjsonb.rb
131 lines (117 loc) · 3.56 KB
/
jsonb.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
# frozen_string_literal: true
module RuboCop
module Cop
module Migration
# Prefer `jsonb` to `json`.
#
# In PostgreSQL, there is no equality operator for the json column type,
# which can cause errors for existing `SELECT DISTINCT` queries in your application.
#
# @safety
# Only meaningful in PostgreSQL.
#
# @example
# # bad
# add_column :users, :properties, :json
#
# # good
# add_column :users, :properties, :jsonb
class Jsonb < RuboCop::Cop::Base
extend AutoCorrector
MSG = 'Prefer `jsonb` to `json`.'
RESTRICT_ON_SEND = %i[
add_column
change
change_column
json
].freeze
# @param node [RuboCop::AST::SendNode]
# @return [void]
def on_send(node)
json_range = json_range_from_target_send_node(node)
return unless json_range
add_offense(json_range) do |corrector|
corrector.replace(json_range, 'jsonb')
end
end
private
# @!method json_type_node_from_add_column(node)
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::SymNode, nil]
def_node_matcher :json_type_node_from_add_column, <<~PATTERN
(send
nil?
_
_
_
$(sym :json)
)
PATTERN
alias json_type_node_from_change_column json_type_node_from_add_column
# @!method json_type_node_from_change(node)
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::SymNode, nil]
def_node_matcher :json_type_node_from_change, <<~PATTERN
(send
lvar
_
_
$(sym :json)
)
PATTERN
# @!method json_type_node_from_json(node)
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::SendNode, nil]
def_node_matcher :json_type_node_from_json, <<~PATTERN
$(send
lvar
_
...
)
PATTERN
# @param corrector [RuboCop::Cop::Corrector]
# @param node [RuboCop::AST::SendNode, RuboCop::AST::SymNode]
# @return [void]
def autocorrect(
corrector,
node
)
corrector.replace(node, 'jsonb')
end
# @param node [RuboCop::AST::SendNode]
# @return [RuboCop::AST::SymNode, nil]
def json_node_from_target_send_node(node)
case node.method_name
when :add_column
json_type_node_from_add_column(node)
when :change
json_type_node_from_change(node)
when :change_column
json_type_node_from_change_column(node)
when :json
json_type_node_from_json(node)
end
end
# @param node [RuboCop::AST::SendNode, RuboCop::AST::SymNode]
# @return [Parser::Source::Range]
def json_range_from_json_node(node)
case node.type
when :send
node.location.selector
when :sym
node.source_range.with(
begin_pos: node.source_range.begin_pos + 1
)
end
end
# @param node [RuboCop::AST::SendNode]
# @return [Parser::Source::Range]
def json_range_from_target_send_node(node)
json_node = json_node_from_target_send_node(node)
return unless json_node
json_range_from_json_node(json_node)
end
end
end
end
end