Skip to content

Commit 56fe211

Browse files
committed
Fixes #2455 -- condition should know what variables the body has declared.
1 parent cf11a57 commit 56fe211

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

lib/coffee-script/nodes.js

Lines changed: 10 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/nodes.coffee

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,23 +1895,22 @@ exports.If = class If extends Base
18951895
if exeq
18961896
return new If(@condition.invert(), @elseBodyNode(), type: 'if').compile o
18971897

1898+
indent = o.indent + TAB
1899+
body = @ensureBlock(@body).compile merge o, {indent}
18981900
cond = @condition.compile o, LEVEL_PAREN
1899-
o.indent += TAB
1900-
body = @ensureBlock(@body)
1901-
ifPart = "if (#{cond}) {\n#{body.compile(o)}\n#{@tab}}"
1901+
ifPart = "if (#{cond}) {\n#{body}\n#{@tab}}"
19021902
ifPart = @tab + ifPart unless child
19031903
return ifPart unless @elseBody
19041904
ifPart + ' else ' + if @isChain
1905-
o.indent = @tab
19061905
o.chainChild = yes
19071906
@elseBody.unwrap().compile o, LEVEL_TOP
19081907
else
1909-
"{\n#{ @elseBody.compile o, LEVEL_TOP }\n#{@tab}}"
1908+
"{\n#{ @elseBody.compile merge(o, {indent}), LEVEL_TOP }\n#{@tab}}"
19101909

19111910
# Compile the `If` as a conditional operator.
19121911
compileExpression: (o) ->
1913-
cond = @condition.compile o, LEVEL_COND
19141912
body = @bodyNode().compile o, LEVEL_LIST
1913+
cond = @condition.compile o, LEVEL_COND
19151914
alt = if @elseBodyNode() then @elseBodyNode().compile(o, LEVEL_LIST) else 'void 0'
19161915
code = "#{cond} ? #{body} : #{alt}"
19171916
if o.level >= LEVEL_COND then "(#{code})" else code

0 commit comments

Comments
 (0)