How to use the acorn/dist/walk.recursive function in acorn

To help you get started, we’ve selected a few acorn 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 dortaldt / notyet / node_modules / with / index.js View on Github external
var originalSource = src
  var hasReturn = false
  var ast = reallyParse(src)
  var ref
  src = src.split('')

  // get a reference to the function that was inserted to add an inner context
  if ((ref = ast.body).length !== 1
   || (ref = ref[0]).type !== 'ExpressionStatement'
   || (ref = ref.expression).type !== 'CallExpression'
   || (ref = ref.callee).type !== 'MemberExpression' || ref.computed !== false || ref.property.name !== 'call'
   || (ref = ref.object).type !== 'FunctionExpression')
    throw new Error('AST does not seem to represent a self-calling function')
  var fn = ref

  walk.recursive(ast, null, {
    Function: function (node, st, c) {
      if (node === fn) {
        c(node.body, st, "ScopeBody");
      }
    },
    ReturnStatement: function (node) {
      hasReturn = true
      replace(node, 'return {value: ' + source(node.argument) + '};');
    }
  });
  function source(node) {
    return src.slice(node.start, node.end).join('')
  }
  function replace(node, str) {
    for (var i = node.start; i < node.end; i++) {
      src[i] = ''
github sx1989827 / DOClever / Desktop / node_modules / webpack-bundle-analyzer / lib / parseUtils.js View on Github external
function parseBundle(bundlePath) {
  var content = fs.readFileSync(bundlePath, 'utf8');
  var ast = acorn.parse(content, {
    sourceType: 'script',
    // I believe in a bright future of ECMAScript!
    // Actually, it's set to `2050` to support the latest ECMAScript version that currently exists.
    // Seems like `acorn` supports such weird option value.
    ecmaVersion: 2050
  });

  var walkState = {
    locations: null
  };

  walk.recursive(ast, walkState, {
    CallExpression(node, state, c) {
      if (state.sizes) return;

      var args = node.arguments;

      // Additional bundle without webpack loader.
      // Modules are stored in second argument, after chunk ids:
      // webpackJsonp([], , ...)
      // As function name may be changed with `output.jsonpFunction` option we can't rely on it's default name.
      if (node.callee.type === 'Identifier' && args.length >= 2 && isArgumentContainsChunkIds(args[0]) && isArgumentContainsModulesList(args[1])) {
        state.locations = getModulesLocationFromFunctionArgument(args[1]);
        return;
      }

      // Additional bundle without webpack loader, with module IDs optimized.
      // Modules are stored in second arguments Array(n).concat() call
github jake-101 / bael-template / node_modules / webpack-bundle-analyzer / lib / parseUtils.js View on Github external
function parseBundle(bundlePath) {
  var content = fs.readFileSync(bundlePath, 'utf8');
  var ast = acorn.parse(content, {
    sourceType: 'script',
    // I believe in a bright future of ECMAScript!
    // Actually, it's set to `2050` to support the latest ECMAScript version that currently exists.
    // Seems like `acorn` supports such weird option value.
    ecmaVersion: 2050
  });

  var walkState = {
    locations: null
  };

  walk.recursive(ast, walkState, {
    CallExpression(node, state, c) {
      if (state.locations) return;

      var args = node.arguments;

      // Main chunk with webpack loader.
      // Modules are stored in first argument:
      // (function (...) {...})()
      if (node.callee.type === 'FunctionExpression' && !node.callee.id && args.length === 1 && isSimpleModulesList(args[0])) {
        state.locations = getModulesLocations(args[0]);
        return;
      }

      // Async Webpack < v4 chunk without webpack loader.
      // webpackJsonp([], , ...)
      // As function name may be changed with `output.jsonpFunction` option we can't rely on it's default name.
github jake-101 / bael-template / node_modules / webpack-bundle-analyzer / src / parseUtils.js View on Github external
function parseBundle(bundlePath) {
  const content = fs.readFileSync(bundlePath, 'utf8');
  const ast = acorn.parse(content, {
    sourceType: 'script',
    // I believe in a bright future of ECMAScript!
    // Actually, it's set to `2050` to support the latest ECMAScript version that currently exists.
    // Seems like `acorn` supports such weird option value.
    ecmaVersion: 2050
  });

  const walkState = {
    locations: null
  };

  walk.recursive(
    ast,
    walkState,
    {
      CallExpression(node, state, c) {
        if (state.locations) return;

        const args = node.arguments;

        // Main chunk with webpack loader.
        // Modules are stored in first argument:
        // (function (...) {...})()
        if (
          node.callee.type === 'FunctionExpression' &&
          !node.callee.id &&
          args.length === 1 &&
          isSimpleModulesList(args[0])
github source-academy / js-slang / src / cfg.ts View on Github external
exactly the global scope before generating CFG`
  )
  invariant(
    context.cfg.scopes[0].node,
    `context.cfg.scopes[0] node
  must be a program from the parser`
  )
  // Reset states
  nodeStack = []
  scopeQueue = [context.cfg.scopes[0]]
  edgeLabel = 'next'

  // Process Node BFS style
  while (scopeQueue.length > 0) {
    const current = scopeQueue[0].node!
    recursive(current, context, walkers)
    scopeQueue.shift()
  }
}
github propelml / propel / website / nb_transpiler.ts View on Github external
function walkRecursiveWithAncestors(node, state, visitors) {
  const ancestors = [];
  const wrappedVisitors = {};

  for (const nodeType of Object.keys(walk.base)) {
    const visitor = visitors[nodeType] || walk.base[nodeType];
    wrappedVisitors[nodeType] = (node, state, c) => {
      const isNew = node !== ancestors[ancestors.length - 1];
      if (isNew) ancestors.push(node);
      visitor(node, state, c, ancestors);
      if (isNew) ancestors.pop();
    };
  }

  return walk.recursive(node, state, wrappedVisitors);
}
github sergioramos / jsctags / src / local-scope / index.js View on Github external
const visitNode = function(state, node, path) {
    if (!node) {
      return;
    }

    walk.recursive(
      node,
      {
        path,
        ids: []
      },
      walkall.makeVisitors((node, st, walk) => {
        if (includes(st.ids, getId(node))) {
          return;
        }

        st.ids.push(getId(node));

        if (!node.scope) {
          return;
        }
github maierfelix / Iroh / dist / iroh-node.es.js View on Github external
Patcher.prototype.walk = function(ast, state, visitors) {
  return recursive(ast, state, visitors, base);
};
github jamesshore / automatopia / node_modules / browserify / node_modules / module-deps / node_modules / detective / index.js View on Github external
if (node.type !== 'CallExpression') return;
        if (isRequire(node)) {
            if (node.arguments.length) {
                var arg = node.arguments[0];
                if (arg.type === 'Literal') {
                    modules.strings.push(arg.value);
                }
                else {
                    modules.expressions.push(src.slice(arg.start, arg.end));
                }
            }
            if (opts.nodes) modules.nodes.push(node);
        }
    }
    
    walk.recursive(ast, null, {
        Statement: visit,
        Expression: visit
    });
    
    return modules;
};