How to use the escodegen.attachComments 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 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 paysdoc / js-mutation-testing / src / JSParserWrapper.js View on Github external
function parse(src, options) {
        var ast = esprima.parse(src, _.merge({range: true, loc: true, tokens: true, comment: true}, options || {})),
            result = escodegen.attachComments(ast, ast.comments, ast.tokens);

        return options && options.tokens ? result : _.omit(result, 'tokens');
    }
github krakenjs / generator-swaggerize / app / update.js View on Github external
key: {
                            type: 'Identifier',
                            name: method.method,
                            range: [handler.range[0], handler.range[0] + method.method.length]
                        },
                        value: handler,
                        range: handler.range,
                        kind: 'init',
                        leadingComments: newast.comments
                    });
                }
            });

        });

        ast = escodegen.attachComments(ast, ast.comments, ast.tokens);

        return escodegen.generate(ast, { comment: true });
    }
github unassert-js / webpack-unassert-loader / index.js View on Github external
module.exports = function(jsCode) {
  var options = this.options;
  var ast = esprima.parse(jsCode, {
    range: true,
    tokens: true,
    comment: true
  });
  var attachedAst = escodegen.attachComments(ast, ast.comments, ast.tokens);
  var modifiedAst = estraverse.replace(attachedAst, unassert.createVisitor(options));
  this.callback(null, escodegen.generate(modifiedAst, {
    comment: true
  }), null);
};
github bakery / baker / .baker / generators / esprima.js View on Github external
parseJSSource(content) {
    let tree = esprima.parse(content, esprimaOptions);
    tree = escodegen.attachComments(tree, tree.comments, tree.tokens);
    return tree;
  },
};
github danwrong / loadbuilder / lib / loadbuilder / file.js View on Github external
ast: function() {
    if (!this.derived.esprimaAst) {
      var ast = esprima.parse(this.source, {
        range: true,
        tokens: true,
        comment: true
      });
      ast = escodegen.attachComments(ast, ast.comments, ast.tokens);
      this.derived.esprimaAst = ast;
    }
    return this.derived.esprimaAst;
  },
  addTranslationMarkers: function(fnName) {
github LANIF-UI / dva-boot-desktop / src / routes / CreateRoute / model / index.js View on Github external
function updateRouteConfig(name, path, routeAST) {
  try {
    escodegen.attachComments(routeAST, comments, tokens);
    walk.simple(routeAST, {
      Program(node) {
        const newImport = acorn.Parser.parse(
          `import ${name} from './${name}';`,
          {
            sourceType: 'module'
          }
        );
        for (let i = 0; i < node.body.length; i++) {
          if (node.body[i].type === 'VariableDeclaration') {
            node.body.splice(i, 0, newImport.body[0]);
            break;
          }
        }
      },
      ObjectExpression(node) {