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 attr res.array{Get,Set} to Array.{get, set} #6343

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@
# 12.0.0-alpha.6 (Unreleased)
- Fix exponential notation syntax. https://github.com/rescript-lang/rescript/pull/7174


#### :bug: Bug fix
- Fix bug where a ref assignment is moved ouside a conditional. https://github.com/rescript-lang/rescript/pull/7176

#### :house: Internal

- Add attribute `res.array{Get,Set}` for `Array.{get, set}`. https://github.com/rescript-lang/rescript-compiler/pull/6343

# 12.0.0-alpha.5

#### :rocket: New Feature
Expand Down
7 changes: 5 additions & 2 deletions compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2057,18 +2057,21 @@ and parse_bracket_access p expr start_pos =
Location.mkloc (Longident.Ldot (Lident "Array", "set")) array_loc
in
let end_pos = p.prev_end_pos in
(* Adding attribute because the internal representation of arr[idx] is Array.set and we don't have a way to distinguish both when issuing the token on the LSP *)
let attr = (Location.mkloc "res.arraySet" array_loc, Parsetree.PStr []) in
let array_set =
Ast_helper.Exp.apply ~loc:(mk_loc start_pos end_pos)
(Ast_helper.Exp.ident ~loc:array_loc array_set)
(Ast_helper.Exp.ident ~loc:array_loc array_set ~attrs:[attr])
[(Nolabel, expr); (Nolabel, access_expr); (Nolabel, rhs_expr)]
in
Parser.eat_breadcrumb p;
array_set
| _ ->
let end_pos = p.prev_end_pos in
let attr = (Location.mkloc "res.arrayGet" array_loc, Parsetree.PStr []) in
let e =
Ast_helper.Exp.apply ~loc:(mk_loc start_pos end_pos)
(Ast_helper.Exp.ident ~loc:array_loc
(Ast_helper.Exp.ident ~loc:array_loc ~attrs:[attr]
(Location.mkloc (Longident.Ldot (Lident "Array", "get")) array_loc))
[(Nolabel, expr); (Nolabel, access_expr)]
in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
Did you forget a `]` here?

let xs =
x.map ((Function$ (fun key -> [|key;(predicates.(key))|]))[@res.arity 1])
x.map
((Function$
(fun key -> [|key;(((Array.get)[@res.arrayGet ]) predicates key)|]))
[@res.arity 1])
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let async =
() |.u async;
async ();
async.async;
{ async = (async.(async)) };
{ async = (((Array.get)[@res.arrayGet ]) async async) };
(result |.u async) |.u
(mapAsync ((Function$ (fun a -> doStuff a))[@res.arity 1])))
[@res.braces ])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ let () =
((let response = ((fetch {js|/users.json|js})[@res.await ]) in
let users = ((response.json ())[@res.await ]) in
let comments =
((((fetch {js|comment.json|js})[@res.await ]).json ())
[@res.await ]).(0) in
((Array.get)[@res.arrayGet ])
((((fetch {js|comment.json|js})[@res.await ]).json ())[@res.await ])
0 in
Js.log2 users comments)
[@res.braces ])
let () = ((delay 10)[@res.braces ][@res.await ])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
[@res.braces ])))
[@res.namedArgLoc ][@res.braces ][@res.arity 1]) ~children:[] ())
[@JSX ])
;;if inclusions.(index) <- (uid, url) then onChange inclusions
;;if ((Array.set)[@res.arraySet ]) inclusions index (uid, url)
then onChange inclusions
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ let e = ((a + b)[@res.braces ])
let e = ((if a then true else false)[@res.braces ][@res.ternary ])
let e = ((if computation a then true else false)
[@res.braces ][@res.ternary ])
let e = ((a.(0))[@res.braces ])
let e = ((((Array.get)[@res.arrayGet ]) a 0)[@res.braces ])
let e = ((f b)[@res.braces ])
let e = (((a.b).c)[@res.braces ])
let e = ((arr.(x) <- 20)[@res.braces ])
let e = ((((Array.set)[@res.arraySet ]) arr x 20)[@res.braces ])
let e = ((Function$ (fun x -> doStuff config (x + 1)))
[@res.braces ][@res.arity 1])
let e = ((doStuff config ((Function$ (fun x -> x + 1))[@res.arity 1]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let y = [%obj { age = 30; name = {js|steve|js} }]
let z =
[%obj { \xff = 1; \u2212 = {js|two|js}; \0 = zero; \o123 = {js|o123|js} }]
let x = (({js|age|js})[@res.braces ])
let x = (({js|age|js}.(0))[@res.braces ])
let x = ((((Array.get)[@res.arrayGet ]) {js|age|js} 0)[@res.braces ])
let x = (({js|age|js} |.u Js.log)[@res.braces ])
let x = ((if {js|age|js} then true else false)[@res.braces ][@res.ternary ])
let x = (({js|age|js} |.u Js.log; (let foo = 1 in let bar = 2 in foo + bar))
Expand All @@ -16,5 +16,7 @@ let x =
[@res.ternary ]);
(let foo = 1 in let bar = 2 in foo + bar))
[@res.braces ])
let x = (({js|age|js}.(0); (let foo = 1 in let bar = 2 in foo + bar))
let x =
((((Array.get)[@res.arrayGet ]) {js|age|js} 0;
(let foo = 1 in let bar = 2 in foo + bar))
[@res.braces ])
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,40 @@ let build_dispatch_table =
[@res.arity 1])
;;(module Three)
;;((module Three) : (module X_int))
;;(module Teenager).(0)
;;((Array.get)[@res.arrayGet ]) (module Teenager) 0
;;((module Teenager) |.u age) |.u Js.log
;;((module Teenager).(0)) |.u Js.log
;;(((Array.get)[@res.arrayGet ]) (module Teenager) 0) |.u Js.log
;;((if ((module Teenager) |.u age) |.u isAdult
then Js.log {js|has responsibilities|js}
else Js.log {js|can play in the playground|js})[@res.ternary ])
;;((if ((module Streets).(0)) |.u isExpensive
;;((if (((Array.get)[@res.arrayGet ]) (module Streets) 0) |.u isExpensive
then Js.log {js|big money|js}
else Js.log {js|affordable|js})[@res.ternary ])
let () = ((((module Teenager) |.u age) |.u Js.log)[@res.braces ])
let () = (((module Teenager).(0))[@res.braces ])
let () = ((((Array.get)[@res.arrayGet ]) (module Teenager) 0)[@res.braces ])
let () =
((if ((module Teenager) |.u age) |.u isAdult
then Js.log {js|has responsibilities|js}
else Js.log {js|can play in the playground|js})
[@res.braces ][@res.ternary ])
let () =
((if ((module Streets).(0)) |.u isExpensive
((if (((Array.get)[@res.arrayGet ]) (module Streets) 0) |.u isExpensive
then Js.log {js|big money|js}
else Js.log {js|affordable|js})
[@res.braces ][@res.ternary ])
let () =
((let a = 1 in
let b = 2 in
(module Teenager).(0); ((module Teenager) |.u age) |.u Js.log)
((Array.get)[@res.arrayGet ]) (module Teenager) 0;
((module Teenager) |.u age) |.u Js.log)
[@res.braces ])
let () =
((let a = 1 in
let b = 2 in
((module Teenager) |.u age) |.u Js.log;
((if (((module Teenager).(0)) |.u age) |.u isAdult
((if
((((Array.get)[@res.arrayGet ]) (module Teenager) 0) |.u age) |.u
isAdult
then Js.log {js|has responsibilities|js}
else Js.log {js|can play in the playground|js})
[@res.ternary ]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ let x = p.Parser.token
let x = (p.Lang.Parser.token).pos
;;lexbuf.lnum <- (lexbuf.lnum + 1)
;;(parser.lexbuf).lnum <- ((parser.lexbuf).lnum + 1)
let x = arr.(0)
let x = arr.((x : int))
let x = (arr.(0)).(1)
let x = (arr.((x : int))).((y : int))
;;arr.(0) <- (a + b)
let x = ((Array.get)[@res.arrayGet ]) arr 0
let x = ((Array.get)[@res.arrayGet ]) arr (x : int)
let x = ((Array.get)[@res.arrayGet ]) (((Array.get)[@res.arrayGet ]) arr 0) 1
let x =
((Array.get)[@res.arrayGet ]) (((Array.get)[@res.arrayGet ]) arr (x : int))
(y : int)
;;((Array.set)[@res.arraySet ]) arr 0 (a + b)
;;f ()
;;(f ()) ()
;;f a
Expand Down