Skip to content

[CS2] Nodes: Missing declarations for generated variables in parameter defaults #4413

@connec

Description

@connec

Note: this is for the 2 branch only.

(a = foo() ? bar()) ->
// Generated by CoffeeScript 2.0.0-alpha
(function(a = (ref = foo()) != null ? ref : bar()) {});

Note that ref is used despite there being no var ref anywhere.

A simple patch to fix this is

diff --git a/src/nodes.coffee b/src/nodes.coffee
index 08b0b4e..65fc7cf 100644
--- a/src/nodes.coffee
+++ b/src/nodes.coffee
@@ -2005,10 +2005,11 @@ exports.Code = class Code extends Base
     modifiers.push '*'        if @isGenerator
 
     signature = [@makeCode '(']
+    paramO    = extend o, scope: o.scope.parent
     for param, i in params
       signature.push @makeCode ', ' if i
       signature.push @makeCode '...' if haveSplatParam and i is params.length - 1
-      signature.push param.compileToFragments(o)...
+      signature.push param.compileToFragments(paramO)...
     signature.push @makeCode ')'
 
     body = @body.compileWithDeclarations o unless @body.isEmpty()

I'm not sure though if such functions should also be compiled in a safety wrapper, though

(function () {
  var ref
  return (function(a = (ref = foo()) != null ? ref : bar()) {});
})()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions