Sha256: 1b8332e94086fdf554aa540fce3197f5872abed51012e789d8e902a8683c525f

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'parser/current'
require 'ruby2js/converter'

module Ruby2JS
  module Filter
    DEFAULTS = []
  end

  def self.convert(source, options={})

    if Proc === source
      file,line = source.source_location
      source = File.read(file)
      ast = find_block( parse(source), line )
    elsif Parser::AST::Node === source
      ast = source
      source = ast.loc.expression.source_buffer.source
    else
      ast = parse( source )
    end

    filters = options[:filters] || Filter::DEFAULTS

    unless filters.empty?
      filter = Parser::AST::Processor
      filters.reverse.each do |mod|
        filter = Class.new(filter) {include mod} 
      end
      ast = filter.new.process(ast)
    end

    ruby2js = Ruby2JS::Converter.new( ast )

    if source.include? "\n"
      ruby2js.enable_vertical_whitespace 
      lines = ruby2js.to_js.split("\n")
      pre = ''
      pending = false
      blank = true
      lines.each do |line|
        if line.start_with? '}' or line.start_with? ']'
          pre.sub!(/^  /,'')
          line.sub!(/;$/,";\n")
          pending = true
        else
          pending = false
        end

        line.sub! /^/, pre
        if line.end_with? '{' or line.end_with? '['
          pre += '  ' 
          line.sub!(/^/,"\n") unless blank or pending
          pending = true
        end

        blank = pending
      end
      lines.join("\n")
    else
      ruby2js.to_js
    end
  end
  
  def self.parse(source)
    # workaround for https://github.com/whitequark/parser/issues/112
    buffer = Parser::Source::Buffer.new('__SOURCE__')
    buffer.raw_source = source.encode('utf-8')
    Parser::CurrentRuby.new.parse(buffer)
  end

  def self.find_block(ast, line)
    if ast.type == :block and ast.loc.expression.line == line
      return ast.children.last
    end

    ast.children.each do |child|
      if Parser::AST::Node === child
        block = find_block child, line
        return block if block
      end
    end

    nil
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby2js-0.2.0 lib/ruby2js.rb