Skip to content

Commit

Permalink
Merge pull request #1811 from opal/elia/empty-x-strings
Browse files Browse the repository at this point in the history
Support empty x-strings
  • Loading branch information
elia committed Apr 30, 2018
2 parents fcc3f78 + da987be commit 2c95900
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/opal/nodes/x_string.rb
Expand Up @@ -38,7 +38,7 @@ def compile_child(child)
when :str
value = child.loc.expression.source
push Fragment.new(value, scope, child)
when :begin, :gvar, :ivar
when :begin, :gvar, :ivar, :nil
push expr(child)
else
raise "Unsupported xstr part: #{child.type}"
Expand All @@ -51,6 +51,8 @@ def compile_single_line(children)
first_child = children.shift
single_child = children.empty?

first_child ||= s(:nil)

if first_child.type == :str
first_value = first_child.loc.expression.source.strip
has_embeded_return = first_value =~ /^return\b/
Expand Down Expand Up @@ -125,10 +127,10 @@ def unpack_return(children)
# }
def strip_empty_children(children)
children = children.dup
empty_line = ->(child) { child.type == :str && child.loc.expression.source.rstrip.empty? }
empty_line = ->(child) { child.nil? || (child.type == :str && child.loc.expression.source.rstrip.empty?) }

children.shift while empty_line[children.first]
children.pop while empty_line[children.last]
children.shift while children.any? && empty_line[children.first]
children.pop while children.any? && empty_line[children.last]

children
end
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/compiler_spec.rb
Expand Up @@ -440,6 +440,27 @@ def self.exist? path
end
end

specify 'when empty' do
expect_compiled(%q{
%x{
}
}).to include("return nil\n")

expect_compiled(%q{
%x{}
}).to include("return nil\n")

expect_compiled(%q{
`
`
}).to include("return nil\n")

expect_compiled(%q{
``
}).to include("return nil\n")
end

def expect_number_of_warnings(code)
warnings_number = 0
compiler = Opal::Compiler.new(code)
Expand Down

0 comments on commit 2c95900

Please sign in to comment.