Sha256: 251abf61655a692e84c03dbabfd98defb75afcebc9f764d0f1d22ebff7534b53

Contents?: true

Size: 1.04 KB

Versions: 24

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module RubyNext
  module Language
    module Rewriters
      class SquigglyHeredoc < Base
        NAME = "squiggly-heredoc"
        SYNTAX_PROBE = "txt = <<~TXT\n  bla\n      TXT"
        MIN_SUPPORTED_VERSION = Gem::Version.new("2.3.0")

        def on_str(node)
          node = super(node) if defined?(super_method)
          return node unless node.loc.respond_to?(:heredoc_body) && node.loc.expression.source.include?("<<~")

          context.track! self

          replace(node.loc.expression, node.loc.expression.source.tr("~", "-"))

          heredoc_loc = node.loc.heredoc_body.join(node.loc.heredoc_end)
          heredoc_source, heredoc_end = heredoc_loc.source.split(/\n([^\n]+)\z/)

          indent = heredoc_source.lines.map { |line| line.match(/^\s*/)[0].size }.min

          new_source = heredoc_source.gsub!(%r{^\s{#{indent}}}, "")

          replace(heredoc_loc, [new_source, heredoc_end].join("\n"))

          node
        end

        alias on_dstr on_str
        alias on_xstr on_str
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
ruby-next-core-1.0.3 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-1.0.2 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-1.0.1 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-1.0.0 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-1.0.0.rc.1 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-0.15.3 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-0.15.2 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-0.15.1 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-0.15.0 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-0.14.1 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-0.14.0 lib/ruby-next/language/rewriters/2.3/squiggly_heredoc.rb
ruby-next-core-0.13.3 lib/ruby-next/language/rewriters/squiggly_heredoc.rb
ruby-next-core-0.13.2 lib/ruby-next/language/rewriters/squiggly_heredoc.rb
ruby-next-core-0.13.1 lib/ruby-next/language/rewriters/squiggly_heredoc.rb
ruby-next-core-0.13.0 lib/ruby-next/language/rewriters/squiggly_heredoc.rb
ruby-next-core-0.12.0 lib/ruby-next/language/rewriters/squiggly_heredoc.rb
ruby-next-core-0.11.1 lib/ruby-next/language/rewriters/squiggly_heredoc.rb
ruby-next-core-0.11.0 lib/ruby-next/language/rewriters/squiggly_heredoc.rb
ruby-next-core-0.10.5 lib/ruby-next/language/rewriters/squiggly_heredoc.rb
ruby-next-core-0.10.4 lib/ruby-next/language/rewriters/squiggly_heredoc.rb