rubocop-graphql 0.4.0 → 0.6.2

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: 5be7099abfc014351e097467d9dfe7c687d4a0323241f7ce592e0abe25f6b0cb
4
- data.tar.gz: da4e69f7810f416873d3f3f0742b70758a3c66c343bbd0d2572c223605156c77
3
+ metadata.gz: 2393f8c4972a479e9618f3ed9f846a819b0efa8bf13e53f319bbc176c50a470b
4
+ data.tar.gz: 4326de68922f560a042ecbee99f70f421a09c05e0502291e4b4624c90a369233
5
5
  SHA512:
6
- metadata.gz: ea0737a86c403dbd57b5777c221ce80ec86e97c6fce499d00f573ef7adeb15f37e2db7b2c8f6275ff7aa58e1a17b12472a748cdf06f26c0411cb9943075386fd
7
- data.tar.gz: c0b0235d1035b9b77ffaaac3ee419b4e240c9eb7363033f37f9396a028930e33f6d11d3145f95c708e68a0b2f7566de0e1cf96264619eb1da0c3626e99304a0b
6
+ metadata.gz: 1d65dbf3f6d5a623afbd74524e485ff4c570157ab23eae396e7ca5e343ff3ea2546d8433fc67dff38e2d63985ebc2881de3ba37e58ed5943ef2521fb2be93d36
7
+ data.tar.gz: 718957be35d17116319a4d18ba707b96236e813e9acd8fa724888623550f6dffafc2c07e774a181f5f576c2e949d92e3c264badc2c30ef4897d440781a3b1c32
@@ -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 # rubocop:disable Lint/MissingSuper
17
16
  end
18
17
 
19
18
  # Special case `Module#<` so that the rspec support rubocop exports
@@ -48,7 +47,7 @@ module RuboCop
48
47
  )
49
48
 
50
49
  # Invoke the original inherited hook so our cops are recognized
51
- def self.inherited(subclass)
50
+ def self.inherited(subclass) # rubocop:disable Lint/MissingSuper
52
51
  RuboCop::Cop::Cop.inherited(subclass)
53
52
  end
54
53
 
@@ -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,11 +105,11 @@ 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
- source_to_insert = "\n" + indent(node) + node.source
112
+ source_to_insert = "\n#{indent(node)}#{node.source}"
113
113
  corrector.insert_after(first_field.loc.expression, source_to_insert)
114
114
 
115
115
  range = range_with_surrounding_space(range: node.loc.expression, side: :left)
@@ -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
 
@@ -142,11 +143,13 @@ module RuboCop
142
143
 
143
144
  field_definition = field_definition_with_body?(node.parent) ? node.parent : node
144
145
 
145
- source_to_insert = indent(method_definition) + field_definition.source + "\n\n"
146
+ source_to_insert = "#{indent(method_definition)}#{field_definition.source}\n\n"
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
 
@@ -57,9 +57,14 @@ module RuboCop
57
57
  method_definition = resolver_method_definition_for(field)
58
58
  suggested_hash_key_name = hash_key_to_use(method_definition)
59
59
 
60
- corrector.insert_after(node.loc.expression, ", hash_key: #{suggested_hash_key_name.inspect}")
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
+ )
61
67
 
62
- range = range_with_surrounding_space(range: method_definition.loc.expression, side: :left)
63
68
  corrector.remove(range)
64
69
  end
65
70
  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
@@ -32,6 +33,10 @@ module RuboCop
32
33
  (send nil? :description (:str $_))
33
34
  PATTERN
34
35
 
36
+ def_node_matcher :has_multiline_string_description?, <<~PATTERN
37
+ (send nil? :description (:dstr ...))
38
+ PATTERN
39
+
35
40
  def_node_matcher :interface?, <<~PATTERN
36
41
  (send nil? :include (const ...))
37
42
  PATTERN
@@ -45,13 +50,17 @@ module RuboCop
45
50
  def on_module(node)
46
51
  return if child_nodes(node).none? { |child_node| interface?(child_node) }
47
52
 
48
- add_offense(node.identifier) if child_nodes(node).none? { |child_node| has_description?(child_node) }
53
+ if child_nodes(node).none? { |child_node| has_description?(child_node) }
54
+ add_offense(node.identifier)
55
+ end
49
56
  end
50
57
 
51
58
  private
52
59
 
53
60
  def has_description?(node)
54
- has_i18n_description?(node) || has_string_description?(node)
61
+ has_i18n_description?(node) ||
62
+ has_string_description?(node) ||
63
+ has_multiline_string_description?(node)
55
64
  end
56
65
 
57
66
  def child_nodes(node)
@@ -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
@@ -13,3 +13,4 @@ require_relative "graphql/field_method"
13
13
  require_relative "graphql/field_name"
14
14
  require_relative "graphql/resolver_method_length"
15
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?
@@ -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.4.0"
3
+ VERSION = "0.6.2".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.4.0
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Tsepelev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-30 00:00:00.000000000 Z
11
+ date: 2021-03-03 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: '2'
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: '2'
69
75
  description: A collection of RuboCop cops to improve GraphQL-related code
70
76
  email:
71
77
  - dmitry.a.tsepelev@gmail.com
@@ -88,6 +94,7 @@ files:
88
94
  - lib/rubocop/cop/graphql/field_method.rb
89
95
  - lib/rubocop/cop/graphql/field_name.rb
90
96
  - lib/rubocop/cop/graphql/object_description.rb
97
+ - lib/rubocop/cop/graphql/ordered_fields.rb
91
98
  - lib/rubocop/cop/graphql/resolver_method_length.rb
92
99
  - lib/rubocop/cop/graphql_cops.rb
93
100
  - lib/rubocop/graphql.rb
@@ -109,7 +116,7 @@ metadata:
109
116
  homepage_uri: https://github.com/DmitryTsepelev/rubocop-graphql
110
117
  source_code_uri: https://github.com/DmitryTsepelev/rubocop-graphql
111
118
  changelog_uri: https://github.com/DmitryTsepelev/rubocop-graphql/CHANGELOG.md
112
- post_install_message:
119
+ post_install_message:
113
120
  rdoc_options: []
114
121
  require_paths:
115
122
  - lib
@@ -117,15 +124,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
124
  requirements:
118
125
  - - ">="
119
126
  - !ruby/object:Gem::Version
120
- version: '0'
127
+ version: '2.4'
121
128
  required_rubygems_version: !ruby/object:Gem::Requirement
122
129
  requirements:
123
130
  - - ">="
124
131
  - !ruby/object:Gem::Version
125
132
  version: '0'
126
133
  requirements: []
127
- rubygems_version: 3.0.3
128
- signing_key:
134
+ rubygems_version: 3.1.2
135
+ signing_key:
129
136
  specification_version: 4
130
137
  summary: Automatic performance checking tool for Ruby code.
131
138
  test_files: []