rubocop-graphql 0.6.2 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/default.yml +9 -5
- data/lib/rubocop-graphql.rb +2 -0
- data/lib/rubocop/cop/graphql/argument_description.rb +1 -1
- data/lib/rubocop/cop/graphql/argument_name.rb +1 -1
- data/lib/rubocop/cop/graphql/extract_input_type.rb +7 -2
- data/lib/rubocop/cop/graphql/extract_type.rb +1 -1
- data/lib/rubocop/cop/graphql/field_definitions.rb +8 -14
- data/lib/rubocop/cop/graphql/field_description.rb +1 -1
- data/lib/rubocop/cop/graphql/field_hash_key.rb +20 -19
- data/lib/rubocop/cop/graphql/field_method.rb +18 -17
- data/lib/rubocop/cop/graphql/field_name.rb +1 -1
- data/lib/rubocop/cop/graphql/legacy_dsl.rb +42 -0
- data/lib/rubocop/cop/graphql/object_description.rb +3 -11
- data/lib/rubocop/cop/graphql/ordered_arguments.rb +96 -0
- data/lib/rubocop/cop/graphql/ordered_fields.rb +15 -11
- data/lib/rubocop/cop/graphql/resolver_method_length.rb +1 -1
- data/lib/rubocop/cop/graphql_cops.rb +2 -2
- data/lib/rubocop/graphql/argument/block.rb +2 -5
- data/lib/rubocop/graphql/argument/kwargs.rb +1 -1
- data/lib/rubocop/graphql/description_method.rb +32 -0
- data/lib/rubocop/graphql/field/block.rb +2 -5
- data/lib/rubocop/graphql/swap_range.rb +26 -0
- data/lib/rubocop/graphql/version.rb +1 -1
- metadata +7 -4
- data/lib/rubocop/cop/graphql/cop.rb +0 -91
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e63911b5c7c77f1d207fc9a1d411c61b874f51b372507bd7fd4c8072addd0663
|
4
|
+
data.tar.gz: 5581dcb49597ece10a23dba6f191b531d80d313e265fb6a61ec6db172691ad75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff897f7cfd8f63f3435915d3c9203803b7d81cb4d3997b5ef6e8e3822cb95ae77cc7eb3573bc6c50aa54c630eec59c550b35dca07d12412bcf7a460e00e3290f
|
7
|
+
data.tar.gz: cb02df1ef01c442dd7c3ad1abd89b6e8457bf83aa52391c78ead27e7c8d0793f8794ac4eb7295e234022763081f640115c3adef8a1f42f81600ef714655f6c06
|
data/config/default.yml
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
- "(?:^|/)graphql/"
|
1
|
+
GraphQL:
|
2
|
+
Include:
|
3
|
+
- "**/graphql/**/*"
|
5
4
|
|
6
5
|
GraphQL/ArgumentDescription:
|
7
6
|
Enabled: true
|
@@ -68,10 +67,15 @@ GraphQL/ExtractType:
|
|
68
67
|
- min
|
69
68
|
- max
|
70
69
|
|
70
|
+
GraphQL/LegacyDsl:
|
71
|
+
Enabled: true
|
72
|
+
VersionAdded: '0.80'
|
73
|
+
Description: 'Checks that types are defined with class-based API'
|
74
|
+
|
71
75
|
GraphQL/ObjectDescription:
|
72
76
|
Enabled: true
|
73
77
|
VersionAdded: '0.80'
|
74
78
|
Description: 'Ensures all types have a description'
|
75
79
|
Exclude:
|
76
80
|
- '**/*_schema.rb'
|
77
|
-
- '**/base_*.rb'
|
81
|
+
- '**/base_*.rb'
|
data/lib/rubocop-graphql.rb
CHANGED
@@ -7,7 +7,9 @@ require_relative "rubocop/graphql/ext/snake_case"
|
|
7
7
|
require_relative "rubocop/graphql"
|
8
8
|
require_relative "rubocop/graphql/version"
|
9
9
|
require_relative "rubocop/graphql/inject"
|
10
|
+
require_relative "rubocop/graphql/description_method"
|
10
11
|
require_relative "rubocop/graphql/node_pattern"
|
12
|
+
require_relative "rubocop/graphql/swap_range"
|
11
13
|
|
12
14
|
require_relative "rubocop/graphql/argument"
|
13
15
|
require_relative "rubocop/graphql/argument/block"
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module GraphQL
|
6
|
-
class ExtractInputType <
|
6
|
+
class ExtractInputType < Base
|
7
7
|
# This cop checks fields on common prefix groups
|
8
8
|
#
|
9
9
|
# # @example
|
@@ -32,7 +32,12 @@ module RuboCop
|
|
32
32
|
if (body = schema_member.body)
|
33
33
|
arguments = body.select { |node| argument?(node) }
|
34
34
|
|
35
|
-
|
35
|
+
excess_arguments = arguments.count - cop_config["MaxArguments"]
|
36
|
+
return unless excess_arguments.positive?
|
37
|
+
|
38
|
+
arguments.last(excess_arguments).each do |excess_argument|
|
39
|
+
add_offense(excess_argument)
|
40
|
+
end
|
36
41
|
end
|
37
42
|
end
|
38
43
|
end
|
@@ -44,7 +44,8 @@ module RuboCop
|
|
44
44
|
# object.contact_data.last_name
|
45
45
|
# end
|
46
46
|
# end
|
47
|
-
class FieldDefinitions <
|
47
|
+
class FieldDefinitions < Base
|
48
|
+
extend AutoCorrector
|
48
49
|
include ConfigurableEnforcedStyle
|
49
50
|
include RuboCop::GraphQL::NodePattern
|
50
51
|
include RuboCop::Cop::RangeHelp
|
@@ -75,17 +76,6 @@ module RuboCop
|
|
75
76
|
end
|
76
77
|
end
|
77
78
|
|
78
|
-
def autocorrect(node)
|
79
|
-
lambda do |corrector|
|
80
|
-
case style
|
81
|
-
when :define_resolver_after_definition
|
82
|
-
place_resolver_after_definitions(corrector, node)
|
83
|
-
when :group_definitions
|
84
|
-
group_field_declarations(corrector, node)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
79
|
private
|
90
80
|
|
91
81
|
GROUP_DEFS_MSG = "Group all field definitions together."
|
@@ -98,7 +88,9 @@ module RuboCop
|
|
98
88
|
fields.each_with_index do |node, idx|
|
99
89
|
next if node.sibling_index == first_field.sibling_index + idx
|
100
90
|
|
101
|
-
add_offense(node, message: GROUP_DEFS_MSG)
|
91
|
+
add_offense(node, message: GROUP_DEFS_MSG) do |corrector|
|
92
|
+
group_field_declarations(corrector, node)
|
93
|
+
end
|
102
94
|
end
|
103
95
|
end
|
104
96
|
|
@@ -133,7 +125,9 @@ module RuboCop
|
|
133
125
|
|
134
126
|
return if method_definition.sibling_index - field_sibling_index == 1
|
135
127
|
|
136
|
-
add_offense(field.node, message: RESOLVER_AFTER_FIELD_MSG)
|
128
|
+
add_offense(field.node, message: RESOLVER_AFTER_FIELD_MSG) do |corrector|
|
129
|
+
place_resolver_after_definitions(corrector, field.node)
|
130
|
+
end
|
137
131
|
end
|
138
132
|
|
139
133
|
def place_resolver_after_definitions(corrector, node)
|
@@ -23,7 +23,8 @@ module RuboCop
|
|
23
23
|
# end
|
24
24
|
# end
|
25
25
|
#
|
26
|
-
class FieldHashKey <
|
26
|
+
class FieldHashKey < Base
|
27
|
+
extend AutoCorrector
|
27
28
|
include RuboCop::GraphQL::NodePattern
|
28
29
|
include RuboCop::Cop::RangeHelp
|
29
30
|
|
@@ -47,32 +48,32 @@ module RuboCop
|
|
47
48
|
method_definition = resolver_method_definition_for(field)
|
48
49
|
|
49
50
|
if (suggested_hash_key_name = hash_key_to_use(method_definition))
|
50
|
-
add_offense(node, message: message(suggested_hash_key_name))
|
51
|
+
add_offense(node, message: message(suggested_hash_key_name)) do |corrector|
|
52
|
+
autocorrect(corrector, node)
|
53
|
+
end
|
51
54
|
end
|
52
55
|
end
|
53
56
|
|
54
|
-
|
55
|
-
lambda do |corrector|
|
56
|
-
field = RuboCop::GraphQL::Field.new(node)
|
57
|
-
method_definition = resolver_method_definition_for(field)
|
58
|
-
suggested_hash_key_name = hash_key_to_use(method_definition)
|
57
|
+
private
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
|
59
|
+
def message(hash_key)
|
60
|
+
format(MSG, hash_key: hash_key)
|
61
|
+
end
|
63
62
|
|
64
|
-
|
65
|
-
|
66
|
-
|
63
|
+
def autocorrect(corrector, node)
|
64
|
+
field = RuboCop::GraphQL::Field.new(node)
|
65
|
+
method_definition = resolver_method_definition_for(field)
|
66
|
+
suggested_hash_key_name = hash_key_to_use(method_definition)
|
67
67
|
|
68
|
-
|
69
|
-
|
70
|
-
|
68
|
+
corrector.insert_after(
|
69
|
+
node.loc.expression, ", hash_key: #{suggested_hash_key_name.inspect}"
|
70
|
+
)
|
71
71
|
|
72
|
-
|
72
|
+
range = range_with_surrounding_space(
|
73
|
+
range: method_definition.loc.expression, side: :left
|
74
|
+
)
|
73
75
|
|
74
|
-
|
75
|
-
format(MSG, hash_key: hash_key)
|
76
|
+
corrector.remove(range)
|
76
77
|
end
|
77
78
|
|
78
79
|
def resolver_method_definition_for(field)
|
@@ -23,7 +23,8 @@ module RuboCop
|
|
23
23
|
# end
|
24
24
|
# end
|
25
25
|
#
|
26
|
-
class FieldMethod <
|
26
|
+
class FieldMethod < Base
|
27
|
+
extend AutoCorrector
|
27
28
|
include RuboCop::GraphQL::NodePattern
|
28
29
|
include RuboCop::Cop::RangeHelp
|
29
30
|
|
@@ -46,22 +47,9 @@ module RuboCop
|
|
46
47
|
method_definition = suggest_method_name_for(field)
|
47
48
|
|
48
49
|
if (suggested_method_name = method_to_use(method_definition))
|
49
|
-
add_offense(node, message: message(suggested_method_name))
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
def autocorrect(node)
|
54
|
-
lambda do |corrector|
|
55
|
-
field = RuboCop::GraphQL::Field.new(node)
|
56
|
-
method_definition = suggest_method_name_for(field)
|
57
|
-
suggested_method_name = method_to_use(method_definition)
|
58
|
-
|
59
|
-
corrector.insert_after(node.loc.expression, ", method: :#{suggested_method_name}")
|
60
|
-
|
61
|
-
range = range_with_surrounding_space(
|
62
|
-
range: method_definition.loc.expression, side: :left
|
63
|
-
)
|
64
|
-
corrector.remove(range)
|
50
|
+
add_offense(node, message: message(suggested_method_name)) do |corrector|
|
51
|
+
autocorrect(corrector, node)
|
52
|
+
end
|
65
53
|
end
|
66
54
|
end
|
67
55
|
|
@@ -71,6 +59,19 @@ module RuboCop
|
|
71
59
|
format(MSG, method_name: method_name)
|
72
60
|
end
|
73
61
|
|
62
|
+
def autocorrect(corrector, node)
|
63
|
+
field = RuboCop::GraphQL::Field.new(node)
|
64
|
+
method_definition = suggest_method_name_for(field)
|
65
|
+
suggested_method_name = method_to_use(method_definition)
|
66
|
+
|
67
|
+
corrector.insert_after(node.loc.expression, ", method: :#{suggested_method_name}")
|
68
|
+
|
69
|
+
range = range_with_surrounding_space(
|
70
|
+
range: method_definition.loc.expression, side: :left
|
71
|
+
)
|
72
|
+
corrector.remove(range)
|
73
|
+
end
|
74
|
+
|
74
75
|
def suggest_method_name_for(field)
|
75
76
|
method_name = field.resolver_method_name
|
76
77
|
field.schema_member.find_method_definition(method_name)
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# This cop checks whether type definitions are class-based or legacy.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# # good
|
9
|
+
#
|
10
|
+
# class Example < BaseType
|
11
|
+
# ....
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# # bad
|
15
|
+
#
|
16
|
+
# Example = GraphQL::ObjectType.define do
|
17
|
+
# ....
|
18
|
+
# ....
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
module GraphQL
|
22
|
+
class LegacyDsl < Base
|
23
|
+
def_node_matcher :legacy_dsl?, <<~PATTERN
|
24
|
+
(block
|
25
|
+
(send
|
26
|
+
(const
|
27
|
+
(const nil? :GraphQL) _) :define)
|
28
|
+
...
|
29
|
+
)
|
30
|
+
PATTERN
|
31
|
+
|
32
|
+
MSG = "Avoid using legacy based type-based definitions. Use class-based defintions instead."
|
33
|
+
|
34
|
+
def on_send(node)
|
35
|
+
return unless node.parent.type == :block
|
36
|
+
|
37
|
+
add_offense(node.parent) if legacy_dsl?(node.parent)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -20,8 +20,9 @@ module RuboCop
|
|
20
20
|
# # ...
|
21
21
|
# end
|
22
22
|
#
|
23
|
-
class ObjectDescription <
|
23
|
+
class ObjectDescription < Base
|
24
24
|
include RuboCop::GraphQL::NodePattern
|
25
|
+
include RuboCop::GraphQL::DescriptionMethod
|
25
26
|
|
26
27
|
MSG = "Missing type description"
|
27
28
|
|
@@ -29,14 +30,6 @@ module RuboCop
|
|
29
30
|
(send nil? :description (send (const nil? :I18n) :t ...))
|
30
31
|
PATTERN
|
31
32
|
|
32
|
-
def_node_matcher :has_string_description?, <<~PATTERN
|
33
|
-
(send nil? :description (:str $_))
|
34
|
-
PATTERN
|
35
|
-
|
36
|
-
def_node_matcher :has_multiline_string_description?, <<~PATTERN
|
37
|
-
(send nil? :description (:dstr ...))
|
38
|
-
PATTERN
|
39
|
-
|
40
33
|
def_node_matcher :interface?, <<~PATTERN
|
41
34
|
(send nil? :include (const ...))
|
42
35
|
PATTERN
|
@@ -59,8 +52,7 @@ module RuboCop
|
|
59
52
|
|
60
53
|
def has_description?(node)
|
61
54
|
has_i18n_description?(node) ||
|
62
|
-
|
63
|
-
has_multiline_string_description?(node)
|
55
|
+
description_kwarg?(node)
|
64
56
|
end
|
65
57
|
|
66
58
|
def child_nodes(node)
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module GraphQL
|
6
|
+
# Arguments should be alphabetically sorted within groups.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # good
|
10
|
+
#
|
11
|
+
# class UpdateProfile < BaseMutation
|
12
|
+
# argument :email, String, required: false
|
13
|
+
# argument :name, String, required: false
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
#
|
18
|
+
# class UpdateProfile < BaseMutation
|
19
|
+
# argument :uuid, ID, required: true
|
20
|
+
#
|
21
|
+
# argument :email, String, required: false
|
22
|
+
# argument :name, String, required: false
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
#
|
27
|
+
# class UserType < BaseType
|
28
|
+
# field :posts, PostType do
|
29
|
+
# argument :created_after, ISO8601DateTime, required: false
|
30
|
+
# argument :created_before, ISO8601DateTime, required: false
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# # bad
|
35
|
+
#
|
36
|
+
# class UpdateProfile < BaseMutation
|
37
|
+
# argument :uuid, ID, required: true
|
38
|
+
# argument :name, String, required: false
|
39
|
+
# argument :email, String, required: false
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# # bad
|
43
|
+
#
|
44
|
+
# class UserType < BaseType
|
45
|
+
# field :posts, PostType do
|
46
|
+
# argument :created_before, ISO8601DateTime, required: false
|
47
|
+
# argument :created_after, ISO8601DateTime, required: false
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
class OrderedArguments < Base
|
52
|
+
extend AutoCorrector
|
53
|
+
|
54
|
+
include RuboCop::GraphQL::SwapRange
|
55
|
+
|
56
|
+
MSG = "Arguments should be sorted in an alphabetical order within their section. " \
|
57
|
+
"Field `%<current>s` should appear before `%<previous>s`."
|
58
|
+
|
59
|
+
def on_class(node)
|
60
|
+
argument_declarations(node).each_cons(2) do |previous, current|
|
61
|
+
next unless consecutive_lines(previous, current)
|
62
|
+
next if argument_name(current) > argument_name(previous)
|
63
|
+
|
64
|
+
register_offense(previous, current)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def register_offense(previous, current)
|
71
|
+
message = format(
|
72
|
+
self.class::MSG,
|
73
|
+
previous: argument_name(previous),
|
74
|
+
current: argument_name(current)
|
75
|
+
)
|
76
|
+
|
77
|
+
add_offense(current, message: message) do |corrector|
|
78
|
+
swap_range(corrector, current, previous)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def argument_name(node)
|
83
|
+
node.first_argument.value.to_s
|
84
|
+
end
|
85
|
+
|
86
|
+
def consecutive_lines(previous, current)
|
87
|
+
previous.source_range.last_line == current.source_range.first_line - 1
|
88
|
+
end
|
89
|
+
|
90
|
+
def_node_search :argument_declarations, <<~PATTERN
|
91
|
+
(send nil? :argument (:sym _) ...)
|
92
|
+
PATTERN
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -30,21 +30,22 @@ module RuboCop
|
|
30
30
|
# field :name, String, null: true
|
31
31
|
# end
|
32
32
|
#
|
33
|
-
class OrderedFields <
|
33
|
+
class OrderedFields < Base
|
34
|
+
extend AutoCorrector
|
35
|
+
|
36
|
+
include RuboCop::GraphQL::SwapRange
|
37
|
+
|
34
38
|
MSG = "Fields should be sorted in an alphabetical order within their "\
|
35
39
|
"section. "\
|
36
40
|
"Field `%<current>s` should appear before `%<previous>s`."
|
37
41
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
.each_cons(2) do |previous, current|
|
43
|
-
next unless consecutive_lines(previous, current)
|
44
|
-
next if field_name(current) > field_name(previous)
|
42
|
+
def on_class(node)
|
43
|
+
field_declarations(node).each_cons(2) do |previous, current|
|
44
|
+
next unless consecutive_lines(previous, current)
|
45
|
+
next if field_name(current) > field_name(previous)
|
45
46
|
|
46
|
-
|
47
|
-
|
47
|
+
register_offense(previous, current)
|
48
|
+
end
|
48
49
|
end
|
49
50
|
|
50
51
|
private
|
@@ -55,7 +56,10 @@ module RuboCop
|
|
55
56
|
previous: field_name(previous),
|
56
57
|
current: field_name(current)
|
57
58
|
)
|
58
|
-
|
59
|
+
|
60
|
+
add_offense(current, message: message) do |corrector|
|
61
|
+
swap_range(corrector, current, previous)
|
62
|
+
end
|
59
63
|
end
|
60
64
|
|
61
65
|
def field_name(node)
|
@@ -7,7 +7,7 @@ module RuboCop
|
|
7
7
|
# Comment lines can optionally be ignored.
|
8
8
|
#
|
9
9
|
# The maximum allowed length is configurable using the Max option.
|
10
|
-
class ResolverMethodLength <
|
10
|
+
class ResolverMethodLength < Base
|
11
11
|
include RuboCop::Cop::ConfigurableMax
|
12
12
|
include RuboCop::Cop::CodeLength
|
13
13
|
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative "graphql/cop"
|
4
|
-
|
5
3
|
require_relative "graphql/argument_description"
|
6
4
|
require_relative "graphql/argument_name"
|
7
5
|
require_relative "graphql/extract_input_type"
|
@@ -11,6 +9,8 @@ require_relative "graphql/field_description"
|
|
11
9
|
require_relative "graphql/field_hash_key"
|
12
10
|
require_relative "graphql/field_method"
|
13
11
|
require_relative "graphql/field_name"
|
12
|
+
require_relative "graphql/legacy_dsl"
|
14
13
|
require_relative "graphql/resolver_method_length"
|
15
14
|
require_relative "graphql/object_description"
|
15
|
+
require_relative "graphql/ordered_arguments"
|
16
16
|
require_relative "graphql/ordered_fields"
|
@@ -5,6 +5,7 @@ module RuboCop
|
|
5
5
|
class Argument
|
6
6
|
class Block
|
7
7
|
extend RuboCop::NodePattern::Macros
|
8
|
+
include DescriptionMethod
|
8
9
|
|
9
10
|
def_node_matcher :argument_block, <<~PATTERN
|
10
11
|
(block
|
@@ -14,16 +15,12 @@ module RuboCop
|
|
14
15
|
)
|
15
16
|
PATTERN
|
16
17
|
|
17
|
-
def_node_matcher :description_kwarg?, <<~PATTERN
|
18
|
-
(send nil? :description (str ...))
|
19
|
-
PATTERN
|
20
|
-
|
21
18
|
def initialize(argument_node)
|
22
19
|
@nodes = argument_block(argument_node) || []
|
23
20
|
end
|
24
21
|
|
25
22
|
def description
|
26
|
-
@nodes
|
23
|
+
find_description_method(@nodes)
|
27
24
|
end
|
28
25
|
end
|
29
26
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module GraphQL
|
5
|
+
# Matches a variety of description formats commonly seen in Rails applications
|
6
|
+
#
|
7
|
+
# description 'blah'
|
8
|
+
#
|
9
|
+
# description "blah"
|
10
|
+
#
|
11
|
+
# description <<~EOT
|
12
|
+
# blah
|
13
|
+
# bloop
|
14
|
+
# EOT
|
15
|
+
#
|
16
|
+
# description <<-EOT.squish
|
17
|
+
# blah
|
18
|
+
# bloop
|
19
|
+
# EOT
|
20
|
+
module DescriptionMethod
|
21
|
+
extend RuboCop::NodePattern::Macros
|
22
|
+
|
23
|
+
def_node_matcher :description_kwarg?, <<~PATTERN
|
24
|
+
(send nil? :description {({str|dstr} ...)|(send ({str|dstr} ...) _)})
|
25
|
+
PATTERN
|
26
|
+
|
27
|
+
def find_description_method(nodes)
|
28
|
+
nodes.find { |kwarg| description_kwarg?(kwarg) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -5,6 +5,7 @@ module RuboCop
|
|
5
5
|
class Field
|
6
6
|
class Block
|
7
7
|
extend RuboCop::NodePattern::Macros
|
8
|
+
include DescriptionMethod
|
8
9
|
|
9
10
|
def_node_matcher :field_block, <<~PATTERN
|
10
11
|
(block
|
@@ -14,16 +15,12 @@ module RuboCop
|
|
14
15
|
)
|
15
16
|
PATTERN
|
16
17
|
|
17
|
-
def_node_matcher :description_kwarg?, <<~PATTERN
|
18
|
-
(send nil? :description (str ...))
|
19
|
-
PATTERN
|
20
|
-
|
21
18
|
def initialize(field_node)
|
22
19
|
@nodes = field_block(field_node) || []
|
23
20
|
end
|
24
21
|
|
25
22
|
def description
|
26
|
-
@nodes
|
23
|
+
find_description_method(@nodes)
|
27
24
|
end
|
28
25
|
end
|
29
26
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module GraphQL
|
5
|
+
module SwapRange
|
6
|
+
def swap_range(corrector, current, previous)
|
7
|
+
current_range = declaration(current)
|
8
|
+
previous_range = declaration(previous)
|
9
|
+
|
10
|
+
src1 = current_range.source
|
11
|
+
src2 = previous_range.source
|
12
|
+
|
13
|
+
corrector.replace(current_range, src2)
|
14
|
+
corrector.replace(previous_range, src1)
|
15
|
+
end
|
16
|
+
|
17
|
+
def declaration(node)
|
18
|
+
buffer = processed_source.buffer
|
19
|
+
begin_pos = node.source_range.begin_pos
|
20
|
+
end_line = buffer.line_for_position(node.loc.expression.end_pos)
|
21
|
+
end_pos = buffer.line_range(end_line).end_pos
|
22
|
+
Parser::Source::Range.new(buffer, begin_pos, end_pos)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubocop-graphql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Tsepelev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -85,7 +85,6 @@ files:
|
|
85
85
|
- lib/rubocop-graphql.rb
|
86
86
|
- lib/rubocop/cop/graphql/argument_description.rb
|
87
87
|
- lib/rubocop/cop/graphql/argument_name.rb
|
88
|
-
- lib/rubocop/cop/graphql/cop.rb
|
89
88
|
- lib/rubocop/cop/graphql/extract_input_type.rb
|
90
89
|
- lib/rubocop/cop/graphql/extract_type.rb
|
91
90
|
- lib/rubocop/cop/graphql/field_definitions.rb
|
@@ -93,7 +92,9 @@ files:
|
|
93
92
|
- lib/rubocop/cop/graphql/field_hash_key.rb
|
94
93
|
- lib/rubocop/cop/graphql/field_method.rb
|
95
94
|
- lib/rubocop/cop/graphql/field_name.rb
|
95
|
+
- lib/rubocop/cop/graphql/legacy_dsl.rb
|
96
96
|
- lib/rubocop/cop/graphql/object_description.rb
|
97
|
+
- lib/rubocop/cop/graphql/ordered_arguments.rb
|
97
98
|
- lib/rubocop/cop/graphql/ordered_fields.rb
|
98
99
|
- lib/rubocop/cop/graphql/resolver_method_length.rb
|
99
100
|
- lib/rubocop/cop/graphql_cops.rb
|
@@ -101,6 +102,7 @@ files:
|
|
101
102
|
- lib/rubocop/graphql/argument.rb
|
102
103
|
- lib/rubocop/graphql/argument/block.rb
|
103
104
|
- lib/rubocop/graphql/argument/kwargs.rb
|
105
|
+
- lib/rubocop/graphql/description_method.rb
|
104
106
|
- lib/rubocop/graphql/ext/snake_case.rb
|
105
107
|
- lib/rubocop/graphql/field.rb
|
106
108
|
- lib/rubocop/graphql/field/block.rb
|
@@ -108,6 +110,7 @@ files:
|
|
108
110
|
- lib/rubocop/graphql/inject.rb
|
109
111
|
- lib/rubocop/graphql/node_pattern.rb
|
110
112
|
- lib/rubocop/graphql/schema_member.rb
|
113
|
+
- lib/rubocop/graphql/swap_range.rb
|
111
114
|
- lib/rubocop/graphql/version.rb
|
112
115
|
homepage: https://github.com/DmitryTsepelev/rubocop-graphql
|
113
116
|
licenses:
|
@@ -124,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
124
127
|
requirements:
|
125
128
|
- - ">="
|
126
129
|
- !ruby/object:Gem::Version
|
127
|
-
version: '2.
|
130
|
+
version: '2.5'
|
128
131
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
132
|
requirements:
|
130
133
|
- - ">="
|
@@ -1,91 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RuboCop
|
4
|
-
# Inspied by https://github.com/rubocop-hq/rubocop-rspec/blob/3a2088f79737e2e8e0e21482783f7d61411bf021/lib/rubocop/cop/rspec/cop.rb
|
5
|
-
module Cop
|
6
|
-
WorkaroundGraphqlCop = Cop.dup
|
7
|
-
|
8
|
-
# Clone of the the normal RuboCop::Cop::Cop class so we can rewrite
|
9
|
-
# the inherited method without breaking functionality
|
10
|
-
class WorkaroundGraphqlCop
|
11
|
-
# Remove the Cop.inherited method to be a noop. Our RSpec::Cop
|
12
|
-
# class will invoke the inherited hook instead
|
13
|
-
class << self
|
14
|
-
undef inherited
|
15
|
-
def inherited(*); end # rubocop:disable Lint/MissingSuper
|
16
|
-
end
|
17
|
-
|
18
|
-
# Special case `Module#<` so that the rspec support rubocop exports
|
19
|
-
# is compatible with our subclass
|
20
|
-
def self.<(other)
|
21
|
-
other.equal?(RuboCop::Cop::Cop) || super
|
22
|
-
end
|
23
|
-
end
|
24
|
-
private_constant(:WorkaroundGraphqlCop)
|
25
|
-
|
26
|
-
module GraphQL
|
27
|
-
# @abstract parent class to graphql-ruby cops
|
28
|
-
#
|
29
|
-
# The criteria for whether rubocop-rspec analyzes a certain ruby file
|
30
|
-
# is configured via `AllCops/GraphQL`. For example, if you want to
|
31
|
-
# customize your project to scan all files within a `graph/` directory
|
32
|
-
# then you could add this to your configuration:
|
33
|
-
#
|
34
|
-
# @example configuring analyzed paths
|
35
|
-
#
|
36
|
-
# AllCops:
|
37
|
-
# GraphQL:
|
38
|
-
# Patterns:
|
39
|
-
# - '(?:^|/)graph/'
|
40
|
-
class Cop < WorkaroundGraphqlCop
|
41
|
-
DEFAULT_CONFIGURATION =
|
42
|
-
RuboCop::GraphQL::CONFIG.fetch("AllCops").fetch("GraphQL")
|
43
|
-
|
44
|
-
DEFAULT_PATTERN_RE = Regexp.union(
|
45
|
-
DEFAULT_CONFIGURATION.fetch("Patterns")
|
46
|
-
.map(&Regexp.public_method(:new))
|
47
|
-
)
|
48
|
-
|
49
|
-
# Invoke the original inherited hook so our cops are recognized
|
50
|
-
def self.inherited(subclass) # rubocop:disable Lint/MissingSuper
|
51
|
-
RuboCop::Cop::Cop.inherited(subclass)
|
52
|
-
end
|
53
|
-
|
54
|
-
def relevant_file?(file)
|
55
|
-
relevant_rubocop_graphql_file?(file) && super
|
56
|
-
end
|
57
|
-
|
58
|
-
private
|
59
|
-
|
60
|
-
def relevant_rubocop_graphql_file?(file)
|
61
|
-
graphql_pattern =~ file
|
62
|
-
end
|
63
|
-
|
64
|
-
def graphql_pattern
|
65
|
-
if rspec_graphql_config?
|
66
|
-
Regexp.union(rspec_graphql_config.map(&Regexp.public_method(:new)))
|
67
|
-
else
|
68
|
-
DEFAULT_PATTERN_RE
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def all_cops_config
|
73
|
-
config
|
74
|
-
.for_all_cops
|
75
|
-
end
|
76
|
-
|
77
|
-
def rspec_graphql_config?
|
78
|
-
return unless all_cops_config.key?("GraphQL")
|
79
|
-
|
80
|
-
all_cops_config.fetch("GraphQL").key?("Patterns")
|
81
|
-
end
|
82
|
-
|
83
|
-
def rspec_graphql_config
|
84
|
-
all_cops_config
|
85
|
-
.fetch("GraphQL", DEFAULT_CONFIGURATION)
|
86
|
-
.fetch("Patterns")
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|