@@ -9,34 +9,34 @@ describe('amdclean specs', function() {
99 it ( 'should convert function return values to immediately invoked function declarations' , function ( ) {
1010 var AMDcode = "define('example', [], function() {});" ,
1111 cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
12- standardJavaScript = "var example=function (){}() ;" ;
12+ standardJavaScript = "var example=undefined ;" ;
1313 expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
1414 } ) ;
1515
1616 it ( 'should passing a file path instead of the code directly' , function ( ) {
1717 var cleanedCode = amdclean . clean ( { filePath : __dirname + '/../filePathTest.js' , escodegen : { format : { compact : true } } } ) ,
18- standardJavaScript = "var example=function (){}() ;" ;
18+ standardJavaScript = "var example=undefined ;" ;
1919 expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
2020 } ) ;
2121
2222 it ( 'should correctly set callback parameters to the callback function' , function ( ) {
23- var AMDcode = "define('example', ['example1', 'example2'], function(one, two) {});" ,
23+ var AMDcode = "define('example', ['example1', 'example2'], function(one, two) {var test = true; });" ,
2424 cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
25- standardJavaScript = "var example=function (one,two){}(example1,example2);" ;
25+ standardJavaScript = "var example=function (one,two){var test=true; }(example1,example2);" ;
2626 expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
2727 } ) ;
2828
2929 it ( 'should correctly normalize relative file paths' , function ( ) {
30- var AMDcode = "define('./modules/example', ['example1', 'example2'], function(one, two) {});" ,
30+ var AMDcode = "define('./modules/example', ['example1', 'example2'], function(one, two) {var test = true; });" ,
3131 cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
32- standardJavaScript = "var modules_example=function (one,two){}(example1,example2);" ;
32+ standardJavaScript = "var modules_example=function (one,two){var test=true; }(example1,example2);" ;
3333 expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
3434 } ) ;
3535
3636 it ( 'should correctly normalize multi-level relative file paths' , function ( ) {
37- var AMDcode = "define('./foo/prototype/commonMethodName.js', ['example1', 'example2'], function(one, two) {});" ,
37+ var AMDcode = "define('./foo/prototype/commonMethodName.js', ['example1', 'example2'], function(one, two) { var test = true; });" ,
3838 cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
39- standardJavaScript = "var foo_prototype_commonMethodNamejs=function (one,two){}(example1,example2);" ;
39+ standardJavaScript = "var foo_prototype_commonMethodNamejs=function (one,two){var test=true; }(example1,example2);" ;
4040 expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
4141 } ) ;
4242
@@ -155,13 +155,41 @@ describe('amdclean specs', function() {
155155 expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
156156 } ) ;
157157
158+ it ( 'should optimize basic define() methods that have an empty factory function' , function ( ) {
159+ var AMDcode = "define('optimized', function () {});" ,
160+ cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
161+ standardJavaScript = "var optimized=undefined;" ;
162+ expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
163+ } ) ;
164+
158165 it ( 'should optimize more complex define() methods that return a function expression' , function ( ) {
159166 var AMDcode = "define('optimized', function () { return function ( thing ) { var anotherThing = true; return !isNaN( parseFloat( thing ) ) && isFinite( thing );};});" ,
160167 cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
161168 standardJavaScript = "var optimized=function(thing){var anotherThing=true;return!isNaN(parseFloat(thing))&&isFinite(thing);};" ;
162169 expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
163170 } ) ;
164171
172+ it ( 'should optimize more complex define() methods that have a "use strict" statement and return a function expression' , function ( ) {
173+ var AMDcode = "define('optimized', function () { 'use strict'; return function ( thing ) { return !isNaN( parseFloat( thing ) ) && isFinite( thing );};});" ,
174+ cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
175+ standardJavaScript = "var optimized=function(thing){return!isNaN(parseFloat(thing))&&isFinite(thing);};" ;
176+ expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
177+ } ) ;
178+
179+ it ( 'should not optimize more complex define() methods that have a "use strict" statement and return a function expression, but have also set the removeUseStricts option to false' , function ( ) {
180+ var AMDcode = "define('optimized', function () { 'use strict'; return function ( thing ) { return !isNaN( parseFloat( thing ) ) && isFinite( thing );};});" ,
181+ cleanedCode = amdclean . clean ( { removeUseStricts : false , code : AMDcode , escodegen : { format : { compact : true } } } ) ,
182+ standardJavaScript = "var optimized=function (){'use strict';return function(thing){return!isNaN(parseFloat(thing))&&isFinite(thing);};}();" ;
183+ expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
184+ } ) ;
185+
186+ it ( 'should not optimize define() methods that have logic outside of the return statement' , function ( ) {
187+ var AMDcode = "define('optimized', [], function () { var test = true; return function ( thing ) { var anotherThing = true; return !isNaN( parseFloat( thing ) ) && isFinite( thing );};});" ,
188+ cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
189+ standardJavaScript = "var optimized=function (){var test=true;return function(thing){var anotherThing=true;return!isNaN(parseFloat(thing))&&isFinite(thing);};}();" ;
190+ expect ( cleanedCode ) . toBe ( standardJavaScript ) ;
191+ } ) ;
192+
165193 it ( 'should not optimize define() methods that have one or more dependencies' , function ( ) {
166194 var AMDcode = "define('optimized', ['exampleDependency'], function () { return function ( thing ) { var anotherThing = true; return !isNaN( parseFloat( thing ) ) && isFinite( thing );};});" ,
167195 cleanedCode = amdclean . clean ( { code : AMDcode , escodegen : { format : { compact : true } } } ) ,
0 commit comments