Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

derequire

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

derequire - npm Package Compare versions

Comparing version 1.0.0 to 1.1.0

31

bin/cmd.js

@@ -6,11 +6,28 @@ #!/usr/bin/env node

var fs = require('fs');
var argv = require('yargs')
.options('t', {
alias : 'to',
default : '_dereq_',
describe: 'token to change the variable into'
})
.options('f', {
alias : 'from',
default : 'require',
describe: 'token to find and change'
})
.help('h')
.alias('h', 'help')
.version(require('../package.json').version, 'v')
.alias('v', 'version')
.argv;
var file = argv._[0];
var input;
if (file && file !== '-') {
input = fs.createReadStream(file);
} else {
input = process.stdin;
}
var file = process.argv[2];
var input = file && file !== '-'
? fs.createReadStream(process.argv[2])
: process.stdin
;
input.pipe(concat(function(buf) {
console.log(derequire(buf.toString('utf8')));
console.log(derequire(buf.toString('utf8'), argv.t, argv.f));
}));
'use strict';
var estraverse = require('estraverse');

@@ -6,44 +7,54 @@ var esprima = require('esprima-fb');

var _requireRegexp = /require.*\(.*['"]/m;
function requireRegexp(token) {
if (token === 'require') {
return _requireRegexp;
}
return new RegExp(token + '.*\\(.*[\'"]', 'm');
if (token === 'require') {
return _requireRegexp;
}
return new RegExp(token + '.*\\(.*[\'"]', 'm');
}
function testParse (code) {
try{
return esprima.parse(code,{range:true});
}catch(e){}
try {
return esprima.parse(code, { range: true });
} catch (e) {}
}
function rename(code, tokenTo, tokenFrom) {
tokenTo = tokenTo || '_dereq_';
tokenFrom = tokenFrom || 'require';
if(tokenTo.length !== tokenFrom.length){
throw new Error('bad stuff will happen if you try to change tokens of different length');
tokenTo = tokenTo || '_dereq_';
tokenFrom = tokenFrom || 'require';
if(tokenTo.length !== tokenFrom.length){
throw new Error('bad stuff will happen if you try to change tokens of different length');
}
if (!requireRegexp(tokenFrom).test(code)) {
return code;
}
var inCode = '!function(){'+code+'\n;}';
var ast = testParse(inCode);
if(!ast){
return code;
}
var ctx = new esrefactor.Context(inCode);
estraverse.traverse(ast,{
enter:function(node, parent) {
var test = parent &&
(parent.type === 'FunctionDeclaration' || parent.type === 'FunctionExpression' ||
parent.type === 'VariableDeclarator') &&
node.name === tokenFrom && node.type === 'Identifier';
if (test) {
ctx._code = ctx.rename(ctx.identify(node.range[0]), tokenTo);
}
}
if (!requireRegexp(tokenFrom).test(code)) return code;
var inCode = '!function(){'+code+'\n;}';
var ast = testParse(inCode);
if(!ast){
return code;
}
var ctx = new esrefactor.Context(inCode);
estraverse.traverse(ast,{
enter:function(node, parent) {
var test =
parent &&
(parent.type === 'FunctionDeclaration' || parent.type === 'FunctionExpression' || parent.type === 'VariableDeclarator') &&
node.name === tokenFrom &&
node.type === 'Identifier';
if(test){
ctx._code = ctx.rename(ctx.identify(node.range[0]), tokenTo);
}
}
});
return ctx._code.slice(12, -3);
});
return ctx._code.slice(12, -3);
}
module.exports = rename;
{
"name": "derequire",
"version": "1.0.0",
"version": "1.1.0",
"description": "remove requires",

@@ -26,3 +26,4 @@ "main": "index.js",

"esrefactor": "~0.1.0",
"estraverse": "~1.5.0"
"estraverse": "~1.5.0",
"yargs": "^1.3.1"
},

@@ -29,0 +30,0 @@ "devDependencies": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc