rescript-lang / rescript-compiler

The compiler for ReScript.
https://rescript-lang.org
Other
6.75k stars 449 forks source link

Print `:>` without wrapping parenthesis #6254

Open ryyppy opened 1 year ago

ryyppy commented 1 year ago

Given the following example:

type a = {"name": string, "age": int}

type b = {"name": string}

let v: a = {
  "name": "Anton",
  "age": 35,
}

let foo = (arg: b) => {
  Js.log("Hi" ++ arg["name"])
}

foo(v :> b)

Playground link

When pretty-printing the code, the coercion expression will be wrapped with parentheses: foo((v :> b)).

Expected behavior: It should print the expression the same way as it was defined; only preserve parentheses if the operation was originally wrapped in parentheses.

cristianoc commented 1 year ago

The parsing of coercion is spread out in several places. Not completely clear how to capture where parens are necessary or not. Will need to either do on a case by case basis, or unify :> parsing with the behaviour of other binary operators. Also, one needs to make sure that removing parens does not make the code more difficult to understand, even if it parses OK.

cristianoc commented 1 year ago

After a little investigation, its not clear which one is preferable in this specific example. So I'll leave it here in case some new data / opinions emerge.

github-actions[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

cknitt commented 2 months ago

7024 just reminded me of this one. @shulhi do you think we should add this one to #6995 to investigate (even though it is not strictly speaking a bug)?

shulhi commented 2 months ago

7024 just reminded me of this one. @shulhi do you think we should add this one to #6995 to investigate (even though it is not strictly speaking a bug)?

Sure thing, I added this to #6995