rubocop-graphql 0.16.0 → 0.19.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -7
- data/config/default.yml +15 -0
- data/lib/rubocop/cop/graphql/unnecessary_argument_camelize.rb +54 -0
- data/lib/rubocop/cop/graphql/unnecessary_field_alias.rb +36 -0
- data/lib/rubocop/cop/graphql/unnecessary_field_camelize.rb +36 -0
- data/lib/rubocop/cop/graphql_cops.rb +3 -0
- data/lib/rubocop/graphql/argument/kwargs.rb +9 -0
- data/lib/rubocop/graphql/field/kwargs.rb +18 -0
- data/lib/rubocop/graphql/swap_range.rb +10 -1
- data/lib/rubocop/graphql/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80b3609f1de18b1e5db7198fdf29f7deed28cfe0e505c9485203dd38172ff4f9
|
4
|
+
data.tar.gz: 0d894dad1bc37192b76372d235d10fab4181e171574f471091e9bad0033212d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c2b2d1bcbae2e53e28567c0f13397f77427cc0b9d87a96171cd27fec5d33649568366a623c6ea7c6960f452709d02081c04e3dae1368b465da4fad304ae9ce5
|
7
|
+
data.tar.gz: b6b5588432b38eaa545bff20b2f52d966859ba1fac6fcad5004f22405fd4d1dab56c04238124f99f3ff1df860b385773e539981221b94d0e8c4c27266d4a7c9e
|
data/README.md
CHANGED
@@ -1,13 +1,7 @@
|
|
1
|
-
# RuboCop::GraphQL
|
1
|
+
# RuboCop::GraphQL ![](https://ruby-gem-downloads-badge.herokuapp.com/rubocop-graphql?type=total)
|
2
2
|
|
3
3
|
[Rubocop](https://github.com/rubocop-hq/rubocop) extension for enforcing [graphql-ruby](https://github.com/rmosolgo/graphql-ruby) best practices.
|
4
4
|
|
5
|
-
<p align="center">
|
6
|
-
<a href="https://evilmartians.com/?utm_source=graphql-rubocop">
|
7
|
-
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
|
8
|
-
</a>
|
9
|
-
</p>
|
10
|
-
|
11
5
|
## Installation
|
12
6
|
|
13
7
|
Install the gem:
|
@@ -69,6 +63,10 @@ GraphQL/ResolverMethodLength:
|
|
69
63
|
Max: 3
|
70
64
|
```
|
71
65
|
|
66
|
+
## Credits
|
67
|
+
|
68
|
+
Initially sponsored by [Evil Martians](http://evilmartians.com).
|
69
|
+
|
72
70
|
## Contributing
|
73
71
|
|
74
72
|
Bug reports and pull requests are welcome on GitHub at https://github.com/DmitryTsepelev/rubocop-graphql.
|
data/config/default.yml
CHANGED
@@ -73,6 +73,8 @@ GraphQL/ExtractInputType:
|
|
73
73
|
VersionAdded: '0.80'
|
74
74
|
Description: 'Suggests using input type instead of many arguments'
|
75
75
|
MaxArguments: 2
|
76
|
+
Include:
|
77
|
+
- '**/graphql/mutations/**/*.rb'
|
76
78
|
|
77
79
|
GraphQL/ExtractType:
|
78
80
|
Enabled: true
|
@@ -99,6 +101,7 @@ GraphQL/ObjectDescription:
|
|
99
101
|
Exclude:
|
100
102
|
- '**/*_schema.rb'
|
101
103
|
- '**/base_*.rb'
|
104
|
+
- '**/graphql/query_context.rb'
|
102
105
|
|
103
106
|
GraphQL/OrderedArguments:
|
104
107
|
Enabled: true
|
@@ -113,3 +116,15 @@ GraphQL/OrderedFields:
|
|
113
116
|
GraphQL/UnusedArgument:
|
114
117
|
Enabled: true
|
115
118
|
Description: 'Arguments should either be listed explicitly or **rest should be in the resolve signature'
|
119
|
+
|
120
|
+
GraphQL/UnnecessaryFieldAlias:
|
121
|
+
Enabled: true
|
122
|
+
Description: 'Field aliases should be different than their field names'
|
123
|
+
|
124
|
+
GraphQL/UnnecessaryArgumentCamelize:
|
125
|
+
Enabled: true
|
126
|
+
Description: "Camelize isn't necessary if the argument name doesn't contain underscores"
|
127
|
+
|
128
|
+
GraphQL/UnnecessaryFieldCamelize:
|
129
|
+
Enabled: true
|
130
|
+
Description: "Camelize isn't necessary if the field name doesn't contain underscores"
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module GraphQL
|
6
|
+
# This cop checks if each argument has an unnecessary camelize.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # good
|
10
|
+
#
|
11
|
+
# class UserType < BaseType
|
12
|
+
# field :name, String, "Name of the user", null: true do
|
13
|
+
# argument :filter, String, required: false, camelize: false
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
#
|
19
|
+
# class UserType < BaseType
|
20
|
+
# argument :filter, String, required: false, camelize: false
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# # bad
|
24
|
+
#
|
25
|
+
# class UserType < BaseType
|
26
|
+
# argument :filter, String, required: false
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# # bad
|
30
|
+
#
|
31
|
+
# class UserType < BaseType
|
32
|
+
# field :name, String, "Name of the user", null: true do
|
33
|
+
# argument :filter, String, required: false
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
class UnnecessaryArgumentCamelize < Base
|
38
|
+
include RuboCop::GraphQL::NodePattern
|
39
|
+
|
40
|
+
MSG = "Unnecessary argument camelize"
|
41
|
+
|
42
|
+
def on_send(node)
|
43
|
+
return unless argument?(node)
|
44
|
+
|
45
|
+
argument = RuboCop::GraphQL::Argument.new(node)
|
46
|
+
|
47
|
+
if argument.name.to_s.split("_").length < 2 && !argument.kwargs.camelize.nil?
|
48
|
+
add_offense(node)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module GraphQL
|
6
|
+
# This cop checks if a field has an unnecessary alias.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # good
|
10
|
+
#
|
11
|
+
# class UserType < BaseType
|
12
|
+
# field :name, String, "Name of the user", null: true, alias: :real_name
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # bad
|
16
|
+
#
|
17
|
+
# class UserType < BaseType
|
18
|
+
# field :name, "Name of the user" String, null: true, alias: :name
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
class UnnecessaryFieldAlias < Base
|
22
|
+
include RuboCop::GraphQL::NodePattern
|
23
|
+
|
24
|
+
MSG = "Unnecessary field alias"
|
25
|
+
|
26
|
+
def on_send(node)
|
27
|
+
return unless field_definition?(node)
|
28
|
+
|
29
|
+
field = RuboCop::GraphQL::Field.new(node)
|
30
|
+
|
31
|
+
add_offense(node) if field.name == field.kwargs.alias
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module GraphQL
|
6
|
+
# This cop checks if each field has an unnecessary camelize.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # good
|
10
|
+
#
|
11
|
+
# class UserType < BaseType
|
12
|
+
# field :name, String, "Name of the user", null: true
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# # bad
|
16
|
+
#
|
17
|
+
# class UserType < BaseType
|
18
|
+
# field :name, "Name of the user", String, null: true, camelize: true
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
class UnnecessaryFieldCamelize < Base
|
22
|
+
include RuboCop::GraphQL::NodePattern
|
23
|
+
|
24
|
+
MSG = "Unnecessary field camelize"
|
25
|
+
|
26
|
+
def on_send(node)
|
27
|
+
return unless field_definition?(node)
|
28
|
+
|
29
|
+
field = RuboCop::GraphQL::Field.new(node)
|
30
|
+
|
31
|
+
add_offense(node) if field.name.to_s.split("_").length < 2 && !field.kwargs.camelize.nil?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -18,3 +18,6 @@ require_relative "graphql/object_description"
|
|
18
18
|
require_relative "graphql/ordered_arguments"
|
19
19
|
require_relative "graphql/ordered_fields"
|
20
20
|
require_relative "graphql/unused_argument"
|
21
|
+
require_relative "graphql/unnecessary_argument_camelize"
|
22
|
+
require_relative "graphql/unnecessary_field_alias"
|
23
|
+
require_relative "graphql/unnecessary_field_camelize"
|
@@ -31,6 +31,11 @@ module RuboCop
|
|
31
31
|
(pair (sym :as) ...)
|
32
32
|
PATTERN
|
33
33
|
|
34
|
+
# @!method camelize_kwarg?(node)
|
35
|
+
def_node_matcher :camelize_kwarg?, <<~PATTERN
|
36
|
+
(pair (sym :camelize) ...)
|
37
|
+
PATTERN
|
38
|
+
|
34
39
|
def initialize(argument_node)
|
35
40
|
@nodes = argument_kwargs(argument_node) || []
|
36
41
|
end
|
@@ -39,6 +44,10 @@ module RuboCop
|
|
39
44
|
@nodes.find { |kwarg| description_kwarg?(kwarg) }
|
40
45
|
end
|
41
46
|
|
47
|
+
def camelize
|
48
|
+
@nodes.find { |kwarg| camelize_kwarg?(kwarg) }
|
49
|
+
end
|
50
|
+
|
42
51
|
def loads
|
43
52
|
@nodes.find { |kwarg| loads_kwarg?(kwarg) }
|
44
53
|
end
|
@@ -26,6 +26,11 @@ module RuboCop
|
|
26
26
|
(pair (sym :method) ...)
|
27
27
|
PATTERN
|
28
28
|
|
29
|
+
# @!method alias_kwarg(node)
|
30
|
+
def_node_matcher :alias_kwarg, <<~PATTERN
|
31
|
+
(pair (sym :alias) (sym $ _))
|
32
|
+
PATTERN
|
33
|
+
|
29
34
|
# @!method hash_key_kwarg?(node)
|
30
35
|
def_node_matcher :hash_key_kwarg?, <<~PATTERN
|
31
36
|
(pair (sym :hash_key) ...)
|
@@ -41,6 +46,11 @@ module RuboCop
|
|
41
46
|
(pair (sym :resolver_method) (sym $...))
|
42
47
|
PATTERN
|
43
48
|
|
49
|
+
# @!method camelize_kwarg?(node)
|
50
|
+
def_node_matcher :camelize_kwarg?, <<~PATTERN
|
51
|
+
(pair (sym :camelize) ...)
|
52
|
+
PATTERN
|
53
|
+
|
44
54
|
def initialize(field_node)
|
45
55
|
@nodes = field_kwargs(field_node) || []
|
46
56
|
end
|
@@ -53,6 +63,14 @@ module RuboCop
|
|
53
63
|
@nodes.find { |kwarg| method_kwarg?(kwarg) }
|
54
64
|
end
|
55
65
|
|
66
|
+
def alias
|
67
|
+
@alias ||= @nodes.map { |kwarg| alias_kwarg(kwarg) }.compact.first
|
68
|
+
end
|
69
|
+
|
70
|
+
def camelize
|
71
|
+
@nodes.find { |kwarg| camelize_kwarg?(kwarg) }
|
72
|
+
end
|
73
|
+
|
56
74
|
def hash_key
|
57
75
|
@nodes.find { |kwarg| hash_key_kwarg?(kwarg) }
|
58
76
|
end
|
@@ -16,11 +16,20 @@ module RuboCop
|
|
16
16
|
def declaration(node)
|
17
17
|
buffer = processed_source.buffer
|
18
18
|
begin_pos = range_by_whole_lines(node.source_range).begin_pos
|
19
|
-
end_line = buffer.line_for_position(node.
|
19
|
+
end_line = buffer.line_for_position(final_end_location(node).end_pos)
|
20
20
|
end_pos = range_by_whole_lines(buffer.line_range(end_line),
|
21
21
|
include_final_newline: true).end_pos
|
22
22
|
Parser::Source::Range.new(buffer, begin_pos, end_pos)
|
23
23
|
end
|
24
|
+
|
25
|
+
def final_end_location(start_node)
|
26
|
+
heredoc_endings =
|
27
|
+
start_node.each_node(:str, :dstr, :xstr)
|
28
|
+
.select(&:heredoc?)
|
29
|
+
.map { |node| node.loc.heredoc_end }
|
30
|
+
|
31
|
+
[start_node.source_range.end, *heredoc_endings].max_by(&:line)
|
32
|
+
end
|
24
33
|
end
|
25
34
|
end
|
26
35
|
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.19.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:
|
11
|
+
date: 2023-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -101,6 +101,9 @@ files:
|
|
101
101
|
- lib/rubocop/cop/graphql/ordered_arguments.rb
|
102
102
|
- lib/rubocop/cop/graphql/ordered_fields.rb
|
103
103
|
- lib/rubocop/cop/graphql/resolver_method_length.rb
|
104
|
+
- lib/rubocop/cop/graphql/unnecessary_argument_camelize.rb
|
105
|
+
- lib/rubocop/cop/graphql/unnecessary_field_alias.rb
|
106
|
+
- lib/rubocop/cop/graphql/unnecessary_field_camelize.rb
|
104
107
|
- lib/rubocop/cop/graphql/unused_argument.rb
|
105
108
|
- lib/rubocop/cop/graphql_cops.rb
|
106
109
|
- lib/rubocop/graphql.rb
|