Sha256: e3a9ba6ecfd5a7fb18b95aefd9c3a42aa3b15b0e8cec82d79ca0d52d3e7fb269

Contents?: true

Size: 1 KB

Versions: 15

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module Opal
  class EofContent
    DATA_SEPARATOR = "__END__\n"

    def initialize(tokens, source)
      @tokens = tokens
      @source = source
    end

    def eof
      return nil if @tokens.empty?

      eof_content = @source[last_token_position..-1]
      return nil unless eof_content

      # On Windows token position is off a bit, because Parser does not seem to compensate for \r\n
      # The first eof_content line on Windows may be for example "end\r\n"
      # Must match for it and \r\n and \n
      eof_content = eof_content.lines.drop_while { |line| /\A.*\r?\n?\z/.match?(line) && !line.start_with?('__END__') }

      if /\A__END__\r?\n?\z/.match?(eof_content[0])
        eof_content = eof_content[1..-1] || []
        eof_content.join
      elsif eof_content == ['__END__']
        ''
      end
    end

    private

    def last_token_position
      _, last_token_info = @tokens.last
      _, last_token_range = last_token_info
      last_token_range.end_pos
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 lib/opal/eof_content.rb
opal-1.8.2 lib/opal/eof_content.rb
opal-1.8.1 lib/opal/eof_content.rb
opal-1.8.0 lib/opal/eof_content.rb
opal-1.8.0.beta1 lib/opal/eof_content.rb
opal-1.7.4 lib/opal/eof_content.rb
opal-1.8.0.alpha1 lib/opal/eof_content.rb
opal-1.7.3 lib/opal/eof_content.rb
opal-1.7.2 lib/opal/eof_content.rb
opal-1.7.1 lib/opal/eof_content.rb
opal-1.7.0 lib/opal/eof_content.rb
opal-1.7.0.rc1 lib/opal/eof_content.rb
opal-1.6.1 lib/opal/eof_content.rb
opal-1.6.0 lib/opal/eof_content.rb
opal-1.6.0.rc1 lib/opal/eof_content.rb