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

undefined is compiled as function name by using template literal as method name. #7199

Closed
sixwinds opened this Issue Jan 11, 2018 · 4 comments

Comments

Projects
None yet
4 participants
@sixwinds

sixwinds commented Jan 11, 2018

BUG REPORT

As metioned on the title I give an example:

var methodName = 'test';
var json = {
  [`${methodName}`]: function (){
    
  }
}

is compiled to following code:

var methodName = 'test';
var json = _defineProperty({}, '' + methodName, function undefined() {}); // function name is undefined

Commonly it's ok, but if I use object deconstruction in method, the compiling result will cause undefined overwriting. For example:

Current Behavior

var methodName = 'test';
var json = {
  [`${methodName}`]: function (){
    let o = {};let {a='2'}=o
    console.log( a )
  }
}

is compiled to following code:

var methodName = 'test';
var json = _defineProperty({}, '' + methodName, function undefined() {
  var o = {};var _o$a = o.a,
      a = _o$a === undefined ? '2' : _o$a; // undefined in this line is referred to current function

  console.log(a);
});

In above code, variable "a" will not be assigned to 2, because _o$a is not equal to "undefined", which is referred to current function; I think following Expected Behavior is proper behavior.

Expected Behavior

var methodName = 'test';
var json = _defineProperty({}, '' + methodName, function () { // no undefined name
  var o = {};var _o$a = o.a,
      a = _o$a === undefined ? '2' : _o$a; 

  console.log(a);
});

Babel Configuration (.babelrc, package.json)

{
  "presets": ["env","react"],
  "plugins": ["syntax-dynamic-import"]
}
{
  "name": "dynamic-import",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "start": "webpack-dev-server --open"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-react": "^6.24.1",
    "clean-webpack-plugin": "^0.1.17",
    "html-webpack-plugin": "^2.30.1",
    "webpack": "^3.10.0",
    "webpack-dev-server": "^2.9.7"
  },
  "dependencies": {
    "react": "^16.2.0",
    "react-dom": "^16.2.0"
  }
}

Environment

software version(s)
Babel see above Babel Configuration part ,
node v8.9.1
npm 5.5.1
Operating System macOS Sierra v10.12.6
@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Jan 11, 2018

Collaborator

Hey @sixwinds! 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.

Collaborator

babel-bot commented Jan 11, 2018

Hey @sixwinds! 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.

@Axnyff

This comment has been minimized.

Show comment
Hide comment
@Axnyff

Axnyff Feb 21, 2018

Contributor

It indeed looks like a bug with computed property and template literals, the name of the function cannot be computed properly.

Reproduction in REPL

I am very willing to try and fix this issue

Contributor

Axnyff commented Feb 21, 2018

It indeed looks like a bug with computed property and template literals, the name of the function cannot be computed properly.

Reproduction in REPL

I am very willing to try and fix this issue

@xtuc xtuc added the i: bug label Feb 21, 2018

@xtuc

This comment has been minimized.

Show comment
Hide comment
@xtuc

xtuc Feb 21, 2018

Member

Yes that's a bug.

@Axnyff It seems that nobody is working on it, you can go ahead! Thanks

Member

xtuc commented Feb 21, 2018

Yes that's a bug.

@Axnyff It seems that nobody is working on it, you can go ahead! Thanks

@xtuc xtuc added the claimed label Feb 21, 2018

@Axnyff

This comment has been minimized.

Show comment
Hide comment
@Axnyff

Axnyff Feb 24, 2018

Contributor

The problem seems to be that that line is getting executed for TemplateLiterals however, they do not have a value so the function name is set to undefined.

I'm not sure what is the best way to fix it though

  • Check on the condition before that the value is not undefined ( or is not falsy)
  • Don't generate an id for a template literal by modifying that condition

We could also try to compute a value for some computed template literals but it seems almost useless as it will only work when template literals could be changed with regular single quotes or double quotes.

I'm also not really sure where / how to add a test for that bug, should it go in the babel-helper-function-name or in the babel-plugin-transform-function-name ( in the first case I can't manage to make fixtures work)

Contributor

Axnyff commented Feb 24, 2018

The problem seems to be that that line is getting executed for TemplateLiterals however, they do not have a value so the function name is set to undefined.

I'm not sure what is the best way to fix it though

  • Check on the condition before that the value is not undefined ( or is not falsy)
  • Don't generate an id for a template literal by modifying that condition

We could also try to compute a value for some computed template literals but it seems almost useless as it will only work when template literals could be changed with regular single quotes or double quotes.

I'm also not really sure where / how to add a test for that bug, should it go in the babel-helper-function-name or in the babel-plugin-transform-function-name ( in the first case I can't manage to make fixtures work)

@lock lock bot added the outdated label Jul 10, 2018

@lock lock bot locked as resolved and limited conversation to collaborators Jul 10, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.