Class: Ruby2JS::Filter::Processor

Inherits:
Parser::AST::Processor
  • Object
show all
Includes:
Ruby2JS::Filter
Defined in:
lib/ruby2js.rb

Constant Summary collapse

BINARY_OPERATORS =
Converter::OPERATORS[2..-1].flatten

Constants included from Ruby2JS::Filter

DEFAULTS

Instance Method Summary collapse

Methods included from Ruby2JS::Filter

exclude, #exclude, #excluded?, include, #include, include_all, #include_all, #include_only, include_only

Constructor Details

#initialize(comments) ⇒ Processor



55
56
57
58
59
# File 'lib/ruby2js.rb', line 55

def initialize(comments)
  @comments = comments
  @ast = nil
  @exclude_methods = []
end

Instance Method Details

#es2015Object



73
74
75
# File 'lib/ruby2js.rb', line 73

def es2015
  @options[:eslevel] >= 2015
end

#es2016Object



77
78
79
# File 'lib/ruby2js.rb', line 77

def es2016
  @options[:eslevel] >= 2016
end

#es2017Object



81
82
83
# File 'lib/ruby2js.rb', line 81

def es2017
  @options[:eslevel] >= 2017
end

#es2018Object



85
86
87
# File 'lib/ruby2js.rb', line 85

def es2018
  @options[:eslevel] >= 2018
end

#es2019Object



89
90
91
# File 'lib/ruby2js.rb', line 89

def es2019
  @options[:eslevel] >= 2019
end

#es2020Object



93
94
95
# File 'lib/ruby2js.rb', line 93

def es2020
  @options[:eslevel] >= 2020
end

#es2021Object



97
98
99
# File 'lib/ruby2js.rb', line 97

def es2021
  @options[:eslevel] >= 2021
end

#on_async(node) ⇒ Object

handle all of the ‘invented/synthetic’ ast types



115
# File 'lib/ruby2js.rb', line 115

def on_async(node); on_def(node); end

#on_asyncs(node) ⇒ Object



116
# File 'lib/ruby2js.rb', line 116

def on_asyncs(node); on_defs(node); end

#on_attr(node) ⇒ Object



117
# File 'lib/ruby2js.rb', line 117

def on_attr(node); on_send(node); end

#on_autoreturn(node) ⇒ Object



118
# File 'lib/ruby2js.rb', line 118

def on_autoreturn(node); on_return(node); end

#on_await(node) ⇒ Object



119
# File 'lib/ruby2js.rb', line 119

def on_await(node); on_send(node); end

#on_call(node) ⇒ Object



120
# File 'lib/ruby2js.rb', line 120

def on_call(node); on_send(node); end

#on_class_extend(node) ⇒ Object



121
# File 'lib/ruby2js.rb', line 121

def on_class_extend(node); on_send(node); end

#on_class_module(node) ⇒ Object



122
# File 'lib/ruby2js.rb', line 122

def on_class_module(node); on_send(node); end

#on_constructor(node) ⇒ Object



123
# File 'lib/ruby2js.rb', line 123

def on_constructor(node); on_def(node); end

#on_defm(node) ⇒ Object



124
# File 'lib/ruby2js.rb', line 124

def on_defm(node); on_defs(node); end

#on_defp(node) ⇒ Object



125
# File 'lib/ruby2js.rb', line 125

def on_defp(node); on_defs(node); end

#on_export(node) ⇒ Object



135
# File 'lib/ruby2js.rb', line 135

def on_export(node); end

#on_for_of(node) ⇒ Object



126
# File 'lib/ruby2js.rb', line 126

def on_for_of(node); on_for(node); end

#on_import(node) ⇒ Object



136
# File 'lib/ruby2js.rb', line 136

def on_import(node); end

#on_in?(node) ⇒ Boolean



127
# File 'lib/ruby2js.rb', line 127

def on_in?(node); on_send(node); end

#on_method(node) ⇒ Object



128
# File 'lib/ruby2js.rb', line 128

def on_method(node); on_send(node); end

#on_nil(node) ⇒ Object



133
# File 'lib/ruby2js.rb', line 133

def on_nil(node); end

#on_prop(node) ⇒ Object



129
# File 'lib/ruby2js.rb', line 129

def on_prop(node); on_array(node); end

#on_prototype(node) ⇒ Object



130
# File 'lib/ruby2js.rb', line 130

def on_prototype(node); on_begin(node); end

#on_send(node) ⇒ Object

convert map(&:symbol) to a block



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ruby2js.rb', line 142

def on_send(node)
  if node.children.length > 2 and node.children.last.type == :block_pass
    method = node.children.last.children.first.children.last
    if BINARY_OPERATORS.include? method
      return on_block s(:block, s(:send, *node.children[0..-2]),
        s(:args, s(:arg, :a), s(:arg, :b)), s(:return,
        process(s(:send, s(:lvar, :a), method, s(:lvar, :b)))))
    else
      return on_block s(:block, s(:send, *node.children[0..-2]),
        s(:args, s(:arg, :item)), s(:return,
        process(s(:attr, s(:lvar, :item), method))))
    end
  end
  super
end

#on_sendw(node) ⇒ Object



131
# File 'lib/ruby2js.rb', line 131

def on_sendw(node); on_send(node); end

#on_sym(node) ⇒ Object

provide a method so filters can call ‘super’



139
# File 'lib/ruby2js.rb', line 139

def on_sym(node); node; end

#on_undefined?(node) ⇒ Boolean



132
# File 'lib/ruby2js.rb', line 132

def on_undefined?(node); on_defined?(node); end

#on_xnode(node) ⇒ Object



134
# File 'lib/ruby2js.rb', line 134

def on_xnode(node); end

#options=(options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby2js.rb', line 61

def options=(options)
  @options = options

  @included = @@included
  @excluded = @@excluded

  include_all if options[:include_all]
  include_only(options[:include_only]) if options[:include_only]
  include(options[:include]) if options[:include]
  exclude(options[:exclude]) if options[:exclude]
end

#process(node) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ruby2js.rb', line 101

def process(node)
  ast, @ast = @ast, node
  replacement = super

  if replacement != node and @comments[node]
    @comments[replacement] = @comments[node]
  end

  replacement
ensure
  @ast = ast
end