Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle single-line classes in field_type_in_block cop #5098

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 6 additions & 6 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 @@ -440,7 +440,7 @@ class Subscription < GraphQL::Schema::Object
"""
UNION
}
SCHEMA
GRAPHQL
assert_equal expected.chomp, GraphQL::Schema::Printer.print_introspection_schema
end
end
Expand Down Expand Up @@ -479,19 +479,19 @@ class Subscription < GraphQL::Schema::Object
custom_subscription = Class.new(PrinterTestSchema::Subscription) { graphql_name "MySubscriptionRoot" }
custom_schema = Class.new(PrinterTestSchema) { subscription(custom_subscription) }

expected = <<SCHEMA
expected = <<GRAPHQL
schema {
query: Query
mutation: Mutation
subscription: MySubscriptionRoot
}
SCHEMA
GRAPHQL

assert_match expected, GraphQL::Schema::Printer.print_schema(custom_schema)
end

it "returns the schema as a string for the defined types" do
expected = <<SCHEMA
expected = <<GRAPHQL
type Audio {
duration: Int!
id: ID!
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