How to use the acorn-walk.simple function in acorn-walk

To help you get started, we’ve selected a few acorn-walk 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 storybookjs / storybook / addons / docs / src / frameworks / react / inspection / acornParser.ts View on Github external
function parseClass(
  classNode: estree.ClassExpression
): ParsingResult {
  let innerJsxElementNode;

  // If there is at least a JSXElement in the body of the class, then it's a React component.
  acornWalk.simple(
    classNode.body,
    {
      JSXElement(node: any) {
        innerJsxElementNode = node;
      },
    },
    ACORN_WALK_VISITORS
  );

  const inferedType: any = {
    type: !isNil(innerJsxElementNode) ? InspectionType.ELEMENT : InspectionType.CLASS,
    identifier: extractIdentifierName(classNode.id),
  };

  return {
    inferedType,
github sverweij / dependency-cruiser / src / extract / ast-extractors / extract-commonJS-deps.js View on Github external
module.exports = (
  pAST,
  pDependencies,
  pModuleSystem,
  pExoticRequireStrings
) => {
  // var/const lalala = require('./lalala');
  // require('./lalala');
  // require('./lalala').doFunkyStuff();
  // require('zoinks!./wappie')
  // require(`./withatemplateliteral`)
  // as well as renamed requires/ require wrappers
  // as passed in pExoticRequireStrings ("need", "window.require")
  walk.simple(
    pAST,
    {
      CallExpression: pushRequireCallsToDependencies(
        pDependencies,
        pModuleSystem,
        pExoticRequireStrings
      )
    },
    // see https://github.com/acornjs/acorn/issues/746
    walk.base
  );
};
github remaxjs / remax / packages / remax-cli / src / build / plugins / rename.ts View on Github external
});

          const extract = (node: Node) => {
            const req =
              getRequireSource(node) ||
              getImportSource(node) ||
              getExportSource(node);

            if (req) {
              const { start, end } = req;
              const newPath = rewrite(req.value, options.map);
              magicString.overwrite(start, end, `'${newPath}'`);
            }
          };

          simple(ast, {
            ImportDeclaration: extract,
            CallExpression: extract,
            ExportAllDeclaration: extract,
            ExportNamedDeclaration: extract,
          });

          if (sourceMaps) {
            file.map = magicString.generateMap();
          }

          file.code = magicString.toString();
        }

        delete bundle[key];
        bundle[rewrite(key, options.map) || key] = file;
      }
github remaxjs / remax / packages / remax-cli / src / build / plugins / fixRegeneratorRuntime.ts View on Github external
sourceType: 'module',
      });

      const remove = (node: any) => {
        if (
          node.callee.type === 'CallExpression' &&
          node.callee.callee.name === 'Function' &&
          node.callee.arguments.length === 2 &&
          node.callee.arguments[0].value === 'r' &&
          node.callee.arguments[1].value === 'regeneratorRuntime = r'
        ) {
          magicString.remove(node.start, node.end);
        }
      };

      simple(ast, {
        CallExpression: remove,
      });

      return {
        code: magicString.toString(),
        map: magicString.generateMap().toString(),
      };
    },
  };
github neftjs / neft / packages / compiler / neft-compiler-binding / src / index.js View on Github external
const changeThisExpressionsToSelfIdentifier = (ast) => {
  walk.simple(ast, {
    ThisExpression(node) {
      node.type = 'Identifier'
      node.name = 'self'
    },
  })
}
github LANIF-UI / dva-boot-desktop / src / routes / CreateProject / model / index.js View on Github external
function deleteExampleRoute(path) {
  const file = readFileSync(path).toString();
  const ast = acorn.Parser.extend(jsx()).parse(file, {
    ranges: true,
    onComment: comments,
    onToken: tokens,
    sourceType: 'module',
    plugins: {
      stage3: true,
      jsx: true
    }
  });
  try {
    escodegen.attachComments(ast, comments, tokens);
    walk.simple(ast, {
      Program(node) {
        const excludes = [
          './Pages/404',
          './Login',
          './Register',
          '@/utils/core',
          '@/layouts/BasicLayout',
          '@/layouts/UserLayout'
        ];
        for (let i = node.body.length - 1; i >= 0; i--) {
          if (
            node.body[i].type === 'ImportDeclaration' &&
            excludes.indexOf(node.body[i].source.value) === -1
          ) {
            node.body.splice(i, 1);
          }
github marijnh / blint / loop.js View on Github external
function check(node, dir) {
    var known = vars[node.name];
    if (!known) return;
    if (dir > 0 && known.down && !known.up ||
        dir < 0 && known.up && !known.down)
      fail("Suspiciously infinite-looking loop", node.loc);
  }
  walk.simple(test, {
    BinaryExpression: function(node) {
      if (node.left.type == "Identifier")
        store(node.left.name, compDir(node.operator));
      if (node.right.type == "Identifier")
        store(node.right.name, -compDir(node.operator));
    }
  });
  walk.simple(update, {
    UpdateExpression: function(node) {
      if (node.argument.type == "Identifier")
        check(node.argument, opDir(node.operator));
    },
    AssignmentExpression: function(node) {
      if (node.left.type == "Identifier") {
        if (node.operator == "=" && node.right.type == "BinaryExpression" && node.right.left.name == node.left.name)
          check(node.left, opDir(node.right.operator));
        else
          check(node.left, opDir(node.operator));
      }
    }
  });
};
github petkaantonov / bluebird / tools / ast_passes.js View on Github external
src.substring(start, end)
                        );
                    }

                    var args = node.arguments;

                    var name = args[0];
                    var nameStr = name.name;
                    var expr = args[1];

                    var e = eval;
                    constants[nameStr] = {
                        identifier: name,
                        value: e(nodeToString(expr))
                    };
                    walk.simple( expr, {
                        Identifier: function( node ) {
                            ignore.push(node);
                        }
                    });
                    global[nameStr] = constants[nameStr].value;
                }
            }
        });
github mikro-orm / mikro-orm / lib / utils / Utils.ts View on Github external
}

      const params = node.value ? node.value.params : node.params;
      ret.push(...params.map((p: any) => {
        switch (p.type) {
          case 'AssignmentPattern':
            return p.left.name;
          case 'RestElement':
            return '...' + p.argument.name;
          default:
            return p.name;
        }
      }));
    };

    walk(parsed, {
      MethodDefinition: (node: any) => checkNode(node, methodName),
      FunctionDeclaration: (node: any) => checkNode(node, methodName),
    });

    return ret;
  }