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



246
247
248
249
# File 'lib/graphql/upgrader/member.rb', line 246

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



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/graphql/upgrader/member.rb', line 251

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