Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent to generate invalid ruby code on embedded heredoc #770

Merged
merged 1 commit into from
May 28, 2018

Conversation

pocke
Copy link
Contributor

@pocke pocke commented Jun 16, 2017

Problem

Slim generates invalid ruby code if ruby: block has a heredoc at end.

For example:

$ cat test.slimo
ruby:
  v = <<-MSG
  foobar
  MSG
= v

$ slimrb test.slim
SyntaxError: test.slim:8: can't find string "MSG" anywhere before EOF
test.slim:2: syntax error, unexpected end-of-input, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
; v = <<-MSG
            ^
  Use --trace for backtrace.

This cause is a semicolon after delimiter of heredoc(MSG).

$ slimrb -c test.slim
_buf = '';
; v = <<-MSG
foobar
MSG;
; _buf << ((::Temple::Utils.escape_html((v))).to_s);
; _buf

Heredoc delimiter does not allow putting a semicolon after the delimiter.
So, slim generates invalid ruby code from the example.

Solution

Add a newline to embedded ruby code.

For example:

$ slimrb test.slim
foobar

$ slimrb -c test.slim
_buf = '';
; v = <<-MSG
foobar
MSG
;
; _buf << ((::Temple::Utils.escape_html((v))).to_s);
; _buf

Problem
======

Slim generates invalid ruby code if `ruby:` block has a heredoc at end.

For example:

```bash
$ cat test.slimo
ruby:
  v = <<-MSG
  foobar
  MSG
= v

$ slimrb test.slim
SyntaxError: test.slim:8: can't find string "MSG" anywhere before EOF
test.slim:2: syntax error, unexpected end-of-input, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
; v = <<-MSG
            ^
  Use --trace for backtrace.
```

This cause is a semicolon after delimiter of heredoc(`MSG`).

```bash
$ slimrb -c test.slim
_buf = '';
; v = <<-MSG
foobar
MSG;
; _buf << ((::Temple::Utils.escape_html((v))).to_s);
; _buf
```

Heredoc delimiter does not allow putting a semicolon after the delimiter.
So, slim generates invalid ruby code from the example.

Solution
=====

Add a newline to embedded ruby code.

For example:

```ruby
$ slimrb test.slim
foobar

$ slimrb -c test.slim
_buf = '';
; v = <<-MSG
foobar
MSG
;
; _buf << ((::Temple::Utils.escape_html((v))).to_s);
; _buf
```
@@ -119,7 +119,7 @@ def test_embedded_ruby1
= unknown_ruby_method
}

assert_ruby_error NameError,"(__TEMPLATE__):6", source
assert_ruby_error NameError,"(__TEMPLATE__):7", source
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is testing that backtrace is properly created, i.e. unknown_ruby_method is called on lineno 6 in source. Consider the case your code after 10 :ruby filters raised error. The error backtrace would say it raised an error on 10 lines after the actual location. It will make it hard to debug Slim template.

So this test should pass without modification. You need to generate another code to fix this problem.

@stonean stonean merged commit 84b6708 into slim-template:master May 28, 2018
@stonean
Copy link
Contributor

stonean commented May 28, 2018

Thank you for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants