undeclareddeprecated

Finds all undeclared identifiers in an AST

Usage no npm install needed!

<script type="module">
  import undeclared from 'https://cdn.skypack.dev/undeclared';
</script>

README

Undeclared.js

Given an AST ( esprima, acorn, ... ), find all undeclared identifiers and returns them as strings in a Set.

Why

Finding all undeclared identifiers could be used in a number of situations. For instance, this module will be part of another module that will automatically try to import all undeclared identifiers in a given script from a custom library.

Install

npm install undeclared

Usage

const
    undeclared = require( "undeclared" ) ,
    esprima = require( "esprima" )

let identifiers = undeclared( esprima.parse("some JS code") )
// => Set { "undeclared ids"... }

example.js

$1 to $7 are undeclared identifiers across multiple scopes

let
    i = 42 ,
    j = $1 + i


function funk() {

    var
        k = 41 ,
        g = (a,b) => a * j + $3

    $2(
        function(b,x) {
            $4 += 1
            $5 = ( function() {
                return function(y) { return $7 + $4 }
                } )()
            return x * $6.prop1.prop2[ $2 + 32 ] - i + $4 + g( $3, i )
            }
        )
    }

Analyse example

const
    fs = require( "fs" ) ,
    esprima = require( "esprima" ) ,
    undeclared = require( "undeclared" )

fs.readFile(
    "test.js" ,
    (err,file) => {
        if ( !err ) {
            const
                ast = esprima.parse( file ) ,
                identifiers = undeclared( ast )
            console.log( identifiers )
            }
        }
    )

// =>  output => Set { "$1", "$2", "$3", "$4", "$5", "$6", "$7" }

License

MIT