rubocop-graphql 0.5.0 → 0.8.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: a3be1c838260c7be6902b6c06cf411ee768c39dab704b078d0204d8d1fa24fc5
4
- data.tar.gz: 1061e5a30067f55254d5a89e87ff6a1597ac8df321e6be74b06a937ddd8da1e1
3
+ metadata.gz: c68112dd907111859f0c8ca78ad31c6bea918c11411d9d89a4a4ae289fc3630d
4
+ data.tar.gz: cfeed828552dcdc4509b476f4d6a9672c111ef2e11001fad01f52b3699d26bc9
5
5
  SHA512:
6
- metadata.gz: 32e42d5a360bf522df3408bcfbc906e830eb7dcbc46aef53e967567c73f3634908a9cb6e9fb51967e79d1e64be24074067eca0e2870d45137c97b66f2b6354f4
7
- data.tar.gz: 83a9f4756bec98ff082561a1cc1b81e64335a914bd343ba9353ccb865f38714219619594b8c6844b020fa04b3202412b5533a6fb01e2cbfb59b2a352d6465f57
6
+ metadata.gz: 7a20bb455185e7eb0ceeffb6da3f33438a416f003ce6fd7287483a34aab61301051a72e0b6b13967ead0c2b14c5ce31d28f4c9c864ed24ab627b34e7f322e40c
7
+ data.tar.gz: e3badf19ec0a0ec03fe7be4d71324cb65e70f64c2ed3919334836f5d532e10fd16b3f54f58f2e909c987b72957027a13fa27fedf35f1bba2d2cceb6c9e66ca69
@@ -12,7 +12,7 @@ module RuboCop
12
12
  # class will invoke the inherited hook instead
13
13
  class << self
14
14
  undef inherited
15
- def inherited(*); end
15
+ def inherited(*); end # rubocop:disable Lint/MissingSuper
16
16
  end
17
17
 
18
18
  # Special case `Module#<` so that the rspec support rubocop exports
@@ -47,7 +47,7 @@ module RuboCop
47
47
  )
48
48
 
49
49
  # Invoke the original inherited hook so our cops are recognized
50
- def self.inherited(subclass)
50
+ def self.inherited(subclass) # rubocop:disable Lint/MissingSuper
51
51
  RuboCop::Cop::Cop.inherited(subclass)
52
52
  end
53
53
 
@@ -32,7 +32,12 @@ module RuboCop
32
32
  if (body = schema_member.body)
33
33
  arguments = body.select { |node| argument?(node) }
34
34
 
35
- add_offense(arguments.last) if arguments.count > cop_config["MaxArguments"]
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
@@ -109,7 +109,7 @@ module RuboCop
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)
@@ -143,7 +143,7 @@ module RuboCop
143
143
 
144
144
  field_definition = field_definition_with_body?(node.parent) ? node.parent : node
145
145
 
146
- source_to_insert = indent(method_definition) + field_definition.source + "\n\n"
146
+ source_to_insert = "#{indent(method_definition)}#{field_definition.source}\n\n"
147
147
  method_range = range_by_whole_lines(method_definition.loc.expression)
148
148
  corrector.insert_before(method_range, source_to_insert)
149
149
 
@@ -33,6 +33,10 @@ module RuboCop
33
33
  (send nil? :description (:str $_))
34
34
  PATTERN
35
35
 
36
+ def_node_matcher :has_multiline_string_description?, <<~PATTERN
37
+ (send nil? :description (:dstr ...))
38
+ PATTERN
39
+
36
40
  def_node_matcher :interface?, <<~PATTERN
37
41
  (send nil? :include (const ...))
38
42
  PATTERN
@@ -54,7 +58,9 @@ module RuboCop
54
58
  private
55
59
 
56
60
  def has_description?(node)
57
- has_i18n_description?(node) || has_string_description?(node)
61
+ has_i18n_description?(node) ||
62
+ has_string_description?(node) ||
63
+ has_multiline_string_description?(node)
58
64
  end
59
65
 
60
66
  def child_nodes(node)
