Module: Reek::AST::SexpExtensions::CasgnNode

Includes:
ConstantDefiningNodeBase
Defined in:
lib/reek/ast/sexp_extensions/module.rb

Overview

Utility methods for constant assignment (:casgn) nodes.

Instance Method Summary collapse

Methods included from ConstantDefiningNodeBase

#full_name, #simple_name

Instance Method Details

#constant_definitionObject (private)

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



178
179
180
181
182
183
184
185
186
187
188
# 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

#defines_module?Boolean

Returns:

  • (Boolean)


103
104
105
106
# File 'lib/reek/ast/sexp_extensions/module.rb', line 103

def defines_module?
  call = constant_definition
  call && call.module_creation_call?
end

#nameObject



129
130
131
# File 'lib/reek/ast/sexp_extensions/module.rb', line 129

def name
  children[1].to_s
end

#superclassObject

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)



123
124
125
126
127
# 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

#valueObject

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)))


143
144
145
# File 'lib/reek/ast/sexp_extensions/module.rb', line 143

def value
  children[2]
end