Skip to content

Functions in default parameters should have separate scope #2584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DanielRosenwasser opened this issue Apr 1, 2015 · 6 comments
Closed
Labels
ES6 Relates to the ES6 Spec Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Milestone

Comments

@DanielRosenwasser
Copy link
Member

function f()
    return (function(a= () => typeof b === "undefined") {
      var b = 1;
      return a();
    }());
}

console.log()

Expected: true
Actual: false

@DanielRosenwasser DanielRosenwasser added Bug A bug in TypeScript Breaking Change Would introduce errors in existing code labels Apr 1, 2015
@RyanCavanaugh
Copy link
Member

We'll need to rename the inner b on emit?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 2, 2015

i think we have a few options:

  1. always evaluate the initializes in there own scope
  2. wrap the body of the function in a different scope
  3. detect cases like that and make them an error, but still evaluate initializes in the function scope
  4. do nothing

I think the least we can do, is when resolving names in initializes do them outside the function scope, so the above example would be an error assuming there is no global 'b' defined.

@falsandtru
Copy link
Contributor

Why this serious difference is not fixed such a long time?

@falsandtru
Copy link
Contributor

falsandtru commented Sep 11, 2017

At least if you fix the type checking of this, Babel can be used to resolve transpile bug.

@Andarist
Copy link
Contributor

Andarist commented Aug 1, 2023

This one still isn't fixed. Since the reported code is not complete, I'll re-post the correct one that can be used as a test case:

// @target: es5

function f() {
    return (function(a= () => typeof b === "undefined") {
      var b = 1;
      return a();
    }());
}

console.log(f())

@RyanCavanaugh RyanCavanaugh added Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it and removed Bug A bug in TypeScript Breaking Change Would introduce errors in existing code labels Feb 23, 2024
@RyanCavanaugh
Copy link
Member

It's been 9 years, no one has sent a PR in that time, and compiling to ES5 at this point is pretty rare. Doesn't seem to merit more consideration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ES6 Relates to the ES6 Spec Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

5 participants