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 0.3.0 to 0.4.0

39

index.js

@@ -5,18 +5,33 @@ 'use strict';

var escodegen = require('escodegen');
var esrefactor = require('esrefactor');
function rename(code, tokenTo, tokenFrom){
tokenTo = tokenTo || '__derequire__';
tokenFrom = tokenFrom || 'require';
var ast = esprima.parse('!function(){'+code+'}');
estraverse.traverse(ast,{
leave:function(node, parent) {
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;
}
}
});
return escodegen.generate(ast.body[0].expression.argument.body.body[0]);
var inCode = '!function(){'+code+'}';
var location, ctx, ast;
var foundOne = true;
while(foundOne){
foundOne = false;
ast = esprima.parse(inCode,{range:true});
estraverse.traverse(ast,{
enter:function(node, parent) {
var isVariableName = node.type === 'Identifier'&&node.name===tokenFrom;
var isArugment = parent && (parent.type === 'FunctionDeclaration' || parent.type === 'FunctionExpression');
if(isVariableName&&isArugment){
location = node.range[0];
foundOne = true;
this.break();
}
}
});
if(foundOne){
ctx = new esrefactor.Context(inCode);
inCode = ctx.rename(ctx.identify(location), tokenTo);
}
}
return escodegen.generate(esprima.parse(inCode).body[0].expression.argument.body.body[0]);
}
module.exports = rename;
{
"name": "derequire",
"version": "0.3.0",
"version": "0.4.0",
"description": "remove requires",

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

"esprima": "~1.0.4",
"escodegen": "~1.0.1"
"escodegen": "~1.0.1",
"esrefactor": "~0.1.0"
},

@@ -25,0 +26,0 @@ "devDependencies": {

@@ -13,5 +13,5 @@ var should = require('chai').should();

it('should handle top level return statments', function(){
var exampleText = 'return require("require");';
derequire(exampleText).should.equal("return __derequire__('require');");
var exampleText = 'return (function(require){return require();}(function(){return "sentinel";}));';
derequire(exampleText).should.equal("return function (__derequire__) {\n return __derequire__();\n}(function () {\n return 'sentinel';\n});");
});
});
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