Skip to content
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

Assigns global properties when defining a function that has a function type argument with default value and rest parameters #10911

Closed
1 task
ryokkkke opened this issue Dec 23, 2019 · 3 comments
Labels
i: bug i: duplicate outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@ryokkkke
Copy link

Bug Report

  • I would like to work on a fix!

Current Behavior
This bug occurrs when transpile TypeScript codes.

  1. define a function that has function type argument with default value and rest parameters.
  2. define a variable that has same name as the argument function's argument name in the function body.
  3. then compile, and 'var' expression in a for sentence is disappearing from output JS codes.
  4. JS runtime will throw an error like ReferenceError: _len is not defined because the assignment of a global variable is not allowed with "use strict";.
  5. change the BUGGY_ARGUMENT to DIFFERENT_ARGUMENT
  6. works fine

Input Code

const func = (
  someFunc: (BUGGY_ARGUMENT: number[]) => boolean = () => false,
  ...restParameters: number[]
) => {
  const BUGGY_ARGUMENT = someFunc(restParameters);
  console.log(BUGGY_ARGUMENT);
};
// output js codes
"use strict";

var func = function func() {
  var someFunc = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {
    return false;
  };
  return function () {
    // THIS '_len' WILL THROW AN ERROR:
    // `ReferenceError: _len is not defined`
    for (_len = arguments.length, restParameters = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      restParameters[_key - 1] = arguments[_key];
    }

    var BUGGY_ARGUMENT = someFunc(restParameters);
    console.log(BUGGY_ARGUMENT);
  }.apply(null, arguments);
};

Expected behavior/code

const func = (
  someFunc: (DIFFERENT_ARGUMENT: number[]) => boolean = () => false,
  ...restParameters: number[]
) => {
  const BUGGY_ARGUMENT = someFunc(restParameters);
  console.log(BUGGY_ARGUMENT);
};
"use strict";

var func = function func() {
  var someFunc = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {
    return false;
  };
  // THIS _len WORKS FINE!!
  for (var _len = arguments.length, restParameters = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    restParameters[_key - 1] = arguments[_key];
  }

  var BUGGY_ARGUMENT = someFunc(restParameters);
  console.log(BUGGY_ARGUMENT);
};

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

  • Filename: .babelrc
{
  "presets": [
    ["@babel/env", {
      "targets": {
        "browsers": ["last 2 versions"]
      }
    }],
    "@babel/typescript"
  ]
}

Environment

  • Babel version(s): 7.7.7
  • Node/npm version: Node 10
  • OS: macOS 10.14.6
  • Monorepo: no
  • How you are using Babel: cli

Possible Solution

Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.

This also occurs on online playground like the following

https://babeljs.io/repl#?browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=MYewdgzgLgBAZgVzMGBeGAKAUDGEQC2ApgGJLABcmAQgKoDi9AmgPoCCASvbQLICiAOQAqVMAgIAjIgCcA2gF0AlGgB8MCSBAAbIgEMwaTMtRq4urRCIAaHDAB0D6UWgAFXdN3EoMiKPFS5eSxjNQBvW1BIWDpGVk5ufmFDfGIyZAwnV3dPIm9pCEUAbgjwfB07LRAAcwwY5nYuXkEhIqwAX2KsRHSMEJgoaQQiVqA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=es2015%2Creact%2Cstage-2%2Ctypescript&prettier=false&targets=&version=7.7.7&externalPlugins=

@babel-bot
Copy link
Collaborator

Hey @ryokkkke! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@JLHwung
Copy link
Contributor

JLHwung commented Dec 23, 2019

Closing this as duplicate of #10798.

@ryokkkke Thank you! It is a nice report.

@ryokkkke
Copy link
Author

@JLHwung
Thank you for checking!
I'm going to watch conversations on that issue from now on:)

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Mar 25, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug i: duplicate outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

3 participants