mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
fixing chained single-line if-elses with a smarter rewriter.
This commit is contained in:
parent
2875de5e73
commit
91e703052c
2 changed files with 11 additions and 3 deletions
|
@ -127,6 +127,7 @@ module CoffeeScript
|
||||||
scan_tokens do |prev, token, post, i|
|
scan_tokens do |prev, token, post, i|
|
||||||
next 1 unless SINGLE_LINERS.include?(token[0]) && post[0] != :INDENT &&
|
next 1 unless SINGLE_LINERS.include?(token[0]) && post[0] != :INDENT &&
|
||||||
!(token[0] == :ELSE && post[0] == :IF) # Elsifs shouldn't get blocks.
|
!(token[0] == :ELSE && post[0] == :IF) # Elsifs shouldn't get blocks.
|
||||||
|
starter = token[0]
|
||||||
line = token[1].line
|
line = token[1].line
|
||||||
@tokens.insert(i + 1, [:INDENT, Value.new(2, line)])
|
@tokens.insert(i + 1, [:INDENT, Value.new(2, line)])
|
||||||
idx = i + 1
|
idx = i + 1
|
||||||
|
@ -134,8 +135,9 @@ module CoffeeScript
|
||||||
loop do
|
loop do
|
||||||
idx += 1
|
idx += 1
|
||||||
tok = @tokens[idx]
|
tok = @tokens[idx]
|
||||||
if !tok || SINGLE_CLOSERS.include?(tok[0]) ||
|
if (!tok || SINGLE_CLOSERS.include?(tok[0]) ||
|
||||||
(tok[0] == ')' && parens == 0)
|
(tok[0] == ')' && parens == 0)) &&
|
||||||
|
!(starter == :ELSE && tok[0] == :ELSE)
|
||||||
@tokens.insert(idx, [:OUTDENT, Value.new(2, line)])
|
@tokens.insert(idx, [:OUTDENT, Value.new(2, line)])
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,3 +8,9 @@ result: if a
|
||||||
true
|
true
|
||||||
|
|
||||||
print result
|
print result
|
||||||
|
|
||||||
|
|
||||||
|
first: if false then false else second: if false then false else true
|
||||||
|
|
||||||
|
print first
|
||||||
|
print second
|
Loading…
Add table
Add a link
Reference in a new issue