<h2 dir="auto">Bug Report</h2>
<p dir="auto"><strong>Current Behavior</strong><br>
const aren't being transpiled in the following situation:</p>
<ul dir="auto">
<li>they are defined on a dependency file, in node_modules</li>
<li>they have a function assigned <code class="notranslate">const a = param =&gt; 'whatever'</code></li>
<li>that function is used by another function that is consumed by the entry point</li>
</ul>
<p dir="auto"><strong>Input Code</strong></p>
<ul dir="auto">
<li>Filename: <code class="notranslate">entry.js</code></li>
</ul>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import { foo } from 'package_in_node_modules';

export default class Whatever {
	constructor(param) {
		console.log(foo(param));
	}
}"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-s1">foo</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'package_in_node_modules'</span><span class="pl-kos">;</span>

<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-k">class</span> <span class="pl-v">Whatever</span> <span class="pl-kos">{</span>
	<span class="pl-en">constructor</span><span class="pl-kos">(</span><span class="pl-s1">param</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
		<span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-s1">param</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>
<ul dir="auto">
<li>Filename: <code class="notranslate">package_in_node_modules/index.js</code></li>
</ul>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const helper = param =&gt; param.length;

export function foo(param) {
	return helper(param)
}"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-en">helper</span> <span class="pl-c1">=</span> <span class="pl-s1">param</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">param</span><span class="pl-kos">.</span><span class="pl-c1">length</span><span class="pl-kos">;</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-s1">param</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
	<span class="pl-k">return</span> <span class="pl-en">helper</span><span class="pl-kos">(</span><span class="pl-s1">param</span><span class="pl-kos">)</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><em><strong>build ouptut</strong></em></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError(&quot;Cannot call a class as a function&quot;);
  }
}

const helper = param =&gt; param.length;

function foo(param) {
  return helper(param);
}

var Whatever = function Whatever(param) {
  _classCallCheck(this, Whatever);

  console.log(foo(param));
};

