rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Add dict literal syntax

Open bloodyowl opened this issue 1 year ago • 8 comments

This PR adds basic support for the dict{} literal syntax (https://github.com/rescript-lang/rescript-compiler/issues/6545).

Parsing

Semantics within the braces are similar to the record with string keys (as dict keys can be any string).

It then turns dict{"foo": "bar"} into Js.Dict.fromArray([("foo", "bar")]).

Printing

Any Js.Dict.fromArray call that contains a literal array of tuples (with no spread) is now printed as dict{...}.

I think this change is safe for the upcoming stdlib change (that'll move Js.Dict into Dict) as the change will be invisible once using this syntax. If that's something we don't want, we can add an attribute when parsing a dict{} expression so that we only print those back and leave Js.Dict.fromArray calls.

bloodyowl avatar May 25 '24 12:05 bloodyowl

Can you add a changelog entry?

IwanKaramazow avatar May 25 '24 14:05 IwanKaramazow

Could you remove the unrelated changes in belt_HashSetString.resi and belt_HashSetInt.resi?

cknitt avatar May 26 '24 07:05 cknitt

If that's something we don't want, we can add an attribute when parsing a dict{} expression so that we only print those back and leave Js.Dict.fromArray calls.

An attribute somehow might actually be beneficial for the editor tooling, if we want to do typed completion for values for instance.

zth avatar May 26 '24 14:05 zth

@zth aren't the rules the same as (Js.)Dict.fromArray? couldn't we generalize autocompletion here?

bloodyowl avatar May 27 '24 07:05 bloodyowl

I'm also curious for the use case of the editor tooling =D Wouldn't we want to upgrade codebases automatically through the formatter?

IwanKaramazow avatar May 27 '24 10:05 IwanKaramazow

Just noting #6617 supports spread syntax if you want to vendor anything across.

JonoPrest avatar May 28 '24 09:05 JonoPrest

@bloodyowl What's the status of this PR? Would be great to get this feature merged!

cknitt avatar Jun 27 '24 17:06 cknitt

Yes, I agree. This would be a cool feature to have.

hiovi avatar Jul 16 '24 15:07 hiovi

@zth Rebased

cknitt avatar Aug 30 '24 14:08 cknitt