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.2.1 to 2.0.0

plugin.js

6

bin/cmd.js

@@ -6,2 +6,3 @@ #!/usr/bin/env node

var fs = require('fs');
var argv = require('yargs')

@@ -23,2 +24,3 @@ .options('t', {

.argv;
var file = argv._[0];

@@ -30,6 +32,6 @@ var input;

input = process.stdin;
}
}
input.pipe(concat(function(buf) {
console.log(derequire(buf.toString('utf8'), argv.t, argv.f));
console.log(derequire(buf, argv.t, argv.f));
}));
'use strict';
var estraverse = require('estraverse');
var esprima = require('esprima-fb');
var esrefactor = require('esrefactor');
var _requireRegexp = /require.*\(.*['"]/m;
var acorn = require('acorn');
var escope = require('escope');
function requireRegexp(token) {
if (token === 'require') {
return _requireRegexp;
var requireRegexp = /\brequire\b/;
function write(arr, str, offset) {
for (var i = 0, l = str.length; i < l; i++) {
arr[offset + i] = str[i];
}
return new RegExp(token + '.*\\(.*[\'"]', 'm');
}
function testParse (code) {
try {
return esprima.parse(code, { range: true });
} catch (e) {}
}
function rename(code, tokenTo, tokenFrom) {
function rename(code, tokenTo, tokenFrom) {
tokenTo = tokenTo || '_dereq_';
tokenFrom = tokenFrom || 'require';
var tokens;
if (!Array.isArray(tokenTo)) {
tokens = [{
to: tokenTo,
from: tokenFrom
from: tokenFrom || 'require',
to: tokenTo || '_dereq_'
}];

@@ -34,43 +25,68 @@ } else {

}
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 (!tokens.some(function (item) {
var results = requireRegexp(item.from).test(code);
return results;
})) {
tokens.forEach(function(token) {
if (token.to.length !== token.from.length) {
throw new Error('"' + token.to + '" and "' + token.from + '" must be the same length');
}
});
if (tokens.length === 1 &&
tokens[0].from === 'require' &&
!requireRegexp.test(code)) {
return code;
}
var inCode = '!function(){'+code+'\n;}';
var ast = testParse(inCode);
if(!ast){
var ast;
try {
ast = acorn.parse(code, {
ecmaVersion: 6,
ranges: true,
allowReturnOutsideFunction: true
});
} catch(err) {
// this should probably log something and/or exit violently
return code;
}
var tokenNames = tokens.map(function (item) {
return item.from;
});
var ctx = new esrefactor.Context(ast);
ctx._code = inCode;
estraverse.traverse(ast,{
enter:function(node, parent) {
var index;
var test = parent &&
(parent.type === 'FunctionDeclaration' || parent.type === 'FunctionExpression' ||
parent.type === 'VariableDeclarator') &&
node.type === 'Identifier' && (index = tokenNames.indexOf(node.name)) !== -1;
if (test) {
ctx._code = ctx.rename(ctx.identify(node.range[0]), tokens[index].to);
//
// heavily inspired by https://github.com/estools/esshorten
//
code = String(code).split('');
var manager = escope.analyze(ast, {optimistic: true, ecmaVersion: 6});
for (var i = 0, iz = manager.scopes.length; i < iz; i++) {
var scope = manager.scopes[i];
for (var j = 0, jz = scope.variables.length; j < jz; j++) {
var variable = scope.variables[j];
if (variable.tainted || variable.identifiers.length === 0) {
continue;
}
for (var k = 0, kz = tokens.length; k < kz; k++) {
var token = tokens[k];
if (variable.name !== token.from) {
continue;
}
for (var l = 0, lz = variable.identifiers.length; l < lz; l++) {
var def = variable.identifiers[l];
write(code, token.to, def.range[0]);
}
for (var m = 0, mz = variable.references.length; m < mz; m++) {
var ref = variable.references[m];
write(code, token.to, ref.identifier.range[0]);
}
}
}
});
return ctx._code.slice(12, -3);
}
return code.join('');
}
module.exports = rename;
{
"name": "derequire",
"version": "1.2.1",
"version": "2.0.0",
"description": "remove requires",

@@ -10,4 +10,11 @@ "main": "index.js",

"scripts": {
"test": "istanbul test _mocha test/test.js"
"test": "istanbul test _mocha test/test.js",
"perf": "node test/perf.js"
},
"files": [
"bin/cmd.js",
"index.js",
"plugin.js",
"readme.md"
],
"repository": {

@@ -24,13 +31,12 @@ "type": "git",

"dependencies": {
"acorn": "^0.12.0",
"concat-stream": "^1.4.6",
"esprima-fb": "^10001.1.0-dev-harmony-fb",
"esrefactor": "~0.1.0",
"estraverse": "~1.9.1",
"yargs": "^1.3.1"
"escope": "^2.0.6",
"through2": "^0.6.3",
"yargs": "^3.4.5"
},
"devDependencies": {
"chai": "~1.8.1",
"mocha": "~1.16.2",
"istanbul": "~0.2.1"
"mocha": "^2.2.0",
"istanbul": "^0.3.7"
}
}
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