I am working with the JavaScript AST as generated by acornjs. I have normalized it such that every binary expression is assigned to a temporary variable to flatten it out, and variables are all defined one-per-line, stuff like that. Not entirely relevant for the question but just some context. I am writing a transpiler from JS to a custom programming language. Now I would like to annotate the variables in the target language with whether the are mutable or not, sort of like in Rust.
How can/should I go about figuring this out programmatically? How do I identify every mutable and immutable variable and mark them appropriately? What is the general approach? It seems overwhelmingly complicated at first, but I'm not sure if I'm missing something simple/obvious.
I would think something along the lines of:
AssignmentExpression nodes, where the left is an Identifier. Mark that identifier as immutable if it is only ever in the left position of an assignment once in the whole AST (after traversing to / visiting each node).MemberExpression nodes as well, see if the object property is reassigned.Do I have this about right? How can I do this properly? Any help would be appreciated.