Class: RstFilter::RecordAll

Inherits:
Parser::TreeRewriter
  • Object
show all
Defined in:
lib/rstfilter/rewriter.rb

Instance Method Summary collapse

Constructor Details

#initialize(opt) ⇒ RecordAll

Returns a new instance of RecordAll.



7
8
9
10
11
# File 'lib/rstfilter/rewriter.rb', line 7

def initialize opt
  @decl = opt.show_decl || (opt.show_all_results == false)

  super()
end

Instance Method Details

#add_paren(node) ⇒ Object



21
22
23
24
25
26
# File 'lib/rstfilter/rewriter.rb', line 21

def add_paren node
  if le = node&.location&.expression
    insert_before(le.begin, '(')
    insert_after(le.end, ")")
  end
end

#add_record(node) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rstfilter/rewriter.rb', line 13

def add_record node
  if le = node&.location&.expression
    pos = [le.begin.line, le.begin.column, le.end.line, le.end.column].join(',')
    insert_before(le.begin, "::RSTFILTER__.record(#{pos}){")
    insert_after(le.end, "}")
  end
end

#on_block(node) ⇒ Object



138
139
140
141
# File 'lib/rstfilter/rewriter.rb', line 138

def on_block node
  _send, _args, block = *node.children
  process block
end

#on_class(node) ⇒ Object



73
74
75
76
77
# File 'lib/rstfilter/rewriter.rb', line 73

def on_class node
  _name, sup, body = node.children
  process sup
  process body
end

#on_const(node) ⇒ Object



63
64
# File 'lib/rstfilter/rewriter.rb', line 63

def on_const node
end

#on_def(node) ⇒ Object



97
98
99
100
101
# File 'lib/rstfilter/rewriter.rb', line 97

def on_def node
  _name, args, body = node.children
  process_args args
  process body
end

#on_defs(node) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/rstfilter/rewriter.rb', line 103

def on_defs node
  recv, _name, args, body = node.children
  process recv
  add_paren recv
  process_args args
  process body
end

#on_dstr(node) ⇒ Object



57
58
# File 'lib/rstfilter/rewriter.rb', line 57

def on_dstr node
end

#on_hash(node) ⇒ Object



121
122
123
# File 'lib/rstfilter/rewriter.rb', line 121

def on_hash node
  process_pairs node.children
end

#on_masgn(node) ⇒ Object



66
67
68
69
70
71
# File 'lib/rstfilter/rewriter.rb', line 66

def on_masgn node
  _mlhs, rhs = node.children
  if rhs.type == :array
    rhs.children.each{|r| process r}
  end
end

#on_module(node) ⇒ Object



79
80
81
82
# File 'lib/rstfilter/rewriter.rb', line 79

def on_module node
  _name, body = node.children
  process body
end

#on_numblock(node) ⇒ Object



143
144
145
# File 'lib/rstfilter/rewriter.rb', line 143

def on_numblock node
  on_block node
end

#on_regexp(node) ⇒ Object



60
61
# File 'lib/rstfilter/rewriter.rb', line 60

def on_regexp node
end

#on_send(node) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rstfilter/rewriter.rb', line 125

def on_send node
  recv, _name, *args = *node.children
  process recv if recv

  args.each{|arg|
    if arg.type == :hash
      process_pairs arg.children
    else
      process arg
    end
  }
end

#process(node) ⇒ Object



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
54
55
# File 'lib/rstfilter/rewriter.rb', line 28

def process node
  return unless node

  super

  case node.type
  when :begin,
       :resbody, :rescue,
       :ensure,
       :return,
       :next,
       :redo,
       :retry,
       :splat,
       :block_pass,
       :lvasgn,
       :when
    # skip
  when :def, :class
    add_record node if @decl
  when :if
    unless node.loc.expression.source.start_with? 'elsif'
      add_record node
    end
  else
    add_record node
  end
end

#process_args(args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rstfilter/rewriter.rb', line 84

def process_args args
  args.children.each{|arg|
    case arg.type
    when :optarg
      _name, opexpr = arg.children
      process opexpr
    when :kwoptarg
      _name, kwexpr = arg.children
      process kwexpr
    end
  }
end

#process_pairs(pairs) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/rstfilter/rewriter.rb', line 111

def process_pairs pairs
  pairs.each{|pair|
    key, val = pair.children
    if key.type != :sym
      process key
    end
    process val
  }
end