Class: GraphQL::Upgrader::InterfacesToImplementsTransform

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

Overview

Transform interfaces [A, B, C] to implements A\nimplements B\nimplements C\n

Constant Summary

PATTERN =
/(?<indent>\s*)(?:interfaces) \[\s*(?<interfaces>(?:[a-zA-Z_0-9:\.,\s]+))\]/m

Instance Method Summary collapse

Methods inherited from Transform

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

Instance Method Details

#apply(input_text) ⇒ Object



601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/graphql/upgrader/member.rb', line 601

def apply(input_text)
  input_text.gsub(PATTERN) do
    indent = $~[:indent]
    interfaces = $~[:interfaces].split(',').map(&:strip).reject(&:empty?)
    # Preserve leading newlines before the `interfaces ...`
    # call, but don't re-insert them between `implements` calls.
    extra_leading_newlines = "\n" * (indent[/^\n*/].length - 1)
    indent = indent.sub(/^\n*/m, "")
    interfaces_calls = interfaces
      .map { |interface| "\n#{indent}implements #{interface}" }
      .join
    extra_leading_newlines + interfaces_calls
  end
end