Skip to content

Commit

Permalink
Merge pull request #31 from archmoj/allow-cwise-transform
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Jun 15, 2020
2 parents 798b0d5 + e619afc commit 09c4b83
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions index.js
@@ -1,9 +1,12 @@
var unparse = require('escodegen').generate;

module.exports = function (ast, vars) {
module.exports = function (ast, vars, opts) {
if(!opts) opts = {};
var rejectAccessToMethodsOnFunctions = !opts.allowAccessToMethodsOnFunctions;

if (!vars) vars = {};
var FAIL = {};

var result = (function walk (node, noExecute) {
if (node.type === 'Literal') {
return node.value;
Expand Down Expand Up @@ -63,7 +66,7 @@ module.exports = function (ast, vars) {
if (l === FAIL) return FAIL;
var r = walk(node.right, noExecute);
if (r === FAIL) return FAIL;

if (op === '==') return l == r;
if (op === '===') return l === r;
if (op === '!=') return l != r;
Expand All @@ -80,7 +83,7 @@ module.exports = function (ast, vars) {
if (op === '|') return l | r;
if (op === '&') return l & r;
if (op === '^') return l ^ r;

return FAIL;
}
else if (node.type === 'Identifier') {
Expand All @@ -100,7 +103,7 @@ module.exports = function (ast, vars) {
if (callee === FAIL) return FAIL;
if (typeof callee !== 'function') return FAIL;


var ctx = node.callee.object ? walk(node.callee.object, noExecute) : FAIL;
if (ctx === FAIL) ctx = null;

Expand All @@ -119,8 +122,9 @@ module.exports = function (ast, vars) {
}
else if (node.type === 'MemberExpression') {
var obj = walk(node.object, noExecute);
// do not allow access to methods on Function
if((obj === FAIL) || (typeof obj == 'function')){
if((obj === FAIL) || (
(typeof obj == 'function') && rejectAccessToMethodsOnFunctions
)){
return FAIL;
}
if (node.property.type === 'Identifier' && !node.computed) {
Expand All @@ -147,7 +151,7 @@ module.exports = function (ast, vars) {
}
else if (node.type === 'FunctionExpression') {
var bodies = node.body.body;

// Create a "scope" for our arguments
var oldVars = {};
Object.keys(vars).forEach(function(element){
Expand All @@ -168,7 +172,7 @@ module.exports = function (ast, vars) {
}
// restore the vars and scope after we walk
vars = oldVars;

var keys = Object.keys(vars);
var vals = keys.map(function(key) {
return vars[key];
Expand Down Expand Up @@ -196,7 +200,7 @@ module.exports = function (ast, vars) {
}
else return FAIL;
})(ast);

return result === FAIL ? undefined : result;
};

Expand Down

0 comments on commit 09c4b83

Please sign in to comment.