Babel-loader: Fat Arrow compiling function twice issue

Created on 14 Aug 2016  路  3Comments  路  Source: babel/babel-loader

When i use a Fat Arrow as function for object keys webpack compiles it like this...


// Before compiling
{
    key1: (arg1) => {
        // ...
    }
}

// After compiling ['es2015']
{
    key1: function key1(arg1) {
         // ...
    }
}

Why is "key1" created twice ?
It should compile like

{
    key1: function(arg1) {
        // ...
    }
}
question

Most helpful comment

Functions that are anonymous automatically get a name based on where they are declared, so in a real ES6 environment you see

var obj = {
    key1: (arg1) => {}
};
console.log(obj.key1.name); // 'key1'

that is the behavior being emulated here.

From http://www.ecma-international.org/ecma-262/7.0/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation

PropertyDefinition: PropertyName: AssignmentExpression

  1. Let propKey be the result of evaluating PropertyName.
  2. ReturnIfAbrupt(propKey).
  3. Let exprValueRef be the result of evaluating AssignmentExpression.
  4. Let propValue be ? GetValue(exprValueRef).
  5. If IsAnonymousFunctionDefinition(AssignmentExpression) is true, then

    1. Let hasNameProperty be ? HasOwnProperty(propValue, "name").

    2. If hasNameProperty is false, perform SetFunctionName(propValue, propKey).

  6. Assert: enumerable is true.
  7. Return CreateDataPropertyOrThrow(object, propKey, propValue).

Specifically step 5 assigns names to any anonymous object properties based on the name of the property.

All 3 comments

This problem is babel core.

Please ask a question in babel.
FYI: https://github.com/babel/babel#looking-for-support

Thanks.

I think this is done so that stacktraces would show 'key1' and not 'anonymous function'. What is the problem you have with it?

Functions that are anonymous automatically get a name based on where they are declared, so in a real ES6 environment you see

var obj = {
    key1: (arg1) => {}
};
console.log(obj.key1.name); // 'key1'

that is the behavior being emulated here.

From http://www.ecma-international.org/ecma-262/7.0/#sec-object-initializer-runtime-semantics-propertydefinitionevaluation

PropertyDefinition: PropertyName: AssignmentExpression

  1. Let propKey be the result of evaluating PropertyName.
  2. ReturnIfAbrupt(propKey).
  3. Let exprValueRef be the result of evaluating AssignmentExpression.
  4. Let propValue be ? GetValue(exprValueRef).
  5. If IsAnonymousFunctionDefinition(AssignmentExpression) is true, then

    1. Let hasNameProperty be ? HasOwnProperty(propValue, "name").

    2. If hasNameProperty is false, perform SetFunctionName(propValue, propKey).

  6. Assert: enumerable is true.
  7. Return CreateDataPropertyOrThrow(object, propKey, propValue).

Specifically step 5 assigns names to any anonymous object properties based on the name of the property.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iwotastic picture iwotastic  路  4Comments

CellChen picture CellChen  路  3Comments

hzoo picture hzoo  路  5Comments

Jovi picture Jovi  路  6Comments

enjoylife picture enjoylife  路  3Comments