Browse Source

place function IDs in correct scope

declarations-and-references
Rich-Harris 9 years ago
parent
commit
96e074d4f1
  1. 2
      src/Module.js
  2. 8
      src/Statement.js
  3. 1
      src/ast/Scope.js
  4. 4
      src/ast/attachScopes.js

2
src/Module.js

@ -535,7 +535,7 @@ export default class Module {
statement.references.forEach( reference => {
const declaration = reference.declaration;
if ( reference.declaration ) {
if ( declaration ) {
const { start, end } = reference;
const name = declaration.render( es6 );

8
src/Statement.js

@ -135,7 +135,13 @@ export default class Statement {
}
if ( isReference( node, parent ) ) {
const reference = new Reference( node, scope );
// function declaration IDs are a special case – they're associated
// with the parent scope
const referenceScope = parent.type === 'FunctionDeclaration' && node === parent.id ?
scope.parent :
scope;
const reference = new Reference( node, referenceScope );
references.push( reference );
reference.isImmediatelyUsed = !readDepth;

1
src/ast/Scope.js

@ -86,6 +86,7 @@ export default class Scope {
this.parent.addDeclaration( node, isBlockDeclaration, isVar );
} else {
extractNames( node.id ).forEach( name => {
if ( this.declarations[ name ] ) throw new Error( `Duplicate declaration (${name})` );
this.declarations[ name ] = new Declaration( name );
});
}

4
src/ast/attachScopes.js

@ -11,8 +11,6 @@ export default function attachScopes ( statement ) {
walk( node, {
enter ( node, parent ) {
let newScope;
// function foo () {...}
// class Foo {...}
if ( /(Function|Class)Declaration/.test( node.type ) ) {
@ -29,6 +27,8 @@ export default function attachScopes ( statement ) {
});
}
let newScope;
// create new function scope
if ( /Function/.test( node.type ) ) {
newScope = new Scope({

Loading…
Cancel
Save