Class: YARD::Handlers::Ruby::Legacy::ClassConditionHandler

Inherits:
Base
  • Object
show all
Defined in:
lib/yard/handlers/ruby/legacy/class_condition_handler.rb

Overview

Matches if/unless conditions inside classes and attempts to process only one branch (by evaluating the condition if possible).

Examples:

A simple class conditional

class Foo
  if 0
    # This method is ignored
    def xyz; end
  end
end

Since:

  • 0.5.4

Constant Summary

Constant Summary

Constants included from Parser::Ruby::Legacy::RubyToken

Parser::Ruby::Legacy::RubyToken::EXPR_ARG, Parser::Ruby::Legacy::RubyToken::EXPR_BEG, Parser::Ruby::Legacy::RubyToken::EXPR_CLASS, Parser::Ruby::Legacy::RubyToken::EXPR_DOT, Parser::Ruby::Legacy::RubyToken::EXPR_END, Parser::Ruby::Legacy::RubyToken::EXPR_FNAME, Parser::Ruby::Legacy::RubyToken::EXPR_MID, Parser::Ruby::Legacy::RubyToken::NEWLINE_TOKEN, Parser::Ruby::Legacy::RubyToken::TkReading2Token, Parser::Ruby::Legacy::RubyToken::TkSymbol2Token

Constants included from CodeObjects

CodeObjects::BUILTIN_ALL, CodeObjects::BUILTIN_CLASSES, CodeObjects::BUILTIN_EXCEPTIONS, CodeObjects::BUILTIN_EXCEPTIONS_HASH, CodeObjects::BUILTIN_MODULES, CodeObjects::CONSTANTMATCH, CodeObjects::CONSTANTSTART, CodeObjects::CSEP, CodeObjects::CSEPQ, CodeObjects::ISEP, CodeObjects::ISEPQ, CodeObjects::METHODMATCH, CodeObjects::METHODNAMEMATCH, CodeObjects::NAMESPACEMATCH, CodeObjects::NSEP, CodeObjects::NSEPQ

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from YARD::Handlers::Base

Instance Attribute Details

#extra_stateObject (readonly) Originally defined in class Base

Returns the value of attribute extra_state

#globalsObject (readonly) Originally defined in class Base

Returns the value of attribute globals

#namespaceObject Originally defined in class Base

Returns the value of attribute namespace

#ownerObject Originally defined in class Base

Returns the value of attribute owner

#parserProcessor (readonly) Originally defined in class Base

Returns the processor object that manages all global state during handling.

Returns:

  • (Processor)

    the processor object that manages all global state during handling.

#scopeObject Originally defined in class Base

Returns the value of attribute scope

#statementObject (readonly) Originally defined in class Base

Returns the statement object currently being processed. Usually refers to one semantic language statement, though the strict definition depends on the parser used.

Returns:

  • (Object)

    the statement object currently being processed. Usually refers to one semantic language statement, though the strict definition depends on the parser used.

#visibilityObject Originally defined in class Base

Returns the value of attribute visibility

Instance Method Details

#parse_conditiontrue, ... (protected)

Parses the condition part of the if/unless statement

Returns:

  • (true, false, nil)

    true if the condition can be definitely parsed to true, false if not, and nil if the condition cannot be parsed with certainty (it's dynamic)

Since:

  • 0.5.5



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
56
57
58
59
60
61
62
63
64
# File 'lib/yard/handlers/ruby/legacy/class_condition_handler.rb', line 28

def parse_condition
  condition = nil

  # Right now we can handle very simple unary conditions like:
  #   if true
  #   if false
  #   if 0
  #   if 100 (not 0)
  #   if defined? SOME_CONSTANT
  #
  # The last case will do a lookup in the registry and then one
  # in the Ruby world (using eval).
  case statement.tokens[1..-1].to_s.strip
  when /^(\d+)$/
    condition = $1 != "0"
  when /^defined\?\s*\(?(.+?)\)?$/
    # defined? keyword used, let's see if we can look up the name
    # in the registry, then we'll try using Ruby's powers. eval() is not
    # *too* dangerous here since code is not actually executed.
    name = $1
    obj = YARD::Registry.resolve(namespace, name, true)
    begin
      condition = true if obj || Object.instance_eval("defined? #{name}")
    rescue SyntaxError, NameError
      condition = false
    end
  when "true"
    condition = true
  when "false"
    condition = false
  end

  if TkUNLESS === statement.tokens.first
    condition = !condition if condition != nil
  end
  condition
end

#parse_else_blockObject (protected)

Since:

  • 0.5.5



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/yard/handlers/ruby/legacy/class_condition_handler.rb', line 72

def parse_else_block
  return unless statement.block
  stmtlist = YARD::Parser::Ruby::Legacy::StatementList
  stmtlist.new(statement.block).each do |stmt|
    if TkELSE === stmt.tokens.first
      push_state(:visibility => visibility) do
        parser.process(stmtlist.new(stmt.block))
      end
    end
  end
end

#parse_then_blockObject (protected)

Since:

  • 0.5.5



67
68
69
# File 'lib/yard/handlers/ruby/legacy/class_condition_handler.rb', line 67

def parse_then_block
  parse_block(:visibility => visibility)
end

#processvoid

This method returns an undefined value.

Main processing callback



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/yard/handlers/ruby/legacy/class_condition_handler.rb', line 7

process do
  condition = parse_condition
  if condition == nil
    # Parse both blocks if we're unsure of the condition
    parse_then_block
    parse_else_block
  elsif condition
    parse_then_block
  else
    parse_else_block
  end
end