Class: GraphQL::Upgrader::RemoveRedundantKwargTransform

Inherits:
Transform
  • Object
show all
Defined in:
lib/graphql/upgrader/member.rb

Overview

Find a keyword whose value is a string or symbol, and if the value is equivalent to the field name, remove the keyword altogether.

Instance Method Summary collapse

Methods inherited from Transform

#apply_processor, #normalize_type_expression, #reindent_lines, #trim_lines, #underscorize

Constructor Details

#initialize(kwarg:) ⇒ RemoveRedundantKwargTransform

Returns a new instance of RemoveRedundantKwargTransform



242
243
244
245
# File 'lib/graphql/upgrader/member.rb', line 242

def initialize(kwarg:)
  @kwarg = kwarg
  @finder_pattern = /(field|return_field|input_field|connection|argument) :(?<name>[a-zA-Z_0-9]*).*#{@kwarg}: ['":](?<kwarg_value>[a-zA-Z_0-9?!]+)['"]?/
end

Instance Method Details

#apply(input_text) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/graphql/upgrader/member.rb', line 247

def apply(input_text)
  if input_text =~ @finder_pattern
    field_name = $~[:name]
    kwarg_value = $~[:kwarg_value]
    if field_name == kwarg_value
      # It's redundant, remove it
      input_text = input_text.sub(/, #{@kwarg}: ['":]#{kwarg_value}['"]?/, "")
    end
  end
  input_text
end