rubocop-graphql 0.3.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 328663a392bcbb8e7a53bfaf2ba8e7323e9382893c8f290fa6b40e965155ebe1
4
- data.tar.gz: cb76dad975e6dab191f1fac2ae6c45b677fb06bddbb26f36494a18108e1acf56
3
+ metadata.gz: 5f19fb45c31f7313ff9617b4fbb5e34848123ec0a7e9e2c9b809f68e2555ffcc
4
+ data.tar.gz: bd33d1bb602dc21f5732e5262a582ed0364d89f72138b48cef5ae01545f3acc5
5
5
  SHA512:
6
- metadata.gz: a87c4e9ee55fb176eaf98ba520a7942c7a4ee4af3531b7cc90a0039f739a5e5400ce4e21b0d694e59b00ca0bb2771585f07a50699c55496a8794eec1a38900f3
7
- data.tar.gz: cedeb68f255f806a1e12de6776722f3aadce5f82fe7c0e4ace7979593d7f84abc8f27fee1cff5ced953076f88247e4aadf0599f30b02b5a35436d946f976a353
6
+ metadata.gz: 721e3f6f4b686ae8fd51a741a1e9379d77098f7569cc6ac9143b4535dc344927f991af71ac104df47fd944a6d9e7cd204ed03c4e860d91f56ef6e9b3dc6f6d08
7
+ data.tar.gz: 2d3179d6818fbabb4a1f1ed54676413db55215ea0f1c6fdc5645adf46943faa3dd2f86faca200736e8ed243b1d208d686b6ace8619c4174631078699d43f220f
@@ -35,6 +35,11 @@ GraphQL/FieldDescription:
35
35
  VersionAdded: '0.80'
36
36
  Description: 'Ensures all fields have a description'
37
37
 
38
+ GraphQL/FieldHashKey:
39
+ Enabled: true
40
+ VersionAdded: '0.80'
41
+ Description: 'Checks :hash_key option is used for appropriate fields'
42
+
38
43
  GraphQL/FieldMethod:
39
44
  Enabled: true
40
45
  VersionAdded: '0.80'
@@ -12,8 +12,7 @@ module RuboCop
12
12
  # class will invoke the inherited hook instead
13
13
  class << self
14
14
  undef inherited
15
- def inherited(*)
16
- end
15
+ def inherited(*); end
17
16
  end
18
17
 
19
18
  # Special case `Module#<` so that the rspec support rubocop exports
@@ -45,7 +45,8 @@ module RuboCop
45
45
 
46
46
  private
47
47
 
48
- MSG = "Consider moving %<field_names>s to a new type and adding the `%<prefix>s` field instead"
48
+ MSG = "Consider moving %<field_names>s to a new type and " \
49
+ "adding the `%<prefix>s` field instead"
49
50
 
50
51
  def check_fields_prefixes(body)
51
52
  sorted_prefixes = fractured(body).sort_by { |k, _| k.size }.reverse
@@ -105,9 +105,9 @@ module RuboCop
105
105
  def group_field_declarations(corrector, node)
106
106
  field = RuboCop::GraphQL::Field.new(node)
107
107
 
108
- first_field = field.schema_member.body.find { |node|
108
+ first_field = field.schema_member.body.find do |node|
109
109
  field_definition?(node) || field_definition_with_body?(node)
110
- }
110
+ end
111
111
 
112
112
  source_to_insert = "\n" + indent(node) + node.source
113
113
  corrector.insert_after(first_field.loc.expression, source_to_insert)
@@ -124,11 +124,12 @@ module RuboCop
124
124
  method_definition = field.schema_member.find_method_definition(field.resolver_method_name)
125
125
  return unless method_definition
126
126
 
127
- field_sibling_index = if field_definition_with_body?(field.parent)
128
- field.parent.sibling_index
129
- else
130
- field.sibling_index
131
- end
127
+ field_sibling_index =
128
+ if field_definition_with_body?(field.parent)
129
+ field.parent.sibling_index
130
+ else
131
+ field.sibling_index
132
+ end
132
133
 
133
134
  return if method_definition.sibling_index - field_sibling_index == 1
134
135
 
@@ -146,7 +147,9 @@ module RuboCop
146
147
  method_range = range_by_whole_lines(method_definition.loc.expression)
147
148
  corrector.insert_before(method_range, source_to_insert)
148
149
 
149
- range_to_remove = range_with_surrounding_space(range: field_definition.loc.expression, side: :left)
150
+ range_to_remove = range_with_surrounding_space(
151
+ range: field_definition.loc.expression, side: :left
152
+ )
150
153
  corrector.remove(range_to_remove)
151
154
  end
152
155
 
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module GraphQL
6
+ # This cop prevents defining unnecessary resolver methods in cases
7
+ # when :hash_key option can be used
8
+ #
9
+ # @example
10
+ # # good
11
+ #
12
+ # class Types::UserType < Types::BaseObject
13
+ # field :phone, String, null: true, hash_key: :home_phone
14
+ # end
15
+ #
16
+ # # bad
17
+ #
18
+ # class Types::UserType < Types::BaseObject
19
+ # field :phone, String, null: true
20
+ #
21
+ # def phone
22
+ # object[:home_phone]
23
+ # end
24
+ # end
25
+ #
26
+ class FieldHashKey < Cop
27
+ include RuboCop::GraphQL::NodePattern
28
+ include RuboCop::Cop::RangeHelp
29
+
30
+ def_node_matcher :hash_key_to_use, <<~PATTERN
31
+ (def
32
+ _
33
+ (args)
34
+ (send
35
+ (send nil? :object) :[]
36
+ (_type $_)
37
+ )
38
+ )
39
+ PATTERN
40
+
41
+ MSG = "Use hash_key: %<hash_key>p"
42
+
43
+ def on_send(node)
44
+ return unless field_definition?(node)
45
+
46
+ field = RuboCop::GraphQL::Field.new(node)
47
+ method_definition = resolver_method_definition_for(field)
48
+
49
+ if (suggested_hash_key_name = hash_key_to_use(method_definition))
50
+ add_offense(node, message: message(suggested_hash_key_name))
51
+ end
52
+ end
53
+
54
+ def autocorrect(node)
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)
59
+
60
+ corrector.insert_after(
61
+ node.loc.expression, ", hash_key: #{suggested_hash_key_name.inspect}"
62
+ )
63
+
64
+ range = range_with_surrounding_space(
65
+ range: method_definition.loc.expression, side: :left
66
+ )
67
+
68
+ corrector.remove(range)
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def message(hash_key)
75
+ format(MSG, hash_key: hash_key)
76
+ end
77
+
78
+ def resolver_method_definition_for(field)
79
+ method_name = field.resolver_method_name
80
+ field.schema_member.find_method_definition(method_name)
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -58,7 +58,9 @@ module RuboCop
58
58
 
59
59
  corrector.insert_after(node.loc.expression, ", method: :#{suggested_method_name}")
60
60
 
61
- range = range_with_surrounding_space(range: method_definition.loc.expression, side: :left)
61
+ range = range_with_surrounding_space(
62
+ range: method_definition.loc.expression, side: :left
63
+ )
62
64
  corrector.remove(range)
63
65
  end
64
66
  end
@@ -3,7 +3,8 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module GraphQL
6
- # This cop checks if a type (object, input, interface, scalar, union, mutation, subscription, and resolver) has a description.
6
+ # This cop checks if a type (object, input, interface, scalar, union,
7
+ # mutation, subscription, and resolver) has a description.
7
8
  #
8
9
  # @example
9
10
  # # good
@@ -45,7 +46,9 @@ module RuboCop
45
46
  def on_module(node)
46
47
  return if child_nodes(node).none? { |child_node| interface?(child_node) }
47
48
 
48
- add_offense(node.identifier) if child_nodes(node).none? { |child_node| has_description?(child_node) }
49
+ if child_nodes(node).none? { |child_node| has_description?(child_node) }
50
+ add_offense(node.identifier)
51
+ end
49
52
  end
50
53
 
51
54
  private
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module GraphQL
6
+ # Field should be alphabetically sorted within groups.
7
+ #
8
+ # @example
9
+ # # good
10
+ #
11
+ # class UserType < BaseType
12
+ # field :name, String, null: true
13
+ # field :phone, String, null: true do
14
+ # argument :something, String, required: false
15
+ # end
16
+ # end
17
+ #
18
+ # # good
19
+ #
20
+ # class UserType < BaseType
21
+ # field :phone, String, null: true
22
+ #
23
+ # field :name, String, null: true
24
+ # end
25
+ #
26
+ # # bad
27
+ #
28
+ # class UserType < BaseType
29
+ # field :phone, String, null: true
30
+ # field :name, String, null: true
31
+ # end
32
+ #
33
+ class OrderedFields < Cop
34
+ MSG = "Fields should be sorted in an alphabetical order within their "\
35
+ "section. "\
36
+ "Field `%<current>s` should appear before `%<previous>s`."
37
+
38
+ def investigate(processed_source)
39
+ return if processed_source.blank?
40
+
41
+ field_declarations(processed_source.ast)
42
+ .each_cons(2) do |previous, current|
43
+ next unless consecutive_lines(previous, current)
44
+ next if field_name(current) > field_name(previous)
45
+
46
+ register_offense(previous, current)
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def register_offense(previous, current)
53
+ message = format(
54
+ self.class::MSG,
55
+ previous: field_name(previous),
56
+ current: field_name(current)
57
+ )
58
+ add_offense(current, message: message)
59
+ end
60
+
61
+ def field_name(node)
62
+ if node.block_type?
63
+ field_name(node.send_node)
64
+ else
65
+ node.first_argument.value.to_s
66
+ end
67
+ end
68
+
69
+ def consecutive_lines(previous, current)
70
+ previous.source_range.last_line == current.source_range.first_line - 1
71
+ end
72
+
73
+ def_node_search :field_declarations, <<~PATTERN
74
+ {
75
+ (send nil? :field (:sym _) ...)
76
+ (block
77
+ (send nil? :field (:sym _) ...) ...)
78
+ }
79
+ PATTERN
80
+ end
81
+ end
82
+ end
83
+ end
@@ -9,9 +9,9 @@ module RuboCop
9
9
  # The maximum allowed length is configurable using the Max option.
10
10
  class ResolverMethodLength < Cop
11
11
  include RuboCop::Cop::ConfigurableMax
12
- include RuboCop::Cop::TooManyLines
12
+ include RuboCop::Cop::CodeLength
13
13
 
14
- LABEL = "ResolverMethod"
14
+ MSG = "ResolverMethod has too many lines. [%<total>d/%<max>d]"
15
15
 
16
16
  def_node_matcher :field_definition, <<~PATTERN
17
17
  (send nil? :field (sym $...) ...)
@@ -21,18 +21,31 @@ module RuboCop
21
21
  excluded_methods = cop_config["ExcludedMethods"]
22
22
  return if excluded_methods.include?(String(node.method_name))
23
23
 
24
- check_code_length(node) if field_is_defined?(node)
24
+ if field_is_defined?(node)
25
+ length = code_length(node)
26
+
27
+ return unless length > max_length
28
+
29
+ add_offense(node, message: message(length))
30
+ end
25
31
  end
26
32
  alias on_defs on_def
27
33
 
28
34
  private
29
35
 
36
+ def code_length(node)
37
+ node.source.lines[1..-2].count { |line| !irrelevant_line(line) }
38
+ end
39
+
30
40
  def field_is_defined?(node)
31
- node.parent.children.flat_map { |child| field_definition(child) }.include?(node.method_name)
41
+ node.parent
42
+ .children
43
+ .flat_map { |child| field_definition(child) }
44
+ .include?(node.method_name)
32
45
  end
