Browse Source

Merge pull request #1168 from rollup/gh-1083

bind function expression ids to the child scope
gh-786
Rich Harris 8 years ago
committed by GitHub
parent
commit
040938850c
  1. 23
      src/ast/nodes/FunctionExpression.js
  2. 11
      test/function/preserves-function-expression-names/_config.js
  3. 5
      test/function/preserves-function-expression-names/main.js

23
src/ast/nodes/FunctionExpression.js

@ -1,12 +1,29 @@
import Node from '../Node.js';
export default class FunctionExpression extends Node {
activate () {
if ( this.activated ) return;
this.activated = true;
const scope = this.body.scope;
this.params.forEach( param => param.run( scope ) ); // in case of assignment patterns
this.body.run();
}
addReference () {
/* noop? */
}
bind () {
if ( this.id ) this.id.bind( this.body.scope );
this.params.forEach( param => param.bind( this.body.scope ) );
this.body.bind();
}
getName () {
return this.id && this.id.name;
}
hasEffects () {
return false;
}
@ -14,7 +31,11 @@ export default class FunctionExpression extends Node {
initialise ( scope ) {
this.body.createScope( scope );
if ( this.id ) this.id.initialise( this.body.scope );
if ( this.id ) {
this.id.initialise( this.body.scope );
this.body.scope.addDeclaration( this.id.name, this, false, false );
}
this.params.forEach( param => param.initialise( this.body.scope ) );
this.body.initialise();
}

11
test/function/preserves-function-expression-names/_config.js

@ -0,0 +1,11 @@
const assert = require( 'assert' );
module.exports = {
description: 'does not rewrite function expression names incorrectly (#1083)',
options: {
external: [ 'path' ]
},
exports ( exports ) {
assert.equal( exports.x.name, 'basename' );
}
};

5
test/function/preserves-function-expression-names/main.js

@ -0,0 +1,5 @@
import { basename } from 'path';
var x = function basename () {};
export { x };
Loading…
Cancel
Save