Skip to content

Commit

Permalink
Fix field_type_in_block cop
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Sep 12, 2024
1 parent a85669b commit d2f9d6c
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require:
AllCops:
DisabledByDefault: true
SuggestExtensions: false
TargetRubyVersion: 2.4
TargetRubyVersion: 2.7
Exclude:
- 'lib/graphql/language/lexer.rb'
- 'lib/graphql/language/parser.rb'
Expand Down
2 changes: 1 addition & 1 deletion graphql.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "minitest-reporters"
s.add_development_dependency "rake"
s.add_development_dependency 'rake-compiler'
s.add_development_dependency "rubocop", "1.12" # for Ruby 2.4 enforcement
s.add_development_dependency "rubocop"
# website stuff
s.add_development_dependency "jekyll"
s.add_development_dependency "yard"
Expand Down
10 changes: 9 additions & 1 deletion lib/graphql/rubocop/graphql/field_type_in_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,15 @@ def delete_type_argument(send_node, type_const)
end

def determine_field_indent(send_node)
surrounding_node = send_node.parent.parent
surrounding_node = send_node.parent
if !surrounding_node.is_a?(RuboCop::AST::ClassNode)
surrounding_node = surrounding_node.parent
end

if !surrounding_node.is_a?(RuboCop::AST::ClassNode)
raise "Invariant: Something went wrong in GraphQL-Ruby, couldn't find surrounding class definition for field (#{send_node}).\n\nPlease report this error on GitHub."
end

surrounding_source = surrounding_node.source
indent_test_idx = send_node.location.expression.begin_pos - surrounding_node.source_range.begin_pos - 1
field_indent = "".dup
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/cop/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ GraphQL/FieldTypeInBlock:
Enabled: true
Include:
- field_type.rb
- small_field_type.rb
- field_type_autocorrect.rb

GraphQL/RootTypesInBlock:
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/cop/small_field_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Types::Admin::FooType < Types::FooType
field :bar, Types::BarType
end
5 changes: 5 additions & 0 deletions spec/graphql/cop/field_type_in_block_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@

assert_rubocop_autocorrects_all("spec/fixtures/cop/field_type.rb")
end

it "works on small classes" do
result = run_rubocop_on("spec/fixtures/cop/small_field_type.rb")
assert_equal 1, rubocop_errors(result)
end
end
4 changes: 2 additions & 2 deletions spec/graphql/schema/printer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Subscription < GraphQL::Schema::Object
describe ".print_introspection_schema" do
it "returns the schema as a string for the introspection types" do
# From https://github.com/graphql/graphql-js/blob/6a0e00fe46951767287f2cc62e1a10b167b2eaa6/src/utilities/__tests__/schemaPrinter-test.js#L599
expected = <<SCHEMA
expected = <<-GRAPHQL
schema {
query: Root
}
Expand Down Expand Up @@ -630,7 +630,7 @@ class Subscription < GraphQL::Schema::Object
someEnum: Choice = FOO
sub: [Sub]
}
SCHEMA
GRAPHQL

assert_equal expected, GraphQL::Schema::Printer.print_schema(schema)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/subscriptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ class MyEnumType < GraphQL::Schema::Enum
end

class MySubscription < GraphQL::Schema::Subscription
argument :my_enum, MyEnumType, required: true
argument :my_enum, MyEnumType
field :my_enum, MyEnumType
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/rubocop_test_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def run_rubocop_on(fixture_path, autocorrect: false)
end

def rubocop_errors(rubocop_result)
rubocop_result =~ /(\d) offenses detected/
rubocop_result =~ /(\d) offenses? detected/
$1.to_i
end

Expand Down

0 comments on commit d2f9d6c

Please sign in to comment.