@@ -0,0 +1,120 @@
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 < Cop
52
+ MSG = "Arguments should be sorted in an alphabetical order within their section. " \
53
+ "Field `%<current>s` should appear before `%<previous>s`."
54
+
55
+ def investigate(processed_source)
56
+ return if processed_source.blank?
57
+
58
+ argument_declarations(processed_source.ast)
59
+ .each_cons(2) do |previous, current|
60
+ next unless consecutive_lines(previous, current)
61
+ next if argument_name(current) > argument_name(previous)
62
+
63
+ register_offense(previous, current)
64
+ end
65
+ end
66
+
67
+ def autocorrect(node)
68
+ declarations = argument_declarations(processed_source.ast)
69
+ node_index = declarations.map(&:location).find_index(node.location)
70
+ previous_declaration = declarations.to_a[node_index - 1]
71
+
72
+ current_range = declaration(node)
73
+ previous_range = declaration(previous_declaration)
74
+
75
+ lambda do |corrector|
76
+ swap_range(corrector, current_range, previous_range)
77
+ end
78
+ end
79
+
80
+ private
81
+
82
+ def declaration(node)
83
+ buffer = processed_source.buffer
84
+ begin_pos = node.source_range.begin_pos
85
+ end_line = buffer.line_for_position(node.loc.expression.end_pos)
86
+ end_pos = buffer.line_range(end_line).end_pos
87
+ Parser::Source::Range.new(buffer, begin_pos, end_pos)
88
+ end
89
+
90
+ def swap_range(corrector, range1, range2)
91
+ src1 = range1.source
92
+ src2 = range2.source
93
+ corrector.replace(range1, src2)
94
+ corrector.replace(range2, src1)
95
+ end
96
+
97
+ def register_offense(previous, current)
98
+ message = format(
99
+ self.class::MSG,
100
+ previous: argument_name(previous),
101
+ current: argument_name(current)
102
+ )
103
+ add_offense(current, message: message)
104
+ end
105
+
106
+ def argument_name(node)
107
+ node.first_argument.value.to_s
108
+ end
109
+
110
+ def consecutive_lines(previous, current)
111
+ previous.source_range.last_line == current.source_range.first_line - 1
112
+ end
113
+
114
+ def_node_search :argument_declarations, <<~PATTERN
115
+ (send nil? :argument (:sym _) ...)
116
+ PATTERN
117
+ end
118
+ end
119
+ end
120
+ end
@@ -47,6 +47,19 @@ module RuboCop
47
47
  end
48
48
  end
49
49
 
50
+ def autocorrect(node)
51
+ declarations = field_declarations(processed_source.ast)
52
+ node_index = declarations.map(&:location).find_index(node.location)
53
+ previous_declaration = declarations.to_a[node_index - 1]
54
+
55
+ current_range = declaration(node)
56
+ previous_range = declaration(previous_declaration)
57
+
58
+ lambda do |corrector|
59
+ swap_range(corrector, current_range, previous_range)
60
+ end
61
+ end
62
+
50
63
  private
51
64
 
52
65
  def register_offense(previous, current)
@@ -70,6 +83,21 @@ module RuboCop
70
83
  previous.source_range.last_line == current.source_range.first_line - 1
71
84
  end
72
85
 
86
+ def declaration(node)
87
+ buffer = processed_source.buffer
88
+ begin_pos = node.source_range.begin_pos
89
+ end_line = buffer.line_for_position(node.loc.expression.end_pos)
90
+ end_pos = buffer.line_range(end_line).end_pos
91
+ Parser::Source::Range.new(buffer, begin_pos, end_pos)
92
+ end
93
+
94
+ def swap_range(corrector, range1, range2)
95
+ src1 = range1.source
96
+ src2 = range2.source
97
+ corrector.replace(range1, src2)
98
+ corrector.replace(range2, src1)
99
+ end
100
+
73
101
  def_node_search :field_declarations, <<~PATTERN
74
102
  {
75
103
  (send nil? :field (:sym _) ...)
@@ -13,4 +13,5 @@ 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_arguments"
16
17
  require_relative "graphql/ordered_fields"
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "0.5.0".freeze
3
+ VERSION = "0.8.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.5.0
4
+ version: 0.8.0
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-10-02 00:00:00.000000000 Z
11
+ date: 2021-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,16 +56,22 @@ dependencies:
56
56
  name: rubocop
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
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
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_arguments.rb
91
98
  - lib/rubocop/cop/graphql/ordered_fields.rb
92
99
  - lib/rubocop/cop/graphql/resolver_method_length.rb
93
100
  - lib/rubocop/cop/graphql_cops.rb
@@ -110,7 +117,7 @@ metadata:
110
117
  homepage_uri: https://github.com/DmitryTsepelev/rubocop-graphql
111
118
  source_code_uri: https://github.com/DmitryTsepelev/rubocop-graphql
112
119
  changelog_uri: https://github.com/DmitryTsepelev/rubocop-graphql/CHANGELOG.md
113
- post_install_message:
120
+ post_install_message:
114
121
  rdoc_options: []
115
122
  require_paths:
116
123
  - lib
@@ -125,8 +132,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
132
  - !ruby/object:Gem::Version
126
133
  version: '0'
127
134
  requirements: []
128
- rubygems_version: 3.0.3
129
- signing_key:
135
+ rubygems_version: 3.1.2
136
+ signing_key:
130
137
  specification_version: 4
131
138
  summary: Automatic performance checking tool for Ruby code.
132
139
  test_files: []