module Reek::AST::SexpExtensions::CasgnNode
Utility methods for constant assignment (:casgn) nodes.
Public Instance Methods
Source
# File lib/reek/ast/sexp_extensions/module.rb, line 103 def defines_module? call = constant_definition call && call.module_creation_call? end
Source
# File lib/reek/ast/sexp_extensions/module.rb, line 129 def name children[1].to_s end
Source
# File lib/reek/ast/sexp_extensions/module.rb, line 123 def superclass return nil unless defines_module? constant_definition.args.first if constant_definition.receiver.name == 'Class' end
Sometimes we assign classes like:
Foo = Class.new(Bar)
This is mapped into the following expression:
s(:casgn, nil :Foo,
s(:send, s(:const, nil, :Class), :new, s(:const, nil, :Bar) )
)
And we are only looking for s(:const, nil, :Bar)
Source
# File lib/reek/ast/sexp_extensions/module.rb, line 143 def value children[2] end
there are two valid forms of the casgn sexp (casgn <namespace> <name> <value>) and (casgn <namespace> <name>) used in or-asgn and mlhs
source = “class Hi; THIS ||= 3; end” (class
(const nil :Hi) nil (or-asgn (casgn nil :THIS) (int 3)))
Private Instance Methods
Source
# File lib/reek/ast/sexp_extensions/module.rb, line 178 def constant_definition return nil unless value case value.type when :block call = value.call call if call.type == :send when :send value end end
This is the right hand side of a constant assignment.
This can be simple:
Foo = 23
s(:casgn, nil, :Foo,
s(:int, 23))
In this cases we do not care and return nil.
Or complicated:
Iterator = Struct.new :exp do … end
s(:casgn, nil, :Iterator,
s(:block, s(:send, s(:const, nil, :Struct), :new, s(:sym, :exp) ), s(:args), ... )
)
In this cases we return the Struct.new part