Sha256: 120eef0f62584dc2f1af0f260b5a35944accc08bfdf724999c4929a4b4b7c479

Contents?: true

Size: 706 Bytes

Versions: 72

Compression:

Stored size: 706 Bytes

Contents

module Temple
  module Filters
    # Convert [:dynamic, code] to [:static, text] if code is static Ruby expression.
    class StaticAnalyzer < Filter
      def call(exp)
        # Optimize only when Ripper is available.
        if ::Temple::StaticAnalyzer.available?
          super
        else
          exp
        end
      end

      def on_dynamic(code)
        if ::Temple::StaticAnalyzer.static?(code)
          exp = [:static, eval(code).to_s]

          newlines = code.count("\n")
          if newlines == 0
            exp
          else
            [:multi, exp, *newlines.times.map { [:newline] }]
          end
        else
          [:dynamic, code]
        end
      end
    end
  end
end

Version data entries

72 entries across 58 versions & 6 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
brakeman-6.1.2 bundle/ruby/3.3.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
brakeman-6.1.1 bundle/ruby/3.0.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
brakeman-6.1.0 bundle/ruby/3.1.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
brakeman-6.0.1 bundle/ruby/3.1.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
brakeman-6.0.0 bundle/ruby/3.0.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
brakeman-5.4.1 bundle/ruby/3.1.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
temple-0.10.0 lib/temple/filters/static_analyzer.rb
brakeman-5.4.0 bundle/ruby/2.7.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
temple-0.9.1 lib/temple/filters/static_analyzer.rb
temple-0.9.0 lib/temple/filters/static_analyzer.rb
brakeman-5.3.1 bundle/ruby/2.7.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
brakeman-5.3.0 bundle/ruby/2.7.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
brakeman-5.2.3 bundle/ruby/2.7.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
brakeman-5.2.2 bundle/ruby/2.7.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb
brakeman-5.2.1 bundle/ruby/2.7.0/gems/temple-0.8.2/lib/temple/filters/static_analyzer.rb