-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
default_null_true.rb
43 lines (40 loc) · 1.29 KB
/
default_null_true.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
# frozen_string_literal: true
require_relative "base_cop"
module GraphQL
module Rubocop
module GraphQL
# Identify (and auto-correct) any field configuration which duplicates
# the default `null: true` property.
#
# `null: true` is default because nullable fields can always be converted
# to non-null fields (`null: false`) without a breaking change. (The opposite change, from `null: false`
# to `null: true`, change.)
#
# @example
# # Both of these define `name: String` in GraphQL:
#
# # bad
# field :name, String, null: true
#
# # good
# field :name, String
#
class DefaultNullTrue < BaseCop
MSG = "`null: true` is the default and can be removed."
def_node_matcher :field_config_with_null_true?, <<-Pattern
(
send nil? :field ... (hash $(pair (sym :null) (true)) ...)
)
Pattern
def on_send(node)
field_config_with_null_true?(node) do |null_config|
add_offense(null_config) do |corrector|
cleaned_node_source = source_without_keyword_argument(node, null_config)
corrector.replace(node.source_range, cleaned_node_source)
end
end
end
end
end
end
end