Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/temple/static_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def static?(code)
return false
end
end
true

!ShorthandSyntaxChecker.shorthand?(code)
end

def syntax_error?(code)
Expand All @@ -73,6 +74,29 @@ def on_parse_error(*)
raise ParseError
end
end

class ShorthandSyntaxChecker < Ripper
class << self
def shorthand?(code)
instance = new(code)
instance.parse
instance.shorthand
end
end

attr_reader :shorthand

def initialize(*)
super
@shorthand = nil
end

private

def on_assoc_new(key, value)
@shorthand = true if value.nil?
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/static_analyzer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
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|
['true', 'false', '"hello world"', "[1, { 2 => 3 }]", "[\n1,\n]", '{a:1}'].each do |exp|
expect(Temple::StaticAnalyzer.static?(exp)).to eq(true)
end
end

it 'should return false if given Ruby expression is dynamic' do
['1 + 2', 'variable', 'method_call(a)', 'CONSTANT'].each do |exp|
['1 + 2', 'variable', 'method_call(a)', 'CONSTANT', '{a:}'].each do |exp|
expect(Temple::StaticAnalyzer.static?(exp)).to eq(false)
end
end
Expand Down