Issue 1:
Issue originally made by Azat Sattarov (azatey)

Bug information

Babel version: 6.3.13

Node version: 4.2.2

npm version: 3.5.0

Options

--presets es2015,react,stage-0

Input code

class AsyncSample {
constructor() {
this.names = [];
//this.fetch();

[1,2,3].forEach(async function(i) {
const newArr = await Promise.resolve([10, 20, 30]);
this.names = newArr;
});

[1,2,3].forEach(async i => {
const newArr = await Promise.resolve([10, 20, 30]);
this.names = newArr;
})
}
}

var app = new AsyncSample();

Description

Both

this.names = ...

assignment operations will fail within anonymous function method and arrow function, because

this

pointer will be undefined and is captured incorrectly.

So this is the part of generated code:

[1, 2, 3].forEach((function () {
var _this = this;

var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(i) {
var newArr;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return Promise.resolve([10, 20, 30]);

case 2:
newArr = _context.sent;

_this.names = newArr;

case 4:
case "end":
return _context.stop();
}
}
}, _callee, _this);
}));

return function (_x) {
return ref.apply(this, arguments);
};
})());

As you can see it assigns

_this

from within the function body and it is undefined there.

Issue 2:
Bug Report

Relates to #2276

Current Behavior

The following Babel behavior is inconsistent and out of compliance with the specification.

// foo.js
export function foo() {}
export default foo;

// other.js
import * as foo from './foo';
foo.foo = 'bar'; // correctly errors
foo.default = 'bar'; // does not error; incorrect
foo.baz = 'bar'; // does not error; incorrect - a Module Namespace Object is not extensible

The relevant parts of the specification:

https://www.ecma-international.org/ecma-262/6.0/#sec-module-namespace-objects

A Module Namespace Object is a module namespace exotic object that provides runtime property-based access to a module’s exported bindings

https://www.ecma-international.org/ecma-262/6.0/#sec-module-namespace-exotic-objects

Each such property has the attributes {[[Configurable]]: false, [[Enumerable]]: true}. Module namespace objects are not extensible.

https://www.ecma-international.org/ecma-262/6.0/#table-42 - the default export is a named export named "default"

Input Code

https://github.com/yossarian21/import-spec

npm install
npm test

Expected behavior/code

Behavior should be consistent when attempting to assign to an exported symbol named "default" or not named "default" and should conform to the ECMAScript specification.

Babel Configuration (.babelrc, package.json, cli command)

https://github.com/yossarian21/import-spec/blob/master/.babelrc

{
"presets": [
"@babel/preset-env"
]
}

Environment

Babel version(s): 7.1.6

Node/npm version: Node 8.11.1, NPM 5.6.0

OS: OSX 10.13.6

Monorepo N/A

How you are using Babel: cli, register, and loader all affected

Possible Solution

For a default export:

// instead of
newObj.default = foo;
// could produce output like a named export:
Object.defineProperty(newObj, 'default', {
enumerable: true,
get: function () {
return foo.default;
}
);