Skip to content

Commit

Permalink
drop unused function and class declarations within functions (#1108, #…
Browse files Browse the repository at this point in the history
  • Loading branch information
kzc committed Dec 25, 2016
1 parent a9f342c commit 2744947
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ast/nodes/ClassDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ export default class ClassDeclaration extends Node {
code.remove( this.leadingCommentStart || this.start, this.next || this.end );
}
}

run ( scope ) {
if ( this.parent.type === 'ExportDefaultDeclaration' ) {
super.run( scope );
}
}
}
6 changes: 6 additions & 0 deletions src/ast/nodes/FunctionDeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ export default class FunctionDeclaration extends Node {
code.remove( this.leadingCommentStart || this.start, this.next || this.end );
}
}

run ( scope ) {
if ( this.parent.type === 'ExportDefaultDeclaration' ) {
super.run( scope );
}
}
}
3 changes: 3 additions & 0 deletions test/form/unused-inner-functions-and-classes/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'does not include unused inner functions and classes'
};
25 changes: 25 additions & 0 deletions test/form/unused-inner-functions-and-classes/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
define(function () { 'use strict';

function bar () { console.log("outer bar"); }



function Baz() {
function bar () { console.log("inner bar"); }
function bog () { console.log("inner bog"); }
return bar(), bog;
}

bar();
var f = Baz();
f();

function getClass () {
class MyClass {}
return MyClass;

}

console.log( getClass().name );

});
23 changes: 23 additions & 0 deletions test/form/unused-inner-functions-and-classes/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

function bar () { console.log("outer bar"); }



function Baz() {
function bar () { console.log("inner bar"); }
function bog () { console.log("inner bog"); }
return bar(), bog;
}

bar();
var f = Baz();
f();

function getClass () {
class MyClass {}
return MyClass;

}

console.log( getClass().name );
21 changes: 21 additions & 0 deletions test/form/unused-inner-functions-and-classes/_expected/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function bar () { console.log("outer bar"); }



function Baz() {
function bar () { console.log("inner bar"); }
function bog () { console.log("inner bog"); }
return bar(), bog;
}

bar();
var f = Baz();
f();

function getClass () {
class MyClass {}
return MyClass;

}

console.log( getClass().name );
26 changes: 26 additions & 0 deletions test/form/unused-inner-functions-and-classes/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(function () {
'use strict';

function bar () { console.log("outer bar"); }



function Baz() {
function bar () { console.log("inner bar"); }
function bog () { console.log("inner bog"); }
return bar(), bog;
}

bar();
var f = Baz();
f();

function getClass () {
class MyClass {}
return MyClass;

}

console.log( getClass().name );

}());
29 changes: 29 additions & 0 deletions test/form/unused-inner-functions-and-classes/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';

function bar () { console.log("outer bar"); }



function Baz() {
function bar () { console.log("inner bar"); }
function bog () { console.log("inner bog"); }
return bar(), bog;
}

bar();
var f = Baz();
f();

function getClass () {
class MyClass {}
return MyClass;

}

console.log( getClass().name );

})));
17 changes: 17 additions & 0 deletions test/form/unused-inner-functions-and-classes/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { foo, bar, bog, boo, baz } from './stuff';

bar();
var f = baz();
f();

function getClass () {
class MyClass {}
class UnusedInnerClass1 {}
return MyClass;
class UnusedInnerClass2 {}
}

class UnusedClass {}

console.log( getClass().name );

15 changes: 15 additions & 0 deletions test/form/unused-inner-functions-and-classes/stuff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function foo () { console.log("outer foo"); }
export function bar () { console.log("outer bar"); }
export function bog () { console.log("outer bog"); }
export function boo () { console.log("outer boo"); }

function Baz() {
function foo () { console.log("inner foo"); }
function bar () { console.log("inner bar"); }
function bog () { console.log("inner bog"); }
function boo () { console.log("inner boo"); }

return bar(), bog;
}

export { Baz as baz };

0 comments on commit 2744947

Please sign in to comment.