Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some more JavaScript rewrite rules ⚡ #52

Merged
merged 3 commits into from
May 10, 2022
Merged

Conversation

jmackie
Copy link
Member

@jmackie jmackie commented May 10, 2022

This was prompted by seeing that...

huh = (res) -> do {
    match res with
    | Ok(_) -> some_effect
    | Err(_) -> some_effect
};

Generates...

function huh(res) {
  return () => {
    return (
      res[0] === "Ok"
        ? some_effect
        : res[0] === "Err"
        ? some_effect
        : (() => {
            throw new Error("Pattern match error");
          })()
    )(/* NOTE: here we're calling `some_effect` */);
  };
}

So I thought it might be sensible to rewrite (condition ? true_value : false_value)() to condition ? true_value() : false_value() (see here). But then I realised that can only be legit if the base case (i.e. the throw) is of a similar type to the other values in the ternary, else you could end up generating something { throw new Error("...") }(). Only if you do that, you can end up deferring the pattern match error, which would be kinda weird (we're not doing laziness here).

So in the end, I didn't do any of the above, but there are a few things left over from that exploration that are worth having.

@codecov
Copy link

codecov bot commented May 10, 2022

Codecov Report

Merging #52 (ef49282) into main (abcde1e) will increase coverage by 0.48%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main      #52      +/-   ##
==========================================
+ Coverage   78.97%   79.45%   +0.48%     
==========================================
  Files         113      113              
  Lines        7700     7755      +55     
==========================================
+ Hits         6081     6162      +81     
+ Misses       1619     1593      -26     
Impacted Files Coverage Δ
crates/ditto-codegen-js/src/convert.rs 96.34% <ø> (+0.40%) ⬆️
crates/ditto-codegen-js/src/optimize/rewrites.rs 100.00% <100.00%> (ø)
...ditto-checker/src/module/value_declarations/mod.rs 99.60% <0.00%> (+0.39%) ⬆️
crates/ditto-codegen-js/src/optimize/mod.rs 95.11% <0.00%> (+0.65%) ⬆️
crates/ditto-codegen-js/src/render.rs 100.00% <0.00%> (+1.68%) ⬆️
crates/ditto-checker/src/typechecker/mod.rs 98.20% <0.00%> (+1.79%) ⬆️
crates/ditto-ast/src/expression.rs 85.18% <0.00%> (+1.85%) ⬆️
crates/ditto-checker/src/typechecker/pre_ast.rs 100.00% <0.00%> (+3.22%) ⬆️
...ates/ditto-checker/src/typechecker/substitution.rs 98.52% <0.00%> (+4.41%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update abcde1e...ef49282. Read the comment docs.

rewrite_tenaray_with_iife_true_clause_to_block(),
rewrite_tenaray_with_iife_false_clause_to_block(),
rewrite_ternary_with_iife_true_clause_to_block(),
rewrite_ternary_with_iife_false_clause_to_block(),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo 🤦

@jmackie jmackie merged commit e3796fd into main May 10, 2022
@jmackie jmackie deleted the more-js-rewrites branch May 10, 2022 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant