kaleidawave / ezno

A JavaScript compiler and TypeScript checker written in Rust with a focus on static analysis and runtime performance
https://kaleidawave.github.io/posts/introducing-ezno/
MIT License
2.3k stars 42 forks source link

Closure TDZ #124

Open kaleidawave opened 3 months ago

kaleidawave commented 3 months ago

Current TDZ finding is solved for standard statements AND for functions (as well as all things function-like). (thanks to #69 these errors have positions!)

This works for conditional function calls

function get_x() {
  return x
}

if (Math.random() > 0.5) {
  // Emits a TDZ error 'Variable 'x' used before declaration'
  get_x()
}

let x = 5;

However as it turns out with closures, TDZ issues are non-deterministic on their reference

let obj = {};
function closure3() {
    obj.get_z = () => {
        return z
    };

    if (Math.random() > 0.5) {
        return;
    }

    let z = 0;
}

closure3();
return obj.get_z()

Half the time this leaks a reference that never gets declared. Somehow we want to catch this mistake.

Things to think about

At some point I think this can't fully support JavaScript and instead sit in a local minimum

kaleidawave commented 3 months ago

Had some thoughts: Instead it will be probably be solved the same way as find_possible_mutations. When a function is leaked outside the function it was defined it, it should run a similar Rust function to find_possible_mutations which checks any free_variables are defined. Thus avoiding any Event stuff