33
46
 
34
- def cop_label
35
- LABEL
47
+ def message(length)
48
+ format(MSG, total: length, max: max_length)
36
49
  end
37
50
  end
38
51
  end
@@ -8,7 +8,9 @@ require_relative "graphql/extract_input_type"
8
8
  require_relative "graphql/extract_type"
9
9
  require_relative "graphql/field_definitions"
10
10
  require_relative "graphql/field_description"
11
+ require_relative "graphql/field_hash_key"
11
12
  require_relative "graphql/field_method"
12
13
  require_relative "graphql/field_name"
13
14
  require_relative "graphql/resolver_method_length"
14
15
  require_relative "graphql/object_description"
16
+ require_relative "graphql/ordered_fields"
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module GraphQL
5
5
  module Ext
6
6
  module SnakeCase
7
- SNAKE_CASE = /^[\da-z_]+[!?=]?$/
7
+ SNAKE_CASE = /^[\da-z_]+[!?=]?$/.freeze
8
8
 
9
9
  refine Symbol do
10
10
  def snake_case?
@@ -69,7 +69,7 @@ module RuboCop
69
69
  end
70
70
 
71
71
  def root_node?(node)
72
- node.parent.nil? || root_with_siblings?(node.parent)
72
+ node.parent.nil? || node.parent.module_type? || root_with_siblings?(node.parent)
73
73
  end
74
74
 
75
75
  def root_with_siblings?(node)
@@ -56,7 +56,8 @@ module RuboCop
56
56
  end
57
57
 
58
58
  def resolver_method_name
59
- @resolver_method_name ||= @nodes.flat_map { |kwarg| resolver_method_option(kwarg) }.compact.first
59
+ @resolver_method_name ||=
60
+ @nodes.flat_map { |kwarg| resolver_method_option(kwarg) }.compact.first
60
61
  end
61
62
  end
62
63
  end
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "0.3.0"
3
+ VERSION = "0.6.0".freeze
4
4
  end
5
5
  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.3.0
4
+ version: 0.6.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: 2020-07-21 00:00:00.000000000 Z
11
+ date: 2020-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -39,33 +39,39 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: standard
42
+ name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '='
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 0.2.0
47
+ version: '3.9'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '='
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 0.2.0
54
+ version: '3.9'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rubocop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 0.71.0
61
+ version: '0.87'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '1.1'
62
65
  type: :runtime
63
66
  prerelease: false
64
67
  version_requirements: !ruby/object:Gem::Requirement
65
68
  requirements:
66
69
  - - ">="
67
70
  - !ruby/object:Gem::Version
68
- version: 0.71.0
71
+ version: '0.87'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.1'
69
75
  description: A collection of RuboCop cops to improve GraphQL-related code
70
76
  email:
71
77
  - dmitry.a.tsepelev@gmail.com
@@ -84,9 +90,11 @@ files:
84
90
  - lib/rubocop/cop/graphql/extract_type.rb
85
91
  - lib/rubocop/cop/graphql/field_definitions.rb
86
92
  - lib/rubocop/cop/graphql/field_description.rb
93
+ - lib/rubocop/cop/graphql/field_hash_key.rb
87
94
  - lib/rubocop/cop/graphql/field_method.rb
88
95
  - lib/rubocop/cop/graphql/field_name.rb
89
96
  - lib/rubocop/cop/graphql/object_description.rb
97
+ - lib/rubocop/cop/graphql/ordered_fields.rb
90
98
  - lib/rubocop/cop/graphql/resolver_method_length.rb
91
99
  - lib/rubocop/cop/graphql_cops.rb
92
100
  - lib/rubocop/graphql.rb
@@ -116,7 +124,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
124
  requirements:
117
125
  - - ">="
118
126
  - !ruby/object:Gem::Version
119
- version: '0'
127
+ version: '2.4'
120
128
  required_rubygems_version: !ruby/object:Gem::Requirement
121
129
  requirements:
122
130
  - - ">="