Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda