Skip to content

Commit

Permalink
add config option "Prefixes" for ExtractType cop
Browse files Browse the repository at this point in the history
  • Loading branch information
0legovich committed Jun 17, 2020
1 parent a112b23 commit d71e1c0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ GraphQL/ExtractType:
VersionAdded: '0.80'
Description: 'Checks fields on common prefix groups'
MaxFields: 2
Prefixes:
- is
- with
- avg
- min
- max
6 changes: 6 additions & 0 deletions lib/rubocop/cop/graphql/extract_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def fractured(body)
next unless field.underscore_name.include?("_")

prefix = field.underscore_name.split("_").first
next if good_prefix?(prefix)

acc[prefix] ||= []
acc[prefix] << field
end
Expand All @@ -74,6 +76,10 @@ def fractured(body)
def message(prefix, field_names)
format(MSG, field_names: field_names, prefix: prefix)
end

def good_prefix?(word)
cop_config["Prefixes"].include?(word)
end
end
end
end
Expand Down
16 changes: 15 additions & 1 deletion spec/rubocop/cop/graphql/extract_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
let(:config) do
RuboCop::Config.new(
"GraphQL/ExtractType" => {
"MaxFields" => 2
"MaxFields" => 2,
"Prefixes" => %w[is avg min max]
}
)
end
Expand Down Expand Up @@ -75,6 +76,19 @@ class UserType < BaseType
RUBY
end
end

context "when common prefix include in config Prefixes" do
it "not registers an offense" do
expect_no_offenses(<<~RUBY)
class UserType < BaseType
field :registered_at, String, null: false
field :is_admin, String, null: false
field :is_director, String, null: false
end
RUBY
end
end
end

context "when count fields with common prefix more than Max fields" do
Expand Down

0 comments on commit d71e1c0

Please sign in to comment.