lib/ruby2js/converter/class2.rb in ruby2js-3.2.0 vs lib/ruby2js/converter/class2.rb in ruby2js-3.3.0

- old
+ new

@@ -54,14 +54,34 @@ cvars = Set.new # find ivars and cvars walk = proc do |ast| ivars << ast.children.first if ast.type === :ivar + ivars << ast.children.first if ast.type === :ivasgn cvars << ast.children.first if ast.type === :cvar + cvars << ast.children.first if ast.type === :cvasgn + ast.children.each do |child| walk[child] if child.is_a? Parser::AST::Node end + + if ast.type == :send and ast.children.first == nil + if ast.children[1] == :attr_accessor + ast.children[2..-1].each_with_index do |child_sym, index2| + ivars << :"@#{child_sym.children.first}" + end + elsif ast.children[1] == :attr_reader + ast.children[2..-1].each_with_index do |child_sym, index2| + ivars << :"@#{child_sym.children.first}" + end + elsif ast.children[1] == :attr_writer + ast.children[2..-1].each_with_index do |child_sym, index2| + ivars << :"@#{child_sym.children.first}" + end + end + end + end walk[@ast] # process leading initializers in constructor while constructor.length == 1 and constructor.first.type == :begin @@ -75,11 +95,11 @@ end end cvars.to_a.sort.each do |cvar| put(index == 0 ? @nl : @sep) index += 1 - put 'static #' + cvar.to_s[2..-1] + put 'static #$' + cvar.to_s[2..-1] end while constructor.length > 0 and constructor.first.type == :ivasgn put(index == 0 ? @nl : @sep) index += 1 @@ -129,11 +149,11 @@ if constructor == [] or constructor == [(:super)] skipped = true next end - m = m.updated(m.type, [@prop, m.children[1], *constructor]) + m = m.updated(m.type, [@prop, m.children[1], s(:begin, *constructor)]) elsif not m.is_method? @prop = "get #{@prop}" m = m.updated(m.type, [*m.children[0..1], s(:autoreturn, m.children[2])]) elsif @prop.to_s.end_with? '=' @@ -145,16 +165,17 @@ m = m.updated(m.type, [@prop, *m.children[1..2]]) end begin @instance_method = m + @class_method = nil parse m # unless skipped ensure @instance_method = nil end - elsif + elsif \ [:defs, :asyncs].include? m.type and m.children.first.type == :self then @prop = "static #{m.children[1]}" if not m.is_method? @@ -170,31 +191,39 @@ end @prop.sub! 'static', 'static async' if m.type == :asyncs m = m.updated(:def, m.children[1..3]) - parse m + begin + @instance_method = nil + @class_method = m + parse m # unless skipped + ensure + @instance_method = nil + end elsif m.type == :send and m.children.first == nil + p = es2020 ? '#' : '_' + if m.children[1] == :attr_accessor m.children[2..-1].each_with_index do |child_sym, index2| put @sep unless index2 == 0 var = child_sym.children.first - put "get #{var}() {#{@nl}return this._#{var}#@nl}#@sep" - put "set #{var}(#{var}) {#{@nl}this._#{var} = #{var}#@nl}" + put "get #{var}() {#{@nl}return this.#{p}#{var}#@nl}#@sep" + put "set #{var}(#{var}) {#{@nl}this.#{p}#{var} = #{var}#@nl}" end elsif m.children[1] == :attr_reader m.children[2..-1].each_with_index do |child_sym, index2| put @sep unless index2 == 0 var = child_sym.children.first - put "get #{var}() {#{@nl}return this._#{var}#@nl}" + put "get #{var}() {#{@nl}return this.#{p}#{var}#@nl}" end elsif m.children[1] == :attr_writer m.children[2..-1].each_with_index do |child_sym, index2| put @sep unless index2 == 0 var = child_sym.children.first - put "set #{var}(#{var}) {#{@nl}this._#{var} = #{var}#@nl}" + put "set #{var}(#{var}) {#{@nl}this.#{p}#{var} = #{var}#@nl}" end elsif [:private, :protected, :public].include? m.children[1] raise Error.new("class #{m.children[1]} is not supported", @ast) else if m.children[1] == :include @@ -206,10 +235,10 @@ skipped = true end else if m.type == :cvasgn and es2020 - put 'static #'; put m.children[0].to_s[2..-1]; put ' = ' + put 'static #$'; put m.children[0].to_s[2..-1]; put ' = ' parse m.children[1] else skipped = true end