Skip to content

Commit

Permalink
If compiling a function parameter creates any generated variables (e.…
Browse files Browse the repository at this point in the history
…g. `ref`), shift the declarations for those variables into the parent scope; fixes jashkenas#4413
  • Loading branch information
GeoffreyBooth committed Aug 14, 2017
1 parent 3a6ffa6 commit 5279558
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/coffeescript/nodes.js

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

7 changes: 7 additions & 0 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2674,7 +2674,14 @@ exports.Code = class Code extends Base
for param, i in params
signature.push @makeCode ', ' if i isnt 0
signature.push @makeCode '...' if haveSplatParam and i is params.length - 1
# Compile this parameter, but if any generated variables get created
# (e.g. `ref`), shift those into the parent scope since we can’t put a
# `var` line inside a function parameter list.
scopeVariablesCount = o.scope.variables.length
signature.push param.compileToFragments(o)...
if scopeVariablesCount isnt o.scope.variables.length
generatedVariables = o.scope.variables.splice scopeVariablesCount
o.scope.parent.variables.push generatedVariables...
signature.push @makeCode ')'
# Block comments between `)` and `->`/`=>` get output between `)` and `{`.
if @funcGlyph?.comments?
Expand Down
9 changes: 9 additions & 0 deletions test/functions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,12 @@ test "#3845/#3446: chain after function glyph", ->
doThing()
.then (@result) =>
.catch handleError

test "#4413: expressions in function parameters that create generated variables have those variables declared correctly", ->
'use strict'
# We’re in strict mode because we want an error to be thrown if the generated
# variable (`ref`) is assigned before being declared.
foo = -> null
bar = -> 33
f = (a = foo() ? bar()) -> a
eq f(), 33

0 comments on commit 5279558

Please sign in to comment.