shapesecurity / shift-scope-js

scope analyser for the Shift AST
http://shift-ast.org/scope.html
Apache License 2.0
11 stars 6 forks source link

node[branch] lookup in recurse function fails due to no "body" branch generated on AST parse #24

Closed brokenalarms closed 9 years ago

brokenalarms commented 9 years ago

I cannot get the scope analyzer working for the life of me.

Executing this code (having to use requires as documented in issue #239 in shift-parser-js):

let parse = require("shift-parser").parseScript;
let analyze = require('shift-scope').default;
let ScopeType = require('shift-scope').ScopeType;
let DeclarationType = require('shift-scope').DeclarationType;

(function createAst() {

  let ast = parse(`

"use strict";

let [x,y,z] = [1,2,3].map( _ => _ * 2 );

class Person {
    constructor(name) {
        this.name = name
    }
}

`);
  let astScope = analyze(ast);
  return ast;
})()

fails in function recurse:

function recurse(branch) {
  return function(reducer, node) {
    let branchNode = node[branch];
    return DRIVER[branchNode.type](reducer, branchNode);
  };
}

...as branch variable is supplied as "body" which is not present in the AST node supplied to the function, so branchNode is assigned undefined. I cannot get the shift-parser to produce anything with a 'body' branch. I have tried both the parseModule and parseScript versions and explored in the visualizer, but the "body" branch always appears absent so it seems like this would always fail. What am I missing?

It would be great if I can get this working, as I would love to use it in a project for the next few months .

michaelficarra commented 9 years ago

This is likely because you are using incompatible versions of shift-scope and shift-parser. Please either use version 1.x of shift-parser or wait until version 2.0.0 of shift-scope is released.