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

Check using the type annotation for default parameter value and try-catch variable #112

Closed kaleidawave closed 2 months ago

kaleidawave commented 5 months ago

putting these together because they are pretty much the same implementation

Default parameter value

currently no exception is raised here

function x(a: number = "hi") {}

it should raise an exception for the default parameter value, as the RHS is not subtype-able under number

the code is here, using type_is_subtype with parameter_constraint against value (default_value) and sending the result to checking_data.diagnostics_container with the position using A::expression_position

https://github.com/kaleidawave/ezno/blob/b6ead3efaebbfa8c1692f296e30860b3be10f17c/checker/src/features/functions.rs#L136-L147

Catch variable

currently no exception is raised here

try {
    throw 5
} catch (e: string) {

}

it should raise an exception here 5 is RHS is not subtype-able under number

the code should go here https://github.com/kaleidawave/ezno/blob/b6ead3efaebbfa8c1692f296e30860b3be10f17c/checker/src/synthesis/statements.rs#L250

The shape of code

Here you can see how it is done for x satisfies Type. You simply want to import the type_is_subtype function and give the type from the annotation as the LHS type/first argument, then follow the rest.

https://github.com/kaleidawave/ezno/blob/b6ead3efaebbfa8c1692f296e30860b3be10f17c/checker/src/types/subtyping.rs#L760-L764

and emitting the error diagnostic

https://github.com/kaleidawave/ezno/blob/b6ead3efaebbfa8c1692f296e30860b3be10f17c/checker/src/lib.rs#L382-L400


You want to create custom TypeCheckErrors for both these scenarios, to annotate that it was a InvalidDefaultValue or ThrowDoesNotMatchCatchVariableAnnotation

You also want to add tests to specification.md for the cases above.

As with any issue, if you want more help or questions feel free to ask

n-keerthi-gayathri commented 5 months ago

@kaleidawave please assign me this issue

CharlesTaylor7 commented 2 months ago

@kaleidawave @n-keerthi-gayathri Hey, I'm interested in taking on this task as my first contribution. I'll be following the detailed steps in the issue description, but I'll post again if I run into any issues.