How to use the escodegen/node_modules/estraverse.replace function in escodegen

To help you get started, we’ve selected a few escodegen examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dritchie / probabilistic-js / probabilistic / transform.js View on Github external
if (!node.skip && node.type == estraverse.Syntax.CallExpression)
		{
			var replacer = makeWrappedCallReplacer(node)
			var wrapast = esprima.parse(replcode.format(nextid)).body[0].expression
			nextid++

			// We do NOT wrap the calls to enterfn, the fn itself, or leavefn
			wrapast.callee.object.body.body[0].expression.skip = true
			node.skip = true
			wrapast.callee.object.body.body[2].expression.skip = true

			// To preserve source map information 
			wrapast.loc = node.loc
			wrapast.callee.object.body.body[1].loc = node.callee.loc

			estraverse.replace(wrapast, replacer)

			// OK, now we need to extract and evaluate any & all args to this call
			//   *before* passing them to the call. This is because if we leave it them
			//   inline, the order of evaluation might get messed up.
			// For example, if we have a function call as one of the args, then this call
			//   will see the id of the outer function call on the stack, which does not reflect
			//   the execution structure of the original program.
			for (var i = 0; i < node.arguments.length; i++)
			{
				var arg = node.arguments[i]
				var decl =
				{
					type: "VariableDeclaration",
					declarations:
					[{
						type: "VariableDeclarator",
github probmods / webchurch / src / probabilistic-js / probabilistic / transform.js View on Github external
if (!node.skip && node.type == estraverse.Syntax.CallExpression)
		{
			var replacer = makeWrappedCallReplacer(node)
			var wrapast = esprima.parse(replcode.format(nextid)).body[0].expression
			nextid++

			// We do NOT wrap the calls to enterfn, the fn itself, or leavefn
			wrapast.callee.object.body.body[0].expression.skip = true
			node.skip = true
			wrapast.callee.object.body.body[2].expression.skip = true

			// To preserve source map information 
			wrapast.loc = node.loc
			wrapast.callee.object.body.body[1].loc = node.callee.loc

			estraverse.replace(wrapast, replacer)

			// OK, now we need to extract and evaluate any & all args to this call
			//   *before* passing them to the call. This is because if we leave it them
			//   inline, the order of evaluation might get messed up.
			// For example, if we have a function call as one of the args, then this call
			//   will see the id of the outer function call on the stack, which does not reflect
			//   the execution structure of the original program.
			for (var i = 0; i < node.arguments.length; i++)
			{
				var arg = node.arguments[i]
				var decl =
				{
					type: "VariableDeclaration",
					declarations:
					[{
						type: "VariableDeclarator",
github probmods / webchurch / src / wctransform.js View on Github external
function templateReplace(template, replacenode) {
  var replacer =
        {
          enter: function(node)
          {
            if (node.type == estraverse.Syntax.Identifier &&
                node.name == '__REPLACEME__')
            {
              return replacenode;
            }
            return node;
          }
        };
  var templateAST = esprima.parse(template).body[0]; //NOTE: template must be expression or single statement.
  return estraverse.replace(templateAST, replacer);
}
github probmods / webchurch / wctransform.js View on Github external
function templateReplace(template, replacenode) {
    var replacer =
	{
            enter: function(node)
	    {
		if (node.type == estraverse.Syntax.Identifier &&
		    node.name == "__REPLACEME__")
		{
		    return replacenode
		}
		return node
	    }
	}
    var templateAST = esprima.parse(template).body[0] //NOTE: template must be expression or single statement.
    return estraverse.replace(templateAST, replacer)
}
github probmods / webchurch / src / wctransform.js View on Github external
function probTransformAST(ast, includePreamble)
{
  estraverse.replace(ast, WrapIfs);
  estraverse.replace(ast, MoveCalls);
  if (includePreamble) {
    ast.body.unshift(esprima.parse(preamble));
  }
  estraverse.replace(ast, BlockStatementCollapser);
  return ast;
}
github dritchie / probabilistic-js / probabilistic / transform.js View on Github external
function probTransform(codeString)
{
	var ast = esprima.parse(codeString)
	estraverse.replace(ast, callWrapper)
    //+ "__pr.setmaxid(" + nextid + ");\n"
	return preamble + escodegen.generate(ast)
}
github probmods / webchurch / js_astify.js View on Github external
}
    }

    function make_expression_statement_list(church_trees) {
	var body = []
	for (var i = 0; i < church_trees.length; i++) {
	    body.push(make_expression_statement(church_trees[i]));
	}
	return body;
    }

    var ast = deep_copy(program_node);
    var body = make_expression_statement_list(church_tree.children);

    ast["body"] = body;
    ast = estraverse.replace(ast, renameIdentifiers)
    return ast;
}