How to use escodegen - 10 common examples

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 jaz303 / spark / app / lib / modes / sketch / index.js View on Github external
code = editor.getCopyText();
                        }

                        try {

                            var ast = esprima.parse(code);

                            // find globals
                            var globals = findGlobals(ast);

                            // detect slow loops
                            // TODO: make this a configuration option
                            slowpoke(ast, {timeout: 5000});

                            // regenerate code
                            code = escodegen.generate(ast);

                        } catch (e) {
                            console.log(e);
                            // Parse failed.
                            // We'll just ignore this and let the runtime throw the error.
                        }
                        
                        var result = self._ctx.evaluate(code);
                        if (isError(result)) {
                            self._console.printObject(result);    
                        } else {
                            // TODO: this is a work-around for not have global
                            // variable extraction/injection.
                            if (setup) {
                                self._ctx.__js_setup();    
                            }
github material-components / material-components-web / utils / uniffe.js View on Github external
escodegen.attachComments(ast, comments, tokens);

  if (ast.body[0].expression === undefined ||
      ast.body[0].expression.callee === undefined) {
    return contents;
  }

  var rootProgram = ast.body[0].expression.callee.body;

  rootProgram.type = 'Program';
  // drop use strict
  rootProgram.body = rootProgram.body.slice(1);
  // attach all leading comments from outside iffe
  rootProgram.leadingComments = ast.body[0].leadingComments;

  return escodegen.generate(rootProgram, {comment: true});
}
github material-components / material-components-web / utils / uniffe.js View on Github external
function uniffe(contents) {
  var comments = [];
  var tokens = [];

  var ast = acorn.parse(contents, {
    ranges: true,
    onComment: comments,
    onToken: tokens
  });

  escodegen.attachComments(ast, comments, tokens);

  if (ast.body[0].expression === undefined ||
      ast.body[0].expression.callee === undefined) {
    return contents;
  }

  var rootProgram = ast.body[0].expression.callee.body;

  rootProgram.type = 'Program';
  // drop use strict
  rootProgram.body = rootProgram.body.slice(1);
  // attach all leading comments from outside iffe
  rootProgram.leadingComments = ast.body[0].leadingComments;

  return escodegen.generate(rootProgram, {comment: true});
}
github omphalos / inductive.js / lib / util.js View on Github external
util.prettify = function(code, options) {
  try {
    if(code.indexOf('\n') < 0 && code.indexOf('//') === 0) {
      // Workaround for issue with one-line comments and escodegen.
      return code
    }
    var ast = esprima.parse(code, {
      tokens: true,
      attachComment: true,
      range: true
    })
    escodegen.attachComments(ast, ast.comments, ast.tokens)
    return escodegen.generate(ast, options)
  } catch(err) {
    tap.comment(code)
    tap.comment(err.stack)
    throw new Error('Unable to prettify code')
  }
}
github aredotna / ervell / node_modules / benv / node_modules / jsdom / lib / jsdom / contextify-shim.js View on Github external
o.run = function (code, filename) {
    var comments = [], tokens = [];
    var ast = acorn.parse(code, {
      ecmaVersion: 6,
      allowReturnOutsideFunction: true,
      ranges: true,
      // collect comments in Esprima's format 
      onComment: comments,
      // collect token ranges 
      onToken: tokens
    });

    // make sure we keep comments
    escodegen.attachComments(ast, comments, tokens);

    var globals = findGlobals(ast);
    for (var i = 0; i < globals.length; ++i) {
      if (globals[i].name === "window") {
        continue;
      }

      var nodes = globals[i].nodes;
      for (var j = 0; j < nodes.length; ++j) {
        var type = nodes[j].type;
        var name = nodes[j].name;
        nodes[j].type = "MemberExpression";
        nodes[j].property = {
          name: name,
          type: type
        };
github tmcw / d12 / transform.js View on Github external
module.exports = function transform(code) {
  const s = new MagicString(code);
  const comments = [];
  const tokens = [];
  const ast = acorn.parse(code, {
    ecmaVersion: 10,
    sourceType: 'module',
    ranges: true,
    onComment: comments,
    onToken: tokens
  });
  escodegen.attachComments(ast, comments, tokens);
  s.appendLeft(0, `export const d12 = global.d12 || (global.d12 = new Map()); // D12\n\n`);
  walk.ancestor(ast, {
    ExportNamedDeclaration(node) {
      const c = getComment(node);
      if (!c) return;
      if (node.declaration) {
        const id = node.declaration.id.name;
        s.appendLeft(node.end, `\nd12.set(${id}, ${c}); // D12\n`);
      }
    },
    FunctionDeclaration(node) {
      const c = getComment(node);
      if (!c) return;
      const id = node.id.name;
      s.appendLeft(node.end, `\nd12.set(${id}, ${c}); // D12\n`);
    },
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 dritchie / probabilistic-js / probabilistic / transform.js View on Github external
enter: function(node)
	{
		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)
github probmods / webchurch / src / probabilistic-js / probabilistic / transform.js View on Github external
enter: function(node)
	{
		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)