How to use the estraverse.attachComments function in estraverse

To help you get started, we’ve selected a few estraverse 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 astoilkov / jsblocks / build / tasks / build.js View on Github external
if (/.\/var\//.test(path)) {
        contents = contents
          .replace(/define\([\w\W]*?return/, '    var ' + (/var\/([\w-]+)/.exec(name)[1]) + ' =')
          .replace(rdefineEnd, '');

      } else {
        contents = contents
          .replace(/\/\*\s*ExcludeStart\s*\*\/[\w\W]*?\/\*\s*ExcludeEnd\s*\*\//ig, '')
          .replace(/\/\/\s*BuildExclude\n\r?[\w\W]*?\n\r?/ig, '');
        var ast = esprima.parse(contents, {
          tokens: true,
          comment: true,
          range: true
        });

        estrvarse.attachComments(ast, ast.comments, ast.tokens);

        if (ast.body[0].expression.callee.name == 'define') {
          var moduleExpression = findModuleExpressionArgument(ast.body[0].expression.arguments);
          if (!moduleExpression || !moduleExpression.body.body[0] || moduleExpression.body.body[0].type == 'ReturnStatement') {
            // Null out empty define statements e.g. define(['./query/ready', '...'])
            // and expresions without an expression or only an return statement e.g. define([], function () { return blocks; })
            contents = '';
          } else {
            var moduleName;
            try {
              moduleName = findModuleExportIdentifier(moduleExpression.body.body) || /\/(\w+).?j?s?$/.exec(name)[1];
            } catch(e) {}
            if (moduleName && definedModuleNames[moduleName] && definedModuleNames[moduleName] != path) {
              grunt.fail.warn('[NamingConflict]: Module ' + path + ' tried to define ' + moduleName + ' which is already defined by ' + definedModuleNames[moduleName] + ' !');
            } else if (moduleName){
              definedModuleNames[moduleName] = path;
github estools / esmangle / bin / esmangle.js View on Github external
});

        comments = concatSingleLineComments(content, tree.comments || []);

        if (preserveLicenseComment || propagateLicenseComment) {
            licenses = comments.reduce(function (results, commentBlock) {
                if (!commentBlock.isLicense()) {
                    return results;
                }
                return results.concat(commentBlock.comments);
            }, []);
        }

        if (preserveLicenseComment) {
            // Attach comments to the tree.
            estraverse.attachComments(tree, licenses, tree.tokens);
        }

        tree = esmangle.optimize(tree, null, {
            destructive: true,
            directive: true,
            preserveCompletionValue: argv.preserveCompletionValue,
            legacy: !argv.nonLegacy,
            topLevelContext: argv.topLevelContext,
            inStrictCode: argv.inStrictCode
        });
        tree = esmangle.mangle(tree, {
            destructive: true,
            distinguishFunctionExpressionScope: false
        });

        if (propagateLicenseComment) {
github fex-team / fis-postprocessor-amd / lib / index.js View on Github external
exports.getAsyncRequires = function getAsyncRequires(content) {
    var ast = esprima.parse(content, {
        loc: true,
        comment: true,
        range: true,
        tokens: true
    });
    var scopes = escope.analyze(ast).scopes;
    var gs = scopes.filter(function(scope) {
        return scope.type == 'global';
    })[0];

    var ret = [];

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

    estraverse.traverse(ast, {

        enter: function(current, parent) {

            // 判断是否是 require(xxx, callback);
            // 后者是 require.async(xxx, callback);
            if (current.type === 'CallExpression' &&

                    (
                        current.callee.type === 'Identifier' &&
                            current.callee.name === 'require' ||

                        current.callee.type === 'MemberExpression' &&
                            current.callee.object.name === 'require' &&
                            current.callee.property.name === 'async'
github assetgraph / assetgraph / lib / parseJavaScript.js View on Github external
const ast = parser.parse();
  ast.comments = comments;
  ast.tokens = tokens;
  const lastToken = tokens.pop();
  // Adjustments from espree:
  if (ast.range) {
    ast.range[0] = ast.body.length ? ast.body[0].range[0] : ast.range[0];
    ast.range[1] = lastToken ? lastToken.range[1] : ast.range[1];
  }
  if (ast.loc) {
    ast.loc.start = ast.body.length ? ast.body[0].loc.start : ast.loc.start;
    ast.loc.end = lastToken ? lastToken.loc.end : ast.loc.end;
  }

  if (options.locations && options.ranges) {
    estraverse.attachComments(ast, ast.comments, ast.tokens);
  }
  return ast;
}
github paeckchen / paeckchen / test / helper.ts View on Github external
export function parse(input: string): ESTree.Program {
  const comments: any[] = [];
  const tokens: any[] = [];
  const acornOptions: IParseOptions = {
    ecmaVersion: 7,
    sourceType: 'module',
    locations: true,
    ranges: true,
    allowHashBang: true,
    onComment: comments,
    onToken: tokens
  };
  const ast = acornParse(input, acornOptions);
  attachComments(ast, comments, tokens);
  return ast;
}
github mehdishojaei / amdextract / index.js View on Github external
traverse(parsedCode, function(object) {
    if (object.type === 'ExpressionStatement') {
      var expression = object.expression;

      if (expression.type === 'CallExpression' && expression.callee &&
          expression.callee.type === 'Identifier' && expression.callee.name === 'define') {
        expression.callee = estraverse.attachComments(expression.callee, comments, tokens);

        var module = {},
          leadingComments = expression.callee.leadingComments,
          exceptsPaths = [],
          id, paths, pathsIndex, callback, callbackIndex;

        if (leadingComments) {
          leadingComments.forEach(function(leadingComment) {
            var matches = /^\s*exceptsPaths\s*:\s*(\S+(?:\s*,\s*\S+)*)\s*$/m.exec(leadingComment.value);

            if (matches) {
              Array.prototype.push.apply(exceptsPaths, matches[1].split(/\s*,\s*/));
            }
          });
        }
github gcanti / tcomb-doc / src / source-parser.js View on Github external
function parse(src) {
  try {
    var types = {};
    var ast = esprima.parse(src, {comment: true, tokens: true, range: true});
    estraverse.attachComments(ast, ast.comments, ast.tokens);
    estraverse.traverse(ast, {
      enter: function(node){
        if (node.type === 'VariableDeclarator'){
          var type = getType(node.init, node.id.name);
          types[node.id.name] = type;
        }
      }
    });
    return types;
  } catch (e) {
    console.error(e);
    return null;
  }
}
github mehdishojaei / amdextract / index.js View on Github external
module.dependencies = callback.params.map(function(param) {
            return estraverse.attachComments(param, comments, tokens);
          });
          module.body = callback.body;
github mehdishojaei / amdextract / index.js View on Github external
module.paths = paths.elements.map(function(element) {
            return estraverse.attachComments(element, comments, tokens);
          });
        }