Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ns.evaluate = function(ast, propagatedConstants) {
var controller = new estraverse.Controller();
// console.log("Compute constants for", codegen.generate(ast), propagatedConstants)
this.setConstants(propagatedConstants);
controller.traverse(ast, {
leave: exitExpression.bind(controller, this)
})
this.setConstants(null);
}
}(exports));
// add all the node types as listeners
Object.keys(rule).forEach(function(nodeType) {
api.on(nodeType, timing.enabled
? timing.time(key, rule[nodeType])
: rule[nodeType]
);
});
} catch (ex) {
ex.message = "Error while loading rule '" + key + "': " + ex.message;
throw ex;
}
});
// save config so rules can access as necessary
currentConfig = config;
controller = new estraverse.Controller();
ecmaFeatures = currentConfig.parserOptions.ecmaFeatures || {};
ecmaVersion = currentConfig.parserOptions.ecmaVersion || 5;
// gather data that may be needed by the rules
scopeManager = escope.analyze(ast, {
ignoreEval: true,
nodejsScope: ecmaFeatures.globalReturn,
impliedStrict: ecmaFeatures.impliedStrict,
ecmaVersion: ecmaVersion,
sourceType: currentConfig.parserOptions.sourceType || "script",
childVisitorKeys: espree.VisitorKeys,
fallback: "none"
});
currentScopes = scopeManager.scopes;
api.on(nodeType, rule[nodeType]);
});
} catch(ex) {
ex.message = "Error while loading rule '" + key + "': " + ex.message;
throw ex;
}
} else {
throw new Error("Definition for rule '" + key + "' was not found.");
}
});
// save config so rules can access as necessary
currentConfig = config;
currentText = text;
controller = new estraverse.Controller();
// gather data that may be needed by the rules
currentScopes = escope.analyze(ast, { ignoreEval: true }).scopes;
/*
* Index the scopes by the start range of their block for efficient
* lookup in getScope.
*/
scopeMap = [];
currentScopes.forEach(function (scope, index) {
var range = scope.block.range[0];
// Sometimes two scopes are returned for a given node. This is
// handled later in a known way, so just don't overwrite here.
if (!scopeMap[range]) {
scopeMap[range] = index;
function replaceEnvCallSelect(tree, envMap) {
var controller = new estraverse.Controller();
var resultTree = controller.replace(tree, {
enter : function (node, parent) {
if (isQxCoreEnvironmentCall(node, ["select"])) {
var envKey = getKeyFromEnvCall(node);
try {
return getEnvSelectValueByKey(node, envMap[envKey]);
} catch (error) {
// intentionally empty
// => no return means no replacement
// possible reasons:
// * envMap has no envKey because the envKey is runtime based (os.version)
// and cannot be configured upfront.
}
}
}
});
replace: function(ast) {
var controller = new walk.Controller(),
context = this.context,
that = this;
ast = controller.replace(ast, {
enter: function (node, parent) {
switch (node.type) {
case Syntax.Identifier:
return enterIdentifier(node, parent, context);
case Syntax.IfStatement:
return enterIfStatement(node);
case Syntax.FunctionDeclaration:
return enterFunctionDeclaration(node, context);
}
},
function replaceEnvCallGet(tree, envMap) {
var controller = new estraverse.Controller();
var resultTree = controller.replace(tree, {
enter : function (node, parent) {
if (isQxCoreEnvironmentCall(node, ["get"])) {
var envKey = getKeyFromEnvCall(node);
if (envMap && envKey in envMap) {
return {
"type": "Literal",
"value": envMap[envKey],
"raw": ((typeof(envMap[envKey]) === "string")
? "\""+envMap[envKey]+"\""
: envMap[envKey].toString())
};
}
}
}
});
ns.annotateRight = function(context, ast, propagatedConstants) {
if(!ast)
throw Error("No node to analyze");
var controller = new estraverse.Controller();
context.setConstants(propagatedConstants || null);
controller.traverse(ast, {
enter: function(node) {
if(node.type == Syntax.VariableDeclaration) {
context.setInDeclaration(true);
}
},
leave: function(node, parent) {
if (handlers.hasOwnProperty(node.type)) {
return handlers[node.type].call(this, node, parent, context);
}
return null;
}
});
annotate: function(tree) {
var controller = new estraverse.Controller();
controller.traverse(tree, {
enter : function (node, parent) {
node[annotateKey] = parent;
}
});
}
};
function findVariableReferences(ast) {
var references = new Set();
var controller = new walk.Controller();
walk.traverse(ast, {
leave: function(node, parent) {
switch(node.type) {
case Syntax.AssignmentExpression:
references = Set.union(references, findVariableReferences(node.right));
controller.break();
break;
case Syntax.Identifier:
if (ASTTools.isVariableReference(node, parent)) {
references.add(node.name);
}
break;
case Syntax.MemberExpression:
if (isMemberReference(node, parent)) {
if (ASTTools.isVariableReference(node.object, node)) {
references.add(node.name);