Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

browserify-transform-tools

Package Overview
Dependencies
6
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.5.3 to 1.6.0

test/functionTransformTest.coffee

138

lib/transformTools.js
// Generated by CoffeeScript 1.9.2
(function() {
var clone, falafel, fs, isRootDir, loadConfig, merge, path, skipFile, through;
var clone, evaluateFunctionArgs, falafel, fs, isRootDir, loadConfig, merge, path, skipFile, through,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

@@ -221,31 +222,6 @@ path = require('path');

transform = exports.makeFalafelTransform(transformName, options, function(node, transformOptions, done) {
var arg, args, dirname, varNames, vars;
var args;
if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'require') {
if (evaluateArguments) {
dirname = path.dirname(transformOptions.file);
varNames = ['__filename', '__dirname', 'path', 'join'];
vars = [transformOptions.file, dirname, path, path.join];
args = node["arguments"].map(function(arg) {
var err, t;
t = "return " + (arg.source());
try {
return Function(varNames, t).apply(null, vars);
} catch (_error) {
err = _error;
return arg.source();
}
});
} else {
args = (function() {
var i, len, ref1, results;
ref1 = node["arguments"];
results = [];
for (i = 0, len = ref1.length; i < len; i++) {
arg = ref1[i];
results.push(arg.source());
}
return results;
})();
}
return transformFn(args, transformOptions, function(err, transformed) {
args = evaluateFunctionArgs(evaluateArguments, transformOptions, node);
return transformFn(args.values(), transformOptions, function(err, transformed) {
if (err) {

@@ -275,2 +251,55 @@ return done(err);

exports.makeFunctionTransform = function(transformName, options, transformFn) {
var evaluateArguments, functionNames, ref, transform;
if (options == null) {
options = {};
}
if (transformFn == null) {
transformFn = options;
options = {};
}
evaluateArguments = (ref = options.evaluateArguments) != null ? ref : true;
functionNames = [];
if (options.functionNames != null) {
if (Array.isArray(options.functionNames) || {}.toString.call(options.functionNames) === '[object Array]') {
functionNames = options.functionNames;
} else if (typeof options.functionNames === 'string') {
functionNames = [options.functionNames];
}
}
if (functionNames.length === 0) {
functionNames.push('require');
}
transform = exports.makeFalafelTransform(transformName, options, function(node, transformOptions, done) {
var args, ref1;
if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && (ref1 = node.callee.name, indexOf.call(functionNames, ref1) >= 0)) {
args = evaluateFunctionArgs(evaluateArguments, transformOptions, node);
return transformFn({
name: node.callee.name,
args: args
}, transformOptions, function(err, transformed) {
if (err) {
return done(err);
}
if (transformed != null) {
node.update(transformed);
}
return done();
});
} else {
return done();
}
});
transform.configure = function(config, configOptions) {
var answer;
if (configOptions == null) {
configOptions = {};
}
answer = exports.makeFunctionTransform(transformName, options, transformFn);
answer.setConfig(config, configOptions);
return answer;
};
return transform;
};
exports.runTransform = function(transform, file, options, done) {

@@ -319,2 +348,53 @@ var doTransform;

evaluateFunctionArgs = function(evaluateArguments, transformOptions, node) {
var arg, args, dirname, varNames, vars;
if (evaluateArguments) {
dirname = path.dirname(transformOptions.file);
varNames = ['__filename', '__dirname', 'path', 'join'];
vars = [transformOptions.file, dirname, path, path.join];
args = node["arguments"].map(function(arg) {
var err, t;
t = "return " + (arg.source());
try {
return {
value: Function(varNames, t).apply(null, vars),
type: arg.type
};
} catch (_error) {
err = _error;
return {
value: arg.source(),
type: arg.type
};
}
});
} else {
args = (function() {
var i, len, ref, results;
ref = node["arguments"];
results = [];
for (i = 0, len = ref.length; i < len; i++) {
arg = ref[i];
results.push({
value: arg.source(),
type: arg.type
});
}
return results;
})();
}
args.values = function() {
var i, len, values;
values = [];
for (i = 0, len = this.length; i < len; i++) {
arg = this[i];
if (arg.value != null) {
values.push(arg.value);
}
}
return values;
};
return args;
};
}).call(this);
{
"name": "browserify-transform-tools",
"version": "1.5.3",
"version": "1.6.0",
"description": "Utilities for writing browserify transforms.",

@@ -5,0 +5,0 @@ "main": "./lib/transformTools.js",

@@ -127,2 +127,29 @@ [![Build Status](https://travis-ci.org/benbria/browserify-transform-tools.svg)](https://travis-ci.org/benbria/browserify-transform-tools)

Creating a Function Transform
============================
These transforms are focused on transforming arbitrary function calls:
```JavaScript
transform = transformTools.makeRequireTransform("requireTransform",
{evaluateArguments: true, functionNames: ["foobar"]},
function(functionParams, opts, cb) {
if (functionParams.args[0].value === "foo") {
return cb(null, functionParams.name + "('bar')");
} else {
return cb();
}
});
```
This will take all calls to `foobar("foo")` and transform them to `foobar('bar')`. Note that makeFunctionTransform can parse many simple expressions, so the above would succesfully parse `foobar("f" + "oo")`, for example. Any expression involving core JavaScript, `__filename`, `__dirname`, `path`, and `join` (where join is an alias for `path.join`) can be parsed. Setting the `evaluateArguments` option to false will disable this behavior, in which case the source code for everything inside the ()s will be returned.
Note that `makeFunctionTransform` expects your function to return the complete `[functionName](...)` call. This makes it possible to write function transforms which will, for example, inline resources.
The option `functionNames` can either be a string or an array of strings. If no functionName is provided `makeFunctionTransform` fallbacks to `require()` calls.
The `functionParams` object which is passed to the given transform function has 2 attributes. The first one is `name` which is the function name The second one is `args` and is an ordered array of the function args. Each entry consists of `value` and `type`. Type can be one of these values: `Literal, Identifier, FunctionExpression, ObjectExpression, ArrayExpression`.
Again, all other options you can pass to `makeStringTransform` are valid here, too.
Loading Configuration

@@ -129,0 +156,0 @@ =====================

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc