rubocop-graphql 0.8.2 → 0.9.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: 22be8928aa9fbce1700f99d21ccddacd9590ab7bd6c6723e4b7458f974e02f66
4
- data.tar.gz: 6e06404047cba621b7493912723fa5eac13c19004d128339e494455758871831
3
+ metadata.gz: e63911b5c7c77f1d207fc9a1d411c61b874f51b372507bd7fd4c8072addd0663
4
+ data.tar.gz: 5581dcb49597ece10a23dba6f191b531d80d313e265fb6a61ec6db172691ad75
5
5
  SHA512:
6
- metadata.gz: 94e9c24116f399d08b0c264e4b1b6c5dc50ac9436833fd8ec9f4916bdae102f5fdd5df5589a31502bca8aa65ccc61448a24b2e57aded363c35e6b0adec1513f6
7
- data.tar.gz: '0855eb683b91a3d5bb6e78b89c7c3089df5832813b1c298b26edf56de2e3c4ee71da837d279bad5ce15fd4dbcf9197fe908f81f77967a7b7345c9a1b3fdcb1f2'
6
+ metadata.gz: ff897f7cfd8f63f3435915d3c9203803b7d81cb4d3997b5ef6e8e3822cb95ae77cc7eb3573bc6c50aa54c630eec59c550b35dca07d12412bcf7a460e00e3290f
7
+ data.tar.gz: cb02df1ef01c442dd7c3ad1abd89b6e8457bf83aa52391c78ead27e7c8d0793f8794ac4eb7295e234022763081f640115c3adef8a1f42f81600ef714655f6c06
data/config/default.yml CHANGED
@@ -67,6 +67,11 @@ GraphQL/ExtractType:
67
67
  - min
68
68
  - max
69
69
 
70
+ GraphQL/LegacyDsl:
71
+ Enabled: true
72
+ VersionAdded: '0.80'
73
+ Description: 'Checks that types are defined with class-based API'
74
+
70
75
  GraphQL/ObjectDescription:
71
76
  Enabled: true
72
77
  VersionAdded: '0.80'
@@ -9,6 +9,7 @@ require_relative "rubocop/graphql/version"
9
9
  require_relative "rubocop/graphql/inject"
10
10
  require_relative "rubocop/graphql/description_method"
11
11
  require_relative "rubocop/graphql/node_pattern"
12
+ require_relative "rubocop/graphql/swap_range"
12
13
 
13
14
  require_relative "rubocop/graphql/argument"
14
15
  require_relative "rubocop/graphql/argument/block"
@@ -18,7 +18,7 @@ module RuboCop
18
18
  # argument :uuid, ID, required: true
19
19
  # end
20
20
  #
21
- class ArgumentDescription < Cop
21
+ class ArgumentDescription < Base
22
22
  include RuboCop::GraphQL::NodePattern
23
23
 
24
24
  MSG = "Missing argument description"
@@ -18,7 +18,7 @@ module RuboCop
18
18
  # argument :userId, ID, required: true
19
19
  # end
20
20
  #
21
- class ArgumentName < Cop
21
+ class ArgumentName < Base
22
22
  include RuboCop::GraphQL::NodePattern
23
23
 
24
24
  using RuboCop::GraphQL::Ext::SnakeCase
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module GraphQL
6
- class ExtractInputType < Cop
6
+ class ExtractInputType < Base
7
7
  # This cop checks fields on common prefix groups
8
8
  #
9
9
  # # @example
@@ -3,7 +3,7 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module GraphQL
6
- class ExtractType < Cop
6
+ class ExtractType < Base
7
7
  # This cop checks fields on common prefix groups
8
8
  #
9
9
  # # @example
@@ -44,7 +44,8 @@ module RuboCop
44
44
  # object.contact_data.last_name
45
45
  # end
46
46
  # end
47
- class FieldDefinitions < Cop
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)
@@ -18,7 +18,7 @@ module RuboCop
18
18
  # field :name, String, null: true
19
19
  # end
20
20
  #
21
- class FieldDescription < Cop
21
+ class FieldDescription < Base
22
22
  include RuboCop::GraphQL::NodePattern
23
23
 
24
24
  MSG = "Missing field description"
@@ -23,7 +23,8 @@ module RuboCop
23
23
  # end
24
24
  # end
25
25
  #
26
- class FieldHashKey < Cop
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
- 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)
57
+ private
59
58
 
60
- corrector.insert_after(
61
- node.loc.expression, ", hash_key: #{suggested_hash_key_name.inspect}"
62
- )
59
+ def message(hash_key)
60
+ format(MSG, hash_key: hash_key)
61
+ end
63
62
 
64
- range = range_with_surrounding_space(
65
- range: method_definition.loc.expression, side: :left
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
- corrector.remove(range)
69
- end
70
- end
68
+ corrector.insert_after(
69
+ node.loc.expression, ", hash_key: #{suggested_hash_key_name.inspect}"
70
+ )
71
71
 
72
- private
72
+ range = range_with_surrounding_space(
73
+ range: method_definition.loc.expression, side: :left
74
+ )
73
75
 
74
- def message(hash_key)
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 < Cop
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
- end
51
- end
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)
@@ -18,7 +18,7 @@ module RuboCop
18
18
  # field :firstName, String, null: true
