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

Clean up the conversion of @bs.* and %bs.* #294

Merged
merged 1 commit into from
Feb 28, 2021
Merged
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
2 changes: 1 addition & 1 deletion .depend
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
src/reactjs_jsx_ppx_v3.cmx : src/reactjs_jsx_ppx_v3.cmi
src/reactjs_jsx_ppx_v3.cmi :
src/res_ast_conversion.cmx : src/res_ast_conversion.cmi
src/res_ast_conversion.cmx : src/res_printer.cmx src/res_ast_conversion.cmi
src/res_ast_conversion.cmi :
src/res_ast_debugger.cmx : src/res_driver.cmx src/res_doc.cmx \
src/res_ast_debugger.cmi
Expand Down
41 changes: 8 additions & 33 deletions src/res_ast_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -324,43 +324,18 @@ let normalize =
extension = (fun mapper ext ->
match ext with
| (id, payload) ->
let contents = match id.txt with
| "bs.raw" -> "raw"
| "bs.obj" -> "obj"
| txt -> txt
in
({id with txt = contents}, default_mapper.payload mapper payload)

(
{id with txt = Res_printer.convertBsExtension id.txt},
default_mapper.payload mapper payload
)
);
attribute = (fun mapper attr ->
match attr with
| (id, payload) ->
(* Reminder, keep this in sync with src/res_printer.ml *)
let contents = match id.txt with
| "bs.val" -> "val"
| "bs.module" -> "module"
| "bs.scope" -> "scope"
| "bs.splice" | "bs.variadic" -> "variadic"
| "bs.set" -> "set"
| "bs.set_index" -> "set_index"
| "bs.get" -> "get"
| "bs.get_index" -> "get_index"
| "bs.new" -> "new"
| "bs.obj" -> "obj"
| "bs.return" -> "return"
| "bs.uncurry" -> "uncurry"
| "bs.this" -> "this"
| "bs.meth" -> "meth"
| "bs.deriving" -> "deriving"
| "bs.string" -> "string"
| "bs.int" -> "int"
| "bs.ignore" -> "ignore"
| "bs.unwrap" -> "unwrap"
| "bs.as" -> "as"
| "bs.optional" -> "optional"
| txt -> txt
in
({id with txt = contents}, default_mapper.payload mapper payload)
(
{id with txt = Res_printer.convertBsExternalAttribute id.txt},
default_mapper.payload mapper payload
)
);
attributes = (fun mapper attrs ->
attrs
Expand Down
75 changes: 45 additions & 30 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,49 @@ type callbackStyle =
*)
| ArgumentsFitOnOneLine

(* Since compiler version 8.3, the bs. prefix is no longer needed *)
(* Synced from
https://github.com/rescript-lang/rescript-compiler/blob/29174de1a5fde3b16cf05d10f5ac109cfac5c4ca/jscomp/frontend/ast_external_process.ml#L291-L367 *)
let convertBsExternalAttribute = function
| "bs.as" -> "as"
| "bs.deriving" -> "deriving"
| "bs.get" -> "get"
| "bs.get_index" -> "get_index"
| "bs.ignore" -> "ignore"
| "bs.inline" -> "inline"
| "bs.int" -> "int"
| "bs.meth" -> "meth"
| "bs.module" -> "module"
| "bs.new" -> "new"
| "bs.obj" -> "obj"
| "bs.optional" -> "optional"
| "bs.return" -> "return"
| "bs.send" -> "send"
| "bs.scope" -> "scope"
| "bs.set" -> "set"
| "bs.set_index" -> "set_index"
| "bs.splice" | "bs.variadic" -> "variadic"
| "bs.string" -> "string"
| "bs.this" -> "this"
| "bs.uncurry" -> "uncurry"
| "bs.unwrap" -> "unwrap"
| "bs.val" -> "val"
(* bs.send.pipe shouldn't be transformed *)
| txt -> txt

(* These haven't been needed for a long time now *)
(* Synced from
https://github.com/rescript-lang/rescript-compiler/blob/29174de1a5fde3b16cf05d10f5ac109cfac5c4ca/jscomp/frontend/ast_exp_extension.ml *)
let convertBsExtension = function
| "bs.debugger" -> "debugger"
| "bs.external" -> "raw"
(* We should never see this one since we use the sugared object form, but still *)
| "bs.obj" -> "obj"
| "bs.raw" -> "raw"
| "bs.re" -> "re"
(* TODO: what about bs.time and bs.node? *)
| txt -> txt

let addParens doc =
Doc.group (
Doc.concat [
Expand Down Expand Up @@ -1985,11 +2028,7 @@ and printPackageConstraint i cmtTbl (longidentLoc, typ) =
]

and printExtension ~atModuleLvl (stringLoc, payload) cmtTbl =
let txt = match stringLoc.Location.txt with
| "bs.raw" -> "raw"
| "bs.obj" -> "obj"
| txt -> txt
in
let txt = convertBsExtension stringLoc.Location.txt in
let extName =
let doc = Doc.concat [
Doc.text "%";
Expand Down Expand Up @@ -4808,34 +4847,10 @@ and printPayload (payload : Parsetree.payload) cmtTbl =
]

and printAttribute ((id, payload) : Parsetree.attribute) cmtTbl =
let contents = match id.txt with
| "bs.val" -> "val"
| "bs.module" -> "module"
| "bs.scope" -> "scope"
| "bs.splice" | "bs.variadic" -> "variadic"
| "bs.set" -> "set"
| "bs.set_index" -> "set_index"
| "bs.get" -> "get"
| "bs.get_index" -> "get_index"
| "bs.new" -> "new"
| "bs.obj" -> "obj"
| "bs.return" -> "return"
| "bs.uncurry" -> "uncurry"
| "bs.this" -> "this"
| "bs.meth" -> "meth"
| "bs.deriving" -> "deriving"
| "bs.string" -> "string"
| "bs.int" -> "int"
| "bs.ignore" -> "ignore"
| "bs.unwrap" -> "unwrap"
| "bs.as" -> "as"
| "bs.optional" -> "optional"
| txt -> txt
in
Doc.group (
Doc.concat [
Doc.text "@";
Doc.text contents;
Doc.text (convertBsExternalAttribute id.txt);
printPayload payload cmtTbl
]
)
Expand Down
2 changes: 2 additions & 0 deletions src/res_printer.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
val convertBsExternalAttribute : string -> string
val convertBsExtension : string -> string

val printTypeParams :
(Parsetree.core_type * Asttypes.variance) list -> Res_comments_table.t -> Res_doc.t
Expand Down
19 changes: 19 additions & 0 deletions tests/conversion/reason/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`attributes.re 1`] = `
"module Color: {
type t = private string

@inline(\\"red\\") let red: t
@inline(\\"black\\") let black: t
} = {
type t = string

@inline let red = \\"red\\"
@inline let black = \\"black\\"
}

@send external map: (array<'a>, 'a => 'b) => array<'b> = \\"map\\"
@send external filter: (array<'a>, 'a => 'b) => array<'b> = \\"filter\\"
list{1, 2, 3}->map(a => a + 1)->filter(a => modulo(a, 2) == 0)->Js.log
"
`;

exports[`bracedJsx.re 1`] = `
"open Belt

Expand Down
18 changes: 18 additions & 0 deletions tests/conversion/reason/attributes.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Color: {
type t = pri string;

[@bs.inline "red"] let red: t;
[@bs.inline "black"] let black: t;
} = {
type t = string;

[@bs.inline] let red = "red";
[@bs.inline] let black = "black";
};

[@bs.send] external map: (array('a), 'a => 'b) => array('b) = "map";
[@bs.send] external filter: (array('a), 'a => 'b) => array('b) = "filter";
[1, 2, 3]
->map(a => a + 1)
->filter(a => modulo(a, 2) == 0)
->Js.log;
2 changes: 1 addition & 1 deletion tests/printer/structure/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ include (
{
@val @module(\\"react\\")
external createElementInternalHack: 'a = \\"createElement\\"
@bs.send
@send
external apply: (
'theFunction,
'theContext,
Expand Down
2 changes: 1 addition & 1 deletion tests/printer/structure/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include (
{
@val @module("react")
external createElementInternalHack: 'a = "createElement"
@bs.send
@send
external apply: (
'theFunction,
'theContext,
Expand Down