Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Hoist default exported anonymous function declarations
Fixes #1784
  • Loading branch information
nicolo-ribaudo committed Dec 10, 2017
1 parent d0ba36b commit e1146fd
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 30 deletions.
12 changes: 7 additions & 5 deletions src/ast/nodes/ExportDefaultDeclaration.js
Expand Up @@ -35,20 +35,22 @@ export default class ExportDefaultDeclaration extends Node {

// paren workaround: find first non-whitespace character position after `export default`
let declaration_start;
let id_index;
if ( this.declaration ) {
const statementStr = code.original.slice( this.start, this.end );
declaration_start = this.start + statementStr.match( /^\s*export\s+default\s*/ )[ 0 ].length;
if ( !this.declaration.id && functionOrClassDeclaration.test( this.declaration.type ) ) {
id_index = this.start + statementStr.match( /^\s*export\s+default\s*(?:function|class)/ )[ 0 ].length;
}
}

if ( this.included || this.declaration.included ) {
if ( this.included ) {
if ( functionOrClassDeclaration.test( this.declaration.type ) ) {
if ( this.declaration.id ) {
code.remove( this.start, declaration_start );
} else {
code.overwrite( this.start, declaration_start, `var ${this.variable.name} = ` );
if ( code.original[ this.end - 1 ] !== ';' ) code.appendLeft( this.end, ';' );
if ( !this.declaration.id ) {
code.appendLeft(id_index, ` ${this.variable.name}`);
}
code.remove( this.start, declaration_start );
}

else {
Expand Down
@@ -1,8 +1,8 @@
define(function () { 'use strict';

var foo = function () {
function foo () {
return 42;
};
}

var str = `
//# sourceMappingURL=main.js.map
Expand Down
@@ -1,8 +1,8 @@
'use strict';

var foo = function () {
function foo () {
return 42;
};
}

var str = `
//# sourceMappingURL=main.js.map
Expand Down
@@ -1,6 +1,6 @@
var foo = function () {
function foo () {
return 42;
};
}

var str = `
//# sourceMappingURL=main.js.map
Expand Down
@@ -1,9 +1,9 @@
(function () {
'use strict';

var foo = function () {
function foo () {
return 42;
};
}

var str = `
//# sourceMappingURL=main.js.map
Expand Down
Expand Up @@ -4,9 +4,9 @@
(factory());
}(this, (function () { 'use strict';

var foo = function () {
function foo () {
return 42;
};
}

var str = `
//# sourceMappingURL=main.js.map
Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/this-in-imports/_expected/amd.js
Expand Up @@ -8,9 +8,9 @@ define(function () { 'use strict';
this.x = 1;
}

var B3 = function () {
function B3 () {
this.x = 1;
};
}

const b1 = B();

Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/this-in-imports/_expected/cjs.js
Expand Up @@ -8,9 +8,9 @@ function B$1 () {
this.x = 1;
}

var B3 = function () {
function B3 () {
this.x = 1;
};
}

const b1 = B();

Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/this-in-imports/_expected/es.js
Expand Up @@ -6,9 +6,9 @@ function B$1 () {
this.x = 1;
}

var B3 = function () {
function B3 () {
this.x = 1;
};
}

const b1 = B();

Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/this-in-imports/_expected/iife.js
Expand Up @@ -9,9 +9,9 @@
this.x = 1;
}

var B3 = function () {
function B3 () {
this.x = 1;
};
}

const b1 = B();

Expand Down
4 changes: 2 additions & 2 deletions test/form/samples/this-in-imports/_expected/umd.js
Expand Up @@ -12,9 +12,9 @@
this.x = 1;
}

var B3 = function () {
function B3 () {
this.x = 1;
};
}

const b1 = B();

Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/unused-called-import/_expected/amd.js
@@ -1,6 +1,6 @@
define(function () { 'use strict';

var foo = function() { return 'foo'; };
function foo() { return 'foo'; }

assert.equal( foo(), 'foo' );

Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/unused-called-import/_expected/cjs.js
@@ -1,5 +1,5 @@
'use strict';

var foo = function() { return 'foo'; };
function foo() { return 'foo'; }

assert.equal( foo(), 'foo' );
2 changes: 1 addition & 1 deletion test/form/samples/unused-called-import/_expected/es.js
@@ -1,3 +1,3 @@
var foo = function() { return 'foo'; };
function foo() { return 'foo'; }

assert.equal( foo(), 'foo' );
2 changes: 1 addition & 1 deletion test/form/samples/unused-called-import/_expected/iife.js
@@ -1,7 +1,7 @@
(function () {
'use strict';

var foo = function() { return 'foo'; };
function foo() { return 'foo'; }

assert.equal( foo(), 'foo' );

Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/unused-called-import/_expected/umd.js
Expand Up @@ -4,7 +4,7 @@
(factory());
}(this, (function () { 'use strict';

var foo = function() { return 'foo'; };
function foo() { return 'foo'; }

assert.equal( foo(), 'foo' );

Expand Down

0 comments on commit e1146fd

Please sign in to comment.