[Haml] Fix if/case parsing when the statement val was assigned to a var.

Closes gh-224
This commit is contained in:
Nathan Weizenbaum 2010-08-02 21:38:34 -07:00
parent 1d65badb0a
commit 7a55155a04
3 changed files with 81 additions and 4 deletions

View File

@ -8,6 +8,9 @@
* Fix an html2haml ERB-parsing bug where ERB blocks were occasionally
left without indentation in Haml.
* Fix parsing of `if` and `case` statements whose values were assigned to variables.
This is still bad style, though.
## 3.0.15
[Tagged on GitHub](http://github.com/nex3/haml/commit/3.0.15).

View File

@ -248,12 +248,20 @@ END
# It's important to preserve tabulation modification for keywords
# that involve choosing between posible blocks of code.
if %w[else elsif when].include?(keyword)
@dont_indent_next_line, @dont_tab_up_next_text = @to_close_stack.last[1..2]
# Whether a script block has already been opened immediately above this line
was_opened = @to_close_stack.last && @to_close_stack.last.first == :script
if was_opened
@dont_indent_next_line, @dont_tab_up_next_text = @to_close_stack.last[1..2]
end
# when is unusual in that either it will be indented twice,
# or the case won't have created its own indentation
if keyword == "when"
push_and_tabulate([:script, @dont_indent_next_line, @dont_tab_up_next_text, false])
# or the case won't have created its own indentation.
# Also, if no block has been opened yet, we need to make sure we add an end
# once we de-indent.
if !was_opened || keyword == "when"
push_and_tabulate([
:script, @dont_indent_next_line, @dont_tab_up_next_text,
!was_opened])
end
elsif block || text =~ /^-\s*(case|if)\b/
push_and_tabulate([:script, @dont_indent_next_line, @dont_tab_up_next_text])

View File

@ -697,6 +697,72 @@ HTML
HAML
end
def test_case_assigned_to_var
assert_equal(<<HTML, render(<<HAML))
bar
HTML
- var = case 12
- when 1; "foo"
- when 12; "bar"
= var
HAML
assert_equal(<<HTML, render(<<HAML))
bar
HTML
- var = case 12
- when 1
- "foo"
- when 12
- "bar"
= var
HAML
assert_equal(<<HTML, render(<<HAML))
bar
HTML
- var = case 12
- when 1
- "foo"
- when 12
- "bar"
= var
HAML
end
def test_if_assigned_to_var
assert_equal(<<HTML, render(<<HAML))
foo
HTML
- var = if false
- else
- "foo"
= var
HAML
assert_equal(<<HTML, render(<<HAML))
foo
HTML
- var = if false
- elsif 12
- "foo"
- elsif 14; "bar"
- else
- "baz"
= var
HAML
assert_equal(<<HTML, render(<<HAML))
foo
HTML
- var = if false
- "bar"
- else
- "foo"
= var
HAML
end
# HTML escaping tests
def test_ampersand_equals_should_escape