How to use the attachComments function from escodegen

Find comprehensive JavaScript escodegen.attachComments code examples handpicked from public code repositorys.

151
152
153
154
155
156
157
158
159
160
// Return a beautified representation of the code (cleaning up indentation, etc.)
beautify(rawCode, aether) {
  let ast;
  try {
    ast = esprima.parse(rawCode, {range: true, tokens: true, comment: true, tolerant: true});
    ast = escodegen.attachComments(ast, ast.comments, ast.tokens);
  } catch (e) {
    console.log('got error beautifying', e);
    ast = acorn_loose.parse_dammit(rawCode, {tabSize: 4, ecmaVersion: 5});
  }
fork icon0
star icon1
watch icon2

700
701
702
703
704
705
706
707
708
709
    // fix for https://github.com/wix/react-templates/issues/157
    // do not include comments for es6 modules due to bug in dependency "escodegen"
    // to be removed when https://github.com/estools/escodegen/issues/263 will be fixed
    // remove also its test case "test/data/comment.rt.es6.js"
    if (options.modules !== 'es6') {
        tree = escodegen.attachComments(tree, tree.comments, tree.tokens)
    }
    return escodegen.generate(tree, {comment: true})
} catch (e) {
    throw new RTCodeError(e.message, e.index, -1)
fork icon218
star icon0
watch icon189

57
58
59
60
61
62
63
64
65
66
  // collect token ranges
  onToken: tokens
});

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

const globals = findGlobals(ast);
for (let i = 0; i < globals.length; ++i) {
  if (globals[i].name === "window" || globals[i].name === "this") {
fork icon0
star icon0
watch icon1

+ 7 other calls in file

74
75
76
77
78
79
80
81
82
83
// jscoverage
else if(checkUrlInclude(url, coverageInclude, coverageExclude)){
    if(coverageBeautify){
        try{
            var syntax = esprima.parse(responseContent, { raw: true, tokens: true, range: true, comment: true });
            syntax = escodegen.attachComments(syntax, syntax.comments, syntax.tokens);
            responseContent = escodegen.generate(syntax, 
                {
                    comment: true,
                    format: {
fork icon0
star icon0
watch icon2

+ 7 other calls in file