Class: Spektr::Targets::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/spektr/targets/base.rb

Direct Known Subclasses

Config, Controller, Model, Routes, View

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, content) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/spektr/targets/base.rb', line 6

def initialize(path, content)
  Spektr.logger.debug "loading #{path}"
  @ast = Spektr::App.parser.parse(content)
  @path = path
  return unless @ast

  @processor = Spektr::Processors::Base.new
  @processor.process(@ast)
  @name = @processor.name
  @name = @path.split('/').last if @name.blank?

  @current_method_type = :public
  @parent = @processor.parent_name
end

Instance Attribute Details

#astObject

Returns the value of attribute ast.



4
5
6
# File 'lib/spektr/targets/base.rb', line 4

def ast
  @ast
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/spektr/targets/base.rb', line 4

def name
  @name
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/spektr/targets/base.rb', line 4

def options
  @options
end

#parentObject

Returns the value of attribute parent.



4
5
6
# File 'lib/spektr/targets/base.rb', line 4

def parent
  @parent
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/spektr/targets/base.rb', line 4

def path
  @path
end

#processorObject

Returns the value of attribute processor.



4
5
6
# File 'lib/spektr/targets/base.rb', line 4

def processor
  @processor
end

Instance Method Details

#ast_to_exp(ast) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/spektr/targets/base.rb', line 97

def ast_to_exp(ast)
  case ast.type
  when :send
    Exp::Send.new(ast)
  when :def
    Exp::Definition.new(ast)
  when :ivasgn, :ivar
    Exp::Ivasgin.new(ast)
  when :lvasign, :lvar
    Exp::Lvasign.new(ast)
  when :const
    Exp::Const.new(ast)
  when :xstr
    Exp::Xstr.new(ast)
  when :sym, :int, :str
    Exp::Base.new(ast)
  else
    raise "Unknown type #{ast.type} #{ast.inspect}"
  end
end

#find(type, name, ast, result = []) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/spektr/targets/base.rb', line 49

def find(type, name, ast, result = [])
  return result unless ast.is_a? Parser::AST::Node

  name_index = case type
               when :def
                 0
               else
                 1
               end
  if node_matches?(ast.type, ast.children[name_index], type, name)
    result << ast
  elsif ast.children.any?
    ast.children.each do |child|
      result = find(type, name, child, result)
    end
  end
  result
end

#find_calls(name, receiver = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/spektr/targets/base.rb', line 21

def find_calls(name, receiver = nil)
  calls = find(:send, name, @ast).map { |ast| Exp::Send.new(ast) }
  if receiver
    calls.select! { |call| call.receiver&.expanded == receiver }
  elsif receiver == false
    calls.select! { |call| call.receiver.nil? }
  end
  calls
end

#find_calls_with_block(name, _receiver = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/spektr/targets/base.rb', line 31

def find_calls_with_block(name, _receiver = nil)
  blocks = find(:block, nil, @ast)
  blocks.each_with_object([]) do |block, memo|
    if block.children.first.children[1] == name
      result = find(:send, name, block).map { |ast| Exp::Send.new(ast) }
      memo << result.first
    end
  end
end

#find_method(name) ⇒ Object



41
42
43
# File 'lib/spektr/targets/base.rb', line 41

def find_method(name)
  find(:def, name, @ast).last
end

#find_methods(ast:, result: [], type: :all) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/spektr/targets/base.rb', line 81

def find_methods(ast:, result: [], type: :all)
  return result unless ast.is_a?(Parser::AST::Node)

  if ast.type == :send && %i[private public protected].include?(ast.children.last)
    @current_method_type = ast.children.last
  end
  if ast.type == :def && [:all, @current_method_type].include?(type)
    result << ast
  elsif ast.children.any?
    ast.children.map do |child|
      result = find_methods(ast: child, result: result, type: type)
    end
  end
  result
end

#find_xstrObject



45
46
47
# File 'lib/spektr/targets/base.rb', line 45

def find_xstr
  find(:xstr, nil, @ast).map { |ast| Exp::Xstr.new(ast) }
end

#node_matches?(node_type, node_name, type, name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/spektr/targets/base.rb', line 68

def node_matches?(node_type, node_name, type, name)
  if node_type == type
    if name.is_a? Regexp
      return node_name =~ name
    elsif name.nil?
      return true
    else
      return node_name == name
    end
  end
  false
end