From ea5c1a1f99b18da2b624a642fbe3bf8994777530 Mon Sep 17 00:00:00 2001 From: Greg Franko Date: Fri, 6 Jun 2014 09:24:52 -0700 Subject: [PATCH] Releasing AMDclean 2.1.0 --- build/amdclean.js | 6 +++++- build/amdclean.min.js | 2 +- src/amdclean.js | 6 +++++- src/modules/utils.js | 12 ++++++++---- test/specs/convert.js | 4 ++-- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/build/amdclean.js b/build/amdclean.js index 4d5693b..ed54756 100644 --- a/build/amdclean.js +++ b/build/amdclean.js @@ -150,7 +150,11 @@ utils = function () { 'value': 'function' } }; - return _.where(node.test, matchObject).length || _.where([node.test], matchObject).length || _.where(node.test.left, matchObject).length || _.where([node.test.left], matchObject).length; + try { + return _.where(node.test, matchObject).length || _.where([node.test], matchObject).length || _.where(node.test.left, matchObject).length || _.where([node.test.left], matchObject).length; + } catch (e) { + return false; + } }, 'returnExpressionIdentifier': function (name) { return { diff --git a/build/amdclean.min.js b/build/amdclean.min.js index 9ec832b..fe30821 100644 --- a/build/amdclean.min.js +++ b/build/amdclean.min.js @@ -26,4 +26,4 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -!function(esprima,estraverse,escodegen,_){var defaultOptions,errorMsgs,defaultValues,utils,convertToIIFE,convertToIIFEDeclaration,normalizeModuleName,convertToFunctionExpression,convertToObjectDeclaration,createAst,convertDefinesAndRequires,traverseAndUpdateAst,getNormalizedModuleName,findAndStoreAllModuleIds,generateCode,clean;defaultOptions={code:"",filePath:"",globalModules:[],esprima:{comment:!0,loc:!0,range:!0,tokens:!0},escodegen:{comment:!0,format:{indent:{adjustMultilineComment:!0}}},commentCleanName:"amdclean",ignoreModules:[],removeModules:[],removeAllRequires:!1,removeUseStricts:!0,transformAMDChecks:!0,createAnonymousAMDModule:!1,shimOverrides:{},prefixMode:"standard",prefixTransform:function(e){return e},wrap:{start:";(function() {\n",end:"\n}());"},aggressiveOptimizations:!1},errorMsgs={emptyCode:"There is no code to generate the AST with",emptyAst:function(e){return"An AST is not being passed to the "+e+"() method"},invalidObject:function(e){return"An object is not being passed as the first parameter to the "+e+"() method"},lodash:"Make sure you have included lodash (https://github.com/lodash/lodash).",esprima:"Make sure you have included esprima (https://github.com/ariya/esprima).",estraverse:"Make sure you have included estraverse (https://github.com/Constellation/estraverse).",escodegen:"Make sure you have included escodegen (https://github.com/Constellation/escodegen)."},defaultValues={dependencyBlacklist:{require:"remove",exports:!0,module:"remove"},defaultLOC:{start:{line:0,column:0}},defaultRange:[0,0]},utils=function(){var utils={isDefine:function(e){var n=e.expression||{},r=n.callee;return _.isObject(e)&&"ExpressionStatement"===e.type&&n&&"CallExpression"===n.type&&"Identifier"===r.type&&"define"===r.name},isRequire:function(e){var n=e.expression||{},r=n.callee;return e&&"ExpressionStatement"===e.type&&n&&"CallExpression"===n.type&&"Identifier"===r.type&&"require"===r.name},isModuleExports:function(e){return e?"AssignmentExpression"===e.type&&e.left&&"MemberExpression"===e.left.type&&e.left.object&&"Identifier"===e.left.object.type&&"module"===e.left.object.name&&e.left.property&&"Identifier"===e.left.property.type&&"exports"===e.left.property.name:!1},isRequireExpression:function(e){return"CallExpression"===e.type&&e.callee&&"require"===e.callee.name},isObjectExpression:function(e){return e&&e&&"ObjectExpression"===e.type},isFunctionExpression:function(e){return e&&e&&"FunctionExpression"===e.type},isFunctionCallExpression:function(e){return e&&e&&"CallExpression"===e.type&&e.callee&&"FunctionExpression"===e.callee.type},isUseStrict:function(e){return e&&e&&"use strict"===e.value&&"Literal"===e.type},isAMDConditional:function(e){if(e&&"IfStatement"!==e.type||!e.test||!e.test.left)return!1;var n={left:{operator:"typeof",argument:{type:"Identifier",name:"define"}},right:{type:"Literal",value:"function"}};return _.where(e.test,n).length||_.where([e.test],n).length||_.where(e.test.left,n).length||_.where([e.test.left],n).length},returnExpressionIdentifier:function(e){return{type:"ExpressionStatement",expression:{type:"Identifier",name:e,range:defaultValues.defaultRange,loc:defaultValues.defaultLOC},range:defaultValues.defaultRange,loc:defaultValues.defaultLOC}},readFile:function(e){if("undefined"!=typeof exports){var n=require("fs");return n.readFileSync(e,"utf8")}return""},isRelativeFilePath:function(e){var n=e.split("/");return-1!==n.length&&("."===n[0]||".."===n[0])},convertToCamelCase:function(e,n){return n=n||"_",e.replace(new RegExp(n+"(.)","g"),function(e,n){return n.toUpperCase()})},prefixReservedWords:function(name){var reservedWord=!1;try{name.length&&eval("var "+name+" = 1;")}catch(e){reservedWord=!0}return reservedWord===!0?"_"+name:name},normalizeDependencyName:function(e,n){if(!e||!n||!utils.isRelativeFilePath(n))return n;var r=function(e){var n,r=e.split("/");return n=_.reduce(r,function(e,n){switch(n){case".":break;case"..":e.pop();break;default:e.push(n)}return e},[]),n.join("/")},t=function(e){var n=e.split("/");return n.pop(),n.join("/")};return r([t(e),n].join("/"))}};return utils}(),convertToIIFE=function(e){var n=e.callbackFuncParams,r=e.callbackFunc,t=e.dependencyNames,a=e.node,o=a.range||defaultValues.defaultRange,s=a.loc||defaultValues.defaultLOC;return{type:"ExpressionStatement",expression:{type:"CallExpression",callee:{type:"FunctionExpression",id:null,params:n,defaults:[],body:r.body,rest:r.rest,generator:r.generator,expression:r.expression,range:o,loc:s},arguments:t,range:o,loc:s},range:o,loc:s}},convertToIIFEDeclaration=function(e){var n=e.moduleName,r=e.callbackFuncParams,t=e.isOptimized,a=e.callbackFunc,o=e.node,s=a.name,i=a.type,l=o.range||defaultValues.defaultRange,u=o.loc||defaultValues.defaultLOC,c=function(){var n=e.callbackFunc;return"Identifier"===i&&"undefined"!==s&&(n={type:"FunctionExpression",id:null,params:[],defaults:[],body:{type:"BlockStatement",body:[{type:"ReturnStatement",argument:{type:"ConditionalExpression",test:{type:"BinaryExpression",operator:"===",left:{type:"UnaryExpression",operator:"typeof",argument:{type:"Identifier",name:s,range:l,loc:u},prefix:!0,range:l,loc:u},right:{type:"Literal",value:"function",raw:"'function'",range:l,loc:u},range:l,loc:u},consequent:{type:"CallExpression",callee:{type:"Identifier",name:s,range:l,loc:u},arguments:r,range:l,loc:u},alternate:{type:"Identifier",name:s,range:l,loc:u},range:l,loc:u},range:l,loc:u}],range:l,loc:u},rest:null,generator:!1,expression:!1,range:l,loc:u}),n}(),d=e.dependencyNames,p=function(){return"Literal"===c.type||"Identifier"===c.type&&"undefined"===c.name||t===!0?c:{type:"CallExpression",callee:{type:"FunctionExpression",id:{type:"Identifier",name:"",range:l,loc:u},params:r,defaults:[],body:c.body,rest:c.rest,generator:c.generator,expression:c.expression,range:l,loc:u},arguments:d,range:l,loc:u}}(),m={type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:{type:"Identifier",name:n,range:l,loc:u},right:p,range:l,loc:u},range:l,loc:u};return estraverse.replace(c,{enter:function(e){return utils.isModuleExports(e)?{type:"AssignmentExpression",operator:"=",left:{type:"Identifier",name:"exports"},right:e.right}:e}}),m},normalizeModuleName=function(e,n){var r,t,a,o=this,s=o.options,i=s.prefixMode,l=s.prefixTransform,u=defaultValues.dependencyBlacklist;return e=e||"","{}"===e?"remove"===u[e]?"":e:(t=utils.prefixReservedWords(e.replace(/\./g,"").replace(/[^A-Za-z0-9_$]/g,"_").replace(/^_+/,"")),a="camelCase"===i?utils.convertToCamelCase(t):t,_.isFunction(l)&&(r=l(a,n),_.isString(r)&&r.length)?r:a)},convertToFunctionExpression=function(e){var n,r,t,a=this,o=a.options,s=o.ignoreModules,i=e.node,l=e.isDefine,u=e.isRequire,c=!1,d=e.moduleName,p=e.moduleId,m=e.dependencies,f=m.length,g=o.aggressiveOptimizations,y=[],h=[],v=defaultValues.defaultRange,b=defaultValues.defaultLOC,x=e.range||v,E=e.loc||b,M=function(){var n,r,t,a,s=e.moduleReturnValue;if(s&&"FunctionExpression"===s.type&&s.body&&_.isArray(s.body.body)&&s.body.body.length){if(n=_.filter(s.body.body,function(e){return o.removeUseStricts===!0?!utils.isUseStrict(e.expression):e}),r=_.where(n,{type:"ReturnStatement"}),y=_.where(n,{left:{type:"Identifier",name:"exports"}}),h=_.where(n,{left:{type:"MemberExpression",object:{type:"Identifier",name:"module"},property:{type:"Identifier",name:"exports"}}}),r.length){if(t=r[0],a=t.argument,!utils.isFunctionExpression(t)&&n.length>1||a&&"Identifier"===a.type)return s;s=a,c=!0,s.params&&(f=s.params.length)}}else s&&"FunctionExpression"===s.type&&s.body&&_.isArray(s.body.body)&&0===s.body.body.length&&(s={type:"Identifier",name:"undefined",range:x,loc:E},f=0);return s}(),w=function(){var e=[];return M&&M.body&&_.isArray(M.body.body)&&(e=_.where(M.body.body,{type:"ReturnStatement"}),e.length)?!0:!1}(),A=function(){var e=M.params||[];return _.where(e,{name:"exports"}).length}(),I={},O=function(){var e,n=[];return _.each(m,function(r){e=normalizeModuleName.call(a,utils.normalizeDependencyName(p,r),p),I[e]=!0,n.push({type:"Identifier",name:e,range:v,loc:b})}),n}(),R=function V(e){e="_"+e+"_";var n=function(){var n=!1;return I[e]?n=!0:estraverse.traverse(M,{enter:function(r){"VariableDeclarator"===r.type&&r.id&&"Identifier"===r.id.type&&r.id.name===e&&(n=!0)}}),n}();return n?V(e):e},C=function(){var e=[];return A&&estraverse.traverse(M,{enter:function(n){var r,t;"VariableDeclarator"===n.type&&utils.isRequireExpression(n.init)&&n.id&&n.id.name&&n.init&&n.init.arguments&&n.init.arguments[0]&&n.init.arguments[0].value&&(r=n.id.name,t=normalizeModuleName.call(a,n.init.arguments[0].value),_.contains(s,t)||r!==t||e.push({originalName:t,newName:R(t),range:n.range||v,loc:n.loc||b}))}}),e}(),j=function(){var e=[];return _.each(C,function(n){e.push({type:"Identifier",name:n.newName?n.newName:n,range:n.range,loc:n.loc})}),e}(),F=function(){var e,r=[],t=_.union(M.params||O||[],j),o={};return _.each(t,function(n,t){e=n?n.name:O[t].name,"{}"!==e&&"remove"!==defaultValues.dependencyBlacklist[e]&&(r.push({type:"Identifier",name:e,range:v,loc:b}),c||g!==!0||a.storedModules[e]||!O[t]||(a.callbackParameterMap[O[t].name]?(o=_.where(a.callbackParameterMap[O[t].name],{name:e}),o.length?(o=o[0],o.count+=1):a.callbackParameterMap[O[t].name].push({name:e,count:1})):a.callbackParameterMap[O[t].name]=[{name:e,count:1}]))}),n=r,_.filter(r||[],function(e){return g===!0?!a.storedModules[e.name]:!0})}(),N=!w&&A,S=y.length||h.length;return O=_.filter(O||[],function(e,r){var t=n[r],o=e.name;return g===!0?!t||a.storedModules[t.name]&&t.name===o?!a.storedModules[o]:!a.storedModules[t.name]:!0}),r=O.length,t=F.length,r&&r>t&&O.splice(r-(t||1),t||1),N&&S&&M.body.body.push({type:"ReturnStatement",argument:{type:"Identifier",name:"exports",range:v,loc:b},range:v,loc:b}),estraverse.replace(M,{enter:function(e){var n,r;return utils.isRequireExpression(e)?e.arguments&&e.arguments[0]&&e.arguments[0].value?(n=normalizeModuleName.call(a,e.arguments[0].value),_.contains(s,n)?e:(_.where(C,{originalName:n}).length&&(r=_.where(C,{originalName:n})[0].newName),{type:"Identifier",name:r?r:n,range:e.range||v,loc:e.loc||b})):e:void 0}}),l?convertToIIFEDeclaration.call(a,{moduleName:d,dependencyNames:O,callbackFuncParams:F,hasExportsParam:A,callbackFunc:M,isOptimized:c,node:i}):u?convertToIIFE.call(a,{dependencyNames:O,callbackFuncParams:F,callbackFunc:M,node:i}):void 0},convertToObjectDeclaration=function(e,n){var r=e.node,t=defaultValues.defaultRange,a=defaultValues.defaultLOC,o=r.range||t,s=r.loc||a,i=e.moduleName,l=function(){var r,t,a,i,l,u;return"functionCallExpression"===n&&(r=e.moduleReturnValue,t=r.callee,a=t.params,a&&a.length&&_.isArray(a)&&_.where(a,{name:"global"})&&_.isObject(t.body)&&_.isArray(t.body.body)&&(i=_.where(t.body.body,{type:"ReturnStatement"})[0],_.isObject(i)&&_.isObject(i.argument)&&"FunctionExpression"===i.argument.type&&(u=i.argument,_.isObject(u.body)&&_.isArray(u.body.body)&&(l=_.where(u.body.body,{type:"ReturnStatement"})[0],_.isObject(l.argument)&&_.isObject(l.argument.right)&&_.isObject(l.argument.right.property)&&l.argument.right.property.name&&(r={type:"MemberExpression",computed:!1,object:{type:"Identifier",name:"window",range:o,loc:s},property:{type:"Identifier",name:l.argument.right.property.name,range:o,loc:s},range:o,loc:s}))))),r=r||e.moduleReturnValue}(),u={type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:{type:"Identifier",name:i,range:o,loc:s},right:l,range:o,loc:s},range:o,loc:s};return u},createAst=function(e){var n=this,r=n.options,t=r.filePath,a=e||r.code||(t?utils.readFile(t):""),o=r.esprima;if(a){if(!_.isPlainObject(esprima)||!_.isFunction(esprima.parse))throw new Error(errorMsgs.esprima);return esprima.parse(a,o)}throw new Error(errorMsgs.emptyCode)},convertDefinesAndRequires=function(e,n){var r,t,a,o,s,i,l,u,c,d=this,p=d.options,m=utils.isDefine(e),f=utils.isRequire(e),g=!1,y="",h=defaultValues.defaultRange,v=defaultValues.defaultLOC,b=e.range||h,x=e.loc||v,E=defaultValues.dependencyBlacklist;if(l=m||f?e.expression.loc.start.line:e&&e.loc&&e.loc.start?e.loc.start.line:null,u=d.matchingCommentLineNumbers[l]||d.matchingCommentLineNumbers[l-1],utils.isAMDConditional(e)){if(!u&&p.transformAMDChecks===!0)return e.test={type:"Literal",value:!0,raw:"true",range:b,loc:x},e;p.transformAMDChecks===!1&&estraverse.traverse(e,{enter:function(e){utils.isDefine(e)&&e.expression&&e.expression.arguments&&e.expression.arguments.length&&"Literal"===e.expression.arguments[0].type&&e.expression.arguments[0].value&&(d.conditionalModulesToIgnore[e.expression.arguments[0].value]=!0,p.createAnonymousAMDModule===!0&&(d.storedModules[e.expression.arguments[0].value]=!1,e.expression.arguments.shift()))}})}if(!m&&!f)return"FunctionExpression"===e.type&&_.isArray(e.params)&&_.where(e.params,{type:"Identifier",name:"exports"}).length&&_.isObject(e.body)&&_.isArray(e.body.body)&&!_.where(e.body.body,{type:"ReturnStatement",argument:{type:"Identifier"}}).length&&(c=function(){return n&&n.arguments?n&&n.arguments&&n.arguments.length?_.where(n.arguments,{type:"FunctionExpression"}).length:!1:!1}(),c&&e.body.body.unshift({type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:{type:"Identifier",name:"exports",range:h,loc:v},right:{type:"LogicalExpression",operator:"||",left:{type:"Identifier",name:"exports",range:h,loc:v},right:{type:"ObjectExpression",properties:[],range:h,loc:v},range:h,loc:v},range:h,loc:v},range:h,loc:v}),e.body.body.push({type:"ReturnStatement",argument:{type:"Identifier",name:"exports",range:h,loc:v},range:h,loc:v})),e;if(t=Array.prototype.slice.call(e.expression.arguments,0),a=function(){var e=f?t[0]:t[t.length-2],n=[];return e=_.isPlainObject(e)?e.elements||[]:[],_.isArray(e)&&e.length&&_.each(e,function(e){"remove"!==E[e.value]&&(E[e.value]?"remove"!==E[e.value]&&n.push("{}"):n.push(e.value))}),n}(),o=f?t[1]:t[t.length-1],s=e.expression.arguments[0].value,r=normalizeModuleName.call(d,s),i={node:e,moduleName:r,moduleId:s,dependencies:a,moduleReturnValue:o,isDefine:m,isRequire:f,range:b,loc:x},m){if(u||!r||d.conditionalModulesToIgnore[r]===!0)return d.options.ignoreModules.push(r),e;if(_.contains(p.removeModules,r))return d.storedModules[r]=!1,{type:"EmptyStatement"};if(_.isObject(p.shimOverrides)&&p.shimOverrides[r]&&(i.moduleReturnValue=createAst.call(d,p.shimOverrides[r]),_.isArray(i.moduleReturnValue.body)&&_.isObject(i.moduleReturnValue.body[0])?_.isObject(i.moduleReturnValue.body[0].expression)&&(i.moduleReturnValue=i.moduleReturnValue.body[0].expression,y="objectExpression"):i.moduleReturnValue=o),i.moduleReturnValue&&"Identifier"===i.moduleReturnValue.type&&(y="functionExpression"),_.contains(p.ignoreModules,r))return e;if(utils.isFunctionExpression(o)||"functionExpression"===y)return convertToFunctionExpression.call(d,i);if(utils.isObjectExpression(o)||"objectExpression"===y)return convertToObjectDeclaration.call(d,i);if(utils.isFunctionCallExpression(o))return convertToObjectDeclaration.call(d,i,"functionCallExpression")}else if(f)return u?e:(g=_.isArray(e.expression.arguments)&&e.expression.arguments.length?e.expression.arguments[1]&&e.expression.arguments[1].body&&e.expression.arguments[1].body.body&&e.expression.arguments[1].body.body.length:!1,p.removeAllRequires!==!0&&g?convertToFunctionExpression.call(d,i):{type:"EmptyStatement",range:b,loc:x})},traverseAndUpdateAst=function(e){var n=this,r=n.options,t=e.ast;if(!_.isPlainObject(e))throw new Error(errorMsgs.invalidObject("traverseAndUpdateAst"));if(!t)throw new Error(errorMsgs.emptyAst("traverseAndUpdateAst"));if(!_.isPlainObject(estraverse)||!_.isFunction(estraverse.replace))throw new Error(errorMsgs.estraverse);return estraverse.replace(t,{enter:function(e,t){var a;return"Program"===e.type?(a=function(){var t=[];return n.comments=e.comments,_.each(e.comments,function(e){var n=e.value.trim();n===r.commentCleanName&&t.push(e)}),t}(),_.each(a,function(e){currentLineNumber=e.loc.start.line,n.matchingCommentLineNumbers[currentLineNumber]=!0}),e):convertDefinesAndRequires.call(n,e,t)},leave:function(e){return e}}),t},getNormalizedModuleName=function(e){if(utils.isDefine(e)){var n=this,r=e.expression.arguments[0].value,t=normalizeModuleName.call(n,r);return t}},findAndStoreAllModuleIds=function(e){var n=this;if(!e)throw new Error(errorMsgs.emptyAst("findAndStoreAllModuleIds"));if(!_.isPlainObject(estraverse)||!_.isFunction(estraverse.traverse))throw new Error(errorMsgs.estraverse);estraverse.traverse(e,{enter:function(e,r){var t=getNormalizedModuleName.call(n,e,r);t&&!n.storedModules[t]&&(n.storedModules[t]=!0)}})},generateCode=function(e){var n=this,r=n.options,t=r.esprima||{},a=r.escodegen||{};if(!_.isPlainObject(escodegen)||!_.isFunction(escodegen.generate))throw new Error(errorMsgs.escodegen);if(t.comment===!0&&a.comment===!0)try{e=escodegen.attachComments(e,e.comments,e.tokens)}catch(o){}return escodegen.generate(e,a)},clean=function(){var e,n=this,r=n.options,t=r.ignoreModules,a={},o={},s=[],i={},l={},u=defaultValues.defaultRange,c=defaultValues.defaultLOC;return a=createAst.call(n),findAndStoreAllModuleIds.call(n,a),o=traverseAndUpdateAst.call(n,{ast:a}),o&&_.isArray(o.body)&&estraverse.replace(o,{enter:function(e,a){var o,s,i,l=e&&e.left&&e.left.name?e.left.name:"",d=e.right,p=[],m={},f=_.filter(n.callbackParameterMap[l],function(e){return e&&e.count>1}),g=[];if(void 0===e||"EmptyStatement"===e.type)_.each(a.body,function(e,n){(void 0===e||"EmptyStatement"===e.type)&&a.body.splice(n,1)});else{if(utils.isRequireExpression(e))return e.arguments&&e.arguments[0]&&e.arguments[0].value?(o=normalizeModuleName.call(n,e.arguments[0].value),-1===t.indexOf(o)?{type:"Identifier",name:o,range:e.range||u,loc:e.loc||c}:e):e;if(r.aggressiveOptimizations===!0&&"AssignmentExpression"===e.type&&l&&(i=_.map(d&&d.callee&&d.callee.params?d.callee.params:[],function(e){return e.name}),s=_.map(d.arguments,function(e){return e.name}),_.each(s,function(e){_.each(n.callbackParameterMap[e],function(e){var n=e.name,r=e.count;_.each(i,function(e,t){r>1&&n===e&&g.push(t)})})}),_.each(g,function(e){d.arguments.splice(e,e+1),d.callee.params.splice(e,e+1)}),n.callbackParameterMap[l]))return e.right=function(){return r.aggressiveOptimizations===!0&&f.length?(p=_.map(f,function(e,n){return{type:"AssignmentExpression",operator:"=",left:{type:"Identifier",name:e.name,range:u,loc:c},right:n1&&(e=n.name,r[e]=!0)})}),r}(),i=_.merge(_.cloneDeep(_.reduce(n.storedModules,function(e,n,r){return n!==!1&&(e[r]=!0),e},{})),l),_.each(i,function(e,n){_.contains(r.ignoreModules,n)||s.push({type:"VariableDeclarator",id:{type:"Identifier",name:n,range:u,loc:c},init:null,range:u,loc:c})}),s.length&&o.body.unshift({type:"VariableDeclaration",declarations:s,kind:"var",range:u,loc:c}),e=generateCode.call(n,o),_.isObject(r.wrap)&&(_.isString(r.wrap.start)&&r.wrap.start.length&&(e=r.wrap.start+e),_.isString(r.wrap.end)&&r.wrap.end.length&&(e+=r.wrap.end)),e},function(){!function(e,n){"function"==typeof define&&define.amd?(n.env="undefined"!=typeof exports?"node":"web",n.amd=!0,define(["esprima","estraverse","escodegen","underscore"],function(r,t,a,o){return n({esprima:r,estraverse:t,escodegen:a,underscore:o},e)})):"undefined"!=typeof exports?(n.env="node",module.exports=n(null,e)):(n.env="web",e.amdclean=n(null,e))}(this,function e(n,r){var t=e.env,a=function(e,n){if(!esprima)throw new Error(errorMsgs.esprima);if(!estraverse)throw new Error(errorMsgs.estraverse);if(!escodegen)throw new Error(errorMsgs.escodegen);if(!_)throw new Error(errorMsgs.lodash);var r=_.cloneDeep(this.defaultOptions||{}),t=e||n||{};!_.isPlainObject(e)&&_.isString(e)&&(t=_.merge({code:e},_.isObject(n)?n:{})),this.storedModules={},this.callbackParameterMap={},this.conditionalModulesToIgnore={},this.matchingCommentLineNumbers={},this.comments=[],this.options=_.merge(r,t)},o={VERSION:"2.1.0",clean:function(e,n){var r=new a(e,n),t=r.clean();return t}};return a.prototype={env:t,clean:clean,defaultOptions:defaultOptions},n=n||{},esprima=function(){return e.amd&&n.esprima&&n.esprima.parse?n.esprima:r&&r.esprima&&r.esprima.parse?r.esprima:"node"===t?require("esprima"):void 0}(),estraverse=function(){return e.amd&&n.estraverse&&n.estraverse.traverse?n.estraverse:r&&r.estraverse&&r.estraverse.traverse?r.estraverse:"node"===t?require("estraverse"):void 0}(),escodegen=function(){return e.amd&&n.escodegen&&n.escodegen.generate?n.escodegen:r&&r.escodegen&&r.escodegen.generate?r.escodegen:"node"===t?require("escodegen"):void 0}(),_=function(){return e.amd&&n.underscore?n.underscore:r&&r._?r._:"node"===t?require("lodash"):void 0}(),o})}()}("undefined"!=typeof esprima?esprima:null,"undefined"!=typeof estraverse?estraverse:null,"undefined"!=typeof escodegen?escodegen:null,"undefined"!=typeof _?_:null); \ No newline at end of file +!function(esprima,estraverse,escodegen,_){var defaultOptions,errorMsgs,defaultValues,utils,convertToIIFE,convertToIIFEDeclaration,normalizeModuleName,convertToFunctionExpression,convertToObjectDeclaration,createAst,convertDefinesAndRequires,traverseAndUpdateAst,getNormalizedModuleName,findAndStoreAllModuleIds,generateCode,clean;defaultOptions={code:"",filePath:"",globalModules:[],esprima:{comment:!0,loc:!0,range:!0,tokens:!0},escodegen:{comment:!0,format:{indent:{adjustMultilineComment:!0}}},commentCleanName:"amdclean",ignoreModules:[],removeModules:[],removeAllRequires:!1,removeUseStricts:!0,transformAMDChecks:!0,createAnonymousAMDModule:!1,shimOverrides:{},prefixMode:"standard",prefixTransform:function(e){return e},wrap:{start:";(function() {\n",end:"\n}());"},aggressiveOptimizations:!1},errorMsgs={emptyCode:"There is no code to generate the AST with",emptyAst:function(e){return"An AST is not being passed to the "+e+"() method"},invalidObject:function(e){return"An object is not being passed as the first parameter to the "+e+"() method"},lodash:"Make sure you have included lodash (https://github.com/lodash/lodash).",esprima:"Make sure you have included esprima (https://github.com/ariya/esprima).",estraverse:"Make sure you have included estraverse (https://github.com/Constellation/estraverse).",escodegen:"Make sure you have included escodegen (https://github.com/Constellation/escodegen)."},defaultValues={dependencyBlacklist:{require:"remove",exports:!0,module:"remove"},defaultLOC:{start:{line:0,column:0}},defaultRange:[0,0]},utils=function(){var utils={isDefine:function(e){var n=e.expression||{},r=n.callee;return _.isObject(e)&&"ExpressionStatement"===e.type&&n&&"CallExpression"===n.type&&"Identifier"===r.type&&"define"===r.name},isRequire:function(e){var n=e.expression||{},r=n.callee;return e&&"ExpressionStatement"===e.type&&n&&"CallExpression"===n.type&&"Identifier"===r.type&&"require"===r.name},isModuleExports:function(e){return e?"AssignmentExpression"===e.type&&e.left&&"MemberExpression"===e.left.type&&e.left.object&&"Identifier"===e.left.object.type&&"module"===e.left.object.name&&e.left.property&&"Identifier"===e.left.property.type&&"exports"===e.left.property.name:!1},isRequireExpression:function(e){return"CallExpression"===e.type&&e.callee&&"require"===e.callee.name},isObjectExpression:function(e){return e&&e&&"ObjectExpression"===e.type},isFunctionExpression:function(e){return e&&e&&"FunctionExpression"===e.type},isFunctionCallExpression:function(e){return e&&e&&"CallExpression"===e.type&&e.callee&&"FunctionExpression"===e.callee.type},isUseStrict:function(e){return e&&e&&"use strict"===e.value&&"Literal"===e.type},isAMDConditional:function(e){if(e&&"IfStatement"!==e.type||!e.test||!e.test.left)return!1;var n={left:{operator:"typeof",argument:{type:"Identifier",name:"define"}},right:{type:"Literal",value:"function"}};try{return _.where(e.test,n).length||_.where([e.test],n).length||_.where(e.test.left,n).length||_.where([e.test.left],n).length}catch(r){return!1}},returnExpressionIdentifier:function(e){return{type:"ExpressionStatement",expression:{type:"Identifier",name:e,range:defaultValues.defaultRange,loc:defaultValues.defaultLOC},range:defaultValues.defaultRange,loc:defaultValues.defaultLOC}},readFile:function(e){if("undefined"!=typeof exports){var n=require("fs");return n.readFileSync(e,"utf8")}return""},isRelativeFilePath:function(e){var n=e.split("/");return-1!==n.length&&("."===n[0]||".."===n[0])},convertToCamelCase:function(e,n){return n=n||"_",e.replace(new RegExp(n+"(.)","g"),function(e,n){return n.toUpperCase()})},prefixReservedWords:function(name){var reservedWord=!1;try{name.length&&eval("var "+name+" = 1;")}catch(e){reservedWord=!0}return reservedWord===!0?"_"+name:name},normalizeDependencyName:function(e,n){if(!e||!n||!utils.isRelativeFilePath(n))return n;var r=function(e){var n,r=e.split("/");return n=_.reduce(r,function(e,n){switch(n){case".":break;case"..":e.pop();break;default:e.push(n)}return e},[]),n.join("/")},t=function(e){var n=e.split("/");return n.pop(),n.join("/")};return r([t(e),n].join("/"))}};return utils}(),convertToIIFE=function(e){var n=e.callbackFuncParams,r=e.callbackFunc,t=e.dependencyNames,a=e.node,o=a.range||defaultValues.defaultRange,s=a.loc||defaultValues.defaultLOC;return{type:"ExpressionStatement",expression:{type:"CallExpression",callee:{type:"FunctionExpression",id:null,params:n,defaults:[],body:r.body,rest:r.rest,generator:r.generator,expression:r.expression,range:o,loc:s},arguments:t,range:o,loc:s},range:o,loc:s}},convertToIIFEDeclaration=function(e){var n=e.moduleName,r=e.callbackFuncParams,t=e.isOptimized,a=e.callbackFunc,o=e.node,s=a.name,i=a.type,l=o.range||defaultValues.defaultRange,u=o.loc||defaultValues.defaultLOC,c=function(){var n=e.callbackFunc;return"Identifier"===i&&"undefined"!==s&&(n={type:"FunctionExpression",id:null,params:[],defaults:[],body:{type:"BlockStatement",body:[{type:"ReturnStatement",argument:{type:"ConditionalExpression",test:{type:"BinaryExpression",operator:"===",left:{type:"UnaryExpression",operator:"typeof",argument:{type:"Identifier",name:s,range:l,loc:u},prefix:!0,range:l,loc:u},right:{type:"Literal",value:"function",raw:"'function'",range:l,loc:u},range:l,loc:u},consequent:{type:"CallExpression",callee:{type:"Identifier",name:s,range:l,loc:u},arguments:r,range:l,loc:u},alternate:{type:"Identifier",name:s,range:l,loc:u},range:l,loc:u},range:l,loc:u}],range:l,loc:u},rest:null,generator:!1,expression:!1,range:l,loc:u}),n}(),d=e.dependencyNames,p=function(){return"Literal"===c.type||"Identifier"===c.type&&"undefined"===c.name||t===!0?c:{type:"CallExpression",callee:{type:"FunctionExpression",id:{type:"Identifier",name:"",range:l,loc:u},params:r,defaults:[],body:c.body,rest:c.rest,generator:c.generator,expression:c.expression,range:l,loc:u},arguments:d,range:l,loc:u}}(),m={type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:{type:"Identifier",name:n,range:l,loc:u},right:p,range:l,loc:u},range:l,loc:u};return estraverse.replace(c,{enter:function(e){return utils.isModuleExports(e)?{type:"AssignmentExpression",operator:"=",left:{type:"Identifier",name:"exports"},right:e.right}:e}}),m},normalizeModuleName=function(e,n){var r,t,a,o=this,s=o.options,i=s.prefixMode,l=s.prefixTransform,u=defaultValues.dependencyBlacklist;return e=e||"","{}"===e?"remove"===u[e]?"":e:(t=utils.prefixReservedWords(e.replace(/\./g,"").replace(/[^A-Za-z0-9_$]/g,"_").replace(/^_+/,"")),a="camelCase"===i?utils.convertToCamelCase(t):t,_.isFunction(l)&&(r=l(a,n),_.isString(r)&&r.length)?r:a)},convertToFunctionExpression=function(e){var n,r,t,a=this,o=a.options,s=o.ignoreModules,i=e.node,l=e.isDefine,u=e.isRequire,c=!1,d=e.moduleName,p=e.moduleId,m=e.dependencies,f=m.length,g=o.aggressiveOptimizations,y=[],h=[],v=defaultValues.defaultRange,b=defaultValues.defaultLOC,x=e.range||v,E=e.loc||b,M=function(){var n,r,t,a,s=e.moduleReturnValue;if(s&&"FunctionExpression"===s.type&&s.body&&_.isArray(s.body.body)&&s.body.body.length){if(n=_.filter(s.body.body,function(e){return o.removeUseStricts===!0?!utils.isUseStrict(e.expression):e}),r=_.where(n,{type:"ReturnStatement"}),y=_.where(n,{left:{type:"Identifier",name:"exports"}}),h=_.where(n,{left:{type:"MemberExpression",object:{type:"Identifier",name:"module"},property:{type:"Identifier",name:"exports"}}}),r.length){if(t=r[0],a=t.argument,!utils.isFunctionExpression(t)&&n.length>1||a&&"Identifier"===a.type)return s;s=a,c=!0,s.params&&(f=s.params.length)}}else s&&"FunctionExpression"===s.type&&s.body&&_.isArray(s.body.body)&&0===s.body.body.length&&(s={type:"Identifier",name:"undefined",range:x,loc:E},f=0);return s}(),w=function(){var e=[];return M&&M.body&&_.isArray(M.body.body)&&(e=_.where(M.body.body,{type:"ReturnStatement"}),e.length)?!0:!1}(),A=function(){var e=M.params||[];return _.where(e,{name:"exports"}).length}(),I={},O=function(){var e,n=[];return _.each(m,function(r){e=normalizeModuleName.call(a,utils.normalizeDependencyName(p,r),p),I[e]=!0,n.push({type:"Identifier",name:e,range:v,loc:b})}),n}(),R=function V(e){e="_"+e+"_";var n=function(){var n=!1;return I[e]?n=!0:estraverse.traverse(M,{enter:function(r){"VariableDeclarator"===r.type&&r.id&&"Identifier"===r.id.type&&r.id.name===e&&(n=!0)}}),n}();return n?V(e):e},C=function(){var e=[];return A&&estraverse.traverse(M,{enter:function(n){var r,t;"VariableDeclarator"===n.type&&utils.isRequireExpression(n.init)&&n.id&&n.id.name&&n.init&&n.init.arguments&&n.init.arguments[0]&&n.init.arguments[0].value&&(r=n.id.name,t=normalizeModuleName.call(a,n.init.arguments[0].value),_.contains(s,t)||r!==t||e.push({originalName:t,newName:R(t),range:n.range||v,loc:n.loc||b}))}}),e}(),j=function(){var e=[];return _.each(C,function(n){e.push({type:"Identifier",name:n.newName?n.newName:n,range:n.range,loc:n.loc})}),e}(),F=function(){var e,r=[],t=_.union(M.params||O||[],j),o={};return _.each(t,function(n,t){e=n?n.name:O[t].name,"{}"!==e&&"remove"!==defaultValues.dependencyBlacklist[e]&&(r.push({type:"Identifier",name:e,range:v,loc:b}),c||g!==!0||a.storedModules[e]||!O[t]||(a.callbackParameterMap[O[t].name]?(o=_.where(a.callbackParameterMap[O[t].name],{name:e}),o.length?(o=o[0],o.count+=1):a.callbackParameterMap[O[t].name].push({name:e,count:1})):a.callbackParameterMap[O[t].name]=[{name:e,count:1}]))}),n=r,_.filter(r||[],function(e){return g===!0?!a.storedModules[e.name]:!0})}(),N=!w&&A,S=y.length||h.length;return O=_.filter(O||[],function(e,r){var t=n[r],o=e.name;return g===!0?!t||a.storedModules[t.name]&&t.name===o?!a.storedModules[o]:!a.storedModules[t.name]:!0}),r=O.length,t=F.length,r&&r>t&&O.splice(r-(t||1),t||1),N&&S&&M.body.body.push({type:"ReturnStatement",argument:{type:"Identifier",name:"exports",range:v,loc:b},range:v,loc:b}),estraverse.replace(M,{enter:function(e){var n,r;return utils.isRequireExpression(e)?e.arguments&&e.arguments[0]&&e.arguments[0].value?(n=normalizeModuleName.call(a,e.arguments[0].value),_.contains(s,n)?e:(_.where(C,{originalName:n}).length&&(r=_.where(C,{originalName:n})[0].newName),{type:"Identifier",name:r?r:n,range:e.range||v,loc:e.loc||b})):e:void 0}}),l?convertToIIFEDeclaration.call(a,{moduleName:d,dependencyNames:O,callbackFuncParams:F,hasExportsParam:A,callbackFunc:M,isOptimized:c,node:i}):u?convertToIIFE.call(a,{dependencyNames:O,callbackFuncParams:F,callbackFunc:M,node:i}):void 0},convertToObjectDeclaration=function(e,n){var r=e.node,t=defaultValues.defaultRange,a=defaultValues.defaultLOC,o=r.range||t,s=r.loc||a,i=e.moduleName,l=function(){var r,t,a,i,l,u;return"functionCallExpression"===n&&(r=e.moduleReturnValue,t=r.callee,a=t.params,a&&a.length&&_.isArray(a)&&_.where(a,{name:"global"})&&_.isObject(t.body)&&_.isArray(t.body.body)&&(i=_.where(t.body.body,{type:"ReturnStatement"})[0],_.isObject(i)&&_.isObject(i.argument)&&"FunctionExpression"===i.argument.type&&(u=i.argument,_.isObject(u.body)&&_.isArray(u.body.body)&&(l=_.where(u.body.body,{type:"ReturnStatement"})[0],_.isObject(l.argument)&&_.isObject(l.argument.right)&&_.isObject(l.argument.right.property)&&l.argument.right.property.name&&(r={type:"MemberExpression",computed:!1,object:{type:"Identifier",name:"window",range:o,loc:s},property:{type:"Identifier",name:l.argument.right.property.name,range:o,loc:s},range:o,loc:s}))))),r=r||e.moduleReturnValue}(),u={type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:{type:"Identifier",name:i,range:o,loc:s},right:l,range:o,loc:s},range:o,loc:s};return u},createAst=function(e){var n=this,r=n.options,t=r.filePath,a=e||r.code||(t?utils.readFile(t):""),o=r.esprima;if(a){if(!_.isPlainObject(esprima)||!_.isFunction(esprima.parse))throw new Error(errorMsgs.esprima);return esprima.parse(a,o)}throw new Error(errorMsgs.emptyCode)},convertDefinesAndRequires=function(e,n){var r,t,a,o,s,i,l,u,c,d=this,p=d.options,m=utils.isDefine(e),f=utils.isRequire(e),g=!1,y="",h=defaultValues.defaultRange,v=defaultValues.defaultLOC,b=e.range||h,x=e.loc||v,E=defaultValues.dependencyBlacklist;if(l=m||f?e.expression.loc.start.line:e&&e.loc&&e.loc.start?e.loc.start.line:null,u=d.matchingCommentLineNumbers[l]||d.matchingCommentLineNumbers[l-1],utils.isAMDConditional(e)){if(!u&&p.transformAMDChecks===!0)return e.test={type:"Literal",value:!0,raw:"true",range:b,loc:x},e;p.transformAMDChecks===!1&&estraverse.traverse(e,{enter:function(e){utils.isDefine(e)&&e.expression&&e.expression.arguments&&e.expression.arguments.length&&"Literal"===e.expression.arguments[0].type&&e.expression.arguments[0].value&&(d.conditionalModulesToIgnore[e.expression.arguments[0].value]=!0,p.createAnonymousAMDModule===!0&&(d.storedModules[e.expression.arguments[0].value]=!1,e.expression.arguments.shift()))}})}if(!m&&!f)return"FunctionExpression"===e.type&&_.isArray(e.params)&&_.where(e.params,{type:"Identifier",name:"exports"}).length&&_.isObject(e.body)&&_.isArray(e.body.body)&&!_.where(e.body.body,{type:"ReturnStatement",argument:{type:"Identifier"}}).length&&(c=function(){return n&&n.arguments?n&&n.arguments&&n.arguments.length?_.where(n.arguments,{type:"FunctionExpression"}).length:!1:!1}(),c&&e.body.body.unshift({type:"ExpressionStatement",expression:{type:"AssignmentExpression",operator:"=",left:{type:"Identifier",name:"exports",range:h,loc:v},right:{type:"LogicalExpression",operator:"||",left:{type:"Identifier",name:"exports",range:h,loc:v},right:{type:"ObjectExpression",properties:[],range:h,loc:v},range:h,loc:v},range:h,loc:v},range:h,loc:v}),e.body.body.push({type:"ReturnStatement",argument:{type:"Identifier",name:"exports",range:h,loc:v},range:h,loc:v})),e;if(t=Array.prototype.slice.call(e.expression.arguments,0),a=function(){var e=f?t[0]:t[t.length-2],n=[];return e=_.isPlainObject(e)?e.elements||[]:[],_.isArray(e)&&e.length&&_.each(e,function(e){"remove"!==E[e.value]&&(E[e.value]?"remove"!==E[e.value]&&n.push("{}"):n.push(e.value))}),n}(),o=f?t[1]:t[t.length-1],s=e.expression.arguments[0].value,r=normalizeModuleName.call(d,s),i={node:e,moduleName:r,moduleId:s,dependencies:a,moduleReturnValue:o,isDefine:m,isRequire:f,range:b,loc:x},m){if(u||!r||d.conditionalModulesToIgnore[r]===!0)return d.options.ignoreModules.push(r),e;if(_.contains(p.removeModules,r))return d.storedModules[r]=!1,{type:"EmptyStatement"};if(_.isObject(p.shimOverrides)&&p.shimOverrides[r]&&(i.moduleReturnValue=createAst.call(d,p.shimOverrides[r]),_.isArray(i.moduleReturnValue.body)&&_.isObject(i.moduleReturnValue.body[0])?_.isObject(i.moduleReturnValue.body[0].expression)&&(i.moduleReturnValue=i.moduleReturnValue.body[0].expression,y="objectExpression"):i.moduleReturnValue=o),i.moduleReturnValue&&"Identifier"===i.moduleReturnValue.type&&(y="functionExpression"),_.contains(p.ignoreModules,r))return e;if(utils.isFunctionExpression(o)||"functionExpression"===y)return convertToFunctionExpression.call(d,i);if(utils.isObjectExpression(o)||"objectExpression"===y)return convertToObjectDeclaration.call(d,i);if(utils.isFunctionCallExpression(o))return convertToObjectDeclaration.call(d,i,"functionCallExpression")}else if(f)return u?e:(g=_.isArray(e.expression.arguments)&&e.expression.arguments.length?e.expression.arguments[1]&&e.expression.arguments[1].body&&e.expression.arguments[1].body.body&&e.expression.arguments[1].body.body.length:!1,p.removeAllRequires!==!0&&g?convertToFunctionExpression.call(d,i):{type:"EmptyStatement",range:b,loc:x})},traverseAndUpdateAst=function(e){var n=this,r=n.options,t=e.ast;if(!_.isPlainObject(e))throw new Error(errorMsgs.invalidObject("traverseAndUpdateAst"));if(!t)throw new Error(errorMsgs.emptyAst("traverseAndUpdateAst"));if(!_.isPlainObject(estraverse)||!_.isFunction(estraverse.replace))throw new Error(errorMsgs.estraverse);return estraverse.replace(t,{enter:function(e,t){var a;return"Program"===e.type?(a=function(){var t=[];return n.comments=e.comments,_.each(e.comments,function(e){var n=e.value.trim();n===r.commentCleanName&&t.push(e)}),t}(),_.each(a,function(e){currentLineNumber=e.loc.start.line,n.matchingCommentLineNumbers[currentLineNumber]=!0}),e):convertDefinesAndRequires.call(n,e,t)},leave:function(e){return e}}),t},getNormalizedModuleName=function(e){if(utils.isDefine(e)){var n=this,r=e.expression.arguments[0].value,t=normalizeModuleName.call(n,r);return t}},findAndStoreAllModuleIds=function(e){var n=this;if(!e)throw new Error(errorMsgs.emptyAst("findAndStoreAllModuleIds"));if(!_.isPlainObject(estraverse)||!_.isFunction(estraverse.traverse))throw new Error(errorMsgs.estraverse);estraverse.traverse(e,{enter:function(e,r){var t=getNormalizedModuleName.call(n,e,r);t&&!n.storedModules[t]&&(n.storedModules[t]=!0)}})},generateCode=function(e){var n=this,r=n.options,t=r.esprima||{},a=r.escodegen||{};if(!_.isPlainObject(escodegen)||!_.isFunction(escodegen.generate))throw new Error(errorMsgs.escodegen);if(t.comment===!0&&a.comment===!0)try{e=escodegen.attachComments(e,e.comments,e.tokens)}catch(o){}return escodegen.generate(e,a)},clean=function(){var e,n=this,r=n.options,t=r.ignoreModules,a={},o={},s=[],i={},l={},u=defaultValues.defaultRange,c=defaultValues.defaultLOC;return a=createAst.call(n),findAndStoreAllModuleIds.call(n,a),o=traverseAndUpdateAst.call(n,{ast:a}),o&&_.isArray(o.body)&&estraverse.replace(o,{enter:function(e,a){var o,s,i,l=e&&e.left&&e.left.name?e.left.name:"",d=e.right,p=[],m={},f=_.filter(n.callbackParameterMap[l],function(e){return e&&e.count>1}),g=[];if(void 0===e||"EmptyStatement"===e.type)_.each(a.body,function(e,n){(void 0===e||"EmptyStatement"===e.type)&&a.body.splice(n,1)});else{if(utils.isRequireExpression(e))return e.arguments&&e.arguments[0]&&e.arguments[0].value?(o=normalizeModuleName.call(n,e.arguments[0].value),-1===t.indexOf(o)?{type:"Identifier",name:o,range:e.range||u,loc:e.loc||c}:e):e;if(r.aggressiveOptimizations===!0&&"AssignmentExpression"===e.type&&l&&(i=_.map(d&&d.callee&&d.callee.params?d.callee.params:[],function(e){return e.name}),s=_.map(d.arguments,function(e){return e.name}),_.each(s,function(e){_.each(n.callbackParameterMap[e],function(e){var n=e.name,r=e.count;_.each(i,function(e,t){r>1&&n===e&&g.push(t)})})}),_.each(g,function(e){d.arguments.splice(e,e+1),d.callee.params.splice(e,e+1)}),n.callbackParameterMap[l]))return e.right=function(){return r.aggressiveOptimizations===!0&&f.length?(p=_.map(f,function(e,n){return{type:"AssignmentExpression",operator:"=",left:{type:"Identifier",name:e.name,range:u,loc:c},right:n1&&(e=n.name,r[e]=!0)})}),r}(),i=_.merge(_.cloneDeep(_.reduce(n.storedModules,function(e,n,r){return n!==!1&&(e[r]=!0),e},{})),l),_.each(i,function(e,n){_.contains(r.ignoreModules,n)||s.push({type:"VariableDeclarator",id:{type:"Identifier",name:n,range:u,loc:c},init:null,range:u,loc:c})}),s.length&&o.body.unshift({type:"VariableDeclaration",declarations:s,kind:"var",range:u,loc:c}),e=generateCode.call(n,o),_.isObject(r.wrap)&&(_.isString(r.wrap.start)&&r.wrap.start.length&&(e=r.wrap.start+e),_.isString(r.wrap.end)&&r.wrap.end.length&&(e+=r.wrap.end)),e},function(){!function(e,n){"function"==typeof define&&define.amd?(n.env="undefined"!=typeof exports?"node":"web",n.amd=!0,define(["esprima","estraverse","escodegen","underscore"],function(r,t,a,o){return n({esprima:r,estraverse:t,escodegen:a,underscore:o},e)})):"undefined"!=typeof exports?(n.env="node",module.exports=n(null,e)):(n.env="web",e.amdclean=n(null,e))}(this,function e(n,r){var t=e.env,a=function(e,n){if(!esprima)throw new Error(errorMsgs.esprima);if(!estraverse)throw new Error(errorMsgs.estraverse);if(!escodegen)throw new Error(errorMsgs.escodegen);if(!_)throw new Error(errorMsgs.lodash);var r=_.cloneDeep(this.defaultOptions||{}),t=e||n||{};!_.isPlainObject(e)&&_.isString(e)&&(t=_.merge({code:e},_.isObject(n)?n:{})),this.storedModules={},this.callbackParameterMap={},this.conditionalModulesToIgnore={},this.matchingCommentLineNumbers={},this.comments=[],this.options=_.merge(r,t)},o={VERSION:"2.1.0",clean:function(e,n){var r=new a(e,n),t=r.clean();return t}};return a.prototype={env:t,clean:clean,defaultOptions:defaultOptions},n=n||{},esprima=function(){return e.amd&&n.esprima&&n.esprima.parse?n.esprima:r&&r.esprima&&r.esprima.parse?r.esprima:"node"===t?require("esprima"):void 0}(),estraverse=function(){return e.amd&&n.estraverse&&n.estraverse.traverse?n.estraverse:r&&r.estraverse&&r.estraverse.traverse?r.estraverse:"node"===t?require("estraverse"):void 0}(),escodegen=function(){return e.amd&&n.escodegen&&n.escodegen.generate?n.escodegen:r&&r.escodegen&&r.escodegen.generate?r.escodegen:"node"===t?require("escodegen"):void 0}(),_=function(){return e.amd&&n.underscore?n.underscore:r&&r._?r._:"node"===t?require("lodash"):void 0}(),o})}()}("undefined"!=typeof esprima?esprima:null,"undefined"!=typeof estraverse?estraverse:null,"undefined"!=typeof escodegen?escodegen:null,"undefined"!=typeof _?_:null); \ No newline at end of file diff --git a/src/amdclean.js b/src/amdclean.js index 4d5693b..ed54756 100644 --- a/src/amdclean.js +++ b/src/amdclean.js @@ -150,7 +150,11 @@ utils = function () { 'value': 'function' } }; - return _.where(node.test, matchObject).length || _.where([node.test], matchObject).length || _.where(node.test.left, matchObject).length || _.where([node.test.left], matchObject).length; + try { + return _.where(node.test, matchObject).length || _.where([node.test], matchObject).length || _.where(node.test.left, matchObject).length || _.where([node.test.left], matchObject).length; + } catch (e) { + return false; + } }, 'returnExpressionIdentifier': function (name) { return { diff --git a/src/modules/utils.js b/src/modules/utils.js index 8f5ab34..7ce9761 100644 --- a/src/modules/utils.js +++ b/src/modules/utils.js @@ -136,10 +136,14 @@ define([ } }; - return (_.where(node.test, matchObject).length || - _.where([node.test], matchObject).length || - _.where(node.test.left, matchObject).length || - _.where([node.test.left], matchObject).length); + try { + return (_.where(node.test, matchObject).length || + _.where([node.test], matchObject).length || + _.where(node.test.left, matchObject).length || + _.where([node.test.left], matchObject).length); + } catch(e) { + return false; + } }, // returnExpressionIdentifier diff --git a/test/specs/convert.js b/test/specs/convert.js index c6ed0d0..9d9c896 100644 --- a/test/specs/convert.js +++ b/test/specs/convert.js @@ -631,9 +631,9 @@ describe('amdclean specs', function() { it('should correctly convert libraries that use factory function parameters', function() { var AMDcode = "(function (factory) {" + "if (typeof exports === 'object') {" + - "module.exports = factory(require('backbone'), require('underscore'));" + + "module.exports = factory(require('backbone'), require('underscore'));" + "} else if (typeof define === 'function' && define.amd) {" + - "define('backbonevalidation', ['backbone', 'underscore'], factory);" + + "define('backbonevalidation', ['backbone', 'underscore'], factory);" + "}" + "}(function (Backbone, _) {" + "//= backbone-validation.js\n" +