-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a cop to enforce prepare argument as a Symbol or String (#150)
- Loading branch information
Showing
4 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rubocop" | ||
|
||
module RuboCop | ||
module Cop | ||
module GraphQL | ||
# Checks that GraphQL Argument definitions prepare arguments use String or constants | ||
# instead of `prepare: CONST_REF` | ||
# This allows better Sorbet typing. | ||
# | ||
# Preference is given to an input object declaring a `def prepare(...); end` method | ||
# | ||
# @example | ||
# # bad | ||
# PREPARE = ->(input, context) { ... } | ||
# | ||
# argument :input, prepare: PREPARE | ||
# | ||
# # good | ||
# def input_prepare(input); ...; end | ||
# | ||
# argument :input, prepare: :input_prepare | ||
# | ||
class PrepareMethod < RuboCop::Cop::Base | ||
extend AutoCorrector | ||
GENERAL_MSG = "Avoid using prepare lambdas, use prepare: :method_name or "\ | ||
"prepare: \"method_name\" instead." | ||
STRING_MSG = "Avoid using prepare lambdas, use prepare: \"method_name\" instead." | ||
PREFER_STRING_MSG = "Avoid using prepare symbols, use prepare: \"%s\" instead." | ||
SYMBOL_MSG = "Avoid using prepare lambdas, use prepare: :method_name instead." | ||
PREFER_SYMBOL_MSG = "Avoid using prepare string, use prepare: :%s instead." | ||
|
||
ARGUMENT_METHODS = Set[:argument, :public_argument].freeze | ||
|
||
# @!method graphql_argument_with_prepare_block?(node) | ||
def_node_matcher :graphql_argument_with_prepare_block?, <<~PATTERN | ||
(send nil? ARGUMENT_METHODS ... (hash ... (pair (sym :prepare) $({ const | block } ...) ))) | ||
PATTERN | ||
|
||
# @!method prepare_method_string_name?(node) | ||
def_node_matcher :prepare_method_string_name?, <<~PATTERN | ||
(send nil? ARGUMENT_METHODS ... (hash ... (pair (sym :prepare) $(str ...) ))) | ||
PATTERN | ||
|
||
# @!method prepare_method_symbol_name?(node) | ||
def_node_matcher :prepare_method_symbol_name?, <<~PATTERN | ||
(send nil? ARGUMENT_METHODS ... (hash ... (pair (sym :prepare) $(sym ...) ))) | ||
PATTERN | ||
|
||
def on_send(node) | ||
type = cop_config["EnforcedStyle"] | ||
|
||
message = case type | ||
when "symbol" | ||
SYMBOL_MSG | ||
when "string" | ||
STRING_MSG | ||
else | ||
GENERAL_MSG | ||
end | ||
graphql_argument_with_prepare_block?(node) do |prepare_definition| | ||
add_offense(prepare_definition, message: message) | ||
end | ||
|
||
if type == "symbol" | ||
prepare_method_string_name?(node) do |prepare_definition| | ||
method_name = prepare_definition.value | ||
add_offense(prepare_definition, | ||
message: format(PREFER_SYMBOL_MSG, method_name)) do |corrector| | ||
autocorrect(corrector, node, "\"#{method_name}\"", ":#{method_name}") | ||
end | ||
end | ||
elsif type == "string" | ||
prepare_method_symbol_name?(node) do |prepare_definition| | ||
method_name = prepare_definition.value | ||
add_offense(prepare_definition, | ||
message: format(PREFER_STRING_MSG, method_name)) do |corrector| | ||
autocorrect(corrector, node, ":#{method_name}", "\"#{method_name}\"") | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def autocorrect(corrector, node, original_method_name, new_method_name) | ||
new_source = node.source.sub("prepare: #{original_method_name}", | ||
"prepare: #{new_method_name}") | ||
corrector.replace(node, new_source) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::GraphQL::PrepareMethod, :config do | ||
let(:config) do | ||
RuboCop::Config.new( | ||
"GraphQL/PrepareMethod" => { | ||
"EnforcedStyle" => nil | ||
} | ||
) | ||
end | ||
|
||
context "when there is a block" do | ||
it "registers an offense when the block is declared directly" do | ||
expect_offense(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: ->(input, context, type: check_type_from_enum) do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using prepare lambdas, use prepare: :method_name or prepare: "method_name" instead. | ||
PREPARE.call(input, context, type) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it "registers offense when prepare block has a lambda defined via a constant" do | ||
expect_offense(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: PREPARE | ||
^^^^^^^ Avoid using prepare lambdas, use prepare: :method_name or prepare: "method_name" instead. | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "when the prepare argument uses a method name" do | ||
it "no offense when prepare block has a method defined as a symbol" do | ||
expect_no_offenses(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: :prepare_method | ||
end | ||
RUBY | ||
end | ||
|
||
it "no offense when prepare block has a method defined as a string" do | ||
expect_no_offenses(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: "prepare_method" | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "when the EnforcedStyle is string" do | ||
let(:config) do | ||
RuboCop::Config.new( | ||
"GraphQL/PrepareMethod" => { | ||
"EnforcedStyle" => "string" | ||
} | ||
) | ||
end | ||
|
||
it "registers an offense when the block is declared directly" do | ||
expect_offense(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: ->(input, context, type: check_type_from_enum) do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using prepare lambdas, use prepare: "method_name" instead. | ||
PREPARE.call(input, context, type) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it "registers offense when prepare argument has a method defined as a symbol" do | ||
expect_offense(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: :prepare_method | ||
^^^^^^^^^^^^^^^ Avoid using prepare symbols, use prepare: "prepare_method" instead. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: "prepare_method" | ||
end | ||
RUBY | ||
end | ||
|
||
it "no offense when prepare block has a method defined as a string" do | ||
expect_no_offenses(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: "prepare_method" | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context "when the EnforcedStyle is symbol" do | ||
let(:config) do | ||
RuboCop::Config.new( | ||
"GraphQL/PrepareMethod" => { | ||
"EnforcedStyle" => "symbol" | ||
} | ||
) | ||
end | ||
|
||
it "registers an offense when the block is declared directly" do | ||
expect_offense(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: ->(input, context, type: check_type_from_enum) do | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using prepare lambdas, use prepare: :method_name instead. | ||
PREPARE.call(input, context, type) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
it "registers offense when prepare block has a method defined as a string" do | ||
expect_offense(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: "prepare_method" | ||
^^^^^^^^^^^^^^^^ Avoid using prepare string, use prepare: :prepare_method instead. | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: :prepare_method | ||
end | ||
RUBY | ||
end | ||
|
||
it "no offense when prepare block has a method defined as a symbol" do | ||
expect_no_offenses(<<~RUBY) | ||
class CheckAlertSourceUsageCreate | ||
public_argument :input, | ||
Types::CheckAlertSourceUsageCreateInput, | ||
Types.t("check_alert_source_usage_create_input", "self"), | ||
required: true, | ||
prepare: prepare_method | ||
end | ||
RUBY | ||
end | ||
end | ||
end |