<blockquote>
<p dir="auto">Issue originally made by Azat Sattarov (azatey)</p>
</blockquote>
<h3 dir="auto">Bug information</h3>
<ul dir="auto">
<li><strong>Babel version:</strong> 6.3.13</li>
<li><strong>Node version:</strong> 4.2.2</li>
<li><strong>npm version:</strong> 3.5.0</li>
</ul>
<h3 dir="auto">Options</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="--presets es2015,react,stage-0"><pre class="notranslate"><code class="notranslate">--presets es2015,react,stage-0
</code></pre></div>
<h3 dir="auto">Input code</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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 =&gt; {
            const newArr = await Promise.resolve([10, 20, 30]);
            this.names = newArr;
        })
    }
}

var app = new AsyncSample();"><pre class="notranslate"><code class="notranslate">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 =&gt; {
            const newArr = await Promise.resolve([10, 20, 30]);
            this.names = newArr;
        })
    }
}

var app = new AsyncSample();
</code></pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">Both</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="this.names = ..."><pre class="notranslate"><code class="notranslate">this.names = ...
</code></pre></div>
<p dir="auto">assignment operations will fail within anonymous function method and arrow function, because</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="this"><pre class="notranslate"><code class="notranslate">this
</code></pre></div>
<p dir="auto">pointer will be undefined and is captured incorrectly.</p>
<p dir="auto">So this is the part of generated code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[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 &quot;end&quot;:
                                return _context.stop();
                        }
                    }
                }, _callee, _this);
            }));

            return function (_x) {
                return ref.apply(this, arguments);
            };
        })());"><pre class="notranslate"><code class="notranslate">[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);
            };
        })());
</code></pre></div>
<p dir="auto">As you can see it assigns</p>
<blockquote>
<p dir="auto">_this<br>
from within the function body and it is undefined there.</p>
</blockquote>
<h2 dir="auto">Bug Report</h2>
<p dir="auto">Relates to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="103685737" data-permission-text="Title is private" data-url="https://github.com/babel/babel/issues/2276" data-hovercard-type="issue" data-hovercard-url="/babel/babel/issues/2276/hovercard" href="https://github.com/babel/babel/issues/2276">#2276</a></p>
<p dir="auto"><strong>Current Behavior</strong><br>
The following Babel behavior is inconsistent and out of compliance with the specification.</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// 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"><pre class="notranslate"><span class="pl-c">// foo.js</span>
<span class="pl-k">export</span> <span class="pl-k">function</span> <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span><span class="pl-kos">}</span>
<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-s1">foo</span><span class="pl-kos">;</span>

<span class="pl-c">// other.js</span>
<span class="pl-k">import</span> <span class="pl-c1">*</span> <span class="pl-k">as</span> <span class="pl-s1">foo</span> <span class="pl-k">from</span> <span class="pl-s">'./foo'</span><span class="pl-kos">;</span>
<span class="pl-s1">foo</span><span class="pl-kos">.</span><span class="pl-c1">foo</span> <span class="pl-c1">=</span> <span class="pl-s">'bar'</span><span class="pl-kos">;</span> <span class="pl-c">// correctly errors</span>
<span class="pl-s1">foo</span><span class="pl-kos">.</span><span class="pl-c1">default</span> <span class="pl-c1">=</span> <span class="pl-s">'bar'</span><span class="pl-kos">;</span> <span class="pl-c">// does not error; incorrect</span>
<span class="pl-s1">foo</span><span class="pl-kos">.</span><span class="pl-c1">baz</span> <span class="pl-c1">=</span> <span class="pl-s">'bar'</span><span class="pl-kos">;</span> <span class="pl-c">// does not error; incorrect - a Module Namespace Object is not extensible</span></pre></div>
<p dir="auto">The relevant parts of the specification:<br>
<a href="https://www.ecma-international.org/ecma-262/6.0/#sec-module-namespace-objects" rel="nofollow">https://www.ecma-international.org/ecma-262/6.0/#sec-module-namespace-objects</a></p>
<blockquote>
<p dir="auto">A Module Namespace Object is a module namespace exotic object that provides runtime <strong>property-based access</strong> to a module’s exported bindings</p>
</blockquote>
<p dir="auto"><a href="https://www.ecma-international.org/ecma-262/6.0/#sec-module-namespace-exotic-objects" rel="nofollow">https://www.ecma-international.org/ecma-262/6.0/#sec-module-namespace-exotic-objects</a></p>
<blockquote>
<p dir="auto">Each such property has the attributes {[[Configurable]]: false, [[Enumerable]]: true}. Module namespace objects are not extensible.</p>
</blockquote>
<p dir="auto"><a href="https://www.ecma-international.org/ecma-262/6.0/#table-42" rel="nofollow">https://www.ecma-international.org/ecma-262/6.0/#table-42</a> - the default export is a named export named "default"</p>
<p dir="auto"><strong>Input Code</strong><br>
<a href="https://github.com/yossarian21/import-spec">https://github.com/yossarian21/import-spec</a></p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="npm install
npm test"><pre class="notranslate">npm install
npm <span class="pl-c1">test</span></pre></div>
<p dir="auto"><strong>Expected behavior/code</strong><br>
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.</p>
<p dir="auto"><strong>Babel Configuration (.babelrc, package.json, cli command)</strong></p>
<p dir="auto"><a href="https://github.com/yossarian21/import-spec/blob/master/.babelrc">https://github.com/yossarian21/import-spec/blob/master/.babelrc</a></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
    &quot;presets&quot;: [
        &quot;@babel/preset-env&quot;
    ]
}"><pre class="notranslate"><span class="pl-kos">{</span>
    <span class="pl-s">"presets"</span>: <span class="pl-kos">[</span>
        <span class="pl-s">"@babel/preset-env"</span>
    <span class="pl-kos">]</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><strong>Environment</strong></p>
<ul dir="auto">
<li>Babel version(s): 7.1.6</li>
<li>Node/npm version: Node 8.11.1, NPM 5.6.0</li>
<li>OS: OSX 10.13.6</li>
<li>Monorepo N/A</li>
<li>How you are using Babel: cli, register, and loader all affected</li>
</ul>
<p dir="auto"><strong>Possible Solution</strong><br>
For a default export:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// instead of
newObj.default = foo;
// could produce output like a named export:
Object.defineProperty(newObj, 'default', {
    enumerable: true,
    get: function () {
        return foo.default;
    }
);"><pre class="notranslate"><span class="pl-c">// instead of</span>
<span class="pl-s1">newObj</span><span class="pl-kos">.</span><span class="pl-c1">default</span> <span class="pl-c1">=</span> <span class="pl-s1">foo</span><span class="pl-kos">;</span>
<span class="pl-c">// could produce output like a named export:</span>
<span class="pl-v">Object</span><span class="pl-kos">.</span><span class="pl-en">defineProperty</span><span class="pl-kos">(</span><span class="pl-s1">newObj</span><span class="pl-kos">,</span> <span class="pl-s">'default'</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
    <span class="pl-c1">enumerable</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
    <span class="pl-en">get</span>: <span class="pl-k">function</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
        <span class="pl-k">return</span> <span class="pl-s1">foo</span><span class="pl-kos">.</span><span class="pl-c1">default</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos"></span>
<span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>