Browse Source

Merge pull request #1179 from kzc/drop-unused-inner-functions-and-classes

drop unused function and class declarations within functions
gh-786
Rich Harris 8 years ago
committed by GitHub
parent
commit
b59263e47e
  1. 6
      src/ast/nodes/ClassDeclaration.js
  2. 6
      src/ast/nodes/FunctionDeclaration.js
  3. 3
      test/form/unused-inner-functions-and-classes/_config.js
  4. 25
      test/form/unused-inner-functions-and-classes/_expected/amd.js
  5. 23
      test/form/unused-inner-functions-and-classes/_expected/cjs.js
  6. 21
      test/form/unused-inner-functions-and-classes/_expected/es.js
  7. 26
      test/form/unused-inner-functions-and-classes/_expected/iife.js
  8. 29
      test/form/unused-inner-functions-and-classes/_expected/umd.js
  9. 17
      test/form/unused-inner-functions-and-classes/main.js
  10. 15
      test/form/unused-inner-functions-and-classes/stuff.js

6
src/ast/nodes/ClassDeclaration.js

@ -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
src/ast/nodes/FunctionDeclaration.js

@ -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
test/form/unused-inner-functions-and-classes/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'does not include unused inner functions and classes'
};

25
test/form/unused-inner-functions-and-classes/_expected/amd.js

@ -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
test/form/unused-inner-functions-and-classes/_expected/cjs.js

@ -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
test/form/unused-inner-functions-and-classes/_expected/es.js

@ -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
test/form/unused-inner-functions-and-classes/_expected/iife.js

@ -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
test/form/unused-inner-functions-and-classes/_expected/umd.js

@ -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
test/form/unused-inner-functions-and-classes/main.js

@ -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
test/form/unused-inner-functions-and-classes/stuff.js

@ -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 };
Loading…
Cancel
Save