Class: UpdateCode

Inherits:
Parser::AST::Processor
  • Object
show all
Includes:
RuboCop::AST::Traversal
Defined in:
app/services/update_code.rb

Overview

require ‘fast’

Defined Under Namespace

Classes: AddMethod, FindMethod, ReplaceCode, ShowCode

Constant Summary collapse

LOCATION_MAP =
{
  Send: :expression,
  MethodDefinition: :name
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(id, code, method) ⇒ Object



6
7
8
# File 'app/services/update_code.rb', line 6

def self.call(id, code, method)
  new.call(id, code, method)
end

Instance Method Details

#call(id, code, method) ⇒ Object



15
16
17
18
19
20
21
22
23
24
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
53
# File 'app/services/update_code.rb', line 15

def call(id, code, method)
  path = Path.find(id)
  code = code.presence || File.read(path.path)

  method = method.to_sym

  # First find all instances
  
  source = RuboCop::ProcessedSource.new(code, 2.7)
  nodes = FindMethod.call(
    source: source,
    name: method,
    method_types: [:def, :send]
  )

  nodes.each do |node|
    # Then look again and change them all one by one
    source = RuboCop::ProcessedSource.new(code, 2.7)
    node = FindMethod.call(
      source: source,
      name: method,
      method_types: [:def, :send]
    ).first

    type = node.loc.class.to_s.split("::").last.to_sym

    ReplaceCode.call(
      code: code,
      node: node,
      location: LOCATION_MAP[type],
      with: 'geoff',
    )
  end

  code

  # Add "after/before" which allows to position
  # AddMethod.call(code: code, pos: 150)
end