[DRE-commits] [ruby-temple] 01/03: New upstream version 0.8.0
Sruthi Chandran
srud-guest at moszumanska.debian.org
Thu Jul 27 09:58:20 UTC 2017
This is an automated email from the git hooks/post-receive script.
srud-guest pushed a commit to branch master
in repository ruby-temple.
commit 3c4f97dacbba6315fa6e6df578b9e65755b4191d
Author: Sruthi Chandran <srud at disroot.org>
Date: Thu Jul 27 00:50:50 2017 +0530
New upstream version 0.8.0
---
.travis.yml | 16 ++++--
CHANGES | 11 ++++-
EXPRESSIONS.md | 1 +
lib/temple.rb | 1 +
lib/temple/filters/static_analyzer.rb | 91 +++++++----------------------------
lib/temple/filters/string_splitter.rb | 6 +--
lib/temple/static_analyzer.rb | 77 +++++++++++++++++++++++++++++
lib/temple/version.rb | 2 +-
test/filters/test_static_analyzer.rb | 32 ++++++++----
test/filters/test_string_splitter.rb | 7 +++
test/test_static_analyzer.rb | 39 +++++++++++++++
11 files changed, 192 insertions(+), 91 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 7b8a413..4ee977d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,13 +1,17 @@
language: ruby
+dist: trusty
+
+cache: bundler
rvm:
- 1.9.3
- 2.0.0
- - 2.1.0
- - 2.3.0
+ - 2.1.10
+ - 2.2.6
+ - 2.3.3
- ruby-head
- jruby-19mode
- - rbx-2
+ - rbx-3
sudo: false
@@ -18,3 +22,9 @@ env:
matrix:
allow_failures:
- rvm: ruby-head
+ - rvm: rbx-3
+ exclude:
+ - rvm: jruby-19mode
+ env: ESCAPE_UTILS=1
+ - rvm: rbx-3
+ env: ESCAPE_UTILS=1
diff --git a/CHANGES b/CHANGES
index 007b02b..7cacee5 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,15 @@
+0.8.0
+
+ * Add Temple::StaticAnalyzer to analyze Ruby expressions
+ * Support newlines in Temple::Filters::StaticAnalyzer
+
+0.7.8
+
+ * Fix an warning in StaticAnalyzer
+
0.7.7
- * Add StaticAnalyzer, StringSplitter
+ * Add Temple::Filters::StaticAnalyzer, Temple::Filters::StringSplitter
* Freeze string literals
0.7.6
diff --git a/EXPRESSIONS.md b/EXPRESSIONS.md
index cbc7727..2f499c0 100644
--- a/EXPRESSIONS.md
+++ b/EXPRESSIONS.md
@@ -227,6 +227,7 @@ Example:
[:html, :tag, 'img', [:html, :attrs, [:html, :attr, 'src', 'image.png']]]
[:html, :tag, 'p', [:multi], [:static, 'Content']]
generates:
+
<img src="image.png"/>
<p>Content</p>
diff --git a/lib/temple.rb b/lib/temple.rb
index 650cd8c..c92bbf8 100644
--- a/lib/temple.rb
+++ b/lib/temple.rb
@@ -13,6 +13,7 @@ module Temple
autoload :ImmutableMap, 'temple/map'
autoload :MutableMap, 'temple/map'
autoload :OptionMap, 'temple/map'
+ autoload :StaticAnalyzer, 'temple/static_analyzer'
module Mixins
autoload :Dispatcher, 'temple/mixins/dispatcher'
diff --git a/lib/temple/filters/static_analyzer.rb b/lib/temple/filters/static_analyzer.rb
index a3bc69a..215c079 100644
--- a/lib/temple/filters/static_analyzer.rb
+++ b/lib/temple/filters/static_analyzer.rb
@@ -1,85 +1,28 @@
-begin
- require 'ripper'
-rescue LoadError
-end
-
module Temple
module Filters
# Convert [:dynamic, code] to [:static, text] if code is static Ruby expression.
class StaticAnalyzer < Filter
- STATIC_TOKENS = [
- :on_tstring_beg, :on_tstring_end, :on_tstring_content,
- :on_embexpr_beg, :on_embexpr_end,
- :on_lbracket, :on_rbracket,
- :on_qwords_beg, :on_words_sep, :on_qwords_sep,
- :on_lparen, :on_rparen,
- :on_lbrace, :on_rbrace, :on_label,
- :on_int, :on_float, :on_imaginary,
- :on_comma, :on_sp,
- ].freeze
-
- DYNAMIC_TOKENS = [
- :on_ident, :on_period,
- ].freeze
-
- STATIC_KEYWORDS = [
- 'true', 'false', 'nil',
- ].freeze
-
- STATIC_OPERATORS = [
- '=>',
- ].freeze
-
- if defined?(Ripper)
- def self.static?(code)
- return false if code.nil? || code.strip.empty?
- return false if SyntaxChecker.syntax_error?(code)
-
- Ripper.lex(code).each do |(_, col), token, str|
- case token
- when *STATIC_TOKENS
- # noop
- when :on_kw
- return false unless STATIC_KEYWORDS.include?(str)
- when :on_op
- return false unless STATIC_OPERATORS.include?(str)
- when *DYNAMIC_TOKENS
- return false
- else
- return false
- end
- end
- true
- end
-
- def on_dynamic(code)
- if StaticAnalyzer.static?(code)
- [:static, eval(code).to_s]
- else
- [:dynamic, code]
- end
+ def call(exp)
+ # Optimize only when Ripper is available.
+ if ::Temple::StaticAnalyzer.available?
+ super
+ else
+ exp
end
+ end
- class SyntaxChecker < Ripper
- class ParseError < StandardError; end
-
- def self.syntax_error?(code)
- self.new(code).parse
- false
- rescue ParseError
- true
- end
-
- private
+ def on_dynamic(code)
+ if ::Temple::StaticAnalyzer.static?(code)
+ exp = [:static, eval(code).to_s]
- def on_parse_error(*)
- raise ParseError
+ newlines = code.count("\n")
+ if newlines == 0
+ exp
+ else
+ [:multi, exp, *newlines.times.map { [:newline] }]
end
- end
- else
- # Do nothing if ripper is unavailable
- def call(ast)
- ast
+ else
+ [:dynamic, code]
end
end
end
diff --git a/lib/temple/filters/string_splitter.rb b/lib/temple/filters/string_splitter.rb
index 3d170fc..6f73094 100644
--- a/lib/temple/filters/string_splitter.rb
+++ b/lib/temple/filters/string_splitter.rb
@@ -16,7 +16,7 @@ module Temple
tokens.pop while tokens.last && [:on_comment, :on_sp].include?(tokens.last[1])
if tokens.size < 2
- raise "Expected token size >= 2 but got: #{tokens.size}"
+ raise(FilterError, "Expected token size >= 2 but got: #{tokens.size}")
end
compile_tokens!(exps, tokens)
end
@@ -27,12 +27,12 @@ module Temple
def strip_quotes!(tokens)
_, type, beg_str = tokens.shift
if type != :on_tstring_beg
- raise "Expected :on_tstring_beg but got: #{type}"
+ raise(FilterError, "Expected :on_tstring_beg but got: #{type}")
end
_, type, end_str = tokens.pop
if type != :on_tstring_end
- raise "Expected :on_tstring_end but got: #{type}"
+ raise(FilterError, "Expected :on_tstring_end but got: #{type}")
end
[beg_str, end_str]
diff --git a/lib/temple/static_analyzer.rb b/lib/temple/static_analyzer.rb
new file mode 100644
index 0000000..42e4f58
--- /dev/null
+++ b/lib/temple/static_analyzer.rb
@@ -0,0 +1,77 @@
+begin
+ require 'ripper'
+rescue LoadError
+end
+
+module Temple
+ module StaticAnalyzer
+ STATIC_TOKENS = [
+ :on_tstring_beg, :on_tstring_end, :on_tstring_content,
+ :on_embexpr_beg, :on_embexpr_end,
+ :on_lbracket, :on_rbracket,
+ :on_qwords_beg, :on_words_sep, :on_qwords_sep,
+ :on_lparen, :on_rparen,
+ :on_lbrace, :on_rbrace, :on_label,
+ :on_int, :on_float, :on_imaginary,
+ :on_comma, :on_sp, :on_ignored_nl,
+ ].freeze
+
+ DYNAMIC_TOKENS = [
+ :on_ident, :on_period,
+ ].freeze
+
+ STATIC_KEYWORDS = [
+ 'true', 'false', 'nil',
+ ].freeze
+
+ STATIC_OPERATORS = [
+ '=>',
+ ].freeze
+
+ class << self
+ def available?
+ defined?(Ripper)
+ end
+
+ def static?(code)
+ return false if code.nil? || code.strip.empty?
+ return false if syntax_error?(code)
+
+ Ripper.lex(code).each do |_, token, str|
+ case token
+ when *STATIC_TOKENS
+ # noop
+ when :on_kw
+ return false unless STATIC_KEYWORDS.include?(str)
+ when :on_op
+ return false unless STATIC_OPERATORS.include?(str)
+ when *DYNAMIC_TOKENS
+ return false
+ else
+ return false
+ end
+ end
+ true
+ end
+
+ def syntax_error?(code)
+ SyntaxChecker.new(code).parse
+ false
+ rescue SyntaxChecker::ParseError
+ true
+ end
+ end
+
+ if defined?(Ripper)
+ class SyntaxChecker < Ripper
+ class ParseError < StandardError; end
+
+ private
+
+ def on_parse_error(*)
+ raise ParseError
+ end
+ end
+ end
+ end
+end
diff --git a/lib/temple/version.rb b/lib/temple/version.rb
index b2fa56e..8de6cf4 100644
--- a/lib/temple/version.rb
+++ b/lib/temple/version.rb
@@ -1,3 +1,3 @@
module Temple
- VERSION = '0.7.7'
+ VERSION = '0.8.0'
end
diff --git a/test/filters/test_static_analyzer.rb b/test/filters/test_static_analyzer.rb
index e2ef2f3..820fdb1 100644
--- a/test/filters/test_static_analyzer.rb
+++ b/test/filters/test_static_analyzer.rb
@@ -1,15 +1,12 @@
require 'helper'
-begin
- require 'ripper'
-rescue LoadError
-end
-if defined?(Ripper)
- describe Temple::Filters::StaticAnalyzer do
- before do
- @filter = Temple::Filters::StaticAnalyzer.new
- end
+describe Temple::Filters::StaticAnalyzer do
+ before do
+ @filter = Temple::Filters::StaticAnalyzer.new
+ @generator = Temple::Generator.new
+ end
+ if Temple::StaticAnalyzer.available?
it 'should convert :dynamic to :static if code is static' do
@filter.call([:dynamic, '"#{"hello"}#{100}"']
).should.equal [:static, 'hello100']
@@ -19,5 +16,22 @@ if defined?(Ripper)
exp = [:dynamic, '"#{hello}#{100}"']
@filter.call(exp).should.equal(exp)
end
+
+ it 'should not change number of newlines in generated code' do
+ exp = [:dynamic, "[100,\n200,\n]"]
+ @filter.call(exp).should.equal([:multi, [:static, '[100, 200]'], [:newline], [:newline]])
+
+ @generator.call(@filter.call(exp)).count("\n").
+ should.equal(@generator.call(exp).count("\n"))
+ end
+ else
+ it 'should do nothing' do
+ [
+ [:dynamic, '"#{"hello"}#{100}"'],
+ [:dynamic, '"#{hello}#{100}"'],
+ ].each do |exp|
+ @filter.call(exp).should.equal(exp)
+ end
+ end
end
end
diff --git a/test/filters/test_string_splitter.rb b/test/filters/test_string_splitter.rb
index bbbf4eb..bbf3194 100644
--- a/test/filters/test_string_splitter.rb
+++ b/test/filters/test_string_splitter.rb
@@ -14,5 +14,12 @@ if defined?(Ripper) && RUBY_VERSION >= "2.0.0"
@filter.call([:dynamic, '"static#{dynamic}"']
).should.equal [:multi, [:static, 'static'], [:dynamic, 'dynamic']]
end
+
+ describe '.compile' do
+ it 'should raise CompileError for non-string literals' do
+ lambda { Temple::Filters::StringSplitter.compile('1') }.
+ should.raise(Temple::FilterError)
+ end
+ end
end
end
diff --git a/test/test_static_analyzer.rb b/test/test_static_analyzer.rb
new file mode 100644
index 0000000..ac29f5f
--- /dev/null
+++ b/test/test_static_analyzer.rb
@@ -0,0 +1,39 @@
+require 'helper'
+
+describe Temple::StaticAnalyzer do
+ describe '.available?' do
+ it 'should return true if its dependency is available' do
+ Temple::StaticAnalyzer.available?.should.equal(defined?(Ripper))
+ end
+ end
+
+ if Temple::StaticAnalyzer.available?
+ describe '.static?' do
+ it 'should return true if given Ruby expression is static' do
+ ['true', 'false', '"hello world"', "[1, { 2 => 3 }]", "[\n1,\n]"].each do |exp|
+ Temple::StaticAnalyzer.static?(exp).should.equal(true)
+ end
+ end
+
+ it 'should return false if given Ruby expression is dynamic' do
+ ['1 + 2', 'variable', 'method_call(a)', 'CONSTANT'].each do |exp|
+ Temple::StaticAnalyzer.static?(exp).should.equal(false)
+ end
+ end
+ end
+
+ describe '.syntax_error?' do
+ it 'should return false if given Ruby expression is valid' do
+ ['Foo.bar.baz { |c| c.d! }', '{ foo: bar }'].each do |exp|
+ Temple::StaticAnalyzer.syntax_error?(exp).should.equal(false)
+ end
+ end
+
+ it 'should return true if given Ruby expression is invalid' do
+ ['Foo.bar.baz { |c| c.d! ', ' foo: bar '].each do |exp|
+ Temple::StaticAnalyzer.syntax_error?(exp).should.equal(true)
+ end
+ end
+ end
+ end
+end
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-ruby-extras/ruby-temple.git
More information about the Pkg-ruby-extras-commits
mailing list