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 Attribute Summary collapse

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

Returns a new instance of Processor.



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby2js.rb', line 66

def initialize(comments)
  @comments = comments

  # check if magic comment is present:
  first_comment = @comments.values.first&.map(&:text)&.first
  @disable_autoimports = first_comment&.include?(" autoimports: false")
  @disable_autoexports = first_comment&.include?(" autoexports: false")

  @ast = nil
  @exclude_methods = []
  @prepend_list = Set.new
end

Instance Attribute Details

#disable_autoimportsObject

Returns the value of attribute disable_autoimports.



64
65
66
# File 'lib/ruby2js.rb', line 64

def disable_autoimports
  @disable_autoimports
end

#prepend_listObject

Returns the value of attribute prepend_list.



64
65
66
# File 'lib/ruby2js.rb', line 64

def prepend_list
  @prepend_list
end

Instance Method Details

#es2015Object



91
92
93
# File 'lib/ruby2js.rb', line 91

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

#es2016Object



95
96
97
# File 'lib/ruby2js.rb', line 95

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

#es2017Object



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

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

#es2018Object



103
104
105
# File 'lib/ruby2js.rb', line 103

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

#es2019Object



107
108
109
# File 'lib/ruby2js.rb', line 107

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

#es2020Object



111
112
113
# File 'lib/ruby2js.rb', line 111

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

#es2021Object



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

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

#on_async(node) ⇒ Object

handle all of the ‘invented/synthetic’ ast types



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

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

#on_asyncs(node) ⇒ Object



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

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

#on_attr(node) ⇒ Object



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

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

#on_autoreturn(node) ⇒ Object



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

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

#on_await(node) ⇒ Object



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

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

#on_call(node) ⇒ Object



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

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

#on_class_extend(node) ⇒ Object



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

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

#on_class_module(node) ⇒ Object



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

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

#on_constructor(node) ⇒ Object



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

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

#on_defm(node) ⇒ Object



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

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

#on_defp(node) ⇒ Object



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

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

#on_export(node) ⇒ Object



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

def on_export(node); end

#on_for_of(node) ⇒ Object



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

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

#on_import(node) ⇒ Object



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

def on_import(node); end

#on_in?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#on_method(node) ⇒ Object



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

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

#on_nil(node) ⇒ Object



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

def on_nil(node); end

#on_prop(node) ⇒ Object



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

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

#on_prototype(node) ⇒ Object



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

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

#on_send(node) ⇒ Object

convert map(&:symbol) to a block



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ruby2js.rb', line 162

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_send!(node) ⇒ Object



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

def on_send!(node); on_send(node); end

#on_sendw(node) ⇒ Object



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

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

#on_sym(node) ⇒ Object

provide a method so filters can call ‘super’



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

def on_sym(node); node; end

#on_taglit(node) ⇒ Object



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

def on_taglit(node); end

#on_undefined?(node) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#on_xnode(node) ⇒ Object



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

def on_xnode(node); end

#options=(options) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ruby2js.rb', line 79

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



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ruby2js.rb', line 119

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