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

List possible variables and type names when cannot find reference #166

Open kaleidawave opened 2 weeks ago

kaleidawave commented 2 weeks ago

When a variable name cannot be found an error is raised

For the current this means the somethin will raise a type error

const something = ...
console.log(somethin)

However the current error is

error: 
    ┌─ demo.tsx:799:12
    │
799 │     const a = not_found
    │               ^^^^^^^^^ Could not find variable 'not_found' in scope

It would be better for the error to include possible alternatives that are in scope:

error: 
    ┌─ demo.tsx:799:12
    │
799 │     const a = not_found
    │               ^^^^^^^^^ Could not find variable 'not_found' in scope
    │     
    |     Did you mean `found` or `is_found`

I started on this, but didn't finish. You can add the possible fields to the TypeCheckerError::CouldNotFindType, use the levenshtein crate to find similar names and parents_iter().map(|env| env.variables) to find some similar variables names (+ same for types)

https://github.com/kaleidawave/ezno/blob/5191329edd448c364914917297bef2846527b91b/checker/src/diagnostics.rs#L293-L298

https://github.com/kaleidawave/ezno/blob/5191329edd448c364914917297bef2846527b91b/checker/Cargo.toml#L40