Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 170b772

Browse files
committed
Clean up the conversion of @bs.* and %bs.*
Fixes #292 Linked tasks: - Check editor plugins' deprecation highlighting (ST, VSCode) - Update docs interop cheat sheet's decorators
1 parent 7ff315d commit 170b772

File tree

8 files changed

+95
-66
lines changed

8 files changed

+95
-66
lines changed

.depend

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
src/reactjs_jsx_ppx_v3.cmx : src/reactjs_jsx_ppx_v3.cmi
22
src/reactjs_jsx_ppx_v3.cmi :
3-
src/res_ast_conversion.cmx : src/res_ast_conversion.cmi
3+
src/res_ast_conversion.cmx : src/res_printer.cmx src/res_ast_conversion.cmi
44
src/res_ast_conversion.cmi :
55
src/res_ast_debugger.cmx : src/res_driver.cmx src/res_doc.cmx \
66
src/res_ast_debugger.cmi

src/res_ast_conversion.ml

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -324,43 +324,18 @@ let normalize =
324324
extension = (fun mapper ext ->
325325
match ext with
326326
| (id, payload) ->
327-
let contents = match id.txt with
328-
| "bs.raw" -> "raw"
329-
| "bs.obj" -> "obj"
330-
| txt -> txt
331-
in
332-
({id with txt = contents}, default_mapper.payload mapper payload)
333-
327+
(
328+
{id with txt = Res_printer.convertBsExtension id.txt},
329+
default_mapper.payload mapper payload
330+
)
334331
);
335332
attribute = (fun mapper attr ->
336333
match attr with
337334
| (id, payload) ->
338-
(* Reminder, keep this in sync with src/res_printer.ml *)
339-
let contents = match id.txt with
340-
| "bs.val" -> "val"
341-
| "bs.module" -> "module"
342-
| "bs.scope" -> "scope"
343-
| "bs.splice" | "bs.variadic" -> "variadic"
344-
| "bs.set" -> "set"
345-
| "bs.set_index" -> "set_index"
346-
| "bs.get" -> "get"
347-
| "bs.get_index" -> "get_index"
348-
| "bs.new" -> "new"
349-
| "bs.obj" -> "obj"
350-
| "bs.return" -> "return"
351-
| "bs.uncurry" -> "uncurry"
352-
| "bs.this" -> "this"
353-
| "bs.meth" -> "meth"
354-
| "bs.deriving" -> "deriving"
355-
| "bs.string" -> "string"
356-
| "bs.int" -> "int"
357-
| "bs.ignore" -> "ignore"
358-
| "bs.unwrap" -> "unwrap"
359-
| "bs.as" -> "as"
360-
| "bs.optional" -> "optional"
361-
| txt -> txt
362-
in
363-
({id with txt = contents}, default_mapper.payload mapper payload)
335+
(
336+
{id with txt = Res_printer.convertBsExternalAttribute id.txt},
337+
default_mapper.payload mapper payload
338+
)
364339
);
365340
attributes = (fun mapper attrs ->
366341
attrs

src/res_printer.ml

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,49 @@ type callbackStyle =
1616
*)
1717
| ArgumentsFitOnOneLine
1818

