Comparing version 1.1.0 to 1.2.0
29
index.js
@@ -25,8 +25,21 @@ 'use strict'; | ||
tokenFrom = tokenFrom || 'require'; | ||
if(tokenTo.length !== tokenFrom.length){ | ||
var tokens; | ||
if (!Array.isArray(tokenTo)) { | ||
tokens = [{ | ||
to: tokenTo, | ||
from: tokenFrom | ||
}]; | ||
} else { | ||
tokens = tokenTo; | ||
} | ||
if(tokens.some(function (item) { | ||
return item.to.length !== item.from.length; | ||
})){ | ||
throw new Error('bad stuff will happen if you try to change tokens of different length'); | ||
} | ||
if (!requireRegexp(tokenFrom).test(code)) { | ||
if (!tokens.some(function (item) { | ||
var results = requireRegexp(item.from).test(code); | ||
return results; | ||
})) { | ||
return code; | ||
@@ -41,3 +54,5 @@ } | ||
} | ||
var tokenNames = tokens.map(function (item) { | ||
return item.from; | ||
}); | ||
var ctx = new esrefactor.Context(inCode); | ||
@@ -47,9 +62,9 @@ | ||
enter:function(node, parent) { | ||
var index; | ||
var test = parent && | ||
(parent.type === 'FunctionDeclaration' || parent.type === 'FunctionExpression' || | ||
parent.type === 'VariableDeclarator') && | ||
node.name === tokenFrom && node.type === 'Identifier'; | ||
node.type === 'Identifier' && (index = tokenNames.indexOf(node.name)) !== -1; | ||
if (test) { | ||
ctx._code = ctx.rename(ctx.identify(node.range[0]), tokenTo); | ||
ctx._code = ctx.rename(ctx.identify(node.range[0]), tokens[index].to); | ||
} | ||
@@ -56,0 +71,0 @@ } |
{ | ||
"name": "derequire", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "remove requires", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
derequire | ||
derequire [![Build Status](https://travis-ci.org/calvinmetcalf/derequire.svg)](https://travis-ci.org/calvinmetcalf/derequire) | ||
==== | ||
@@ -13,4 +13,19 @@ | ||
takes a string of code and replaces all instances of the identifier `tokenFrom` (default 'require') and replaces them with tokenTo (default '\_dereq\_') but only if they are functional arguments and subsequent uses of said argument, then returnes the code. | ||
takes a string of code and replaces all instances of the identifier `tokenFrom` (default 'require') and replaces them with tokenTo (default '\_dereq\_') but only if they are functional arguments or variable declerations and subsequent uses of said argument, then returnes the code. | ||
For multiple renames at the same times accepts the syntax | ||
```js | ||
derequire(code, [ | ||
{ | ||
from: 'require', | ||
to: '_dereq_' | ||
}, | ||
{ | ||
from: 'define', | ||
to: '_defi_' | ||
} | ||
]); | ||
``` | ||
__Note:__ in order to avoid quite a few headaches the token you're changing from and the token you're changing to need to be the same length. |
@@ -83,3 +83,15 @@ var should = require('chai').should(); | ||
}); | ||
it('should work on multiple things', function () { | ||
var file = fs.readFileSync('./test/define.js', {encoding: 'utf8'}); | ||
derequire(file, [ | ||
{ | ||
from: 'require', | ||
to: '_derec_' | ||
}, | ||
{ | ||
from: 'define', | ||
to: '_defi_' | ||
} | ||
]).should.equal(fs.readFileSync('./test/define.dereq.js', {encoding: 'utf8'})); | ||
}); | ||
}); |
754237
15
21141
31