Class: Reek::CodeParser

Inherits:
Object show all
Defined in:
lib/reek/code_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report, smells, ctx = StopContext.new) ⇒ CodeParser

Creates a new Ruby code checker. Any smells discovered will be stored in report.



32
33
34
35
36
# File 'lib/reek/code_parser.rb', line 32

def initialize(report, smells, ctx = StopContext.new)
  @report = report
  @smells = smells
  @element = ctx
end

Class Method Details

.count_statements(exp) ⇒ Object



191
192
193
194
195
196
# File 'lib/reek/code_parser.rb', line 191

def self.count_statements(exp)
  stmts = exp[1..-1]
  ignore = 0
  ignore += 1 if stmts[1] == s(:nil)
  stmts.length - ignore
end

Instance Method Details

#count_clause(sexp) ⇒ Object



185
186
187
188
189
# File 'lib/reek/code_parser.rb', line 185

def count_clause(sexp)
  if sexp and !sexp.has_type?(:block)
    @element.count_statements(1)
  end
end

#process(exp) ⇒ Object



38
39
40
41
42
43
# File 'lib/reek/code_parser.rb', line 38

def process(exp)
  meth = "process_#{exp[0]}"
  meth = :process_default unless self.respond_to?(meth)
  self.send(meth, exp)
  @element
end

#process_args(exp) ⇒ Object



71
72
73
# File 'lib/reek/code_parser.rb', line 71

def process_args(exp)
  exp[1..-1].each {|sym| @element.record_parameter(sym) }
end

#process_attrasgn(exp) ⇒ Object



112
113
114
# File 'lib/reek/code_parser.rb', line 112

def process_attrasgn(exp)
  process_call(exp)
end

#process_attrset(exp) ⇒ Object



75
76
77
# File 'lib/reek/code_parser.rb', line 75

def process_attrset(exp)
  @element.record_depends_on_self if /^@/ === exp[1].to_s
end

#process_block(exp) ⇒ Object



94
95
96
97
# File 'lib/reek/code_parser.rb', line 94

def process_block(exp)
  @element.count_statements(CodeParser.count_statements(exp))
  process_default(exp)
end

#process_call(exp) ⇒ Object



103
104
105
106
# File 'lib/reek/code_parser.rb', line 103

def process_call(exp)
  @element.record_call_to(exp)
  process_default(exp)
end

#process_case(exp) ⇒ Object



156
157
158
159
# File 'lib/reek/code_parser.rb', line 156

def process_case(exp)
  process_default(exp)
  @element.count_statements(-1)
end

#process_cfunc(exp) ⇒ Object



108
109
110
# File 'lib/reek/code_parser.rb', line 108

def process_cfunc(exp)
  @element.record_depends_on_self
end

#process_class(exp) ⇒ Object



56
57
58
59
60
61
# File 'lib/reek/code_parser.rb', line 56

def process_class(exp)
  push(ClassContext.create(@element, exp)) do
    process_default(exp) unless @element.is_struct?
    check_smells(:class)
  end
end

#process_dasgn_curr(exp) ⇒ Object



89
90
91
92
# File 'lib/reek/code_parser.rb', line 89

def process_dasgn_curr(exp)
  @element.record_parameter(exp[1])
  process_default(exp)
end

#process_default(exp) ⇒ Object



45
46
47
# File 'lib/reek/code_parser.rb', line 45

def process_default(exp)
  exp[0..-1].each { |sub| process(sub) if Array === sub }
end

#process_defn(exp) ⇒ Object



63
64
65
# File 'lib/reek/code_parser.rb', line 63

def process_defn(exp)
  handle_context(MethodContext, :defn, exp)
end

#process_defs(exp) ⇒ Object



67
68
69
# File 'lib/reek/code_parser.rb', line 67

def process_defs(exp)
  handle_context(SingletonMethodContext, :defs, exp)
end

#process_for(exp) ⇒ Object



139
140
141
142
143
# File 'lib/reek/code_parser.rb', line 139

def process_for(exp)
  count_clause(exp[3])
  process_default(exp)
  @element.count_statements(-1)
end

#process_iasgn(exp) ⇒ Object



175
176
177
178
179
# File 'lib/reek/code_parser.rb', line 175

def process_iasgn(exp)
  @element.record_instance_variable(exp[1])
  @element.record_depends_on_self
  process_default(exp)
end

#process_if(exp) ⇒ Object



120
121
122
123
124
125
# File 'lib/reek/code_parser.rb', line 120

def process_if(exp)
  count_clause(exp[2])
  count_clause(exp[3])
  handle_context(IfContext, :if, exp)
  @element.count_statements(-1)
end

#process_iter(exp) ⇒ Object



84
85
86
87
# File 'lib/reek/code_parser.rb', line 84

def process_iter(exp)
  process(exp[1])
  handle_context(BlockContext, :iter, exp[2..-1])
end

#process_ivar(exp) ⇒ Object



166
167
168
# File 'lib/reek/code_parser.rb', line 166

def process_ivar(exp)
  process_iasgn(exp)
end

#process_lasgn(exp) ⇒ Object



170
171
172
173
# File 'lib/reek/code_parser.rb', line 170

def process_lasgn(exp)
  @element.record_local_variable(exp[1])
  process_default(exp)
end

#process_lit(exp) ⇒ Object



79
80
81
82
# File 'lib/reek/code_parser.rb', line 79

def process_lit(exp)
  val = exp[1]
  @element.record_depends_on_self if val == :self
end

#process_module(exp) ⇒ Object



49
50
51
52
53
54
# File 'lib/reek/code_parser.rb', line 49

def process_module(exp)
  push(ModuleContext.create(@element, exp)) do
    process_default(exp)
    check_smells(:module)
  end
end

#process_op_asgn1(exp) ⇒ Object



116
117
118
# File 'lib/reek/code_parser.rb', line 116

def process_op_asgn1(exp)
  process_call(exp)
end

#process_resbody(exp) ⇒ Object



151
152
153
154
# File 'lib/reek/code_parser.rb', line 151

def process_resbody(exp)
  count_clause(exp[2])
  process_default(exp)
end

#process_rescue(exp) ⇒ Object



145
146
147
148
149
# File 'lib/reek/code_parser.rb', line 145

def process_rescue(exp)
  count_clause(exp[1])
  process_default(exp)
  @element.count_statements(-1)
end

#process_self(exp) ⇒ Object



181
182
183
# File 'lib/reek/code_parser.rb', line 181

def process_self(exp)
  @element.record_depends_on_self
end

#process_until(exp) ⇒ Object



133
134
135
136
137
# File 'lib/reek/code_parser.rb', line 133

def process_until(exp)
  count_clause(exp[2])
  process_default(exp)
  @element.count_statements(-1)
end

#process_when(exp) ⇒ Object



161
162
163
164
# File 'lib/reek/code_parser.rb', line 161

def process_when(exp)
  count_clause(exp[2])
  process_default(exp)
end

#process_while(exp) ⇒ Object



127
128
129
130
131
# File 'lib/reek/code_parser.rb', line 127

def process_while(exp)
  count_clause(exp[2])
  process_default(exp)
  @element.count_statements(-1)
end

#process_yield(exp) ⇒ Object



99
100
101
# File 'lib/reek/code_parser.rb', line 99

def process_yield(exp)
  handle_context(YieldCallContext, :yield, exp)
end