julianjensen / ast-flow-graph

Creates a CFG from JavaScript source code.
Other
66 stars 11 forks source link

I can't run your demo, help~ #20

Closed su-action closed 4 years ago

su-action commented 4 years ago

My demo code are as follows:

const
    CFG = require( 'ast-flow-graph' ),
    fs = require( 'fs' ),
    src = fs.readFileSync( 'js_test.js', 'utf8' ),
    cfg = new CFG( src, {
        parser:    {
            loc:          true,
            range:        true,
            comment:      true,
            tokens:       true,
            ecmaVersion:  9,
            sourceType:   'module',
            ecmaFeatures: {
                impliedStrict: true,
                experimentalObjectRestSpread: true
            }
        }
    } );

cfg.generate();     // Generate a CFG for all functions (and main module)
// or for just one function
const graph = cfg.generate( 'hello_world' );

// Create all graphs and then get one of them
cfg.generate(); // You only need to do this once.
const myFunc = cfg.by_name( 'hello_world' );
// ...
console.log( cfg.toTable() );    // Display all functions as tables
// Create a graph-viz .dot file

console.log( cfg.create_dot( myFunc ) );

js_test.js:

function hello_world(){
    alert("hello world");
}
hello_world();

console error message:

/usr/local/bin/node /Users/water/cfg_codes/ast-flow-graph-demo.js
/Users/water/node_modules/ast-flow-graph/src/ast.js:376
        if ( node.type === Syntax.Program )
                  ^

TypeError: Cannot read property 'type' of undefined
    at AST.get_from_function (/Users/water/node_modules/ast-flow-graph/src/ast.js:376:19)
    at new AST (/Users/water/node_modules/ast-flow-graph/src/ast.js:82:36)
    at new CFG (/Users/water/node_modules/ast-flow-graph/src/cfg.js:54:21)
    at new load (/Users/water/node_modules/ast-flow-graph/index.js:34:12)
    at Object.<anonymous> (/Users/water/cfg_codes/ast-flow-graph-demo.js:5:11)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)

Process finished with exit code 1
selvaatusa commented 4 years ago

I seem to run into the same issue. As above stacktrace indicates, this is within the the api, and after the code fragment has been sent to the AST parser (acorn), and while attempting to run through the code fragments. The expected node is supposed to be a root node for either function, or the main program, hence the node-type is tested. However node is 'undefined' so node.type fails. Particularly issue starts here:

        this.root          = this.ast = plugin( 'parse', null, source, options );

which returns undefined because pluginManager is not defined, as is required:

    plugin = function( topKey, subKey, ...args ) {
        if ( pluginManager ) return pluginManager.callback( topKey, subKey, ...args );
    };
fakhrinail commented 1 year ago

Did you find the solution to run it? I just ran to the exact same thing