Sha256: bfd15887b719635ff1c38931da899f33b733a55cc12e7a94756fb016d69b10a0

Contents?: true

Size: 1.2 KB

Versions: 18

Compression:

Stored size: 1.2 KB

Contents

require 'ruby2js'

module Ruby2JS
  module Filter
    module TaggedTemplates
      include SEXP

      def initialize(*args)
        super
      end

      def on_send(node)
        target, method, *args = node.children
        return super unless target.nil? and es2015

        tagged_methods = @options[:template_literal_tags] || [:html, :css]

        if tagged_methods.include?(method) && args.length == 1
          strnode = process args.first
          if strnode.type == :str
            # convert regular strings to literal strings
            strnode = strnode.updated(:dstr, [s(:str, strnode.children.first.chomp("\n"))])
          elsif strnode.type == :dstr
            # for literal strings, chomp a newline off the end
            if strnode.children.last.type == :str && strnode.children.last.children[0].end_with?("\n")
             children = [*strnode.children.take(strnode.children.length - 1), s(:str, strnode.children.last.children[0].chomp)]
             strnode = s(:dstr, *children)
            end
          else
            return super
          end

          S(:taglit, s(:sym, method), strnode)
        else
          super
        end
      end
    end

    DEFAULTS.push TaggedTemplates
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ruby2js-5.1.1 lib/ruby2js/filter/tagged_templates.rb
ruby2js-5.1.0 lib/ruby2js/filter/tagged_templates.rb
ruby2js-5.0.1 lib/ruby2js/filter/tagged_templates.rb
ruby2js-5.0.0 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.2.2 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.2.1 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.2.0 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.1.7 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.1.6 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.1.5 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.1.4 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.1.3 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.1.2 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.1.1 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.1.0 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.0.5 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.0.4 lib/ruby2js/filter/tagged_templates.rb
ruby2js-4.0.3 lib/ruby2js/filter/tagged_templates.rb