Skip to content

Commit

Permalink
Merge pull request jashkenas#1851 from michaelficarra/issue1844
Browse files Browse the repository at this point in the history
fixes jashkenas#1844: bound functions in nested comprehensions causing empty var statements
  • Loading branch information
jashkenas committed Nov 14, 2011
2 parents fe78e65 + 496978a commit 1e25c9d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/coffee-script/coffee-script.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/coffee-script/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 15 additions & 10 deletions lib/coffee-script/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions lib/coffee-script/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions lib/coffee-script/scope.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,15 @@ exports.Block = class Block extends Base
if scope.expressions is this
declars = o.scope.hasDeclarations()
assigns = scope.hasAssignments
if (declars or assigns) and i
code += '\n'
if declars
code += "#{@tab}var #{ scope.declaredVariables().join(', ') };\n"
if assigns
code += "#{@tab}var #{ multident scope.assignedVariables().join(', '), @tab };\n"
if declars or assigns
code += '\n' if i
code += "#{@tab}var "
if declars
code += scope.declaredVariables().join ', '
if assigns
code += ",\n#{@tab + TAB}" if declars
code += scope.assignedVariables().join ",\n#{@tab + TAB}"
code += ';\n'
code + post

# Wrap up the given nodes as a **Block**, unless it already happens
Expand Down
6 changes: 3 additions & 3 deletions src/scope.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ exports.Scope = class Scope
# Adds a new variable or overrides an existing one.
add: (name, type, immediate) ->
return @parent.add name, type, immediate if @shared and not immediate
if typeof (pos = @positions[name]) is 'number'
@variables[pos].type = type
if @positions.hasOwnProperty name
@variables[@positions[name]].type = type
else
@positions[name] = @variables.push({name, type}) - 1

Expand Down Expand Up @@ -73,7 +73,7 @@ exports.Scope = class Scope
# Ensure that an assignment is made at the top of this scope
# (or at the top-level scope, if requested).
assign: (name, value) ->
@add name, value: value, assigned: true
@add name, {value, assigned: yes}, yes
@hasAssignments = yes

# Does this scope have any declared variables?
Expand Down
4 changes: 4 additions & 0 deletions test/functions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,7 @@ test "arguments vs parameters", ->
doesNotThrow -> CoffeeScript.compile "f(x) ->"
f = (g) -> g()
eq 5, f (x) -> 5

test "#1844: bound functions in nested comprehensions causing empty var statements", ->
a = ((=>) for a in [0] for b in [0])
eq 1, a.length

0 comments on commit 1e25c9d

Please sign in to comment.