0

I am trying to detect all global variables referenced by a JavaScript code snippet using Jint.Parser.JavaScriptParser. I'm doing this by following the code example from this issue to retrieve a list of tokens and then removing the names of any declared functions like so:

private List<string> FindIdentifiers()
{   
    JavaScriptParser parser = new JavaScriptParser();
    Program program = parser.Parse(Source, new ParserOptions { Tokens = true });
    List<string> allTokens = program.Tokens
        .Where(t => t.Type == Jint.Parser.Tokens.Identifier)
        .Select(t => t.Value.ToString())
        .Distinct()
        .ToList();
    foreach (FunctionDeclaration declaration in program.FunctionDeclarations)
    {
        allTokens.Remove(declaration.Id.Name);
    }
    return allTokens;
}

This works fine for simple primitive variables, but when referencing a global variable that is an object, the parser also returns members of those objects as being of type Identifier. Which is correct, but I need to distinguish between top-level identifiers and other kinds of identifiers like this, but as far as I can see, there is no way to do this. Anyone have a clever workaround?

Dan
  • 901
  • 11
  • 25
  • You need to find an `Identifier` *expression* (AST node), not an `Identifier` token. And just omitting those where there's a declaration somewhere won't find you the global ones, what you need for that is some kind of [scope analyzer](https://github.com/estools/escope) – Bergi Apr 06 '20 at 15:27
  • I'm not sure that the `JavaScriptParser` gives me access to the abstract syntax tree. Is there something I'm missing? – Dan Apr 06 '20 at 19:09
  • I don't know .net and I don't know that particular library nor where to find its documentation, but in the issue you linked there's a comment by the author that says "*Not sure the getTokens methods is available though. But the parsed tree is, which IMHO is better.*" – Bergi Apr 06 '20 at 20:35

0 Answers0