19+
(* Since compiler version 8.3, the bs. prefix is no longer needed *)
20+
(* Synced from
21+
https://github.com/rescript-lang/rescript-compiler/blob/29174de1a5fde3b16cf05d10f5ac109cfac5c4ca/jscomp/frontend/ast_external_process.ml#L291-L367 *)
22+
let convertBsExternalAttribute = function
23+
| "bs.as" -> "as"
24+
| "bs.deriving" -> "deriving"
25+
| "bs.get" -> "get"
26+
| "bs.get_index" -> "get_index"
27+
| "bs.ignore" -> "ignore"
28+
| "bs.inline" -> "inline"
29+
| "bs.int" -> "int"
30+
| "bs.meth" -> "meth"
31+
| "bs.module" -> "module"
32+
| "bs.new" -> "new"
33+
| "bs.obj" -> "obj"
34+
| "bs.optional" -> "optional"
35+
| "bs.return" -> "return"
36+
| "bs.send" -> "send"
37+
| "bs.scope" -> "scope"
38+
| "bs.set" -> "set"
39+
| "bs.set_index" -> "set_index"
40+
| "bs.splice" | "bs.variadic" -> "variadic"
41+
| "bs.string" -> "string"
42+
| "bs.this" -> "this"
43+
| "bs.uncurry" -> "uncurry"
44+
| "bs.unwrap" -> "unwrap"
45+
| "bs.val" -> "val"
46+
(* bs.send.pipe shouldn't be transformed *)
47+
| txt -> txt
48+
49+
(* These haven't been needed for a long time now *)
50+
(* Synced from
51+
https://github.com/rescript-lang/rescript-compiler/blob/29174de1a5fde3b16cf05d10f5ac109cfac5c4ca/jscomp/frontend/ast_exp_extension.ml *)
52+
let convertBsExtension = function
53+
| "bs.debugger" -> "debugger"
54+
| "bs.external" -> "raw"
55+
(* We should never see this one since we use the sugared object form, but still *)
56+
| "bs.obj" -> "obj"
57+
| "bs.raw" -> "raw"
58+
| "bs.re" -> "re"
59+
(* TODO: what about bs.time and bs.node? *)
60+
| txt -> txt
61+
1962
let addParens doc =
2063
Doc.group (
2164
Doc.concat [
@@ -1985,11 +2028,7 @@ and printPackageConstraint i cmtTbl (longidentLoc, typ) =
19852028
]
19862029

19872030
and printExtension ~atModuleLvl (stringLoc, payload) cmtTbl =
1988-
let txt = match stringLoc.Location.txt with
1989-
| "bs.raw" -> "raw"
1990-
| "bs.obj" -> "obj"
1991-
| txt -> txt
1992-
in
2031+
let txt = convertBsExtension stringLoc.Location.txt in
19932032
let extName =
19942033
let doc = Doc.concat [
19952034
Doc.text "%";
@@ -4808,34 +4847,10 @@ and printPayload (payload : Parsetree.payload) cmtTbl =
48084847
]
48094848

48104849
and printAttribute ((id, payload) : Parsetree.attribute) cmtTbl =
4811-
let contents = match id.txt with
4812-
| "bs.val" -> "val"
4813-
| "bs.module" -> "module"
4814-
| "bs.scope" -> "scope"
4815-
| "bs.splice" | "bs.variadic" -> "variadic"
4816-
| "bs.set" -> "set"
4817-
| "bs.set_index" -> "set_index"
4818-
| "bs.get" -> "get"
4819-
| "bs.get_index" -> "get_index"
4820-
| "bs.new" -> "new"
4821-
| "bs.obj" -> "obj"
4822-
| "bs.return" -> "return"
4823-
| "bs.uncurry" -> "uncurry"
4824-
| "bs.this" -> "this"
4825-
| "bs.meth" -> "meth"
4826-
| "bs.deriving" -> "deriving"
4827-
| "bs.string" -> "string"
4828-
| "bs.int" -> "int"
4829-
| "bs.ignore" -> "ignore"
4830-
| "bs.unwrap" -> "unwrap"
4831-
| "bs.as" -> "as"
4832-
| "bs.optional" -> "optional"
4833-
| txt -> txt
4834-
in
48354850
Doc.group (
48364851
Doc.concat [
48374852
Doc.text "@";
4838-
Doc.text contents;
4853+
Doc.text (convertBsExternalAttribute id.txt);
48394854
printPayload payload cmtTbl
48404855
]
48414856
)

src/res_printer.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
val convertBsExternalAttribute : string -> string
2+
val convertBsExtension : string -> string
13

24
val printTypeParams :
35
(Parsetree.core_type * Asttypes.variance) list -> Res_comments_table.t -> Res_doc.t

tests/conversion/reason/__snapshots__/render.spec.js.snap

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`attributes.re 1`] = `
4+
"module Color: {
5+
type t = private string
6+
7+
@inline(\\"red\\") let red: t
8+
@inline(\\"black\\") let black: t
9+
} = {
10+
type t = string
11+
12+
@inline let red = \\"red\\"
13+
@inline let black = \\"black\\"
14+
}
15+
16+
@send external map: (array<'a>, 'a => 'b) => array<'b> = \\"map\\"
17+
@send external filter: (array<'a>, 'a => 'b) => array<'b> = \\"filter\\"
18+
list{1, 2, 3}->map(a => a + 1)->filter(a => modulo(a, 2) == 0)->Js.log
19+
"
20+
`;
21+
322
exports[`bracedJsx.re 1`] = `
423
"open Belt
524

tests/conversion/reason/attributes.re

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Color: {
2+
type t = pri string;
3+
4+
[@bs.inline "red"] let red: t;
5+
[@bs.inline "black"] let black: t;
6+
} = {
7+
type t = string;
8+
9+
[@bs.inline] let red = "red";
10+
[@bs.inline] let black = "black";
11+
};
12+
13+
[@bs.send] external map: (array('a), 'a => 'b) => array('b) = "map";
14+
[@bs.send] external filter: (array('a), 'a => 'b) => array('b) = "filter";
15+
[1, 2, 3]
16+
->map(a => a + 1)
17+
->filter(a => modulo(a, 2) == 0)
18+
->Js.log;

tests/printer/structure/__snapshots__/render.spec.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ include (
117117
{
118118
@val @module(\\"react\\")
119119
external createElementInternalHack: 'a = \\"createElement\\"
120-
@bs.send
120+
@send
121121
external apply: (
122122
'theFunction,
123123
'theContext,

tests/printer/structure/include.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ include (
88
{
99
@val @module("react")
1010
external createElementInternalHack: 'a = "createElement"
11-
@bs.send
11+
@send
1212
external apply: (
1313
'theFunction,
1414
'theContext,

0 commit comments

Comments
 (0)