Class: Rails5::SpecConverter::TextTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/rails5/spec_converter/text_transformer.rb

Instance Method Summary collapse

Constructor Details

#initialize(content, options = TextTransformerOptions.new) ⇒ TextTransformer

Returns a new instance of TextTransformer.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rails5/spec_converter/text_transformer.rb', line 12

def initialize(content, options = TextTransformerOptions.new)
  @options = options
  @content = content

  @source_buffer = Parser::Source::Buffer.new('(string)')
  @source_buffer.source = @content

  ast_builder = Astrolabe::Builder.new
  @parser = Parser::CurrentRuby.new(ast_builder)

  @source_rewriter = Parser::Source::Rewriter.new(@source_buffer)
end

Instance Method Details

#transformObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rails5/spec_converter/text_transformer.rb', line 25

def transform
  root_node = @parser.parse(@source_buffer)
  root_node.each_node(:send) do |node|
    target, verb, action, *args = node.children
    next unless args.length > 0
    next unless target.nil? && HTTP_VERBS.include?(verb)

    if args[0].hash_type? && args[0].children.length == 0
      wrap_arg(args[0], 'params')
    elsif args[0].hash_type?
      next if looks_like_route_definition?(args[0])
      next if has_kwsplat?(args[0])
      next if has_key?(args[0], :params)

      write_params_hash(
        hash_node: args[0],
        original_indent: node.loc.expression.source_line.match(/^(\s*)/)[1]
      )
    else
      warn_about_ambiguous_params(node) if @options.warn_about_ambiguous_params?
      wrap_arg(args[0], 'params') if @options.wrap_ambiguous_params?
    end

    wrap_arg(args[1], 'headers') if args[1]
  end

  @source_rewriter.process
end