Fork me on GitHub

Shift Scope Analyser

generates an ECMAScript program's scope tree from a Shift AST


Introduction

The Shift Scope Analyser produces a data structure called a scope tree that represents all of the scoping information of a given program. Each element of the scope tree represents a single scope in the analysed program, and contains many pieces of information, including:

  • the scope type (there are 12 of them!)
  • the AST node associated with the scope
  • variables declared within that scope, each of which points to its declarations and references
  • whether the scope contains a with statement or direct call to eval, making it dynamic

Demo

The program entered on the left will be rendered on the right. Mousing over any identifier will highlight all other identifiers which refer to the same variable, color-coded by type: declarations, read references, write references, and read-write references.

function bytes(n, maxBytes) { if (maxBytes == null) { maxBytes = Math.max(1, Math.ceil(Math.log2(n + 1) / 8)); } var rv = Array(maxBytes); for (var bIndex = maxBytes - 1; bIndex >= 0; --bIndex) { rv[bIndex] = n & 0xFF; n >>>= 8; } return rv; }

Installation

npm install shift-scope

Usage

import analyze from "shift-scope";
let globalScope = analyze(/* Shift AST */);