code2flow Documentation

repository·master·Indexed 26 days ago

https://github.com/scottrogowski/code2flow

A tool to generate call graphs from source code. Supports Python, JavaScript, Ruby, and PHP. It can be used via a CLI or as an imported Python library to analyze files and directories, producing DOT files based on AST approximations.

Tokens
618
Snippets
2
Records
4
Agent score
39%

What's inside code2flow

  1. Use the code2flow CLI to generate call graphs

    master

    Run code2flow followed by the files or directories you wish to analyze. By default, it generates a DOT file.

    Basic Usage

    • Python: code2flow mypythonfile.py
    • JavaScript: code2flow myjavascriptfile.js

    Advanced CLI Usage

    • Multiple files/directories: code2flow path/to/file1.js path/to/dir/
    • Wildcards: code2flow project/directory/*.js
    • Specify language: code2flow project/directory --language js
    • Subset of graph: Use --target-function to focus on a specific function, and --upstream-depth or --downstream-depth to limit the scope of the graph.

    To see all available command line options, run code2flow --help.

  2. Use code2flow as an imported Python library

    master

    You can integrate code2flow directly into your Python code. The code2flow.code2flow function accepts arguments similar to the CLI.

    Arguments:

    • paths: A list of file or directory paths to analyze.
    • output_file: The path where the resulting file should be saved.

    Refer to engine.py for the full list of available keyword arguments.

    import code2flow
    code2flow.code2flow(['path/to/filea', 'path/to/fileb'], 'path/to/outputfile')
  3. Understand code2flow limitations

    master

    Because code2flow uses ASTs to approximate structure in dynamic languages, certain behaviors may occur:

    • Skipped Functions: Functions without definitions (e.g., if a file is missing), anonymous functions (lambdas/factories), or functions that have been renamed/passed as parameters are skipped.
    • Namespace Collisions: Functions with identical names in different namespaces are skipped to avoid ambiguity.
    • Import Ambiguity: Imported functions from outside the project directory that share names with local functions may be incorrectly linked to your local definitions.
    • Dynamic Behavior: The tool cannot resolve function calls that depend on runtime values (e.g., a function returned by a factory).