export default Whatever;"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">_classCallCheck</span><span class="pl-kos">(</span><span class="pl-s1">instance</span><span class="pl-kos">,</span> <span class="pl-v">Constructor</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-kos">(</span><span class="pl-s1">instance</span> <span class="pl-k">instanceof</span> <span class="pl-v">Constructor</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-v">TypeError</span><span class="pl-kos">(</span><span class="pl-s">"Cannot call a class as a function"</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">const</span> <span class="pl-en">helper</span> <span class="pl-c1">=</span> <span class="pl-s1">param</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">param</span><span class="pl-kos">.</span><span class="pl-c1">length</span><span class="pl-kos">;</span>

<span class="pl-k">function</span> <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-s1">param</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">return</span> <span class="pl-en">helper</span><span class="pl-kos">(</span><span class="pl-s1">param</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">var</span> <span class="pl-v">Whatever</span> <span class="pl-c1">=</span> <span class="pl-k">function</span> <span class="pl-v">Whatever</span><span class="pl-kos">(</span><span class="pl-s1">param</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-en">_classCallCheck</span><span class="pl-kos">(</span><span class="pl-smi">this</span><span class="pl-kos">,</span> <span class="pl-v">Whatever</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-s1">param</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>

<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-v">Whatever</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><strong>Expected behavior/code</strong><br>
<code class="notranslate">const helper</code> being replaced by <code class="notranslate">var helper</code></p>
<p dir="auto"><em><strong>expected ouptut</strong></em></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="function _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError(&quot;Cannot call a class as a function&quot;);
  }
}

var helper = param =&gt; param.length;

function foo(param) {
  return helper(param);
}

var Whatever = function Whatever(param) {
  _classCallCheck(this, Whatever);

  console.log(foo(param));
};

export default Whatever;"><pre class="notranslate"><span class="pl-k">function</span> <span class="pl-en">_classCallCheck</span><span class="pl-kos">(</span><span class="pl-s1">instance</span><span class="pl-kos">,</span> <span class="pl-v">Constructor</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-c1">!</span><span class="pl-kos">(</span><span class="pl-s1">instance</span> <span class="pl-k">instanceof</span> <span class="pl-v">Constructor</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-k">throw</span> <span class="pl-k">new</span> <span class="pl-v">TypeError</span><span class="pl-kos">(</span><span class="pl-s">"Cannot call a class as a function"</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">var</span> <span class="pl-en">helper</span> <span class="pl-c1">=</span> <span class="pl-s1">param</span> <span class="pl-c1">=&gt;</span> <span class="pl-s1">param</span><span class="pl-kos">.</span><span class="pl-c1">length</span><span class="pl-kos">;</span>

<span class="pl-k">function</span> <span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-s1">param</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-k">return</span> <span class="pl-en">helper</span><span class="pl-kos">(</span><span class="pl-s1">param</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span>

<span class="pl-k">var</span> <span class="pl-v">Whatever</span> <span class="pl-c1">=</span> <span class="pl-k">function</span> <span class="pl-v">Whatever</span><span class="pl-kos">(</span><span class="pl-s1">param</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
  <span class="pl-en">_classCallCheck</span><span class="pl-kos">(</span><span class="pl-smi">this</span><span class="pl-kos">,</span> <span class="pl-v">Whatever</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">log</span><span class="pl-kos">(</span><span class="pl-en">foo</span><span class="pl-kos">(</span><span class="pl-s1">param</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>

<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-v">Whatever</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><strong>Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)</strong></p>
<ul dir="auto">
<li>Filename: <code class="notranslate">.babelrc</code></li>
</ul>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
	&quot;env&quot;: {
		&quot;es5&quot;: {
			&quot;presets&quot;: [
				[&quot;@babel/preset-env&quot;, {
					&quot;useBuiltIns&quot;: &quot;usage&quot;,
					&quot;corejs&quot;: 3,
					&quot;modules&quot;: false,
					&quot;targets&quot;:  &quot;&gt;= 0.01%&quot;
				}]
			]
		}
	}
}"><pre class="notranslate"><span class="pl-kos">{</span>
	<span class="pl-s">"env"</span>: <span class="pl-kos">{</span>
		<span class="pl-s">"es5"</span>: <span class="pl-kos">{</span>
			<span class="pl-s">"presets"</span>: <span class="pl-kos">[</span>
				<span class="pl-kos">[</span><span class="pl-s">"@babel/preset-env"</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
					<span class="pl-s">"useBuiltIns"</span>: <span class="pl-s">"usage"</span><span class="pl-kos">,</span>
					<span class="pl-s">"corejs"</span>: <span class="pl-c1">3</span><span class="pl-kos">,</span>
					<span class="pl-s">"modules"</span>: <span class="pl-c1">false</span><span class="pl-kos">,</span>
					<span class="pl-s">"targets"</span>:  <span class="pl-s">"&gt;= 0.01%"</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>
<span class="pl-kos">}</span></pre></div>
<ul dir="auto">
<li>Filename: <code class="notranslate">rollup.config.js</code></li>
</ul>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default {
	input: ['./entry.js'],
	output: [
		{
			file: `./dist/index.es5.js`,
			format: 'esm'
		}
	],
	plugins: [
		resolve(),
		commonjs(),
		babel({
			envName: 'es5'
		})
	]
};"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">babel</span> <span class="pl-k">from</span> <span class="pl-s">'rollup-plugin-babel'</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-s1">resolve</span> <span class="pl-k">from</span> <span class="pl-s">'rollup-plugin-node-resolve'</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-s1">commonjs</span> <span class="pl-k">from</span> <span class="pl-s">'rollup-plugin-commonjs'</span><span class="pl-kos">;</span>

<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-kos">{</span>
	<span class="pl-c1">input</span>: <span class="pl-kos">[</span><span class="pl-s">'./entry.js'</span><span class="pl-kos">]</span><span class="pl-kos">,</span>
	<span class="pl-c1">output</span>: <span class="pl-kos">[</span>
		<span class="pl-kos">{</span>
			<span class="pl-c1">file</span>: <span class="pl-s">`./dist/index.es5.js`</span><span class="pl-kos">,</span>
			<span class="pl-c1">format</span>: <span class="pl-s">'esm'</span>
		<span class="pl-kos">}</span>
	<span class="pl-kos">]</span><span class="pl-kos">,</span>
	<span class="pl-c1">plugins</span>: <span class="pl-kos">[</span>
		<span class="pl-en">resolve</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
		<span class="pl-en">commonjs</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
		<span class="pl-en">babel</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
			<span class="pl-c1">envName</span>: <span class="pl-s">'es5'</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>
<p dir="auto"><strong>Environment</strong></p>
<ul dir="auto">
<li>Babel version(s): 7.7.4</li>
<li>Node/npm version: node 8.16.2 /  npm: 6.4.1</li>
<li>OS: macOS 10.15.1</li>
<li>Monorepo: Lerna</li>
<li>How you are using Babel: loader</li>
</ul>
<h3 dir="auto">Input Code</h3>
<p dir="auto">In a new folder run <code class="notranslate">npm install babel-core babylon</code>, then put the following code in <code class="notranslate">traverse-error.js</code></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="var code = `
import foo from './foo'
const foo = undefined
`

var babelCore = require('babel-core')
var babylon = require('babylon')

var ast = {
    type: 'File',
    start: 0,
    end: code.length,
    program: babylon.parse(code, {
        sourceType: 'module'
    })
}

babelCore.traverse(ast, {})"><pre class="notranslate"><span class="pl-k">var</span> <span class="pl-s1">code</span> <span class="pl-c1">=</span> <span class="pl-s">`</span>
<span class="pl-s">import foo from './foo'</span>
<span class="pl-s">const foo = undefined</span>
<span class="pl-s">`</span>

<span class="pl-k">var</span> <span class="pl-s1">babelCore</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'babel-core'</span><span class="pl-kos">)</span>
<span class="pl-k">var</span> <span class="pl-s1">babylon</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'babylon'</span><span class="pl-kos">)</span>

<span class="pl-k">var</span> <span class="pl-s1">ast</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span>
    <span class="pl-c1">type</span>: <span class="pl-s">'File'</span><span class="pl-kos">,</span>
    <span class="pl-c1">start</span>: <span class="pl-c1">0</span><span class="pl-kos">,</span>
    <span class="pl-c1">end</span>: <span class="pl-s1">code</span><span class="pl-kos">.</span><span class="pl-c1">length</span><span class="pl-kos">,</span>
    <span class="pl-c1">program</span>: <span class="pl-s1">babylon</span><span class="pl-kos">.</span><span class="pl-en">parse</span><span class="pl-kos">(</span><span class="pl-s1">code</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
        <span class="pl-c1">sourceType</span>: <span class="pl-s">'module'</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span>
<span class="pl-kos">}</span>

<span class="pl-s1">babelCore</span><span class="pl-kos">.</span><span class="pl-en">traverse</span><span class="pl-kos">(</span><span class="pl-s1">ast</span><span class="pl-kos">,</span> <span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">)</span></pre></div>
<h3 dir="auto">Expected Behavior</h3>
<p dir="auto"><code class="notranslate">node traverse-error.js</code> should run with no output. If you simply rename the <code class="notranslate">import foo</code> or <code class="notranslate">const foo</code> to avoid the collision, it will run as expected.</p>
<h3 dir="auto">Current Behavior</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="asa@asa:/tmp/babel-issue$ node traverse-error.js 
/tmp/babel-issue/node_modules/babel-traverse/lib/scope/index.js:397
      throw this.hub.file.buildCodeFrameError(id, messages.get(&quot;scopeDuplicateDeclaration&quot;, name), TypeError);
                    ^

TypeError: Cannot read property 'file' of undefined
    at Scope.checkBlockScopedCollisions (/tmp/babel-issue/node_modules/babel-traverse/lib/scope/index.js:397:21)
    at Scope.registerBinding (/tmp/babel-issue/node_modules/babel-traverse/lib/scope/index.js:579:16)
    at Scope.registerDeclaration (/tmp/babel-issue/node_modules/babel-traverse/lib/scope/index.js:483:14)
    at Object.BlockScoped (/tmp/babel-issue/node_modules/babel-traverse/lib/scope/index.js:185:28)
    at Object.newFn (/tmp/babel-issue/node_modules/babel-traverse/lib/visitors.js:318:17)
    at NodePath._call (/tmp/babel-issue/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/tmp/babel-issue/node_modules/babel-traverse/lib/path/context.js:44:14)
    at NodePath.visit (/tmp/babel-issue/node_modules/babel-traverse/lib/path/context.js:105:12)
    at TraversalContext.visitQueue (/tmp/babel-issue/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitMultiple (/tmp/babel-issue/node_modules/babel-traverse/lib/context.js:103:17)"><pre class="notranslate"><code class="notranslate">asa@asa:/tmp/babel-issue$ node traverse-error.js 
/tmp/babel-issue/node_modules/babel-traverse/lib/scope/index.js:397
      throw this.hub.file.buildCodeFrameError(id, messages.get("scopeDuplicateDeclaration", name), TypeError);
                    ^

TypeError: Cannot read property 'file' of undefined
    at Scope.checkBlockScopedCollisions (/tmp/babel-issue/node_modules/babel-traverse/lib/scope/index.js:397:21)
    at Scope.registerBinding (/tmp/babel-issue/node_modules/babel-traverse/lib/scope/index.js:579:16)
    at Scope.registerDeclaration (/tmp/babel-issue/node_modules/babel-traverse/lib/scope/index.js:483:14)
    at Object.BlockScoped (/tmp/babel-issue/node_modules/babel-traverse/lib/scope/index.js:185:28)
    at Object.newFn (/tmp/babel-issue/node_modules/babel-traverse/lib/visitors.js:318:17)
    at NodePath._call (/tmp/babel-issue/node_modules/babel-traverse/lib/path/context.js:76:18)
    at NodePath.call (/tmp/babel-issue/node_modules/babel-traverse/lib/path/context.js:44:14)
    at NodePath.visit (/tmp/babel-issue/node_modules/babel-traverse/lib/path/context.js:105:12)
    at TraversalContext.visitQueue (/tmp/babel-issue/node_modules/babel-traverse/lib/context.js:150:16)
    at TraversalContext.visitMultiple (/tmp/babel-issue/node_modules/babel-traverse/lib/context.js:103:17)
</code></pre></div>
<h3 dir="auto">Context</h3>
<p dir="auto">I have an Atom plugin called <a href="https://atom.io/packages/js-hyperclick" rel="nofollow">js-hyperclick</a>. It parses your JS files and allows you to ctrl+click filenames or variables to jump to their definitions. Today I've been working on getting <code class="notranslate">js-hyperclick</code> to inform the user when it can't function. If you have a syntax error that causes <code class="notranslate">babylon.parse</code> to throw I can help the user:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/324999/19017137/c1863948-87e4-11e6-8fca-4cccdbecc5f1.png"><img src="https://cloud.githubusercontent.com/assets/324999/19017137/c1863948-87e4-11e6-8fca-4cccdbecc5f1.png" alt="selection_019" style="max-width: 100%;"></a></p>
<p dir="auto">Because this error happens when traversing and the error points to <code class="notranslate">babel-traverse</code> code, showing the user doesn't help them:</p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://cloud.githubusercontent.com/assets/324999/19017179/8ae985ba-87e5-11e6-9cd4-6d852dc01d2f.png"><img src="https://cloud.githubusercontent.com/assets/324999/19017179/8ae985ba-87e5-11e6-9cd4-6d852dc01d2f.png" alt="selection_020" style="max-width: 100%;"></a></p>
<p dir="auto">I tend to run into this when I'm trying to extract <code class="notranslate">foo</code> into its own file. I'll add <code class="notranslate">import foo from './new-file'</code>, then if this were able to parse I could ctrl+click the <code class="notranslate">./new-file</code> or <code class="notranslate">foo</code> to open a new editor at that path and cut/paste whatever <code class="notranslate">foo</code> needs.</p>
<h3 dir="auto">Your Environment</h3>

<table role="table">
<thead>
<tr>
<th>software</th>
<th>version</th>
</tr>
</thead>
<tbody>
<tr>
<td>Babel</td>
<td>5.8.25 and 6.17.0</td>
</tr>
<tr>
<td>node</td>
<td>4 and 6</td>
</tr>
</tbody>
</table>
<p dir="auto">I wouldn't expect it to get fixed back in Babel5, but the code above also demonstrates the error with <code class="notranslate">npm install babel-core@5</code> and replace <code class="notranslate">babylon.parse</code> with <code class="notranslate">babelCore.parse</code></p>