<h2 dir="auto">Bug Report</h2>
<p dir="auto"><strong>Current Behavior</strong><br>
A clear and concise description of the behavior.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/3963651/45399034-3584e500-b679-11e8-9b39-74505e92b71e.png"><img src="https://user-images.githubusercontent.com/3963651/45399034-3584e500-b679-11e8-9b39-74505e92b71e.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Input Code</strong></p>
<ul dir="auto">
<li>REPL or Repo link if applicable:</li>
</ul>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="TypeError: Cannot read property 'buildError' of undefined
    at Scope.checkBlockScopedCollisions (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/scope/index.js:347:22)
    at Scope.registerBinding (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/scope/index.js:504:16)
    at Scope.registerDeclaration (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/scope/index.js:452:14)
    at Object.Declaration (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/scope/index.js:125:12)
    at NodePath._call (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/path/context.js:40:17)
    at NodePath.visit (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitMultiple (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/context.js:85:17)
    at TraversalContext.visit (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/context.js:144:19)
"><pre class="notranslate">TypeError: Cannot <span class="pl-c1">read</span> property <span class="pl-s"><span class="pl-pds">'</span>buildError<span class="pl-pds">'</span></span> of undefined
    at Scope.checkBlockScopedCollisions (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/scope/index.js:347:22)
    at Scope.registerBinding (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/scope/index.js:504:16)
    at Scope.registerDeclaration (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/scope/index.js:452:14)
    at Object.Declaration (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/scope/index.js:125:12)
    at NodePath._call (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/path/context.js:40:17)
    at NodePath.visit (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitMultiple (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/context.js:85:17)
    at TraversalContext.visit (/Users/puss/work/ioc/parrot-tool-must/node_modules/_@babel_traverse@7.0.0@@babel/traverse/lib/context.js:144:19)
</pre></div>
<p dir="auto"><strong>Environment</strong></p>
<ul dir="auto">
<li>Babel version(s): [v7.0.0]</li>
<li>Node/npm version: [Node v10.9.0/npm 5.15.0]</li>
<li>OS: [e.g. OSX 10.13.6]</li>
<li>Monorepo [e.g. yes/no/Lerna]</li>
<li>How you are using Babel: [<code class="notranslate">AST parser</code>]<br>
.</li>
</ul>
<h2 dir="auto">Bug Report</h2>
<p dir="auto"><strong>Current Behavior</strong><br>
<code class="notranslate">@babel/traverse</code> tries to throw a <code class="notranslate">Duplicate declaration</code> error for key duplicates in different ambient module declarations in one file. <code class="notranslate">@babel/parser</code> with <code class="notranslate">typescript</code> plugin is used to build an AST.<br>
<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="180482095" data-permission-text="Title is private" data-url="https://github.com/babel/babel/issues/4640" data-hovercard-type="issue" data-hovercard-url="/babel/babel/issues/4640/hovercard" href="https://github.com/babel/babel/issues/4640">#4640</a> explains why <code class="notranslate">Cannot read property 'file' of undefined</code> is thrown.</p>
<p dir="auto"><strong>Input Code</strong></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const parse = require('@babel/parser').parse;
const traverse = require('@babel/traverse').default;

const code = `
declare module 'module1' {
  export const variable: string;
}

declare module 'module2' {
  export const variable: number;
}
`;

const ast = parse(code, {
  sourceType: 'module',
  plugins: ['typescript']
});

traverse(ast, {
  enter(path) {
    // smth
  }
});"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-s1">parse</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'@babel/parser'</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">parse</span><span class="pl-kos">;</span>
<span class="pl-k">const</span> <span class="pl-s1">traverse</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">(</span><span class="pl-s">'@babel/traverse'</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-c1">default</span><span class="pl-kos">;</span>

<span class="pl-k">const</span> <span class="pl-s1">code</span> <span class="pl-c1">=</span> <span class="pl-s">`</span>
<span class="pl-s">declare module 'module1' {</span>
<span class="pl-s">  export const variable: string;</span>
<span class="pl-s">}</span>
<span class="pl-s"></span>
<span class="pl-s">declare module 'module2' {</span>
<span class="pl-s">  export const variable: number;</span>
<span class="pl-s">}</span>
<span class="pl-s">`</span><span class="pl-kos">;</span>

<span class="pl-k">const</span> <span class="pl-s1">ast</span> <span class="pl-c1">=</span> <span class="pl-s1">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-c1">plugins</span>: <span class="pl-kos">[</span><span class="pl-s">'typescript'</span><span class="pl-kos">]</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-s1">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-en">enter</span><span class="pl-kos">(</span><span class="pl-s1">path</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
    <span class="pl-c">// smth</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>Output</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="...\node_modules\@babel\traverse\lib\scope\index.js:347
      throw this.hub.file.buildCodeFrameError(id, `Duplicate declaration &quot;${name}&quot;`, TypeError);
                     ^

TypeError: Cannot read property 'file' of undefined
    at Scope.checkBlockScopedCollisions (...\node_modules\@babel\traverse\lib\scope\index.js:347:22)
    at Scope.registerBinding (...\node_modules\@babel\traverse\lib\scope\index.js:506:16)
    at Scope.registerDeclaration (...\node_modules\@babel\traverse\lib\scope\index.js:454:14)
    at Object.Declaration (...\node_modules\@babel\traverse\lib\scope\index.js:125:12)
    at NodePath._call (...\node_modules\@babel\traverse\lib\path\context.js:53:20)
    at NodePath.call (...\node_modules\@babel\traverse\lib\path\context.js:40:17)
    at NodePath.visit (...\node_modules\@babel\traverse\lib\path\context.js:88:12)
    at TraversalContext.visitQueue (...\node_modules\@babel\traverse\lib\context.js:118:16)
    at TraversalContext.visitMultiple (...\node_modules\@babel\traverse\lib\context.js:85:17)
    at TraversalContext.visit (...\node_modules\@babel\traverse\lib\context.js:144:19)"><pre class="notranslate"><code class="notranslate">...\node_modules\@babel\traverse\lib\scope\index.js:347
      throw this.hub.file.buildCodeFrameError(id, `Duplicate declaration "${name}"`, TypeError);
                     ^

TypeError: Cannot read property 'file' of undefined
    at Scope.checkBlockScopedCollisions (...\node_modules\@babel\traverse\lib\scope\index.js:347:22)
    at Scope.registerBinding (...\node_modules\@babel\traverse\lib\scope\index.js:506:16)
    at Scope.registerDeclaration (...\node_modules\@babel\traverse\lib\scope\index.js:454:14)
    at Object.Declaration (...\node_modules\@babel\traverse\lib\scope\index.js:125:12)
    at NodePath._call (...\node_modules\@babel\traverse\lib\path\context.js:53:20)
    at NodePath.call (...\node_modules\@babel\traverse\lib\path\context.js:40:17)
    at NodePath.visit (...\node_modules\@babel\traverse\lib\path\context.js:88:12)
    at TraversalContext.visitQueue (...\node_modules\@babel\traverse\lib\context.js:118:16)
    at TraversalContext.visitMultiple (...\node_modules\@babel\traverse\lib\context.js:85:17)
    at TraversalContext.visit (...\node_modules\@babel\traverse\lib\context.js:144:19)
</code></pre></div>
<p dir="auto"><strong>Expected behavior</strong><br>
Each module declaration has its own block scope, so the <code class="notranslate">Duplicate declaration</code> error shouldn't be thrown.</p>
<p dir="auto"><strong>Environment</strong></p>
<ul dir="auto">
<li>Babel version: v7.0.0-beta.51</li>
<li>Node/npm version: node 8.11.3 / npm 5.6.0</li>
<li>OS: Windows 10</li>
</ul>