Comparing version 0.1.0 to 0.2.0
@@ -11,3 +11,6 @@ 'use strict'; | ||
leave:function(node, parent) { | ||
if (node.type == 'Identifier'&&node.name===tokenFrom){ | ||
var isVariableName = node.type === 'Identifier'&&node.name===tokenFrom; | ||
var isArugment = parent && (parent.type === 'FunctionDeclaration' || parent.type === 'FunctionExpression'); | ||
var isCall = parent && (parent.type === 'CallExpression' && parent.callee.type === 'Identifier' && parent.callee.name === tokenFrom); | ||
if (isVariableName && (isArugment||isCall)){ | ||
node.name = tokenTo; | ||
@@ -14,0 +17,0 @@ } |
{ | ||
"name": "derequire", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "remove requires", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,2 +13,2 @@ derequire | ||
takes a string of code and replaces all instances of the identifier `tokenFrom` (default 'require') and replaces them with tokenTo (default '\_\_derequire\_\_'), returnes the code. | ||
takes a string of code and replaces all instances of the identifier `tokenFrom` (default 'require') and replaces them with tokenTo (default '\_\_derequire\_\_'), returnes the code, but only if they are used as function arguments or if they are called as function. |
var should = require('chai').should(); | ||
var derequire = require('../'); | ||
var exampleText = "var x=function(require,module,exports){var process=require(\"__browserify_process\");var requireText = \"require\";}"; | ||
describe('derequire', function(){ | ||
it('should work', function(){ | ||
var exampleText = "var x=function(require,module,exports){var process=require(\"__browserify_process\");var requireText = \"require\";}"; | ||
derequire(exampleText).should.equal("var x=function(__derequire__,module,exports){var process=__derequire__('__browserify_process');var requireText='require'}"); | ||
}); | ||
it('should only replace arguments and calls',function(){ | ||
var exampleText = "function x(require,module,exports){var process=require(\"__browserify_process\");var requireText = {}; requireText.require = \"require\";(function(){var require = 'blah';}())}"; | ||
derequire(exampleText).should.equal("function x(__derequire__,module,exports){var process=__derequire__('__browserify_process');var requireText={};requireText.require='require';(function(){var require='blah'}())}"); | ||
}); | ||
}); |
3195
38