From 2744947b0394088659c7ca691bf8cb485b859f13 Mon Sep 17 00:00:00 2001 From: kzc Date: Sat, 24 Dec 2016 12:36:44 -0500 Subject: [PATCH] drop unused function and class declarations within functions (#1108, #1178) --- src/ast/nodes/ClassDeclaration.js | 6 ++++ src/ast/nodes/FunctionDeclaration.js | 6 ++++ .../_config.js | 3 ++ .../_expected/amd.js | 25 ++++++++++++++++ .../_expected/cjs.js | 23 +++++++++++++++ .../_expected/es.js | 21 ++++++++++++++ .../_expected/iife.js | 26 +++++++++++++++++ .../_expected/umd.js | 29 +++++++++++++++++++ .../main.js | 17 +++++++++++ .../stuff.js | 15 ++++++++++ 10 files changed, 171 insertions(+) create mode 100644 test/form/unused-inner-functions-and-classes/_config.js create mode 100644 test/form/unused-inner-functions-and-classes/_expected/amd.js create mode 100644 test/form/unused-inner-functions-and-classes/_expected/cjs.js create mode 100644 test/form/unused-inner-functions-and-classes/_expected/es.js create mode 100644 test/form/unused-inner-functions-and-classes/_expected/iife.js create mode 100644 test/form/unused-inner-functions-and-classes/_expected/umd.js create mode 100644 test/form/unused-inner-functions-and-classes/main.js create mode 100644 test/form/unused-inner-functions-and-classes/stuff.js diff --git a/src/ast/nodes/ClassDeclaration.js b/src/ast/nodes/ClassDeclaration.js index f1a1daa..aa42d1d 100644 --- a/src/ast/nodes/ClassDeclaration.js +++ b/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 ); + } + } } diff --git a/src/ast/nodes/FunctionDeclaration.js b/src/ast/nodes/FunctionDeclaration.js index dffed95..ce808fc 100644 --- a/src/ast/nodes/FunctionDeclaration.js +++ b/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 ); + } + } } diff --git a/test/form/unused-inner-functions-and-classes/_config.js b/test/form/unused-inner-functions-and-classes/_config.js new file mode 100644 index 0000000..76b31d1 --- /dev/null +++ b/test/form/unused-inner-functions-and-classes/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'does not include unused inner functions and classes' +}; diff --git a/test/form/unused-inner-functions-and-classes/_expected/amd.js b/test/form/unused-inner-functions-and-classes/_expected/amd.js new file mode 100644 index 0000000..e02ef3f --- /dev/null +++ b/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 ); + +}); diff --git a/test/form/unused-inner-functions-and-classes/_expected/cjs.js b/test/form/unused-inner-functions-and-classes/_expected/cjs.js new file mode 100644 index 0000000..4cd7c7d --- /dev/null +++ b/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 ); diff --git a/test/form/unused-inner-functions-and-classes/_expected/es.js b/test/form/unused-inner-functions-and-classes/_expected/es.js new file mode 100644 index 0000000..7ce1e94 --- /dev/null +++ b/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 ); diff --git a/test/form/unused-inner-functions-and-classes/_expected/iife.js b/test/form/unused-inner-functions-and-classes/_expected/iife.js new file mode 100644 index 0000000..44561ee --- /dev/null +++ b/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 ); + +}()); diff --git a/test/form/unused-inner-functions-and-classes/_expected/umd.js b/test/form/unused-inner-functions-and-classes/_expected/umd.js new file mode 100644 index 0000000..6ce2b35 --- /dev/null +++ b/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 ); + +}))); diff --git a/test/form/unused-inner-functions-and-classes/main.js b/test/form/unused-inner-functions-and-classes/main.js new file mode 100644 index 0000000..7a1975e --- /dev/null +++ b/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 ); + diff --git a/test/form/unused-inner-functions-and-classes/stuff.js b/test/form/unused-inner-functions-and-classes/stuff.js new file mode 100644 index 0000000..b43a480 --- /dev/null +++ b/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 };