19
19
  # end
20
20
  #
21
- class FieldName < Cop
21
+ class FieldName < Base
22
22
  include RuboCop::GraphQL::NodePattern
23
23
 
24
24
  using RuboCop::GraphQL::Ext::SnakeCase
@@ -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,7 +20,7 @@ module RuboCop
20
20
  # # ...
21
21
  # end
22
22
  #
23
- class ObjectDescription < Cop
23
+ class ObjectDescription < Base
24
24
  include RuboCop::GraphQL::NodePattern
25
25
  include RuboCop::GraphQL::DescriptionMethod
26
26
 
@@ -48,15 +48,16 @@ module RuboCop
48
48
  # end
49
49
  # end
50
50
  #
51
- class OrderedArguments < Cop
51
+ class OrderedArguments < Base
52
+ extend AutoCorrector
53
+
54
+ include RuboCop::GraphQL::SwapRange
55
+
52
56
  MSG = "Arguments should be sorted in an alphabetical order within their section. " \
53
57
  "Field `%<current>s` should appear before `%<previous>s`."
54
58
 
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|
59
+ def on_class(node)
60
+ argument_declarations(node).each_cons(2) do |previous, current|
60
61
  next unless consecutive_lines(previous, current)
61
62
  next if argument_name(current) > argument_name(previous)
62
63
 
@@ -64,43 +65,18 @@ module RuboCop
64
65
  end
65
66
  end
66
67
 
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
68
  private
81
69
 
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
70
  def register_offense(previous, current)
98
71
  message = format(
99
72
  self.class::MSG,
100
73
  previous: argument_name(previous),
101
74
  current: argument_name(current)
102
75
  )
103
- add_offense(current, message: message)
76
+
77
+ add_offense(current, message: message) do |corrector|
78
+ swap_range(corrector, current, previous)
79
+ end
104
80
  end
105
81
 
106
82
  def argument_name(node)
@@ -30,33 +30,21 @@ module RuboCop
30
30
  # field :name, String, null: true
31
31
  # end
32
32
  #
33
- class OrderedFields < Cop
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 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
- 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)
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)
57
46
 
58
- lambda do |corrector|
59
- swap_range(corrector, current_range, previous_range)
47
+ register_offense(previous, current)
60
48
  end
61
49
  end
62
50
 
@@ -68,7 +56,10 @@ module RuboCop
68
56
  previous: field_name(previous),
69
57
  current: field_name(current)
70
58
  )
71
- add_offense(current, message: message)
59
+
60
+ add_offense(current, message: message) do |corrector|
61
+ swap_range(corrector, current, previous)
62
+ end
72
63
  end
73
64
 
74
65
  def field_name(node)
@@ -83,21 +74,6 @@ module RuboCop
83
74
  previous.source_range.last_line == current.source_range.first_line - 1
84
75
  end
85
76
 
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
-
101
77
  def_node_search :field_declarations, <<~PATTERN
102
78
  {
103
79
  (send nil? :field (:sym _) ...)
@@ -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 < Cop
10
+ class ResolverMethodLength < Base
11
11
  include RuboCop::Cop::ConfigurableMax
12
12
  include RuboCop::Cop::CodeLength
13
13
 
@@ -9,6 +9,7 @@ require_relative "graphql/field_description"
9
9
  require_relative "graphql/field_hash_key"
10
10
  require_relative "graphql/field_method"
11
11
  require_relative "graphql/field_name"
12
+ require_relative "graphql/legacy_dsl"
12
13
  require_relative "graphql/resolver_method_length"
13
14
  require_relative "graphql/object_description"
14
15
  require_relative "graphql/ordered_arguments"
@@ -20,7 +20,7 @@ module RuboCop
20
20
  PATTERN
21
21
 
22
22
  def initialize(argument_node)
23
- @nodes = argument_kwargs(argument_node)
23
+ @nodes = argument_kwargs(argument_node) || []
24
24
  end
25
25
 
26
26
  def description
@@ -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
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module GraphQL
3
- VERSION = "0.8.2".freeze
3
+ VERSION = "0.9.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.8.2
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-04-23 00:00:00.000000000 Z
11
+ date: 2021-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,6 +92,7 @@ files:
92
92
  - lib/rubocop/cop/graphql/field_hash_key.rb
93
93
  - lib/rubocop/cop/graphql/field_method.rb
94
94
  - lib/rubocop/cop/graphql/field_name.rb
95
+ - lib/rubocop/cop/graphql/legacy_dsl.rb
95
96
  - lib/rubocop/cop/graphql/object_description.rb
96
97
  - lib/rubocop/cop/graphql/ordered_arguments.rb
97
98
  - lib/rubocop/cop/graphql/ordered_fields.rb
@@ -109,6 +110,7 @@ files:
109
110
  - lib/rubocop/graphql/inject.rb
110
111
  - lib/rubocop/graphql/node_pattern.rb
111
112
  - lib/rubocop/graphql/schema_member.rb
113
+ - lib/rubocop/graphql/swap_range.rb
112
114
  - lib/rubocop/graphql/version.rb
113
115
  homepage: https://github.com/DmitryTsepelev/rubocop-graphql
114
116
  licenses: