Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

253
Views
How to determine if a variable is immutable or not in an AST?

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:

  1. Look for 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).
  2. What about function parameters? I guess it doesn't really matter in JS because you can't reassign a variable on the outside when passed into a function like you can in C. But technically that does count as being mutable, since it started in the function param and was reassigned. But how to handle that or conceptualize of it?
  3. Check MemberExpression nodes as well, see if the object property is reassigned.
  4. That's about all that I can think of.

Do I have this about right? How can I do this properly? Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!