lib/ruby2js.rb in ruby2js-2.1.24 vs lib/ruby2js.rb in ruby2js-3.0.0
- old
+ new
@@ -12,10 +12,29 @@
module Ruby2JS
class SyntaxError < RuntimeError
end
+ @@eslevel_default = 2009 # ecmascript 5
+ @@strict_default = false
+
+ def self.eslevel_default
+ @@eslevel_default
+ end
+
+ def self.eslevel_default=(level)
+ @@eslevel_default = level
+ end
+
+ def self.strict_default
+ @@strict_default
+ end
+
+ def self.strict_default=(level)
+ @@strict_default = level
+ end
+
module Filter
DEFAULTS = []
module SEXP
# construct an AST Node
@@ -38,10 +57,22 @@
def options=(options)
@options = options
end
+ def es2015
+ @options[:eslevel] >= 2015
+ end
+
+ def es2016
+ @options[:eslevel] >= 2016
+ end
+
+ def es2017
+ @options[:eslevel] >= 2017
+ end
+
def process(node)
ast, @ast = @ast, node
replacement = super
if replacement != node and @comments[node]
@@ -52,15 +83,20 @@
ensure
@ast = ast
end
# handle all of the 'invented' ast types
+ def on_async(node); on_def(node); end
+ def on_asyncs(node); on_defs(node); end
def on_attr(node); on_send(node); end
def on_autoreturn(node); on_return(node); end
+ def on_await(node); on_send(node); end
def on_call(node); on_send(node); end
def on_constructor(node); on_def(node); end
def on_defp(node); on_defs(node); end
+ def on_for_of(node); on_for(node); end
+ def on_in?(node); on_send(node); end
def on_method(node); on_send(node); end
def on_prop(node); on_array(node); end
def on_prototype(node); on_begin(node); end
def on_sendw(node); on_send(node); end
def on_undefined?(node); on_defined?(node); end
@@ -86,10 +122,13 @@
end
end
end
def self.convert(source, options={})
+ options[:eslevel] ||= @@eslevel_default
+ options[:strict] = @@strict_default if options[:strict] == nil
+
if Proc === source
file,line = source.source_location
source = File.read(file.dup.untaint).untaint
ast, comments = parse(source)
comments = Parser::Source::Comment.associate(ast, comments) if ast
@@ -117,9 +156,11 @@
ruby2js = Ruby2JS::Converter.new(ast, comments)
ruby2js.binding = options[:binding]
ruby2js.ivars = options[:ivars]
+ ruby2js.eslevel = options[:eslevel]
+ ruby2js.strict = options[:strict]
if ruby2js.binding and not ruby2js.ivars
ruby2js.ivars = ruby2js.binding.eval \
'Hash[instance_variables.map {|var| [var, instance_variable_get(var)]}]'
elsif options[:scope] and not ruby2js.ivars
scope = options.delete(:scope)